xref: /aosp_15_r20/external/cronet/base/process/launch.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker // This file contains functions for launching subprocesses.
6*6777b538SAndroid Build Coastguard Worker 
7*6777b538SAndroid Build Coastguard Worker #ifndef BASE_PROCESS_LAUNCH_H_
8*6777b538SAndroid Build Coastguard Worker #define BASE_PROCESS_LAUNCH_H_
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
11*6777b538SAndroid Build Coastguard Worker 
12*6777b538SAndroid Build Coastguard Worker #include <string>
13*6777b538SAndroid Build Coastguard Worker #include <string_view>
14*6777b538SAndroid Build Coastguard Worker #include <utility>
15*6777b538SAndroid Build Coastguard Worker #include <vector>
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
18*6777b538SAndroid Build Coastguard Worker #include "base/command_line.h"
19*6777b538SAndroid Build Coastguard Worker #include "base/environment.h"
20*6777b538SAndroid Build Coastguard Worker #include "base/files/file_path.h"
21*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h"
22*6777b538SAndroid Build Coastguard Worker #include "base/process/process.h"
23*6777b538SAndroid Build Coastguard Worker #include "base/process/process_handle.h"
24*6777b538SAndroid Build Coastguard Worker #include "base/threading/thread_restrictions.h"
25*6777b538SAndroid Build Coastguard Worker #include "build/blink_buildflags.h"
26*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
27*6777b538SAndroid Build Coastguard Worker 
28*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
29*6777b538SAndroid Build Coastguard Worker #include "base/win/windows_types.h"
30*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_FUCHSIA)
31*6777b538SAndroid Build Coastguard Worker #include <lib/fdio/spawn.h>
32*6777b538SAndroid Build Coastguard Worker #include <zircon/types.h>
33*6777b538SAndroid Build Coastguard Worker #endif
34*6777b538SAndroid Build Coastguard Worker 
35*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
36*6777b538SAndroid Build Coastguard Worker #include "base/posix/file_descriptor_shuffle.h"
37*6777b538SAndroid Build Coastguard Worker #endif
38*6777b538SAndroid Build Coastguard Worker 
39*6777b538SAndroid Build Coastguard Worker namespace base {
40*6777b538SAndroid Build Coastguard Worker 
41*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_APPLE)
42*6777b538SAndroid Build Coastguard Worker class MachRendezvousPort;
43*6777b538SAndroid Build Coastguard Worker using MachPortsForRendezvous = std::map<uint32_t, MachRendezvousPort>;
44*6777b538SAndroid Build Coastguard Worker #endif
45*6777b538SAndroid Build Coastguard Worker 
46*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
47*6777b538SAndroid Build Coastguard Worker typedef std::vector<HANDLE> HandlesToInheritVector;
48*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_FUCHSIA)
49*6777b538SAndroid Build Coastguard Worker struct PathToTransfer {
50*6777b538SAndroid Build Coastguard Worker   base::FilePath path;
51*6777b538SAndroid Build Coastguard Worker   zx_handle_t handle;
52*6777b538SAndroid Build Coastguard Worker };
53*6777b538SAndroid Build Coastguard Worker struct HandleToTransfer {
54*6777b538SAndroid Build Coastguard Worker   uint32_t id;
55*6777b538SAndroid Build Coastguard Worker   zx_handle_t handle;
56*6777b538SAndroid Build Coastguard Worker };
57*6777b538SAndroid Build Coastguard Worker typedef std::vector<HandleToTransfer> HandlesToTransferVector;
58*6777b538SAndroid Build Coastguard Worker typedef std::vector<std::pair<int, int>> FileHandleMappingVector;
59*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_POSIX)
60*6777b538SAndroid Build Coastguard Worker typedef std::vector<std::pair<int, int>> FileHandleMappingVector;
61*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_WIN)
62*6777b538SAndroid Build Coastguard Worker 
63*6777b538SAndroid Build Coastguard Worker // Options for launching a subprocess that are passed to LaunchProcess().
64*6777b538SAndroid Build Coastguard Worker // The default constructor constructs the object with default options.
65*6777b538SAndroid Build Coastguard Worker struct BASE_EXPORT LaunchOptions {
66*6777b538SAndroid Build Coastguard Worker #if (BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)) && !BUILDFLAG(IS_APPLE)
67*6777b538SAndroid Build Coastguard Worker   // Delegate to be run in between fork and exec in the subprocess (see
68*6777b538SAndroid Build Coastguard Worker   // pre_exec_delegate below)
69*6777b538SAndroid Build Coastguard Worker   class BASE_EXPORT PreExecDelegate {
70*6777b538SAndroid Build Coastguard Worker    public:
71*6777b538SAndroid Build Coastguard Worker     PreExecDelegate() = default;
72*6777b538SAndroid Build Coastguard Worker 
73*6777b538SAndroid Build Coastguard Worker     PreExecDelegate(const PreExecDelegate&) = delete;
74*6777b538SAndroid Build Coastguard Worker     PreExecDelegate& operator=(const PreExecDelegate&) = delete;
75*6777b538SAndroid Build Coastguard Worker 
76*6777b538SAndroid Build Coastguard Worker     virtual ~PreExecDelegate() = default;
77*6777b538SAndroid Build Coastguard Worker 
78*6777b538SAndroid Build Coastguard Worker     // Since this is to be run between fork and exec, and fork may have happened
79*6777b538SAndroid Build Coastguard Worker     // while multiple threads were running, this function needs to be async
80*6777b538SAndroid Build Coastguard Worker     // safe.
81*6777b538SAndroid Build Coastguard Worker     virtual void RunAsyncSafe() = 0;
82*6777b538SAndroid Build Coastguard Worker   };
83*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_POSIX)
84*6777b538SAndroid Build Coastguard Worker 
85*6777b538SAndroid Build Coastguard Worker   LaunchOptions();
86*6777b538SAndroid Build Coastguard Worker   LaunchOptions(const LaunchOptions&);
87*6777b538SAndroid Build Coastguard Worker   ~LaunchOptions();
88*6777b538SAndroid Build Coastguard Worker 
89*6777b538SAndroid Build Coastguard Worker   // If true, wait for the process to complete.
90*6777b538SAndroid Build Coastguard Worker   bool wait = false;
91*6777b538SAndroid Build Coastguard Worker 
92*6777b538SAndroid Build Coastguard Worker   // If not empty, change to this directory before executing the new process.
93*6777b538SAndroid Build Coastguard Worker   base::FilePath current_directory;
94*6777b538SAndroid Build Coastguard Worker 
95*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
96*6777b538SAndroid Build Coastguard Worker   bool start_hidden = false;
97*6777b538SAndroid Build Coastguard Worker 
98*6777b538SAndroid Build Coastguard Worker   // Process will be started using ShellExecuteEx instead of CreateProcess so
99*6777b538SAndroid Build Coastguard Worker   // that it is elevated. LaunchProcess with this flag will have different
100*6777b538SAndroid Build Coastguard Worker   // behaviour due to ShellExecuteEx. Some common operations like OpenProcess
101*6777b538SAndroid Build Coastguard Worker   // will fail. Currently the only other supported LaunchOptions are
102*6777b538SAndroid Build Coastguard Worker   // |start_hidden| and |wait|.
103*6777b538SAndroid Build Coastguard Worker   bool elevated = false;
104*6777b538SAndroid Build Coastguard Worker 
105*6777b538SAndroid Build Coastguard Worker   // Sets STARTF_FORCEOFFFEEDBACK so that the feedback cursor is forced off
106*6777b538SAndroid Build Coastguard Worker   // while the process is starting.
107*6777b538SAndroid Build Coastguard Worker   bool feedback_cursor_off = false;
108*6777b538SAndroid Build Coastguard Worker 
109*6777b538SAndroid Build Coastguard Worker   // Windows can inherit handles when it launches child processes.
110*6777b538SAndroid Build Coastguard Worker   // See https://blogs.msdn.microsoft.com/oldnewthing/20111216-00/?p=8873
111*6777b538SAndroid Build Coastguard Worker   // for a good overview of Windows handle inheritance.
112*6777b538SAndroid Build Coastguard Worker   //
113*6777b538SAndroid Build Coastguard Worker   // Implementation note: it might be nice to implement in terms of
114*6777b538SAndroid Build Coastguard Worker   // std::optional<>, but then the natural default state (vector not present)
115*6777b538SAndroid Build Coastguard Worker   // would be "all inheritable handles" while we want "no inheritance."
116*6777b538SAndroid Build Coastguard Worker   enum class Inherit {
117*6777b538SAndroid Build Coastguard Worker     // Only those handles in |handles_to_inherit| vector are inherited. If the
118*6777b538SAndroid Build Coastguard Worker     // vector is empty, no handles are inherited. The handles in the vector must
119*6777b538SAndroid Build Coastguard Worker     // all be inheritable.
120*6777b538SAndroid Build Coastguard Worker     kSpecific,
121*6777b538SAndroid Build Coastguard Worker 
122*6777b538SAndroid Build Coastguard Worker     // All handles in the current process which are inheritable are inherited.
123*6777b538SAndroid Build Coastguard Worker     // In production code this flag should be used only when running
124*6777b538SAndroid Build Coastguard Worker     // short-lived, trusted binaries, because open handles from other libraries
125*6777b538SAndroid Build Coastguard Worker     // and subsystems will leak to the child process, causing errors such as
126*6777b538SAndroid Build Coastguard Worker     // open socket hangs. There are also race conditions that can cause handle
127*6777b538SAndroid Build Coastguard Worker     // over-sharing.
128*6777b538SAndroid Build Coastguard Worker     //
129*6777b538SAndroid Build Coastguard Worker     // |handles_to_inherit| must be null.
130*6777b538SAndroid Build Coastguard Worker     //
131*6777b538SAndroid Build Coastguard Worker     // DEPRECATED. THIS SHOULD NOT BE USED. Explicitly map all handles that
132*6777b538SAndroid Build Coastguard Worker     // need to be shared in new code.
133*6777b538SAndroid Build Coastguard Worker     // TODO(brettw) bug 748258: remove this.
134*6777b538SAndroid Build Coastguard Worker     kAll
135*6777b538SAndroid Build Coastguard Worker   };
136*6777b538SAndroid Build Coastguard Worker   Inherit inherit_mode = Inherit::kSpecific;
137*6777b538SAndroid Build Coastguard Worker   HandlesToInheritVector handles_to_inherit;
138*6777b538SAndroid Build Coastguard Worker 
139*6777b538SAndroid Build Coastguard Worker   // If non-null, runs as if the user represented by the token had launched it.
140*6777b538SAndroid Build Coastguard Worker   // Whether the application is visible on the interactive desktop depends on
141*6777b538SAndroid Build Coastguard Worker   // the token belonging to an interactive logon session.
142*6777b538SAndroid Build Coastguard Worker   //
143*6777b538SAndroid Build Coastguard Worker   // To avoid hard to diagnose problems, when specified this loads the
144*6777b538SAndroid Build Coastguard Worker   // environment variables associated with the user and if this operation fails
145*6777b538SAndroid Build Coastguard Worker   // the entire call fails as well.
146*6777b538SAndroid Build Coastguard Worker   UserTokenHandle as_user = nullptr;
147*6777b538SAndroid Build Coastguard Worker 
148*6777b538SAndroid Build Coastguard Worker   // If true, use an empty string for the desktop name.
149*6777b538SAndroid Build Coastguard Worker   bool empty_desktop_name = false;
150*6777b538SAndroid Build Coastguard Worker 
151*6777b538SAndroid Build Coastguard Worker   // If non-null, launches the application in that job object. The process will
152*6777b538SAndroid Build Coastguard Worker   // be terminated immediately and LaunchProcess() will fail if assignment to
153*6777b538SAndroid Build Coastguard Worker   // the job object fails.
154*6777b538SAndroid Build Coastguard Worker   HANDLE job_handle = nullptr;
155*6777b538SAndroid Build Coastguard Worker 
156*6777b538SAndroid Build Coastguard Worker   // Handles for the redirection of stdin, stdout and stderr. The caller should
157*6777b538SAndroid Build Coastguard Worker   // either set all three of them or none (i.e. there is no way to redirect
158*6777b538SAndroid Build Coastguard Worker   // stderr without redirecting stdin).
159*6777b538SAndroid Build Coastguard Worker   //
160*6777b538SAndroid Build Coastguard Worker   // The handles must be inheritable. Pseudo handles are used when stdout and
161*6777b538SAndroid Build Coastguard Worker   // stderr redirect to the console. In that case, GetFileType() will return
162*6777b538SAndroid Build Coastguard Worker   // FILE_TYPE_CHAR and they're automatically inherited by child processes. See
163*6777b538SAndroid Build Coastguard Worker   // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075.aspx
164*6777b538SAndroid Build Coastguard Worker   // Otherwise, the caller must ensure that the |inherit_mode| and/or
165*6777b538SAndroid Build Coastguard Worker   // |handles_to_inherit| set so that the handles are inherited.
166*6777b538SAndroid Build Coastguard Worker   HANDLE stdin_handle = nullptr;
167*6777b538SAndroid Build Coastguard Worker   HANDLE stdout_handle = nullptr;
168*6777b538SAndroid Build Coastguard Worker   HANDLE stderr_handle = nullptr;
169*6777b538SAndroid Build Coastguard Worker 
170*6777b538SAndroid Build Coastguard Worker   // If set to true, ensures that the child process is launched with the
171*6777b538SAndroid Build Coastguard Worker   // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent
172*6777b538SAndroid Build Coastguard Worker   // job if any.
173*6777b538SAndroid Build Coastguard Worker   bool force_breakaway_from_job_ = false;
174*6777b538SAndroid Build Coastguard Worker 
175*6777b538SAndroid Build Coastguard Worker   // If set to true, permission to bring windows to the foreground is passed to
176*6777b538SAndroid Build Coastguard Worker   // the launched process if the current process has such permission.
177*6777b538SAndroid Build Coastguard Worker   bool grant_foreground_privilege = false;
178*6777b538SAndroid Build Coastguard Worker 
179*6777b538SAndroid Build Coastguard Worker   // If set to true, sets a process mitigation flag to disable Hardware-enforced
180*6777b538SAndroid Build Coastguard Worker   // Stack Protection for the process.
181*6777b538SAndroid Build Coastguard Worker   // This overrides /cetcompat if set on the executable. See:
182*6777b538SAndroid Build Coastguard Worker   // https://docs.microsoft.com/en-us/cpp/build/reference/cetcompat?view=msvc-160
183*6777b538SAndroid Build Coastguard Worker   // If not supported by Windows, has no effect. This flag weakens security by
184*6777b538SAndroid Build Coastguard Worker   // turning off ROP protection.
185*6777b538SAndroid Build Coastguard Worker   bool disable_cetcompat = false;
186*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
187*6777b538SAndroid Build Coastguard Worker   // Remap file descriptors according to the mapping of src_fd->dest_fd to
188*6777b538SAndroid Build Coastguard Worker   // propagate FDs into the child process.
189*6777b538SAndroid Build Coastguard Worker   FileHandleMappingVector fds_to_remap;
190*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_WIN)
191*6777b538SAndroid Build Coastguard Worker 
192*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
193*6777b538SAndroid Build Coastguard Worker   // Set/unset environment variables. These are applied on top of the parent
194*6777b538SAndroid Build Coastguard Worker   // process environment.  Empty (the default) means to inherit the same
195*6777b538SAndroid Build Coastguard Worker   // environment. See internal::AlterEnvironment().
196*6777b538SAndroid Build Coastguard Worker   EnvironmentMap environment;
197*6777b538SAndroid Build Coastguard Worker 
198*6777b538SAndroid Build Coastguard Worker   // Clear the environment for the new process before processing changes from
199*6777b538SAndroid Build Coastguard Worker   // |environment|.
200*6777b538SAndroid Build Coastguard Worker   bool clear_environment = false;
201*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
202*6777b538SAndroid Build Coastguard Worker 
203*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
204*6777b538SAndroid Build Coastguard Worker   // If non-zero, start the process using clone(), using flags as provided.
205*6777b538SAndroid Build Coastguard Worker   // Unlike in clone, clone_flags may not contain a custom termination signal
206*6777b538SAndroid Build Coastguard Worker   // that is sent to the parent when the child dies. The termination signal will
207*6777b538SAndroid Build Coastguard Worker   // always be set to SIGCHLD.
208*6777b538SAndroid Build Coastguard Worker   int clone_flags = 0;
209*6777b538SAndroid Build Coastguard Worker 
210*6777b538SAndroid Build Coastguard Worker   // By default, child processes will have the PR_SET_NO_NEW_PRIVS bit set. If
211*6777b538SAndroid Build Coastguard Worker   // true, then this bit will not be set in the new child process.
212*6777b538SAndroid Build Coastguard Worker   bool allow_new_privs = false;
213*6777b538SAndroid Build Coastguard Worker 
214*6777b538SAndroid Build Coastguard Worker   // Sets parent process death signal to SIGKILL.
215*6777b538SAndroid Build Coastguard Worker   bool kill_on_parent_death = false;
216*6777b538SAndroid Build Coastguard Worker 
217*6777b538SAndroid Build Coastguard Worker   // File descriptors of the parent process with FD_CLOEXEC flag to be removed
218*6777b538SAndroid Build Coastguard Worker   // before calling exec*().
219*6777b538SAndroid Build Coastguard Worker   std::vector<int> fds_to_remove_cloexec;
220*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
221*6777b538SAndroid Build Coastguard Worker 
222*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
223*6777b538SAndroid Build Coastguard Worker   // Mach ports that will be accessible to the child process. These are not
224*6777b538SAndroid Build Coastguard Worker   // directly inherited across process creation, but they are stored by a Mach
225*6777b538SAndroid Build Coastguard Worker   // IPC server that a child process can communicate with to retrieve them.
226*6777b538SAndroid Build Coastguard Worker   //
227*6777b538SAndroid Build Coastguard Worker   // After calling LaunchProcess(), any rights that were transferred with MOVE
228*6777b538SAndroid Build Coastguard Worker   // dispositions will be consumed, even on failure.
229*6777b538SAndroid Build Coastguard Worker   //
230*6777b538SAndroid Build Coastguard Worker   // See base/mac/mach_port_rendezvous.h for details.
231*6777b538SAndroid Build Coastguard Worker   MachPortsForRendezvous mach_ports_for_rendezvous;
232*6777b538SAndroid Build Coastguard Worker 
233*6777b538SAndroid Build Coastguard Worker   // Apply a process scheduler policy to enable mitigations against CPU side-
234*6777b538SAndroid Build Coastguard Worker   // channel attacks.
235*6777b538SAndroid Build Coastguard Worker   bool enable_cpu_security_mitigations = false;
236*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
237*6777b538SAndroid Build Coastguard Worker 
238*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_MAC)
239*6777b538SAndroid Build Coastguard Worker   // When a child process is launched, the system tracks the parent process
240*6777b538SAndroid Build Coastguard Worker   // with a concept of "responsibility". The responsible process will be
241*6777b538SAndroid Build Coastguard Worker   // associated with any requests for private data stored on the system via
242*6777b538SAndroid Build Coastguard Worker   // the TCC subsystem. When launching processes that run foreign/third-party
243*6777b538SAndroid Build Coastguard Worker   // code, the responsibility for the child process should be disclaimed so
244*6777b538SAndroid Build Coastguard Worker   // that any TCC requests are not associated with the parent.
245*6777b538SAndroid Build Coastguard Worker   bool disclaim_responsibility = false;
246*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
247*6777b538SAndroid Build Coastguard Worker 
248*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_FUCHSIA)
249*6777b538SAndroid Build Coastguard Worker   // If valid, launches the application in that job object.
250*6777b538SAndroid Build Coastguard Worker   zx_handle_t job_handle = ZX_HANDLE_INVALID;
251*6777b538SAndroid Build Coastguard Worker 
252*6777b538SAndroid Build Coastguard Worker   // Specifies additional handles to transfer (not duplicate) to the child
253*6777b538SAndroid Build Coastguard Worker   // process. Each entry is an <id,handle> pair, with an |id| created using the
254*6777b538SAndroid Build Coastguard Worker   // PA_HND() macro. The child retrieves the handle
255*6777b538SAndroid Build Coastguard Worker   // |zx_take_startup_handle(id)|. The supplied handles are consumed by
256*6777b538SAndroid Build Coastguard Worker   // LaunchProcess() even on failure.
257*6777b538SAndroid Build Coastguard Worker   // Note that PA_USER1 ids are reserved for use by AddHandleToTransfer(), below
258*6777b538SAndroid Build Coastguard Worker   // and by convention PA_USER0 is reserved for use by the embedding
259*6777b538SAndroid Build Coastguard Worker   // application.
260*6777b538SAndroid Build Coastguard Worker   HandlesToTransferVector handles_to_transfer;
261*6777b538SAndroid Build Coastguard Worker 
262*6777b538SAndroid Build Coastguard Worker   // Allocates a unique id for |handle| in |handles_to_transfer|, inserts it,
263*6777b538SAndroid Build Coastguard Worker   // and returns the generated id.
264*6777b538SAndroid Build Coastguard Worker   static uint32_t AddHandleToTransfer(
265*6777b538SAndroid Build Coastguard Worker       HandlesToTransferVector* handles_to_transfer,
266*6777b538SAndroid Build Coastguard Worker       zx_handle_t handle);
267*6777b538SAndroid Build Coastguard Worker 
268*6777b538SAndroid Build Coastguard Worker   // Specifies which basic capabilities to grant to the child process.
269*6777b538SAndroid Build Coastguard Worker   // By default the child process will receive the caller's complete namespace,
270*6777b538SAndroid Build Coastguard Worker   // access to the current base::GetDefaultJob(), handles for stdio and access
271*6777b538SAndroid Build Coastguard Worker   // to the dynamic library loader.
272*6777b538SAndroid Build Coastguard Worker   // Note that the child is always provided access to the loader service.
273*6777b538SAndroid Build Coastguard Worker   uint32_t spawn_flags = FDIO_SPAWN_CLONE_NAMESPACE | FDIO_SPAWN_CLONE_STDIO |
274*6777b538SAndroid Build Coastguard Worker                          FDIO_SPAWN_CLONE_JOB;
275*6777b538SAndroid Build Coastguard Worker 
276*6777b538SAndroid Build Coastguard Worker   // Specifies paths to clone from the calling process' namespace into that of
277*6777b538SAndroid Build Coastguard Worker   // the child process. If |paths_to_clone| is empty then the process will
278*6777b538SAndroid Build Coastguard Worker   // receive either a full copy of the parent's namespace, or an empty one,
279*6777b538SAndroid Build Coastguard Worker   // depending on whether FDIO_SPAWN_CLONE_NAMESPACE is set.
280*6777b538SAndroid Build Coastguard Worker   // Process launch will fail if `paths_to_clone` and `paths_to_transfer`
281*6777b538SAndroid Build Coastguard Worker   // together contain conflicting paths (e.g. overlaps or duplicates).
282*6777b538SAndroid Build Coastguard Worker   std::vector<FilePath> paths_to_clone;
283*6777b538SAndroid Build Coastguard Worker 
284*6777b538SAndroid Build Coastguard Worker   // Specifies handles which will be installed as files or directories in the
285*6777b538SAndroid Build Coastguard Worker   // child process' namespace.
286*6777b538SAndroid Build Coastguard Worker   // Process launch will fail if `paths_to_clone` and `paths_to_transfer`
287*6777b538SAndroid Build Coastguard Worker   // together contain conflicting paths (e.g. overlaps or duplicates).
288*6777b538SAndroid Build Coastguard Worker   std::vector<PathToTransfer> paths_to_transfer;
289*6777b538SAndroid Build Coastguard Worker 
290*6777b538SAndroid Build Coastguard Worker   // Suffix that will be added to the process name. When specified process name
291*6777b538SAndroid Build Coastguard Worker   // will be set to "<binary_name><process_suffix>".
292*6777b538SAndroid Build Coastguard Worker   std::string process_name_suffix;
293*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_FUCHSIA)
294*6777b538SAndroid Build Coastguard Worker 
295*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_POSIX)
296*6777b538SAndroid Build Coastguard Worker   // If not empty, launch the specified executable instead of
297*6777b538SAndroid Build Coastguard Worker   // cmdline.GetProgram(). This is useful when it is necessary to pass a custom
298*6777b538SAndroid Build Coastguard Worker   // argv[0].
299*6777b538SAndroid Build Coastguard Worker   base::FilePath real_path;
300*6777b538SAndroid Build Coastguard Worker 
301*6777b538SAndroid Build Coastguard Worker #if !BUILDFLAG(IS_APPLE)
302*6777b538SAndroid Build Coastguard Worker   // If non-null, a delegate to be run immediately prior to executing the new
303*6777b538SAndroid Build Coastguard Worker   // program in the child process.
304*6777b538SAndroid Build Coastguard Worker   //
305*6777b538SAndroid Build Coastguard Worker   // WARNING: If LaunchProcess is called in the presence of multiple threads,
306*6777b538SAndroid Build Coastguard Worker   // code running in this delegate essentially needs to be async-signal safe
307*6777b538SAndroid Build Coastguard Worker   // (see man 7 signal for a list of allowed functions).
308*6777b538SAndroid Build Coastguard Worker   raw_ptr<PreExecDelegate> pre_exec_delegate = nullptr;
309*6777b538SAndroid Build Coastguard Worker #endif  // !BUILDFLAG(IS_APPLE)
310*6777b538SAndroid Build Coastguard Worker 
311*6777b538SAndroid Build Coastguard Worker   // Each element is an RLIMIT_* constant that should be raised to its
312*6777b538SAndroid Build Coastguard Worker   // rlim_max.  This pointer is owned by the caller and must live through
313*6777b538SAndroid Build Coastguard Worker   // the call to LaunchProcess().
314*6777b538SAndroid Build Coastguard Worker   raw_ptr<const std::vector<int>> maximize_rlimits = nullptr;
315*6777b538SAndroid Build Coastguard Worker 
316*6777b538SAndroid Build Coastguard Worker   // If true, start the process in a new process group, instead of
317*6777b538SAndroid Build Coastguard Worker   // inheriting the parent's process group.  The pgid of the child process
318*6777b538SAndroid Build Coastguard Worker   // will be the same as its pid.
319*6777b538SAndroid Build Coastguard Worker   bool new_process_group = false;
320*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_POSIX)
321*6777b538SAndroid Build Coastguard Worker 
322*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS)
323*6777b538SAndroid Build Coastguard Worker   // If non-negative, the specified file descriptor will be set as the launched
324*6777b538SAndroid Build Coastguard Worker   // process' controlling terminal.
325*6777b538SAndroid Build Coastguard Worker   int ctrl_terminal_fd = -1;
326*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_CHROMEOS)
327*6777b538SAndroid Build Coastguard Worker };
328*6777b538SAndroid Build Coastguard Worker 
329*6777b538SAndroid Build Coastguard Worker // Launch a process via the command line |cmdline|.
330*6777b538SAndroid Build Coastguard Worker // See the documentation of LaunchOptions for details on |options|.
331*6777b538SAndroid Build Coastguard Worker //
332*6777b538SAndroid Build Coastguard Worker // Returns a valid Process upon success.
333*6777b538SAndroid Build Coastguard Worker //
334*6777b538SAndroid Build Coastguard Worker // Unix-specific notes:
335*6777b538SAndroid Build Coastguard Worker // - All file descriptors open in the parent process will be closed in the
336*6777b538SAndroid Build Coastguard Worker //   child process except for any preserved by options::fds_to_remap, and
337*6777b538SAndroid Build Coastguard Worker //   stdin, stdout, and stderr. If not remapped by options::fds_to_remap,
338*6777b538SAndroid Build Coastguard Worker //   stdin is reopened as /dev/null, and the child is allowed to inherit its
339*6777b538SAndroid Build Coastguard Worker //   parent's stdout and stderr.
340*6777b538SAndroid Build Coastguard Worker // - If the first argument on the command line does not contain a slash,
341*6777b538SAndroid Build Coastguard Worker //   PATH will be searched.  (See man execvp.)
342*6777b538SAndroid Build Coastguard Worker BASE_EXPORT Process LaunchProcess(const CommandLine& cmdline,
343*6777b538SAndroid Build Coastguard Worker                                   const LaunchOptions& options);
344*6777b538SAndroid Build Coastguard Worker 
345*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
346*6777b538SAndroid Build Coastguard Worker // Windows-specific LaunchProcess that takes the command line as a
347*6777b538SAndroid Build Coastguard Worker // string.  Useful for situations where you need to control the
348*6777b538SAndroid Build Coastguard Worker // command line arguments directly, but prefer the CommandLine version
349*6777b538SAndroid Build Coastguard Worker // if launching Chrome itself. Also prefer the CommandLine version if
350*6777b538SAndroid Build Coastguard Worker // `options.elevated` is set because `cmdline` needs to be parsed for
351*6777b538SAndroid Build Coastguard Worker // ShellExecuteEx.
352*6777b538SAndroid Build Coastguard Worker //
353*6777b538SAndroid Build Coastguard Worker // The first command line argument should be the path to the process,
354*6777b538SAndroid Build Coastguard Worker // and don't forget to quote it.
355*6777b538SAndroid Build Coastguard Worker //
356*6777b538SAndroid Build Coastguard Worker // Example (including literal quotes)
357*6777b538SAndroid Build Coastguard Worker //  cmdline = "c:\windows\explorer.exe" -foo "c:\bar\"
358*6777b538SAndroid Build Coastguard Worker BASE_EXPORT Process LaunchProcess(const CommandLine::StringType& cmdline,
359*6777b538SAndroid Build Coastguard Worker                                   const LaunchOptions& options);
360*6777b538SAndroid Build Coastguard Worker 
361*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
362*6777b538SAndroid Build Coastguard Worker // A POSIX-specific version of LaunchProcess that takes an argv array
363*6777b538SAndroid Build Coastguard Worker // instead of a CommandLine.  Useful for situations where you need to
364*6777b538SAndroid Build Coastguard Worker // control the command line arguments directly, but prefer the
365*6777b538SAndroid Build Coastguard Worker // CommandLine version if launching Chrome itself.
366*6777b538SAndroid Build Coastguard Worker BASE_EXPORT Process LaunchProcess(const std::vector<std::string>& argv,
367*6777b538SAndroid Build Coastguard Worker                                   const LaunchOptions& options);
368*6777b538SAndroid Build Coastguard Worker 
369*6777b538SAndroid Build Coastguard Worker #if !BUILDFLAG(IS_APPLE)
370*6777b538SAndroid Build Coastguard Worker // Close all file descriptors, except those which are a destination in the
371*6777b538SAndroid Build Coastguard Worker // given multimap. Only call this function in a child process where you know
372*6777b538SAndroid Build Coastguard Worker // that there aren't any other threads.
373*6777b538SAndroid Build Coastguard Worker BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map);
374*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_APPLE)
375*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_WIN)
376*6777b538SAndroid Build Coastguard Worker 
377*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
378*6777b538SAndroid Build Coastguard Worker // Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION
379*6777b538SAndroid Build Coastguard Worker // BasicLimitInformation.LimitFlags to |limit_flags|.
380*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags);
381*6777b538SAndroid Build Coastguard Worker 
382*6777b538SAndroid Build Coastguard Worker // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran
383*6777b538SAndroid Build Coastguard Worker // chrome. This is not thread-safe: only call from main thread.
384*6777b538SAndroid Build Coastguard Worker BASE_EXPORT void RouteStdioToConsole(bool create_console_if_not_found);
385*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_WIN)
386*6777b538SAndroid Build Coastguard Worker 
387*6777b538SAndroid Build Coastguard Worker // Executes the application specified by |cl| and wait for it to exit. Stores
388*6777b538SAndroid Build Coastguard Worker // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true
389*6777b538SAndroid Build Coastguard Worker // on success (application launched and exited cleanly, with exit code
390*6777b538SAndroid Build Coastguard Worker // indicating success).
391*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output);
392*6777b538SAndroid Build Coastguard Worker 
393*6777b538SAndroid Build Coastguard Worker // Like GetAppOutput, but also includes stderr.
394*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutputAndError(const CommandLine& cl,
395*6777b538SAndroid Build Coastguard Worker                                       std::string* output);
396*6777b538SAndroid Build Coastguard Worker 
397*6777b538SAndroid Build Coastguard Worker // A version of |GetAppOutput()| which also returns the exit code of the
398*6777b538SAndroid Build Coastguard Worker // executed command. Returns true if the application runs and exits cleanly. If
399*6777b538SAndroid Build Coastguard Worker // this is the case the exit code of the application is available in
400*6777b538SAndroid Build Coastguard Worker // |*exit_code|.
401*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl,
402*6777b538SAndroid Build Coastguard Worker                                           std::string* output, int* exit_code);
403*6777b538SAndroid Build Coastguard Worker 
404*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
405*6777b538SAndroid Build Coastguard Worker // A Windows-specific version of GetAppOutput that takes a command line string
406*6777b538SAndroid Build Coastguard Worker // instead of a CommandLine object. Useful for situations where you need to
407*6777b538SAndroid Build Coastguard Worker // control the command line arguments directly.
408*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutput(CommandLine::StringPieceType cl,
409*6777b538SAndroid Build Coastguard Worker                               std::string* output);
410*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
411*6777b538SAndroid Build Coastguard Worker // A POSIX-specific version of GetAppOutput that takes an argv array
412*6777b538SAndroid Build Coastguard Worker // instead of a CommandLine.  Useful for situations where you need to
413*6777b538SAndroid Build Coastguard Worker // control the command line arguments directly.
414*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv,
415*6777b538SAndroid Build Coastguard Worker                               std::string* output);
416*6777b538SAndroid Build Coastguard Worker 
417*6777b538SAndroid Build Coastguard Worker // Like the above POSIX-specific version of GetAppOutput, but also includes
418*6777b538SAndroid Build Coastguard Worker // stderr.
419*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutputAndError(const std::vector<std::string>& argv,
420*6777b538SAndroid Build Coastguard Worker                                       std::string* output);
421*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_WIN)
422*6777b538SAndroid Build Coastguard Worker 
423*6777b538SAndroid Build Coastguard Worker // If supported on the platform, and the user has sufficent rights, increase
424*6777b538SAndroid Build Coastguard Worker // the current process's scheduling priority to a high priority.
425*6777b538SAndroid Build Coastguard Worker BASE_EXPORT void RaiseProcessToHighPriority();
426*6777b538SAndroid Build Coastguard Worker 
427*6777b538SAndroid Build Coastguard Worker // Creates a LaunchOptions object suitable for launching processes in a test
428*6777b538SAndroid Build Coastguard Worker // binary. This should not be called in production/released code.
429*6777b538SAndroid Build Coastguard Worker BASE_EXPORT LaunchOptions LaunchOptionsForTest();
430*6777b538SAndroid Build Coastguard Worker 
431*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
432*6777b538SAndroid Build Coastguard Worker // A wrapper for clone with fork-like behavior, meaning that it returns the
433*6777b538SAndroid Build Coastguard Worker // child's pid in the parent and 0 in the child. |flags|, |ptid|, and |ctid| are
434*6777b538SAndroid Build Coastguard Worker // as in the clone system call (the CLONE_VM flag is not supported).
435*6777b538SAndroid Build Coastguard Worker //
436*6777b538SAndroid Build Coastguard Worker // This function uses the libc clone wrapper (which updates libc's pid cache)
437*6777b538SAndroid Build Coastguard Worker // internally, so callers may expect things like getpid() to work correctly
438*6777b538SAndroid Build Coastguard Worker // after in both the child and parent.
439*6777b538SAndroid Build Coastguard Worker //
440*6777b538SAndroid Build Coastguard Worker // As with fork(), callers should be extremely careful when calling this while
441*6777b538SAndroid Build Coastguard Worker // multiple threads are running, since at the time the fork happened, the
442*6777b538SAndroid Build Coastguard Worker // threads could have been in any state (potentially holding locks, etc.).
443*6777b538SAndroid Build Coastguard Worker // Callers should most likely call execve() in the child soon after calling
444*6777b538SAndroid Build Coastguard Worker // this.
445*6777b538SAndroid Build Coastguard Worker //
446*6777b538SAndroid Build Coastguard Worker // It is unsafe to use any pthread APIs after ForkWithFlags().
447*6777b538SAndroid Build Coastguard Worker // However, performing an exec() will lift this restriction.
448*6777b538SAndroid Build Coastguard Worker BASE_EXPORT pid_t ForkWithFlags(int flags, pid_t* ptid, pid_t* ctid);
449*6777b538SAndroid Build Coastguard Worker #endif
450*6777b538SAndroid Build Coastguard Worker 
451*6777b538SAndroid Build Coastguard Worker namespace internal {
452*6777b538SAndroid Build Coastguard Worker 
453*6777b538SAndroid Build Coastguard Worker // Friend and derived class of ScopedAllowBaseSyncPrimitives which allows
454*6777b538SAndroid Build Coastguard Worker // GetAppOutputInternal() to join a process. GetAppOutputInternal() can't itself
455*6777b538SAndroid Build Coastguard Worker // be a friend of ScopedAllowBaseSyncPrimitives because it is in the anonymous
456*6777b538SAndroid Build Coastguard Worker // namespace.
457*6777b538SAndroid Build Coastguard Worker class [[maybe_unused, nodiscard]] GetAppOutputScopedAllowBaseSyncPrimitives
458*6777b538SAndroid Build Coastguard Worker     : public base::ScopedAllowBaseSyncPrimitives{};
459*6777b538SAndroid Build Coastguard Worker 
460*6777b538SAndroid Build Coastguard Worker }  // namespace internal
461*6777b538SAndroid Build Coastguard Worker 
462*6777b538SAndroid Build Coastguard Worker }  // namespace base
463*6777b538SAndroid Build Coastguard Worker 
464*6777b538SAndroid Build Coastguard Worker #endif  // BASE_PROCESS_LAUNCH_H_
465