Backed out 8 changesets (bug 1873776) for causing vendor failures. CLOSED TREE
[gecko.git] / ipc / glue / ForkServiceChild.h
blob73d090d556a5e0bbfee3f3b94d86703b8ac98213
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"
11 #include "nsString.h"
12 #include "mozilla/ipc/MiniTransceiver.h"
13 #include "mozilla/ipc/LaunchError.h"
14 #include "mozilla/Result.h"
16 #include <sys/types.h>
17 #include <poll.h>
19 namespace mozilla {
20 namespace ipc {
22 class GeckoChildProcessHost;
24 /**
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 {
32 public:
33 ForkServiceChild(int aFd, GeckoChildProcessHost* aProcess);
34 virtual ~ForkServiceChild();
36 struct Args {
37 #if defined(XP_LINUX) && defined(MOZ_SANDBOX)
38 int mForkFlags = 0;
39 bool mChroot = false;
40 #endif
41 nsTArray<nsCString> mArgv;
42 nsTArray<EnvVar> mEnv;
43 nsTArray<FdMapping> mFdsRemap;
46 /**
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);
60 /**
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();
69 /**
70 * Return the singleton.
72 static ForkServiceChild* Get() {
73 auto child = sForkServiceChild.get();
74 return child == nullptr || child->mFailed ? nullptr : child;
77 private:
78 // Called when a message is received.
79 void OnMessageReceived(UniquePtr<IPC::Message> message);
80 void OnError();
82 UniquePtr<MiniTransceiver> mTcver;
83 static UniquePtr<ForkServiceChild> sForkServiceChild;
84 pid_t mRecvPid;
85 bool mFailed; // The forkserver has crashed or disconnected.
86 GeckoChildProcessHost* mProcess;
89 /**
90 * Start a fork server at |xpcom-startup| from the chrome process.
92 class ForkServerLauncher : public nsIObserver {
93 public:
94 NS_DECL_ISUPPORTS
95 NS_DECL_NSIOBSERVER
97 ForkServerLauncher();
98 static already_AddRefed<ForkServerLauncher> Create();
100 private:
101 friend class ForkServiceChild;
102 virtual ~ForkServerLauncher();
104 static void RestartForkServer();
106 static bool mHaveStartedClient;
107 static StaticRefPtr<ForkServerLauncher> mSingleton;
110 } // namespace ipc
111 } // namespace mozilla
113 #endif /* __FORKSERVICE_CHILD_H_ */