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"
27 CFDictionaryRef
CreateServiceProcessLaunchdPlist(CommandLine
* cmd_line
,
28 bool for_auto_launch
);
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
: public base::MessageLoopForIO::Watcher
{
42 kTerminateMessage
= 0xdecea5e
45 explicit ServiceProcessTerminateMonitor(const base::Closure
& terminate_task
);
46 virtual ~ServiceProcessTerminateMonitor();
48 // MessageLoopForIO::Watcher overrides
49 virtual void OnFileCanReadWithoutBlocking(int fd
) OVERRIDE
;
50 virtual void OnFileCanWriteWithoutBlocking(int fd
) OVERRIDE
;
53 base::Closure terminate_task_
;
56 struct ServiceProcessState::StateData
57 : public base::RefCountedThreadSafe
<ServiceProcessState::StateData
> {
60 // WatchFileDescriptor needs to be set up by the thread that is going
61 // to be monitoring it.
62 void SignalReady(base::WaitableEvent
* signal
, bool* success
);
65 // TODO(jhawkins): Either make this a class or rename these public member
66 // variables to remove the trailing underscore.
68 #if defined(OS_MACOSX)
69 bool WatchExecutable();
71 base::ScopedCFTypeRef
<CFDictionaryRef
> launchd_conf_
;
72 base::FilePathWatcher executable_watcher_
;
74 #if defined(OS_POSIX) && !defined(OS_MACOSX)
75 scoped_ptr
<MultiProcessLock
> initializing_lock_
;
76 scoped_ptr
<MultiProcessLock
> running_lock_
;
78 scoped_ptr
<ServiceProcessTerminateMonitor
> terminate_monitor_
;
79 base::MessageLoopForIO::FileDescriptorWatcher watcher_
;
81 struct sigaction old_action_
;
85 friend class base::RefCountedThreadSafe
<ServiceProcessState::StateData
>;
89 #endif // CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_