Home | History | Annotate | Download | only in common
      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.
      4 
      5 #ifndef CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_
      6 #define CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_
      7 
      8 #include "service_process_util.h"
      9 
     10 #include <signal.h>
     11 
     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"
     16 
     17 #if defined(OS_POSIX) && !defined(OS_MACOSX)
     18 #include "chrome/common/multi_process_lock.h"
     19 MultiProcessLock* TakeServiceRunningLock(bool waiting);
     20 #endif
     21 
     22 #if defined(OS_MACOSX)
     23 #include "base/files/file_path_watcher.h"
     24 #include "base/mac/scoped_cftyperef.h"
     25 
     26 namespace base {
     27 class CommandLine;
     28 }
     29 
     30 CFDictionaryRef CreateServiceProcessLaunchdPlist(base::CommandLine* cmd_line,
     31                                                  bool for_auto_launch);
     32 #endif  // OS_MACOSX
     33 
     34 namespace base {
     35 class WaitableEvent;
     36 }
     37 
     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 {
     42  public:
     43 
     44   enum {
     45     kTerminateMessage = 0xdecea5e
     46   };
     47 
     48   explicit ServiceProcessTerminateMonitor(const base::Closure& terminate_task);
     49   virtual ~ServiceProcessTerminateMonitor();
     50 
     51   // MessageLoopForIO::Watcher overrides
     52   virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
     53   virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
     54 
     55  private:
     56   base::Closure terminate_task_;
     57 };
     58 
     59 struct ServiceProcessState::StateData
     60     : public base::RefCountedThreadSafe<ServiceProcessState::StateData> {
     61   StateData();
     62 
     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);
     66 
     67 
     68   // TODO(jhawkins): Either make this a class or rename these public member
     69   // variables to remove the trailing underscore.
     70 
     71 #if defined(OS_MACOSX)
     72   bool WatchExecutable();
     73 
     74   base::ScopedCFTypeRef<CFDictionaryRef> launchd_conf_;
     75   base::FilePathWatcher executable_watcher_;
     76 #endif  // OS_MACOSX
     77 #if defined(OS_POSIX) && !defined(OS_MACOSX)
     78   scoped_ptr<MultiProcessLock> initializing_lock_;
     79   scoped_ptr<MultiProcessLock> running_lock_;
     80 #endif
     81   scoped_ptr<ServiceProcessTerminateMonitor> terminate_monitor_;
     82   base::MessageLoopForIO::FileDescriptorWatcher watcher_;
     83   int sockets_[2];
     84   struct sigaction old_action_;
     85   bool set_action_;
     86 
     87  protected:
     88   friend class base::RefCountedThreadSafe<ServiceProcessState::StateData>;
     89   virtual ~StateData();
     90 };
     91 
     92 #endif  // CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_
     93