xref: /aosp_15_r20/external/libchrome/base/process/launch.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker // This file contains functions for launching subprocesses.
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #ifndef BASE_PROCESS_LAUNCH_H_
8*635a8641SAndroid Build Coastguard Worker #define BASE_PROCESS_LAUNCH_H_
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include <string>
13*635a8641SAndroid Build Coastguard Worker #include <utility>
14*635a8641SAndroid Build Coastguard Worker #include <vector>
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/environment.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/process/process.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/process/process_handle.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_piece.h"
22*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
25*635a8641SAndroid Build Coastguard Worker #include <windows.h>
26*635a8641SAndroid Build Coastguard Worker #elif defined(OS_FUCHSIA)
27*635a8641SAndroid Build Coastguard Worker #include <lib/fdio/spawn.h>
28*635a8641SAndroid Build Coastguard Worker #include <zircon/types.h>
29*635a8641SAndroid Build Coastguard Worker #endif
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA)
32*635a8641SAndroid Build Coastguard Worker #include "base/posix/file_descriptor_shuffle.h"
33*635a8641SAndroid Build Coastguard Worker #endif
34*635a8641SAndroid Build Coastguard Worker 
35*635a8641SAndroid Build Coastguard Worker namespace base {
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker class CommandLine;
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
40*635a8641SAndroid Build Coastguard Worker typedef std::vector<HANDLE> HandlesToInheritVector;
41*635a8641SAndroid Build Coastguard Worker #elif defined(OS_FUCHSIA)
42*635a8641SAndroid Build Coastguard Worker struct PathToTransfer {
43*635a8641SAndroid Build Coastguard Worker   base::FilePath path;
44*635a8641SAndroid Build Coastguard Worker   zx_handle_t handle;
45*635a8641SAndroid Build Coastguard Worker };
46*635a8641SAndroid Build Coastguard Worker struct HandleToTransfer {
47*635a8641SAndroid Build Coastguard Worker   uint32_t id;
48*635a8641SAndroid Build Coastguard Worker   zx_handle_t handle;
49*635a8641SAndroid Build Coastguard Worker };
50*635a8641SAndroid Build Coastguard Worker typedef std::vector<HandleToTransfer> HandlesToTransferVector;
51*635a8641SAndroid Build Coastguard Worker typedef std::vector<std::pair<int, int>> FileHandleMappingVector;
52*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX)
53*635a8641SAndroid Build Coastguard Worker typedef std::vector<std::pair<int, int>> FileHandleMappingVector;
54*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker // Options for launching a subprocess that are passed to LaunchProcess().
57*635a8641SAndroid Build Coastguard Worker // The default constructor constructs the object with default options.
58*635a8641SAndroid Build Coastguard Worker struct BASE_EXPORT LaunchOptions {
59*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA)
60*635a8641SAndroid Build Coastguard Worker   // Delegate to be run in between fork and exec in the subprocess (see
61*635a8641SAndroid Build Coastguard Worker   // pre_exec_delegate below)
62*635a8641SAndroid Build Coastguard Worker   class BASE_EXPORT PreExecDelegate {
63*635a8641SAndroid Build Coastguard Worker    public:
64*635a8641SAndroid Build Coastguard Worker     PreExecDelegate() = default;
65*635a8641SAndroid Build Coastguard Worker     virtual ~PreExecDelegate() = default;
66*635a8641SAndroid Build Coastguard Worker 
67*635a8641SAndroid Build Coastguard Worker     // Since this is to be run between fork and exec, and fork may have happened
68*635a8641SAndroid Build Coastguard Worker     // while multiple threads were running, this function needs to be async
69*635a8641SAndroid Build Coastguard Worker     // safe.
70*635a8641SAndroid Build Coastguard Worker     virtual void RunAsyncSafe() = 0;
71*635a8641SAndroid Build Coastguard Worker 
72*635a8641SAndroid Build Coastguard Worker    private:
73*635a8641SAndroid Build Coastguard Worker     DISALLOW_COPY_AND_ASSIGN(PreExecDelegate);
74*635a8641SAndroid Build Coastguard Worker   };
75*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_POSIX)
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker   LaunchOptions();
78*635a8641SAndroid Build Coastguard Worker   LaunchOptions(const LaunchOptions&);
79*635a8641SAndroid Build Coastguard Worker   ~LaunchOptions();
80*635a8641SAndroid Build Coastguard Worker 
81*635a8641SAndroid Build Coastguard Worker   // If true, wait for the process to complete.
82*635a8641SAndroid Build Coastguard Worker   bool wait = false;
83*635a8641SAndroid Build Coastguard Worker 
84*635a8641SAndroid Build Coastguard Worker   // If not empty, change to this directory before executing the new process.
85*635a8641SAndroid Build Coastguard Worker   base::FilePath current_directory;
86*635a8641SAndroid Build Coastguard Worker 
87*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
88*635a8641SAndroid Build Coastguard Worker   bool start_hidden = false;
89*635a8641SAndroid Build Coastguard Worker 
90*635a8641SAndroid Build Coastguard Worker   // Windows can inherit handles when it launches child processes.
91*635a8641SAndroid Build Coastguard Worker   // See https://blogs.msdn.microsoft.com/oldnewthing/20111216-00/?p=8873
92*635a8641SAndroid Build Coastguard Worker   // for a good overview of Windows handle inheritance.
93*635a8641SAndroid Build Coastguard Worker   //
94*635a8641SAndroid Build Coastguard Worker   // Implementation note: it might be nice to implement in terms of
95*635a8641SAndroid Build Coastguard Worker   // base::Optional<>, but then the natural default state (vector not present)
96*635a8641SAndroid Build Coastguard Worker   // would be "all inheritable handles" while we want "no inheritance."
97*635a8641SAndroid Build Coastguard Worker   enum class Inherit {
98*635a8641SAndroid Build Coastguard Worker     // Only those handles in |handles_to_inherit| vector are inherited. If the
99*635a8641SAndroid Build Coastguard Worker     // vector is empty, no handles are inherited. The handles in the vector must
100*635a8641SAndroid Build Coastguard Worker     // all be inheritable.
101*635a8641SAndroid Build Coastguard Worker     kSpecific,
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker     // All handles in the current process which are inheritable are inherited.
104*635a8641SAndroid Build Coastguard Worker     // In production code this flag should be used only when running
105*635a8641SAndroid Build Coastguard Worker     // short-lived, trusted binaries, because open handles from other libraries
106*635a8641SAndroid Build Coastguard Worker     // and subsystems will leak to the child process, causing errors such as
107*635a8641SAndroid Build Coastguard Worker     // open socket hangs. There are also race conditions that can cause handle
108*635a8641SAndroid Build Coastguard Worker     // over-sharing.
109*635a8641SAndroid Build Coastguard Worker     //
110*635a8641SAndroid Build Coastguard Worker     // |handles_to_inherit| must be null.
111*635a8641SAndroid Build Coastguard Worker     //
112*635a8641SAndroid Build Coastguard Worker     // DEPRECATED. THIS SHOULD NOT BE USED. Explicitly map all handles that
113*635a8641SAndroid Build Coastguard Worker     // need to be shared in new code.
114*635a8641SAndroid Build Coastguard Worker     // TODO(brettw) bug 748258: remove this.
115*635a8641SAndroid Build Coastguard Worker     kAll
116*635a8641SAndroid Build Coastguard Worker   };
117*635a8641SAndroid Build Coastguard Worker   Inherit inherit_mode = Inherit::kSpecific;
118*635a8641SAndroid Build Coastguard Worker   HandlesToInheritVector handles_to_inherit;
119*635a8641SAndroid Build Coastguard Worker 
120*635a8641SAndroid Build Coastguard Worker   // If non-null, runs as if the user represented by the token had launched it.
121*635a8641SAndroid Build Coastguard Worker   // Whether the application is visible on the interactive desktop depends on
122*635a8641SAndroid Build Coastguard Worker   // the token belonging to an interactive logon session.
123*635a8641SAndroid Build Coastguard Worker   //
124*635a8641SAndroid Build Coastguard Worker   // To avoid hard to diagnose problems, when specified this loads the
125*635a8641SAndroid Build Coastguard Worker   // environment variables associated with the user and if this operation fails
126*635a8641SAndroid Build Coastguard Worker   // the entire call fails as well.
127*635a8641SAndroid Build Coastguard Worker   UserTokenHandle as_user = nullptr;
128*635a8641SAndroid Build Coastguard Worker 
129*635a8641SAndroid Build Coastguard Worker   // If true, use an empty string for the desktop name.
130*635a8641SAndroid Build Coastguard Worker   bool empty_desktop_name = false;
131*635a8641SAndroid Build Coastguard Worker 
132*635a8641SAndroid Build Coastguard Worker   // If non-null, launches the application in that job object. The process will
133*635a8641SAndroid Build Coastguard Worker   // be terminated immediately and LaunchProcess() will fail if assignment to
134*635a8641SAndroid Build Coastguard Worker   // the job object fails.
135*635a8641SAndroid Build Coastguard Worker   HANDLE job_handle = nullptr;
136*635a8641SAndroid Build Coastguard Worker 
137*635a8641SAndroid Build Coastguard Worker   // Handles for the redirection of stdin, stdout and stderr. The caller should
138*635a8641SAndroid Build Coastguard Worker   // either set all three of them or none (i.e. there is no way to redirect
139*635a8641SAndroid Build Coastguard Worker   // stderr without redirecting stdin).
140*635a8641SAndroid Build Coastguard Worker   //
141*635a8641SAndroid Build Coastguard Worker   // The handles must be inheritable. Pseudo handles are used when stdout and
142*635a8641SAndroid Build Coastguard Worker   // stderr redirect to the console. In that case, GetFileType() will return
143*635a8641SAndroid Build Coastguard Worker   // FILE_TYPE_CHAR and they're automatically inherited by child processes. See
144*635a8641SAndroid Build Coastguard Worker   // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075.aspx
145*635a8641SAndroid Build Coastguard Worker   // Otherwise, the caller must ensure that the |inherit_mode| and/or
146*635a8641SAndroid Build Coastguard Worker   // |handles_to_inherit| set so that the handles are inherited.
147*635a8641SAndroid Build Coastguard Worker   HANDLE stdin_handle = nullptr;
148*635a8641SAndroid Build Coastguard Worker   HANDLE stdout_handle = nullptr;
149*635a8641SAndroid Build Coastguard Worker   HANDLE stderr_handle = nullptr;
150*635a8641SAndroid Build Coastguard Worker 
151*635a8641SAndroid Build Coastguard Worker   // If set to true, ensures that the child process is launched with the
152*635a8641SAndroid Build Coastguard Worker   // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent
153*635a8641SAndroid Build Coastguard Worker   // job if any.
154*635a8641SAndroid Build Coastguard Worker   bool force_breakaway_from_job_ = false;
155*635a8641SAndroid Build Coastguard Worker 
156*635a8641SAndroid Build Coastguard Worker   // If set to true, permission to bring windows to the foreground is passed to
157*635a8641SAndroid Build Coastguard Worker   // the launched process if the current process has such permission.
158*635a8641SAndroid Build Coastguard Worker   bool grant_foreground_privilege = false;
159*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
160*635a8641SAndroid Build Coastguard Worker   // Set/unset environment variables. These are applied on top of the parent
161*635a8641SAndroid Build Coastguard Worker   // process environment.  Empty (the default) means to inherit the same
162*635a8641SAndroid Build Coastguard Worker   // environment. See AlterEnvironment().
163*635a8641SAndroid Build Coastguard Worker   EnvironmentMap environ;
164*635a8641SAndroid Build Coastguard Worker 
165*635a8641SAndroid Build Coastguard Worker   // Clear the environment for the new process before processing changes from
166*635a8641SAndroid Build Coastguard Worker   // |environ|.
167*635a8641SAndroid Build Coastguard Worker   bool clear_environ = false;
168*635a8641SAndroid Build Coastguard Worker 
169*635a8641SAndroid Build Coastguard Worker   // Remap file descriptors according to the mapping of src_fd->dest_fd to
170*635a8641SAndroid Build Coastguard Worker   // propagate FDs into the child process.
171*635a8641SAndroid Build Coastguard Worker   FileHandleMappingVector fds_to_remap;
172*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
173*635a8641SAndroid Build Coastguard Worker 
174*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX)
175*635a8641SAndroid Build Coastguard Worker   // If non-zero, start the process using clone(), using flags as provided.
176*635a8641SAndroid Build Coastguard Worker   // Unlike in clone, clone_flags may not contain a custom termination signal
177*635a8641SAndroid Build Coastguard Worker   // that is sent to the parent when the child dies. The termination signal will
178*635a8641SAndroid Build Coastguard Worker   // always be set to SIGCHLD.
179*635a8641SAndroid Build Coastguard Worker   int clone_flags = 0;
180*635a8641SAndroid Build Coastguard Worker 
181*635a8641SAndroid Build Coastguard Worker   // By default, child processes will have the PR_SET_NO_NEW_PRIVS bit set. If
182*635a8641SAndroid Build Coastguard Worker   // true, then this bit will not be set in the new child process.
183*635a8641SAndroid Build Coastguard Worker   bool allow_new_privs = false;
184*635a8641SAndroid Build Coastguard Worker 
185*635a8641SAndroid Build Coastguard Worker   // Sets parent process death signal to SIGKILL.
186*635a8641SAndroid Build Coastguard Worker   bool kill_on_parent_death = false;
187*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_LINUX)
188*635a8641SAndroid Build Coastguard Worker 
189*635a8641SAndroid Build Coastguard Worker #if defined(OS_FUCHSIA)
190*635a8641SAndroid Build Coastguard Worker   // If valid, launches the application in that job object.
191*635a8641SAndroid Build Coastguard Worker   zx_handle_t job_handle = ZX_HANDLE_INVALID;
192*635a8641SAndroid Build Coastguard Worker 
193*635a8641SAndroid Build Coastguard Worker   // Specifies additional handles to transfer (not duplicate) to the child
194*635a8641SAndroid Build Coastguard Worker   // process. Each entry is an <id,handle> pair, with an |id| created using the
195*635a8641SAndroid Build Coastguard Worker   // PA_HND() macro. The child retrieves the handle
196*635a8641SAndroid Build Coastguard Worker   // |zx_take_startup_handle(id)|. The supplied handles are consumed by
197*635a8641SAndroid Build Coastguard Worker   // LaunchProcess() even on failure.
198*635a8641SAndroid Build Coastguard Worker   HandlesToTransferVector handles_to_transfer;
199*635a8641SAndroid Build Coastguard Worker 
200*635a8641SAndroid Build Coastguard Worker   // Specifies which basic capabilities to grant to the child process.
201*635a8641SAndroid Build Coastguard Worker   // By default the child process will receive the caller's complete namespace,
202*635a8641SAndroid Build Coastguard Worker   // access to the current base::fuchsia::DefaultJob(), handles for stdio and
203*635a8641SAndroid Build Coastguard Worker   // access to the dynamic library loader.
204*635a8641SAndroid Build Coastguard Worker   // Note that the child is always provided access to the loader service.
205*635a8641SAndroid Build Coastguard Worker   uint32_t spawn_flags = FDIO_SPAWN_CLONE_NAMESPACE | FDIO_SPAWN_CLONE_STDIO |
206*635a8641SAndroid Build Coastguard Worker                          FDIO_SPAWN_CLONE_JOB;
207*635a8641SAndroid Build Coastguard Worker 
208*635a8641SAndroid Build Coastguard Worker   // Specifies paths to clone from the calling process' namespace into that of
209*635a8641SAndroid Build Coastguard Worker   // the child process. If |paths_to_clone| is empty then the process will
210*635a8641SAndroid Build Coastguard Worker   // receive either a full copy of the parent's namespace, or an empty one,
211*635a8641SAndroid Build Coastguard Worker   // depending on whether FDIO_SPAWN_CLONE_NAMESPACE is set.
212*635a8641SAndroid Build Coastguard Worker   std::vector<FilePath> paths_to_clone;
213*635a8641SAndroid Build Coastguard Worker 
214*635a8641SAndroid Build Coastguard Worker   // Specifies handles which will be installed as files or directories in the
215*635a8641SAndroid Build Coastguard Worker   // child process' namespace. Paths installed by |paths_to_clone| will be
216*635a8641SAndroid Build Coastguard Worker   // overridden by these entries.
217*635a8641SAndroid Build Coastguard Worker   std::vector<PathToTransfer> paths_to_transfer;
218*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_FUCHSIA)
219*635a8641SAndroid Build Coastguard Worker 
220*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX)
221*635a8641SAndroid Build Coastguard Worker   // If not empty, launch the specified executable instead of
222*635a8641SAndroid Build Coastguard Worker   // cmdline.GetProgram(). This is useful when it is necessary to pass a custom
223*635a8641SAndroid Build Coastguard Worker   // argv[0].
224*635a8641SAndroid Build Coastguard Worker   base::FilePath real_path;
225*635a8641SAndroid Build Coastguard Worker 
226*635a8641SAndroid Build Coastguard Worker   // If non-null, a delegate to be run immediately prior to executing the new
227*635a8641SAndroid Build Coastguard Worker   // program in the child process.
228*635a8641SAndroid Build Coastguard Worker   //
229*635a8641SAndroid Build Coastguard Worker   // WARNING: If LaunchProcess is called in the presence of multiple threads,
230*635a8641SAndroid Build Coastguard Worker   // code running in this delegate essentially needs to be async-signal safe
231*635a8641SAndroid Build Coastguard Worker   // (see man 7 signal for a list of allowed functions).
232*635a8641SAndroid Build Coastguard Worker   PreExecDelegate* pre_exec_delegate = nullptr;
233*635a8641SAndroid Build Coastguard Worker 
234*635a8641SAndroid Build Coastguard Worker   // Each element is an RLIMIT_* constant that should be raised to its
235*635a8641SAndroid Build Coastguard Worker   // rlim_max.  This pointer is owned by the caller and must live through
236*635a8641SAndroid Build Coastguard Worker   // the call to LaunchProcess().
237*635a8641SAndroid Build Coastguard Worker   const std::vector<int>* maximize_rlimits = nullptr;
238*635a8641SAndroid Build Coastguard Worker 
239*635a8641SAndroid Build Coastguard Worker   // If true, start the process in a new process group, instead of
240*635a8641SAndroid Build Coastguard Worker   // inheriting the parent's process group.  The pgid of the child process
241*635a8641SAndroid Build Coastguard Worker   // will be the same as its pid.
242*635a8641SAndroid Build Coastguard Worker   bool new_process_group = false;
243*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_POSIX)
244*635a8641SAndroid Build Coastguard Worker 
245*635a8641SAndroid Build Coastguard Worker #if defined(OS_CHROMEOS)
246*635a8641SAndroid Build Coastguard Worker   // If non-negative, the specified file descriptor will be set as the launched
247*635a8641SAndroid Build Coastguard Worker   // process' controlling terminal.
248*635a8641SAndroid Build Coastguard Worker   int ctrl_terminal_fd = -1;
249*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_CHROMEOS)
250*635a8641SAndroid Build Coastguard Worker };
251*635a8641SAndroid Build Coastguard Worker 
252*635a8641SAndroid Build Coastguard Worker // Launch a process via the command line |cmdline|.
253*635a8641SAndroid Build Coastguard Worker // See the documentation of LaunchOptions for details on |options|.
254*635a8641SAndroid Build Coastguard Worker //
255*635a8641SAndroid Build Coastguard Worker // Returns a valid Process upon success.
256*635a8641SAndroid Build Coastguard Worker //
257*635a8641SAndroid Build Coastguard Worker // Unix-specific notes:
258*635a8641SAndroid Build Coastguard Worker // - All file descriptors open in the parent process will be closed in the
259*635a8641SAndroid Build Coastguard Worker //   child process except for any preserved by options::fds_to_remap, and
260*635a8641SAndroid Build Coastguard Worker //   stdin, stdout, and stderr. If not remapped by options::fds_to_remap,
261*635a8641SAndroid Build Coastguard Worker //   stdin is reopened as /dev/null, and the child is allowed to inherit its
262*635a8641SAndroid Build Coastguard Worker //   parent's stdout and stderr.
263*635a8641SAndroid Build Coastguard Worker // - If the first argument on the command line does not contain a slash,
264*635a8641SAndroid Build Coastguard Worker //   PATH will be searched.  (See man execvp.)
265*635a8641SAndroid Build Coastguard Worker BASE_EXPORT Process LaunchProcess(const CommandLine& cmdline,
266*635a8641SAndroid Build Coastguard Worker                                   const LaunchOptions& options);
267*635a8641SAndroid Build Coastguard Worker 
268*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
269*635a8641SAndroid Build Coastguard Worker // Windows-specific LaunchProcess that takes the command line as a
270*635a8641SAndroid Build Coastguard Worker // string.  Useful for situations where you need to control the
271*635a8641SAndroid Build Coastguard Worker // command line arguments directly, but prefer the CommandLine version
272*635a8641SAndroid Build Coastguard Worker // if launching Chrome itself.
273*635a8641SAndroid Build Coastguard Worker //
274*635a8641SAndroid Build Coastguard Worker // The first command line argument should be the path to the process,
275*635a8641SAndroid Build Coastguard Worker // and don't forget to quote it.
276*635a8641SAndroid Build Coastguard Worker //
277*635a8641SAndroid Build Coastguard Worker // Example (including literal quotes)
278*635a8641SAndroid Build Coastguard Worker //  cmdline = "c:\windows\explorer.exe" -foo "c:\bar\"
279*635a8641SAndroid Build Coastguard Worker BASE_EXPORT Process LaunchProcess(const string16& cmdline,
280*635a8641SAndroid Build Coastguard Worker                                   const LaunchOptions& options);
281*635a8641SAndroid Build Coastguard Worker 
282*635a8641SAndroid Build Coastguard Worker // Launches a process with elevated privileges.  This does not behave exactly
283*635a8641SAndroid Build Coastguard Worker // like LaunchProcess as it uses ShellExecuteEx instead of CreateProcess to
284*635a8641SAndroid Build Coastguard Worker // create the process.  This means the process will have elevated privileges
285*635a8641SAndroid Build Coastguard Worker // and thus some common operations like OpenProcess will fail. Currently the
286*635a8641SAndroid Build Coastguard Worker // only supported LaunchOptions are |start_hidden| and |wait|.
287*635a8641SAndroid Build Coastguard Worker BASE_EXPORT Process LaunchElevatedProcess(const CommandLine& cmdline,
288*635a8641SAndroid Build Coastguard Worker                                           const LaunchOptions& options);
289*635a8641SAndroid Build Coastguard Worker 
290*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
291*635a8641SAndroid Build Coastguard Worker // A POSIX-specific version of LaunchProcess that takes an argv array
292*635a8641SAndroid Build Coastguard Worker // instead of a CommandLine.  Useful for situations where you need to
293*635a8641SAndroid Build Coastguard Worker // control the command line arguments directly, but prefer the
294*635a8641SAndroid Build Coastguard Worker // CommandLine version if launching Chrome itself.
295*635a8641SAndroid Build Coastguard Worker BASE_EXPORT Process LaunchProcess(const std::vector<std::string>& argv,
296*635a8641SAndroid Build Coastguard Worker                                   const LaunchOptions& options);
297*635a8641SAndroid Build Coastguard Worker 
298*635a8641SAndroid Build Coastguard Worker // Close all file descriptors, except those which are a destination in the
299*635a8641SAndroid Build Coastguard Worker // given multimap. Only call this function in a child process where you know
300*635a8641SAndroid Build Coastguard Worker // that there aren't any other threads.
301*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map);
302*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
303*635a8641SAndroid Build Coastguard Worker 
304*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
305*635a8641SAndroid Build Coastguard Worker // Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION
306*635a8641SAndroid Build Coastguard Worker // BasicLimitInformation.LimitFlags to |limit_flags|.
307*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags);
308*635a8641SAndroid Build Coastguard Worker 
309*635a8641SAndroid Build Coastguard Worker // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran
310*635a8641SAndroid Build Coastguard Worker // chrome. This is not thread-safe: only call from main thread.
311*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void RouteStdioToConsole(bool create_console_if_not_found);
312*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
313*635a8641SAndroid Build Coastguard Worker 
314*635a8641SAndroid Build Coastguard Worker // Executes the application specified by |cl| and wait for it to exit. Stores
315*635a8641SAndroid Build Coastguard Worker // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true
316*635a8641SAndroid Build Coastguard Worker // on success (application launched and exited cleanly, with exit code
317*635a8641SAndroid Build Coastguard Worker // indicating success).
318*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output);
319*635a8641SAndroid Build Coastguard Worker 
320*635a8641SAndroid Build Coastguard Worker // Like GetAppOutput, but also includes stderr.
321*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutputAndError(const CommandLine& cl,
322*635a8641SAndroid Build Coastguard Worker                                       std::string* output);
323*635a8641SAndroid Build Coastguard Worker 
324*635a8641SAndroid Build Coastguard Worker // A version of |GetAppOutput()| which also returns the exit code of the
325*635a8641SAndroid Build Coastguard Worker // executed command. Returns true if the application runs and exits cleanly. If
326*635a8641SAndroid Build Coastguard Worker // this is the case the exit code of the application is available in
327*635a8641SAndroid Build Coastguard Worker // |*exit_code|.
328*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl,
329*635a8641SAndroid Build Coastguard Worker                                           std::string* output, int* exit_code);
330*635a8641SAndroid Build Coastguard Worker 
331*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
332*635a8641SAndroid Build Coastguard Worker // A Windows-specific version of GetAppOutput that takes a command line string
333*635a8641SAndroid Build Coastguard Worker // instead of a CommandLine object. Useful for situations where you need to
334*635a8641SAndroid Build Coastguard Worker // control the command line arguments directly.
335*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutput(const StringPiece16& cl, std::string* output);
336*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
337*635a8641SAndroid Build Coastguard Worker // A POSIX-specific version of GetAppOutput that takes an argv array
338*635a8641SAndroid Build Coastguard Worker // instead of a CommandLine.  Useful for situations where you need to
339*635a8641SAndroid Build Coastguard Worker // control the command line arguments directly.
340*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv,
341*635a8641SAndroid Build Coastguard Worker                               std::string* output);
342*635a8641SAndroid Build Coastguard Worker 
343*635a8641SAndroid Build Coastguard Worker // Like the above POSIX-specific version of GetAppOutput, but also includes
344*635a8641SAndroid Build Coastguard Worker // stderr.
345*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool GetAppOutputAndError(const std::vector<std::string>& argv,
346*635a8641SAndroid Build Coastguard Worker                                       std::string* output);
347*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
348*635a8641SAndroid Build Coastguard Worker 
349*635a8641SAndroid Build Coastguard Worker // If supported on the platform, and the user has sufficent rights, increase
350*635a8641SAndroid Build Coastguard Worker // the current process's scheduling priority to a high priority.
351*635a8641SAndroid Build Coastguard Worker BASE_EXPORT void RaiseProcessToHighPriority();
352*635a8641SAndroid Build Coastguard Worker 
353*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
354*635a8641SAndroid Build Coastguard Worker // An implementation of LaunchProcess() that uses posix_spawn() instead of
355*635a8641SAndroid Build Coastguard Worker // fork()+exec(). This does not support the |pre_exec_delegate| and
356*635a8641SAndroid Build Coastguard Worker // |current_directory| options.
357*635a8641SAndroid Build Coastguard Worker Process LaunchProcessPosixSpawn(const std::vector<std::string>& argv,
358*635a8641SAndroid Build Coastguard Worker                                 const LaunchOptions& options);
359*635a8641SAndroid Build Coastguard Worker 
360*635a8641SAndroid Build Coastguard Worker // Restore the default exception handler, setting it to Apple Crash Reporter
361*635a8641SAndroid Build Coastguard Worker // (ReportCrash).  When forking and execing a new process, the child will
362*635a8641SAndroid Build Coastguard Worker // inherit the parent's exception ports, which may be set to the Breakpad
363*635a8641SAndroid Build Coastguard Worker // instance running inside the parent.  The parent's Breakpad instance should
364*635a8641SAndroid Build Coastguard Worker // not handle the child's exceptions.  Calling RestoreDefaultExceptionHandler
365*635a8641SAndroid Build Coastguard Worker // in the child after forking will restore the standard exception handler.
366*635a8641SAndroid Build Coastguard Worker // See http://crbug.com/20371/ for more details.
367*635a8641SAndroid Build Coastguard Worker void RestoreDefaultExceptionHandler();
368*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_MACOSX)
369*635a8641SAndroid Build Coastguard Worker 
370*635a8641SAndroid Build Coastguard Worker // Creates a LaunchOptions object suitable for launching processes in a test
371*635a8641SAndroid Build Coastguard Worker // binary. This should not be called in production/released code.
372*635a8641SAndroid Build Coastguard Worker BASE_EXPORT LaunchOptions LaunchOptionsForTest();
373*635a8641SAndroid Build Coastguard Worker 
374*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) || defined(OS_NACL_NONSFI)
375*635a8641SAndroid Build Coastguard Worker // A wrapper for clone with fork-like behavior, meaning that it returns the
376*635a8641SAndroid Build Coastguard Worker // child's pid in the parent and 0 in the child. |flags|, |ptid|, and |ctid| are
377*635a8641SAndroid Build Coastguard Worker // as in the clone system call (the CLONE_VM flag is not supported).
378*635a8641SAndroid Build Coastguard Worker //
379*635a8641SAndroid Build Coastguard Worker // This function uses the libc clone wrapper (which updates libc's pid cache)
380*635a8641SAndroid Build Coastguard Worker // internally, so callers may expect things like getpid() to work correctly
381*635a8641SAndroid Build Coastguard Worker // after in both the child and parent.
382*635a8641SAndroid Build Coastguard Worker //
383*635a8641SAndroid Build Coastguard Worker // As with fork(), callers should be extremely careful when calling this while
384*635a8641SAndroid Build Coastguard Worker // multiple threads are running, since at the time the fork happened, the
385*635a8641SAndroid Build Coastguard Worker // threads could have been in any state (potentially holding locks, etc.).
386*635a8641SAndroid Build Coastguard Worker // Callers should most likely call execve() in the child soon after calling
387*635a8641SAndroid Build Coastguard Worker // this.
388*635a8641SAndroid Build Coastguard Worker //
389*635a8641SAndroid Build Coastguard Worker // It is unsafe to use any pthread APIs after ForkWithFlags().
390*635a8641SAndroid Build Coastguard Worker // However, performing an exec() will lift this restriction.
391*635a8641SAndroid Build Coastguard Worker BASE_EXPORT pid_t ForkWithFlags(unsigned long flags, pid_t* ptid, pid_t* ctid);
392*635a8641SAndroid Build Coastguard Worker #endif
393*635a8641SAndroid Build Coastguard Worker 
394*635a8641SAndroid Build Coastguard Worker }  // namespace base
395*635a8641SAndroid Build Coastguard Worker 
396*635a8641SAndroid Build Coastguard Worker #endif  // BASE_PROCESS_LAUNCH_H_
397