ForkServiceChild.h (4079B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #ifndef __FORKSERVICE_CHILD_H_ 7 #define __FORKSERVICE_CHILD_H_ 8 9 #include "base/process_util.h" 10 #include "mozilla/GeckoArgs.h" 11 #include "nsIObserver.h" 12 #include "nsString.h" 13 #include "mozilla/ipc/MiniTransceiver.h" 14 #include "mozilla/ipc/LaunchError.h" 15 #include "mozilla/Mutex.h" 16 #include "mozilla/RefPtr.h" 17 #include "mozilla/Result.h" 18 #include "mozilla/StaticMutex.h" 19 #include "mozilla/StaticPtr.h" 20 #include "mozilla/ThreadSafety.h" 21 22 #include <sys/types.h> 23 #include <poll.h> 24 25 namespace mozilla { 26 namespace ipc { 27 28 class GeckoChildProcessHost; 29 30 /** 31 * This is the interface to the fork server. 32 * 33 * When the chrome process calls |ForkServiceChild| to create a new 34 * process, this class send a message to the fork server through a 35 * pipe and get the PID of the new process from the reply. 36 */ 37 class ForkServiceChild final { 38 public: 39 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ForkServiceChild) 40 /** 41 * Ask the fork server to create a new process with given parameters. 42 * 43 * \param aArgs arguments with file attachments used in the content process. 44 * \param aOptions other options which will be used to create the process. 45 * Not all launch options are supported. 46 * \param aPid returns the PID of the content 47 * process created. \return true if success. 48 */ 49 Result<Ok, LaunchError> SendForkNewSubprocess( 50 geckoargs::ChildProcessArgs&& aArgs, base::LaunchOptions&& aOptions, 51 pid_t* aPid) MOZ_EXCLUDES(mMutex); 52 53 /** 54 * Contains a status value as from waitpid (for use with macros 55 * like WIFEXITED). 56 */ 57 struct ProcStatus { 58 int status; 59 }; 60 61 /** 62 * Ask the fork server to call waitpid on a child process. 63 * 64 * \param aPid The process ID of the child process. 65 * \param aBlock Whether to block until the child process exits; 66 * use with caution. 67 * 68 * \return Ok(status) if the process exited; Err(0) if the process 69 * is still running; and Err(errno) if there was an error. 70 */ 71 Result<ProcStatus, int> SendWaitPid(pid_t aPid, bool aBlock); 72 73 /** 74 * Create a fork server process and the singleton of this class. 75 * 76 * This function uses |GeckoChildProcessHost| to launch the fork 77 * server, getting the fd of a pipe/socket to the fork server from 78 * it's |IPC::Channel|. 79 */ 80 static void StartForkServer(); 81 static void StopForkServer(); 82 83 /** 84 * Return the singleton. May return nullptr if the fork server is 85 * not running or in the process of being restarted. 86 */ 87 static RefPtr<ForkServiceChild> Get(); 88 89 /** 90 * Returns whether the fork server was ever active. Thread-safe. 91 */ 92 static bool WasUsed() { return sForkServiceUsed; } 93 94 private: 95 ForkServiceChild(int aFd, GeckoChildProcessHost* aProcess); 96 ~ForkServiceChild(); 97 98 void OnError() MOZ_REQUIRES(mMutex); 99 100 static StaticMutex sMutex; 101 static StaticRefPtr<ForkServiceChild> sSingleton MOZ_GUARDED_BY(sMutex); 102 static Atomic<bool> sForkServiceUsed; 103 Mutex mMutex; 104 UniquePtr<MiniTransceiver> mTcver MOZ_GUARDED_BY(mMutex); 105 bool mFailed MOZ_GUARDED_BY(mMutex); // crashed or disconnected. 106 // mProcess is accessed only by the dtor so should be inherently 107 // thread-safe 108 GeckoChildProcessHost* mProcess; 109 }; 110 111 /** 112 * Start a fork server at |xpcom-startup| from the chrome process. 113 */ 114 class ForkServerLauncher final : public nsIObserver { 115 public: 116 NS_DECL_ISUPPORTS 117 NS_DECL_NSIOBSERVER 118 119 ForkServerLauncher(); 120 static already_AddRefed<ForkServerLauncher> Create(); 121 122 private: 123 friend class ForkServiceChild; 124 ~ForkServerLauncher(); 125 126 static void RestartForkServer(); 127 128 static bool sHaveStartedClient; 129 static StaticRefPtr<ForkServerLauncher> sSingleton; 130 }; 131 132 } // namespace ipc 133 } // namespace mozilla 134 135 #endif /* __FORKSERVICE_CHILD_H_ */