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_
9 #include "base/process_util.h"
10 #include "nsIObserver.h"
12 #include "mozilla/ipc/MiniTransceiver.h"
13 #include "mozilla/ipc/LaunchError.h"
14 #include "mozilla/Result.h"
16 #include <sys/types.h>
22 class GeckoChildProcessHost
;
25 * This is the interface to the fork server.
27 * When the chrome process calls |ForkServiceChild| to create a new
28 * process, this class send a message to the fork server through a
29 * pipe and get the PID of the new process from the reply.
31 class ForkServiceChild
{
33 ForkServiceChild(int aFd
, GeckoChildProcessHost
* aProcess
);
34 virtual ~ForkServiceChild();
37 #if defined(XP_LINUX) && defined(MOZ_SANDBOX)
41 nsTArray
<nsCString
> mArgv
;
42 nsTArray
<EnvVar
> mEnv
;
43 nsTArray
<FdMapping
> mFdsRemap
;
47 * Ask the fork server to create a new process with given parameters.
49 * The fork server uses |base::LaunchApp()| to create a new
50 * content process with the following parameters.
52 * \param aArgv assigns |argv| of the content process.
53 * \param aEnvMap sets |LaunchOptions::env_map|.
54 * \param aFdsRemap sets |LaunchOptions::fd_to_remap|.
55 * \param aPid returns the PID of the content process created.
56 * \return true if success.
58 Result
<Ok
, LaunchError
> SendForkNewSubprocess(const Args
& aArgs
, pid_t
* aPid
);
61 * Create a fork server process and the singleton of this class.
63 * This function uses |GeckoChildProcessHost| to launch the fork
64 * server, getting the fd of a pipe/socket to the fork server from
65 * it's |IPC::Channel|.
67 static void StartForkServer();
68 static void StopForkServer();
70 * Return the singleton.
72 static ForkServiceChild
* Get() {
73 auto child
= sForkServiceChild
.get();
74 return child
== nullptr || child
->mFailed
? nullptr : child
;
78 * Returns whether the fork server was ever active. Thread-safe.
80 static bool WasUsed() { return sForkServiceUsed
; }
83 // Called when a message is received.
84 void OnMessageReceived(UniquePtr
<IPC::Message
> message
);
87 UniquePtr
<MiniTransceiver
> mTcver
;
88 static UniquePtr
<ForkServiceChild
> sForkServiceChild
;
89 static Atomic
<bool> sForkServiceUsed
;
91 bool mFailed
; // The forkserver has crashed or disconnected.
92 GeckoChildProcessHost
* mProcess
;
96 * Start a fork server at |xpcom-startup| from the chrome process.
98 class ForkServerLauncher
: public nsIObserver
{
103 ForkServerLauncher();
104 static already_AddRefed
<ForkServerLauncher
> Create();
107 friend class ForkServiceChild
;
108 virtual ~ForkServerLauncher();
110 static void RestartForkServer();
112 static bool mHaveStartedClient
;
113 static StaticRefPtr
<ForkServerLauncher
> mSingleton
;
117 } // namespace mozilla
119 #endif /* __FORKSERVICE_CHILD_H_ */