1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Use of this source code is governed by a BSD-style license that can be
3dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// found in the LICENSE file.
4dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
5dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#ifndef CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_
6dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#define CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_
7dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
8dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "service_process_util.h"
9dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
10dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include <signal.h>
11dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
12dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "base/basictypes.h"
13ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h"
14dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "base/message_loop.h"
15dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "base/message_pump_libevent.h"
16dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
17dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#if defined(OS_LINUX)
18dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/common/multi_process_lock.h"
19dc0f95d653279beabeb9817299e2902918ba123eKristian MonsenMultiProcessLock* TakeServiceRunningLock(bool waiting);
20dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#endif  // OS_LINUX
21dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
22dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#if defined(OS_MACOSX)
23ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/files/file_path_watcher.h"
24dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "base/mac/scoped_cftyperef.h"
25ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
26dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass CommandLine;
27dc0f95d653279beabeb9817299e2902918ba123eKristian MonsenCFDictionaryRef CreateServiceProcessLaunchdPlist(CommandLine* cmd_line,
28dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen                                                 bool for_auto_launch);
29dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#endif  // OS_MACOSX
30dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsennamespace base {
32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass WaitableEvent;
33ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
34ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
35dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Watches for |kShutDownMessage| to be written to the file descriptor it is
36dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// watching. When it reads |kShutDownMessage|, it performs |shutdown_task_|.
37dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Used here to monitor the socket listening to g_signal_socket.
38dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass ServiceProcessShutdownMonitor
39dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    : public base::MessagePumpLibevent::Watcher {
40dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen public:
41dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
42dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  enum {
43dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    kShutDownMessage = 0xdecea5e
44dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  };
45dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
46dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  explicit ServiceProcessShutdownMonitor(Task* shutdown_task);
47dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  virtual ~ServiceProcessShutdownMonitor();
48dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
49dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // base::MessagePumpLibevent::Watcher overrides
50dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  virtual void OnFileCanReadWithoutBlocking(int fd);
51dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  virtual void OnFileCanWriteWithoutBlocking(int fd);
52dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
53dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen private:
54dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  scoped_ptr<Task> shutdown_task_;
55dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen};
56dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
57dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenstruct ServiceProcessState::StateData
58dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    : public base::RefCountedThreadSafe<ServiceProcessState::StateData> {
59dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  StateData();
60dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
61dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // WatchFileDescriptor needs to be set up by the thread that is going
62dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // to be monitoring it.
63ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void SignalReady(base::WaitableEvent* signal, bool* success);
64ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
65ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
66ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // TODO(jhawkins): Either make this a class or rename these public member
67ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // variables to remove the trailing underscore.
68dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
69dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#if defined(OS_MACOSX)
70ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool WatchExecutable();
71ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
72dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  base::mac::ScopedCFTypeRef<CFDictionaryRef> launchd_conf_;
73ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  base::files::FilePathWatcher executable_watcher_;
74dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#endif  // OS_MACOSX
75dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#if defined(OS_LINUX)
76dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  scoped_ptr<MultiProcessLock> initializing_lock_;
77dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  scoped_ptr<MultiProcessLock> running_lock_;
78dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#endif  // OS_LINUX
79dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  scoped_ptr<ServiceProcessShutdownMonitor> shut_down_monitor_;
80dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  base::MessagePumpLibevent::FileDescriptorWatcher watcher_;
81dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  int sockets_[2];
82dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  struct sigaction old_action_;
83dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  bool set_action_;
84dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
85dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen protected:
86dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  friend class base::RefCountedThreadSafe<ServiceProcessState::StateData>;
87dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  virtual ~StateData();
88dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen};
89dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
90dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#endif  // CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_
91