blob: 56990778cb970e21b2c8a55a395f758e2a974fa2 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]300c3862013-07-17 18:12:402// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file contains functions for launching subprocesses.
6
7#ifndef BASE_PROCESS_LAUNCH_H_
8#define BASE_PROCESS_LAUNCH_H_
9
avibeced7c2015-12-24 06:47:5910#include <stddef.h>
11
[email protected]300c3862013-07-17 18:12:4012#include <string>
Helmut Januschkabfa62f72024-04-04 15:18:3113#include <string_view>
[email protected]300c3862013-07-17 18:12:4014#include <utility>
15#include <vector>
16
17#include "base/base_export.h"
Jan Wilken Dörrie6bdce492019-11-05 11:36:5018#include "base/command_line.h"
[email protected]b345c482013-08-30 18:00:3919#include "base/environment.h"
Lei Zhang17575082021-05-10 20:19:1820#include "base/files/file_path.h"
Wez4e861052023-07-12 12:18:5721#include "base/memory/raw_ptr.h"
rvargas6293e5b2014-12-01 22:53:0922#include "base/process/process.h"
[email protected]300c3862013-07-17 18:12:4023#include "base/process/process_handle.h"
Gabriel Charette6836c0d52021-01-11 17:40:2624#include "base/threading/thread_restrictions.h"
Dave Tapuska6cd52722023-03-06 20:25:2925#include "build/blink_buildflags.h"
avibeced7c2015-12-24 06:47:5926#include "build/build_config.h"
[email protected]300c3862013-07-17 18:12:4027
Xiaohan Wang37e81612022-01-15 18:27:0028#if BUILDFLAG(IS_WIN)
Bruce Dawson9df510e2021-07-14 15:23:0829#include "base/win/windows_types.h"
Xiaohan Wang37e81612022-01-15 18:27:0030#elif BUILDFLAG(IS_FUCHSIA)
Wez5c3c6f152018-06-09 18:24:0231#include <lib/fdio/spawn.h>
Scott Grahamfe0e9f462017-09-18 21:25:0432#include <zircon/types.h>
scottmge5a1d492017-05-24 23:41:4333#endif
34
Xiaohan Wang37e81612022-01-15 18:27:0035#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3936#include "base/posix/file_descriptor_shuffle.h"
37#endif
38
[email protected]285bc482023-03-02 22:35:3639namespace base {
40
Dave Tapuska6cd52722023-03-06 20:25:2941#if BUILDFLAG(IS_APPLE)
42class MachRendezvousPort;
43using MachPortsForRendezvous = std::map<uint32_t, MachRendezvousPort>;
44#endif
45
Xiaohan Wang37e81612022-01-15 18:27:0046#if BUILDFLAG(IS_WIN)
[email protected]991bd8a2013-12-12 18:45:4547typedef std::vector<HANDLE> HandlesToInheritVector;
Xiaohan Wang37e81612022-01-15 18:27:0048#elif BUILDFLAG(IS_FUCHSIA)
Kevin Marshallad910ae22018-06-16 05:40:5349struct PathToTransfer {
50 base::FilePath path;
51 zx_handle_t handle;
52};
Wez1603c322017-08-10 05:24:5453struct HandleToTransfer {
54 uint32_t id;
Scott Grahamfe0e9f462017-09-18 21:25:0455 zx_handle_t handle;
Wez1603c322017-08-10 05:24:5456};
57typedef std::vector<HandleToTransfer> HandlesToTransferVector;
brettw3c98c7d32017-07-25 01:44:2058typedef std::vector<std::pair<int, int>> FileHandleMappingVector;
Xiaohan Wang37e81612022-01-15 18:27:0059#elif BUILDFLAG(IS_POSIX)
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3960typedef std::vector<std::pair<int, int>> FileHandleMappingVector;
Xiaohan Wang37e81612022-01-15 18:27:0061#endif // BUILDFLAG(IS_WIN)
[email protected]300c3862013-07-17 18:12:4062
63// Options for launching a subprocess that are passed to LaunchProcess().
64// The default constructor constructs the object with default options.
[email protected]b345c482013-08-30 18:00:3965struct BASE_EXPORT LaunchOptions {
Xiaohan Wang37e81612022-01-15 18:27:0066#if (BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)) && !BUILDFLAG(IS_APPLE)
rickyza0b860b2015-01-16 18:19:3467 // Delegate to be run in between fork and exec in the subprocess (see
68 // pre_exec_delegate below)
69 class BASE_EXPORT PreExecDelegate {
70 public:
Chris Watkins091d6292017-12-13 04:25:5871 PreExecDelegate() = default;
Peter Boström7319bbd2021-09-15 22:59:3872
73 PreExecDelegate(const PreExecDelegate&) = delete;
74 PreExecDelegate& operator=(const PreExecDelegate&) = delete;
75
Chris Watkins091d6292017-12-13 04:25:5876 virtual ~PreExecDelegate() = default;
rickyza0b860b2015-01-16 18:19:3477
78 // Since this is to be run between fork and exec, and fork may have happened
79 // while multiple threads were running, this function needs to be async
80 // safe.
81 virtual void RunAsyncSafe() = 0;
rickyza0b860b2015-01-16 18:19:3482 };
Xiaohan Wang37e81612022-01-15 18:27:0083#endif // BUILDFLAG(IS_POSIX)
rickyza0b860b2015-01-16 18:19:3484
[email protected]b345c482013-08-30 18:00:3985 LaunchOptions();
vmpstr7c7877062016-02-18 22:12:2486 LaunchOptions(const LaunchOptions&);
[email protected]b345c482013-08-30 18:00:3987 ~LaunchOptions();
[email protected]300c3862013-07-17 18:12:4088
89 // If true, wait for the process to complete.
gab21691da2016-08-02 20:19:5890 bool wait = false;
[email protected]300c3862013-07-17 18:12:4091
sergeyu782055162016-04-06 08:57:5992 // If not empty, change to this directory before executing the new process.
93 base::FilePath current_directory;
94
Xiaohan Wang37e81612022-01-15 18:27:0095#if BUILDFLAG(IS_WIN)
gab21691da2016-08-02 20:19:5896 bool start_hidden = false;
[email protected]300c3862013-07-17 18:12:4097
Joe Masoncae7d8a2022-03-25 00:10:0298 // Process will be started using ShellExecuteEx instead of CreateProcess so
99 // that it is elevated. LaunchProcess with this flag will have different
100 // behaviour due to ShellExecuteEx. Some common operations like OpenProcess
101 // will fail. Currently the only other supported LaunchOptions are
102 // |start_hidden| and |wait|.
Alex Goughb2453862022-01-11 19:29:28103 bool elevated = false;
104
S. Ganeshc18d5932018-11-05 03:45:31105 // Sets STARTF_FORCEOFFFEEDBACK so that the feedback cursor is forced off
106 // while the process is starting.
107 bool feedback_cursor_off = false;
108
brettw3c98c7d32017-07-25 01:44:20109 // Windows can inherit handles when it launches child processes.
110 // See https://blogs.msdn.microsoft.com/oldnewthing/20111216-00/?p=8873
111 // for a good overview of Windows handle inheritance.
112 //
113 // Implementation note: it might be nice to implement in terms of
Arthur Sonzognie5fff99c2024-02-21 15:58:24114 // std::optional<>, but then the natural default state (vector not present)
brettw3c98c7d32017-07-25 01:44:20115 // would be "all inheritable handles" while we want "no inheritance."
116 enum class Inherit {
117 // Only those handles in |handles_to_inherit| vector are inherited. If the
118 // vector is empty, no handles are inherited. The handles in the vector must
119 // all be inheritable.
120 kSpecific,
[email protected]991bd8a2013-12-12 18:45:45121
brettw3c98c7d32017-07-25 01:44:20122 // All handles in the current process which are inheritable are inherited.
123 // In production code this flag should be used only when running
124 // short-lived, trusted binaries, because open handles from other libraries
125 // and subsystems will leak to the child process, causing errors such as
126 // open socket hangs. There are also race conditions that can cause handle
127 // over-sharing.
128 //
129 // |handles_to_inherit| must be null.
130 //
131 // DEPRECATED. THIS SHOULD NOT BE USED. Explicitly map all handles that
132 // need to be shared in new code.
133 // TODO(brettw) bug 748258: remove this.
134 kAll
135 };
136 Inherit inherit_mode = Inherit::kSpecific;
137 HandlesToInheritVector handles_to_inherit;
[email protected]300c3862013-07-17 18:12:40138
[email protected]991bd8a2013-12-12 18:45:45139 // If non-null, runs as if the user represented by the token had launched it.
[email protected]300c3862013-07-17 18:12:40140 // Whether the application is visible on the interactive desktop depends on
141 // the token belonging to an interactive logon session.
142 //
143 // To avoid hard to diagnose problems, when specified this loads the
144 // environment variables associated with the user and if this operation fails
145 // the entire call fails as well.
gab21691da2016-08-02 20:19:58146 UserTokenHandle as_user = nullptr;
[email protected]300c3862013-07-17 18:12:40147
148 // If true, use an empty string for the desktop name.
gab21691da2016-08-02 20:19:58149 bool empty_desktop_name = false;
[email protected]300c3862013-07-17 18:12:40150
[email protected]991bd8a2013-12-12 18:45:45151 // If non-null, launches the application in that job object. The process will
[email protected]300c3862013-07-17 18:12:40152 // be terminated immediately and LaunchProcess() will fail if assignment to
153 // the job object fails.
gab21691da2016-08-02 20:19:58154 HANDLE job_handle = nullptr;
[email protected]300c3862013-07-17 18:12:40155
brettw3c98c7d32017-07-25 01:44:20156 // Handles for the redirection of stdin, stdout and stderr. The caller should
157 // either set all three of them or none (i.e. there is no way to redirect
158 // stderr without redirecting stdin).
159 //
160 // The handles must be inheritable. Pseudo handles are used when stdout and
161 // stderr redirect to the console. In that case, GetFileType() will return
162 // FILE_TYPE_CHAR and they're automatically inherited by child processes. See
163 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075.aspx
164 // Otherwise, the caller must ensure that the |inherit_mode| and/or
165 // |handles_to_inherit| set so that the handles are inherited.
gab21691da2016-08-02 20:19:58166 HANDLE stdin_handle = nullptr;
167 HANDLE stdout_handle = nullptr;
168 HANDLE stderr_handle = nullptr;
[email protected]300c3862013-07-17 18:12:40169
170 // If set to true, ensures that the child process is launched with the
171 // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent
172 // job if any.
gab21691da2016-08-02 20:19:58173 bool force_breakaway_from_job_ = false;
Greg Thompson47faf202018-05-18 20:59:03174
175 // If set to true, permission to bring windows to the foreground is passed to
176 // the launched process if the current process has such permission.
177 bool grant_foreground_privilege = false;
Alex Goughd383c772021-02-17 04:39:34178
179 // If set to true, sets a process mitigation flag to disable Hardware-enforced
180 // Stack Protection for the process.
181 // This overrides /cetcompat if set on the executable. See:
182 // https://docs.microsoft.com/en-us/cpp/build/reference/cetcompat?view=msvc-160
183 // If not supported by Windows, has no effect. This flag weakens security by
184 // turning off ROP protection.
185 bool disable_cetcompat = false;
Xiaohan Wang37e81612022-01-15 18:27:00186#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
brettw3c98c7d32017-07-25 01:44:20187 // Remap file descriptors according to the mapping of src_fd->dest_fd to
188 // propagate FDs into the child process.
189 FileHandleMappingVector fds_to_remap;
Xiaohan Wang37e81612022-01-15 18:27:00190#endif // BUILDFLAG(IS_WIN)
[email protected]300c3862013-07-17 18:12:40191
Xiaohan Wang37e81612022-01-15 18:27:00192#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
David Benjamin76ee79eb2019-03-15 17:02:09193 // Set/unset environment variables. These are applied on top of the parent
194 // process environment. Empty (the default) means to inherit the same
195 // environment. See internal::AlterEnvironment().
196 EnvironmentMap environment;
197
198 // Clear the environment for the new process before processing changes from
199 // |environment|.
200 bool clear_environment = false;
Xiaohan Wang37e81612022-01-15 18:27:00201#endif // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
David Benjamin76ee79eb2019-03-15 17:02:09202
Xiaohan Wang37e81612022-01-15 18:27:00203#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
[email protected]300c3862013-07-17 18:12:40204 // If non-zero, start the process using clone(), using flags as provided.
rickyzf1eb9cc2015-01-13 22:59:48205 // Unlike in clone, clone_flags may not contain a custom termination signal
206 // that is sent to the parent when the child dies. The termination signal will
207 // always be set to SIGCHLD.
gab21691da2016-08-02 20:19:58208 int clone_flags = 0;
[email protected]d0786912014-04-09 20:06:26209
210 // By default, child processes will have the PR_SET_NO_NEW_PRIVS bit set. If
211 // true, then this bit will not be set in the new child process.
gab21691da2016-08-02 20:19:58212 bool allow_new_privs = false;
phajdan.jred5ed8f42015-03-13 21:40:13213
214 // Sets parent process death signal to SIGKILL.
gab21691da2016-08-02 20:19:58215 bool kill_on_parent_death = false;
Peter Vargabfba1072023-11-08 21:30:11216
217 // File descriptors of the parent process with FD_CLOEXEC flag to be removed
218 // before calling exec*().
219 std::vector<int> fds_to_remove_cloexec;
Xiaohan Wang37e81612022-01-15 18:27:00220#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
[email protected]300c3862013-07-17 18:12:40221
Dave Tapuska6cd52722023-03-06 20:25:29222#if BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
Robert Seseka6d59862019-03-05 16:06:47223 // Mach ports that will be accessible to the child process. These are not
224 // directly inherited across process creation, but they are stored by a Mach
225 // IPC server that a child process can communicate with to retrieve them.
226 //
227 // After calling LaunchProcess(), any rights that were transferred with MOVE
228 // dispositions will be consumed, even on failure.
229 //
230 // See base/mac/mach_port_rendezvous.h for details.
231 MachPortsForRendezvous mach_ports_for_rendezvous;
Robert Sesek34025cb92019-08-22 00:07:53232
Dave Tapuskaed322d02023-03-07 23:33:33233 // Apply a process scheduler policy to enable mitigations against CPU side-
234 // channel attacks.
235 bool enable_cpu_security_mitigations = false;
236#endif // BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
237
238#if BUILDFLAG(IS_MAC)
Robert Sesek34025cb92019-08-22 00:07:53239 // When a child process is launched, the system tracks the parent process
240 // with a concept of "responsibility". The responsible process will be
241 // associated with any requests for private data stored on the system via
242 // the TCC subsystem. When launching processes that run foreign/third-party
243 // code, the responsibility for the child process should be disclaimed so
244 // that any TCC requests are not associated with the parent.
245 bool disclaim_responsibility = false;
Dave Tapuska6cd52722023-03-06 20:25:29246#endif // BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
Robert Seseka6d59862019-03-05 16:06:47247
Xiaohan Wang37e81612022-01-15 18:27:00248#if BUILDFLAG(IS_FUCHSIA)
scottmge5a1d492017-05-24 23:41:43249 // If valid, launches the application in that job object.
Scott Grahamfe0e9f462017-09-18 21:25:04250 zx_handle_t job_handle = ZX_HANDLE_INVALID;
Wez1603c322017-08-10 05:24:54251
252 // Specifies additional handles to transfer (not duplicate) to the child
Wez0629d402018-06-06 00:26:43253 // process. Each entry is an <id,handle> pair, with an |id| created using the
Kevin Marshall40dc43602018-06-16 01:33:55254 // PA_HND() macro. The child retrieves the handle
255 // |zx_take_startup_handle(id)|. The supplied handles are consumed by
256 // LaunchProcess() even on failure.
Wez35e50b52018-12-01 01:52:44257 // Note that PA_USER1 ids are reserved for use by AddHandleToTransfer(), below
258 // and by convention PA_USER0 is reserved for use by the embedding
259 // application.
Wez1603c322017-08-10 05:24:54260 HandlesToTransferVector handles_to_transfer;
Kevin Marshall65c26702017-09-25 18:21:42261
Wez35e50b52018-12-01 01:52:44262 // Allocates a unique id for |handle| in |handles_to_transfer|, inserts it,
263 // and returns the generated id.
264 static uint32_t AddHandleToTransfer(
265 HandlesToTransferVector* handles_to_transfer,
266 zx_handle_t handle);
267
Wez0629d402018-06-06 00:26:43268 // Specifies which basic capabilities to grant to the child process.
269 // By default the child process will receive the caller's complete namespace,
Fabrice de Gans-Riberi930bbf4b2021-02-04 23:18:56270 // access to the current base::GetDefaultJob(), handles for stdio and access
271 // to the dynamic library loader.
Wez0629d402018-06-06 00:26:43272 // Note that the child is always provided access to the loader service.
273 uint32_t spawn_flags = FDIO_SPAWN_CLONE_NAMESPACE | FDIO_SPAWN_CLONE_STDIO |
274 FDIO_SPAWN_CLONE_JOB;
Kevin Marshall2bd04552018-02-01 21:23:45275
Wez0629d402018-06-06 00:26:43276 // Specifies paths to clone from the calling process' namespace into that of
Kevin Marshallad910ae22018-06-16 05:40:53277 // the child process. If |paths_to_clone| is empty then the process will
278 // receive either a full copy of the parent's namespace, or an empty one,
279 // depending on whether FDIO_SPAWN_CLONE_NAMESPACE is set.
Wez821a3b82023-04-04 17:55:32280 // Process launch will fail if `paths_to_clone` and `paths_to_transfer`
281 // together contain conflicting paths (e.g. overlaps or duplicates).
Wez4e861052023-07-12 12:18:57282 std::vector<FilePath> paths_to_clone;
Kevin Marshallad910ae22018-06-16 05:40:53283
284 // Specifies handles which will be installed as files or directories in the
Wez821a3b82023-04-04 17:55:32285 // child process' namespace.
286 // Process launch will fail if `paths_to_clone` and `paths_to_transfer`
287 // together contain conflicting paths (e.g. overlaps or duplicates).
Kevin Marshallad910ae22018-06-16 05:40:53288 std::vector<PathToTransfer> paths_to_transfer;
Sergey Ulanov53ab7dd2019-08-27 17:53:18289
290 // Suffix that will be added to the process name. When specified process name
291 // will be set to "<binary_name><process_suffix>".
292 std::string process_name_suffix;
Xiaohan Wang37e81612022-01-15 18:27:00293#endif // BUILDFLAG(IS_FUCHSIA)
scottmge5a1d492017-05-24 23:41:43294
Xiaohan Wang37e81612022-01-15 18:27:00295#if BUILDFLAG(IS_POSIX)
rkjnsn732f03d2016-10-03 17:59:54296 // If not empty, launch the specified executable instead of
297 // cmdline.GetProgram(). This is useful when it is necessary to pass a custom
298 // argv[0].
299 base::FilePath real_path;
300
Xiaohan Wang37e81612022-01-15 18:27:00301#if !BUILDFLAG(IS_APPLE)
rickyza0b860b2015-01-16 18:19:34302 // If non-null, a delegate to be run immediately prior to executing the new
303 // program in the child process.
304 //
305 // WARNING: If LaunchProcess is called in the presence of multiple threads,
306 // code running in this delegate essentially needs to be async-signal safe
307 // (see man 7 signal for a list of allowed functions).
Keishi Hattori0e45c022021-11-27 09:25:52308 raw_ptr<PreExecDelegate> pre_exec_delegate = nullptr;
Xiaohan Wang37e81612022-01-15 18:27:00309#endif // !BUILDFLAG(IS_APPLE)
Wezdc9eb2b122018-01-09 04:43:07310
311 // Each element is an RLIMIT_* constant that should be raised to its
312 // rlim_max. This pointer is owned by the caller and must live through
313 // the call to LaunchProcess().
Keishi Hattori0e45c022021-11-27 09:25:52314 raw_ptr<const std::vector<int>> maximize_rlimits = nullptr;
Wezdc9eb2b122018-01-09 04:43:07315
316 // If true, start the process in a new process group, instead of
317 // inheriting the parent's process group. The pgid of the child process
318 // will be the same as its pid.
319 bool new_process_group = false;
Xiaohan Wang37e81612022-01-15 18:27:00320#endif // BUILDFLAG(IS_POSIX)
rickyza0b860b2015-01-16 18:19:34321
Eric Willigers611cf542022-04-28 02:22:14322#if BUILDFLAG(IS_CHROMEOS)
[email protected]300c3862013-07-17 18:12:40323 // If non-negative, the specified file descriptor will be set as the launched
324 // process' controlling terminal.
gab21691da2016-08-02 20:19:58325 int ctrl_terminal_fd = -1;
Eric Willigers611cf542022-04-28 02:22:14326#endif // BUILDFLAG(IS_CHROMEOS)
[email protected]300c3862013-07-17 18:12:40327};
328
329// Launch a process via the command line |cmdline|.
330// See the documentation of LaunchOptions for details on |options|.
331//
rvargasc40cfc62014-12-02 02:46:36332// Returns a valid Process upon success.
[email protected]300c3862013-07-17 18:12:40333//
334// Unix-specific notes:
335// - All file descriptors open in the parent process will be closed in the
336// child process except for any preserved by options::fds_to_remap, and
337// stdin, stdout, and stderr. If not remapped by options::fds_to_remap,
338// stdin is reopened as /dev/null, and the child is allowed to inherit its
339// parent's stdout and stderr.
340// - If the first argument on the command line does not contain a slash,
341// PATH will be searched. (See man execvp.)
rvargasc40cfc62014-12-02 02:46:36342BASE_EXPORT Process LaunchProcess(const CommandLine& cmdline,
343 const LaunchOptions& options);
344
Xiaohan Wang37e81612022-01-15 18:27:00345#if BUILDFLAG(IS_WIN)
[email protected]300c3862013-07-17 18:12:40346// Windows-specific LaunchProcess that takes the command line as a
347// string. Useful for situations where you need to control the
348// command line arguments directly, but prefer the CommandLine version
Joe Masoncae7d8a2022-03-25 00:10:02349// if launching Chrome itself. Also prefer the CommandLine version if
350// `options.elevated` is set because `cmdline` needs to be parsed for
351// ShellExecuteEx.
[email protected]300c3862013-07-17 18:12:40352//
353// The first command line argument should be the path to the process,
354// and don't forget to quote it.
355//
356// Example (including literal quotes)
357// cmdline = "c:\windows\explorer.exe" -foo "c:\bar\"
Jan Wilken Dörrie6bdce492019-11-05 11:36:50358BASE_EXPORT Process LaunchProcess(const CommandLine::StringType& cmdline,
rvargas61812772014-12-05 03:14:54359 const LaunchOptions& options);
[email protected]300c3862013-07-17 18:12:40360
Xiaohan Wang37e81612022-01-15 18:27:00361#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]300c3862013-07-17 18:12:40362// A POSIX-specific version of LaunchProcess that takes an argv array
363// instead of a CommandLine. Useful for situations where you need to
364// control the command line arguments directly, but prefer the
365// CommandLine version if launching Chrome itself.
rvargas02a99862015-01-10 00:46:12366BASE_EXPORT Process LaunchProcess(const std::vector<std::string>& argv,
367 const LaunchOptions& options);
368
Xiaohan Wang37e81612022-01-15 18:27:00369#if !BUILDFLAG(IS_APPLE)
[email protected]300c3862013-07-17 18:12:40370// Close all file descriptors, except those which are a destination in the
371// given multimap. Only call this function in a child process where you know
372// that there aren't any other threads.
373BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map);
Xiaohan Wang37e81612022-01-15 18:27:00374#endif // BUILDFLAG(IS_APPLE)
375#endif // BUILDFLAG(IS_WIN)
[email protected]300c3862013-07-17 18:12:40376
Xiaohan Wang37e81612022-01-15 18:27:00377#if BUILDFLAG(IS_WIN)
[email protected]15db0822013-09-13 21:24:47378// Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION
379// BasicLimitInformation.LimitFlags to |limit_flags|.
380BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags);
[email protected]300c3862013-07-17 18:12:40381
382// Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran
383// chrome. This is not thread-safe: only call from main thread.
jam79dc59a2015-08-17 03:38:16384BASE_EXPORT void RouteStdioToConsole(bool create_console_if_not_found);
Xiaohan Wang37e81612022-01-15 18:27:00385#endif // BUILDFLAG(IS_WIN)
[email protected]300c3862013-07-17 18:12:40386
387// Executes the application specified by |cl| and wait for it to exit. Stores
388// the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true
389// on success (application launched and exited cleanly, with exit code
390// indicating success).
391BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output);
392
jam79dc59a2015-08-17 03:38:16393// Like GetAppOutput, but also includes stderr.
394BASE_EXPORT bool GetAppOutputAndError(const CommandLine& cl,
395 std::string* output);
396
Zijie Hee9d42a32017-07-17 20:37:55397// A version of |GetAppOutput()| which also returns the exit code of the
398// executed command. Returns true if the application runs and exits cleanly. If
399// this is the case the exit code of the application is available in
400// |*exit_code|.
401BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl,
402 std::string* output, int* exit_code);
403
Xiaohan Wang37e81612022-01-15 18:27:00404#if BUILDFLAG(IS_WIN)
[email protected]7eb6bec62013-12-05 22:41:04405// A Windows-specific version of GetAppOutput that takes a command line string
406// instead of a CommandLine object. Useful for situations where you need to
407// control the command line arguments directly.
Jan Wilken Dörrie6bdce492019-11-05 11:36:50408BASE_EXPORT bool GetAppOutput(CommandLine::StringPieceType cl,
409 std::string* output);
Xiaohan Wang37e81612022-01-15 18:27:00410#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]300c3862013-07-17 18:12:40411// A POSIX-specific version of GetAppOutput that takes an argv array
412// instead of a CommandLine. Useful for situations where you need to
413// control the command line arguments directly.
414BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv,
415 std::string* output);
416
jbudorick86c756c2017-03-29 17:33:54417// Like the above POSIX-specific version of GetAppOutput, but also includes
418// stderr.
419BASE_EXPORT bool GetAppOutputAndError(const std::vector<std::string>& argv,
420 std::string* output);
Xiaohan Wang37e81612022-01-15 18:27:00421#endif // BUILDFLAG(IS_WIN)
[email protected]300c3862013-07-17 18:12:40422
423// If supported on the platform, and the user has sufficent rights, increase
424// the current process's scheduling priority to a high priority.
425BASE_EXPORT void RaiseProcessToHighPriority();
426
[email protected]d0786912014-04-09 20:06:26427// Creates a LaunchOptions object suitable for launching processes in a test
428// binary. This should not be called in production/released code.
429BASE_EXPORT LaunchOptions LaunchOptionsForTest();
430
Xiaohan Wang37e81612022-01-15 18:27:00431#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
rickyza2f6d742015-01-21 21:57:34432// A wrapper for clone with fork-like behavior, meaning that it returns the
433// child's pid in the parent and 0 in the child. |flags|, |ptid|, and |ctid| are
434// as in the clone system call (the CLONE_VM flag is not supported).
435//
436// This function uses the libc clone wrapper (which updates libc's pid cache)
437// internally, so callers may expect things like getpid() to work correctly
Mostyn Bramley-Moored0ecd6a2017-12-06 19:13:21438// after in both the child and parent.
rickyza2f6d742015-01-21 21:57:34439//
440// As with fork(), callers should be extremely careful when calling this while
441// multiple threads are running, since at the time the fork happened, the
442// threads could have been in any state (potentially holding locks, etc.).
443// Callers should most likely call execve() in the child soon after calling
444// this.
Tom Anderson24df41952017-07-25 02:41:01445//
446// It is unsafe to use any pthread APIs after ForkWithFlags().
447// However, performing an exec() will lift this restriction.
Peter Kasting25acd5942022-07-07 17:45:15448BASE_EXPORT pid_t ForkWithFlags(int flags, pid_t* ptid, pid_t* ctid);
rickyza2f6d742015-01-21 21:57:34449#endif
450
Gabriel Charette6836c0d52021-01-11 17:40:26451namespace internal {
452
453// Friend and derived class of ScopedAllowBaseSyncPrimitives which allows
454// GetAppOutputInternal() to join a process. GetAppOutputInternal() can't itself
455// be a friend of ScopedAllowBaseSyncPrimitives because it is in the anonymous
456// namespace.
Peter Kasting89a70ae32023-03-09 02:15:39457class [[maybe_unused, nodiscard]] GetAppOutputScopedAllowBaseSyncPrimitives
458 : public base::ScopedAllowBaseSyncPrimitives{};
Gabriel Charette6836c0d52021-01-11 17:40:26459
460} // namespace internal
461
[email protected]300c3862013-07-17 18:12:40462} // namespace base
463
464#endif // BASE_PROCESS_LAUNCH_H_