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/memory/scoped_ptr.h"
     14 #include "base/message_loop.h"
     15 #include "base/message_pump_libevent.h"
     16 
     17 #if defined(OS_LINUX)
     18 #include "chrome/common/multi_process_lock.h"
     19 MultiProcessLock* TakeServiceRunningLock(bool waiting);
     20 #endif  // OS_LINUX
     21 
     22 #if defined(OS_MACOSX)
     23 #include "base/files/file_path_watcher.h"
     24 #include "base/mac/scoped_cftyperef.h"
     25 
     26 class CommandLine;
     27 CFDictionaryRef CreateServiceProcessLaunchdPlist(CommandLine* cmd_line,
     28                                                  bool for_auto_launch);
     29 #endif  // OS_MACOSX
     30 
     31 namespace base {
     32 class WaitableEvent;
     33 }
     34 
     35 // Watches for |kShutDownMessage| to be written to the file descriptor it is
     36 // watching. When it reads |kShutDownMessage|, it performs |shutdown_task_|.
     37 // Used here to monitor the socket listening to g_signal_socket.
     38 class ServiceProcessShutdownMonitor
     39     : public base::MessagePumpLibevent::Watcher {
     40  public:
     41 
     42   enum {
     43     kShutDownMessage = 0xdecea5e
     44   };
     45 
     46   explicit ServiceProcessShutdownMonitor(Task* shutdown_task);
     47   virtual ~ServiceProcessShutdownMonitor();
     48 
     49   // base::MessagePumpLibevent::Watcher overrides
     50   virtual void OnFileCanReadWithoutBlocking(int fd);
     51   virtual void OnFileCanWriteWithoutBlocking(int fd);
     52 
     53  private:
     54   scoped_ptr<Task> shutdown_task_;
     55 };
     56 
     57 struct ServiceProcessState::StateData
     58     : public base::RefCountedThreadSafe<ServiceProcessState::StateData> {
     59   StateData();
     60 
     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);
     64 
     65 
     66   // TODO(jhawkins): Either make this a class or rename these public member
     67   // variables to remove the trailing underscore.
     68 
     69 #if defined(OS_MACOSX)
     70   bool WatchExecutable();
     71 
     72   base::mac::ScopedCFTypeRef<CFDictionaryRef> launchd_conf_;
     73   base::files::FilePathWatcher executable_watcher_;
     74 #endif  // OS_MACOSX
     75 #if defined(OS_LINUX)
     76   scoped_ptr<MultiProcessLock> initializing_lock_;
     77   scoped_ptr<MultiProcessLock> running_lock_;
     78 #endif  // OS_LINUX
     79   scoped_ptr<ServiceProcessShutdownMonitor> shut_down_monitor_;
     80   base::MessagePumpLibevent::FileDescriptorWatcher watcher_;
     81   int sockets_[2];
     82   struct sigaction old_action_;
     83   bool set_action_;
     84 
     85  protected:
     86   friend class base::RefCountedThreadSafe<ServiceProcessState::StateData>;
     87   virtual ~StateData();
     88 };
     89 
     90 #endif  // CHROME_COMMON_SERVICE_PROCESS_UTIL_POSIX_H_
     91