Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / service_process_util_posix.h
blobd2471308de3ecb9825111c869df98fdbd2353169
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_
6 #define CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_
8 #include "service_process_util.h"
10 #include <signal.h>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop.h"
17 #if defined(OS_POSIX) && !defined(OS_MACOSX)
18 #include "chrome/common/multi_process_lock.h"
19 MultiProcessLock* TakeServiceRunningLock(bool waiting);
20 #endif
22 #if defined(OS_MACOSX)
23 #include "base/files/file_path_watcher.h"
24 #include "base/mac/scoped_cftyperef.h"
26 class CommandLine;
27 CFDictionaryRef CreateServiceProcessLaunchdPlist(CommandLine* cmd_line,
28 bool for_auto_launch);
29 #endif // OS_MACOSX
31 namespace base {
32 class WaitableEvent;
35 // Watches for |kTerminateMessage| to be written to the file descriptor it is
36 // watching. When it reads |kTerminateMessage|, it performs |terminate_task_|.
37 // Used here to monitor the socket listening to g_signal_socket.
38 class ServiceProcessTerminateMonitor
39 : public MessageLoopForIO::Watcher {
40 public:
42 enum {
43 kTerminateMessage = 0xdecea5e
46 explicit ServiceProcessTerminateMonitor(const base::Closure& terminate_task);
47 virtual ~ServiceProcessTerminateMonitor();
49 // MessageLoopForIO::Watcher overrides
50 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
51 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
53 private:
54 base::Closure terminate_task_;
57 struct ServiceProcessState::StateData
58 : public base::RefCountedThreadSafe<ServiceProcessState::StateData> {
59 StateData();
61 // WatchFileDescriptor needs to be set up by the thread that is going
62 // to be monitoring it.
63 void SignalReady(base::WaitableEvent* signal, bool* success);
66 // TODO(jhawkins): Either make this a class or rename these public member
67 // variables to remove the trailing underscore.
69 #if defined(OS_MACOSX)
70 bool WatchExecutable();
72 base::mac::ScopedCFTypeRef<CFDictionaryRef> launchd_conf_;
73 base::files::FilePathWatcher executable_watcher_;
74 #endif // OS_MACOSX
75 #if defined(OS_POSIX) && !defined(OS_MACOSX)
76 scoped_ptr<MultiProcessLock> initializing_lock_;
77 scoped_ptr<MultiProcessLock> running_lock_;
78 #endif
79 scoped_ptr<ServiceProcessTerminateMonitor> terminate_monitor_;
80 MessageLoopForIO::FileDescriptorWatcher watcher_;
81 int sockets_[2];
82 struct sigaction old_action_;
83 bool set_action_;
85 protected:
86 friend class base::RefCountedThreadSafe<ServiceProcessState::StateData>;
87 virtual ~StateData();
90 #endif // CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_