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"
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
17 #if defined(OS_POSIX) && !defined(OS_MACOSX)
18 #include "chrome/common/multi_process_lock.h"
19 MultiProcessLock
* TakeServiceRunningLock(bool waiting
);
22 #if defined(OS_MACOSX)
23 #include "base/files/file_path_watcher.h"
24 #include "base/mac/scoped_cftyperef.h"
30 CFDictionaryRef
CreateServiceProcessLaunchdPlist(base::CommandLine
* cmd_line
,
31 bool for_auto_launch
);
38 // Watches for |kTerminateMessage| to be written to the file descriptor it is
39 // watching. When it reads |kTerminateMessage|, it performs |terminate_task_|.
40 // Used here to monitor the socket listening to g_signal_socket.
41 class ServiceProcessTerminateMonitor
: public base::MessageLoopForIO::Watcher
{
45 kTerminateMessage
= 0xdecea5e
48 explicit ServiceProcessTerminateMonitor(const base::Closure
& terminate_task
);
49 virtual ~ServiceProcessTerminateMonitor();
51 // MessageLoopForIO::Watcher overrides
52 virtual void OnFileCanReadWithoutBlocking(int fd
) OVERRIDE
;
53 virtual void OnFileCanWriteWithoutBlocking(int fd
) OVERRIDE
;
56 base::Closure terminate_task_
;
59 struct ServiceProcessState::StateData
60 : public base::RefCountedThreadSafe
<ServiceProcessState::StateData
> {
63 // WatchFileDescriptor needs to be set up by the thread that is going
64 // to be monitoring it.
65 void SignalReady(base::WaitableEvent
* signal
, bool* success
);
68 // TODO(jhawkins): Either make this a class or rename these public member
69 // variables to remove the trailing underscore.
71 #if defined(OS_MACOSX)
72 bool WatchExecutable();
74 base::ScopedCFTypeRef
<CFDictionaryRef
> launchd_conf_
;
75 base::FilePathWatcher executable_watcher_
;
77 #if defined(OS_POSIX) && !defined(OS_MACOSX)
78 scoped_ptr
<MultiProcessLock
> initializing_lock_
;
79 scoped_ptr
<MultiProcessLock
> running_lock_
;
81 scoped_ptr
<ServiceProcessTerminateMonitor
> terminate_monitor_
;
82 base::MessageLoopForIO::FileDescriptorWatcher watcher_
;
84 struct sigaction old_action_
;
88 friend class base::RefCountedThreadSafe
<ServiceProcessState::StateData
>;
92 #endif // CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_