blob: 2b2040c7d420cc4c9e6b273c7e5907c03811c3a6 [file] [log] [blame]
[email protected]991bd8a2013-12-12 18:45:451// Copyright 2013 The Chromium Authors. All rights reserved.
[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>
13#include <utility>
14#include <vector>
15
16#include "base/base_export.h"
[email protected]b345c482013-08-30 18:00:3917#include "base/environment.h"
avibeced7c2015-12-24 06:47:5918#include "base/macros.h"
rvargas6293e5b2014-12-01 22:53:0919#include "base/process/process.h"
[email protected]300c3862013-07-17 18:12:4020#include "base/process/process_handle.h"
[email protected]7eb6bec62013-12-05 22:41:0421#include "base/strings/string_piece.h"
avibeced7c2015-12-24 06:47:5922#include "build/build_config.h"
[email protected]300c3862013-07-17 18:12:4023
24#if defined(OS_POSIX)
25#include "base/posix/file_descriptor_shuffle.h"
26#elif defined(OS_WIN)
27#include <windows.h>
28#endif
29
[email protected]300c3862013-07-17 18:12:4030namespace base {
31
[email protected]2f3b1cc2014-03-17 23:07:1532class CommandLine;
33
[email protected]991bd8a2013-12-12 18:45:4534#if defined(OS_WIN)
35typedef std::vector<HANDLE> HandlesToInheritVector;
36#endif
37// TODO(viettrungluu): Only define this on POSIX?
[email protected]300c3862013-07-17 18:12:4038typedef std::vector<std::pair<int, int> > FileHandleMappingVector;
39
40// Options for launching a subprocess that are passed to LaunchProcess().
41// The default constructor constructs the object with default options.
[email protected]b345c482013-08-30 18:00:3942struct BASE_EXPORT LaunchOptions {
rickyza0b860b2015-01-16 18:19:3443#if defined(OS_POSIX)
44 // Delegate to be run in between fork and exec in the subprocess (see
45 // pre_exec_delegate below)
46 class BASE_EXPORT PreExecDelegate {
47 public:
48 PreExecDelegate() {}
49 virtual ~PreExecDelegate() {}
50
51 // Since this is to be run between fork and exec, and fork may have happened
52 // while multiple threads were running, this function needs to be async
53 // safe.
54 virtual void RunAsyncSafe() = 0;
55
56 private:
57 DISALLOW_COPY_AND_ASSIGN(PreExecDelegate);
58 };
59#endif // defined(OS_POSIX)
60
[email protected]b345c482013-08-30 18:00:3961 LaunchOptions();
vmpstr7c7877062016-02-18 22:12:2462 LaunchOptions(const LaunchOptions&);
[email protected]b345c482013-08-30 18:00:3963 ~LaunchOptions();
[email protected]300c3862013-07-17 18:12:4064
65 // If true, wait for the process to complete.
gab21691da2016-08-02 20:19:5866 bool wait = false;
[email protected]300c3862013-07-17 18:12:4067
sergeyu782055162016-04-06 08:57:5968 // If not empty, change to this directory before executing the new process.
69 base::FilePath current_directory;
70
[email protected]300c3862013-07-17 18:12:4071#if defined(OS_WIN)
gab21691da2016-08-02 20:19:5872 bool start_hidden = false;
[email protected]300c3862013-07-17 18:12:4073
[email protected]991bd8a2013-12-12 18:45:4574 // If non-null, inherit exactly the list of handles in this vector (these
anantaf2651872016-06-16 22:21:0275 // handles must be inheritable).
gab21691da2016-08-02 20:19:5876 HandlesToInheritVector* handles_to_inherit = nullptr;
[email protected]991bd8a2013-12-12 18:45:4577
[email protected]300c3862013-07-17 18:12:4078 // If true, the new process inherits handles from the parent. In production
79 // code this flag should be used only when running short-lived, trusted
80 // binaries, because open handles from other libraries and subsystems will
81 // leak to the child process, causing errors such as open socket hangs.
[email protected]991bd8a2013-12-12 18:45:4582 // Note: If |handles_to_inherit| is non-null, this flag is ignored and only
anantaf2651872016-06-16 22:21:0283 // those handles will be inherited.
gab21691da2016-08-02 20:19:5884 bool inherit_handles = false;
[email protected]300c3862013-07-17 18:12:4085
[email protected]991bd8a2013-12-12 18:45:4586 // If non-null, runs as if the user represented by the token had launched it.
[email protected]300c3862013-07-17 18:12:4087 // Whether the application is visible on the interactive desktop depends on
88 // the token belonging to an interactive logon session.
89 //
90 // To avoid hard to diagnose problems, when specified this loads the
91 // environment variables associated with the user and if this operation fails
92 // the entire call fails as well.
gab21691da2016-08-02 20:19:5893 UserTokenHandle as_user = nullptr;
[email protected]300c3862013-07-17 18:12:4094
95 // If true, use an empty string for the desktop name.
gab21691da2016-08-02 20:19:5896 bool empty_desktop_name = false;
[email protected]300c3862013-07-17 18:12:4097
[email protected]991bd8a2013-12-12 18:45:4598 // If non-null, launches the application in that job object. The process will
[email protected]300c3862013-07-17 18:12:4099 // be terminated immediately and LaunchProcess() will fail if assignment to
100 // the job object fails.
gab21691da2016-08-02 20:19:58101 HANDLE job_handle = nullptr;
[email protected]300c3862013-07-17 18:12:40102
103 // Handles for the redirection of stdin, stdout and stderr. The handles must
104 // be inheritable. Caller should either set all three of them or none (i.e.
105 // there is no way to redirect stderr without redirecting stdin). The
106 // |inherit_handles| flag must be set to true when redirecting stdio stream.
gab21691da2016-08-02 20:19:58107 HANDLE stdin_handle = nullptr;
108 HANDLE stdout_handle = nullptr;
109 HANDLE stderr_handle = nullptr;
[email protected]300c3862013-07-17 18:12:40110
111 // If set to true, ensures that the child process is launched with the
112 // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent
113 // job if any.
gab21691da2016-08-02 20:19:58114 bool force_breakaway_from_job_ = false;
115#else // !defined(OS_WIN)
[email protected]6f38c1342014-06-05 22:16:13116 // Set/unset environment variables. These are applied on top of the parent
117 // process environment. Empty (the default) means to inherit the same
118 // environment. See AlterEnvironment().
[email protected]b345c482013-08-30 18:00:39119 EnvironmentMap environ;
[email protected]300c3862013-07-17 18:12:40120
[email protected]6f38c1342014-06-05 22:16:13121 // Clear the environment for the new process before processing changes from
122 // |environ|.
gab21691da2016-08-02 20:19:58123 bool clear_environ = false;
[email protected]6f38c1342014-06-05 22:16:13124
[email protected]991bd8a2013-12-12 18:45:45125 // If non-null, remap file descriptors according to the mapping of
[email protected]300c3862013-07-17 18:12:40126 // src fd->dest fd to propagate FDs into the child process.
127 // This pointer is owned by the caller and must live through the
128 // call to LaunchProcess().
gab21691da2016-08-02 20:19:58129 const FileHandleMappingVector* fds_to_remap = nullptr;
[email protected]300c3862013-07-17 18:12:40130
131 // Each element is an RLIMIT_* constant that should be raised to its
132 // rlim_max. This pointer is owned by the caller and must live through
133 // the call to LaunchProcess().
gab21691da2016-08-02 20:19:58134 const std::vector<int>* maximize_rlimits = nullptr;
[email protected]300c3862013-07-17 18:12:40135
136 // If true, start the process in a new process group, instead of
137 // inheriting the parent's process group. The pgid of the child process
138 // will be the same as its pid.
gab21691da2016-08-02 20:19:58139 bool new_process_group = false;
[email protected]300c3862013-07-17 18:12:40140
141#if defined(OS_LINUX)
142 // If non-zero, start the process using clone(), using flags as provided.
rickyzf1eb9cc2015-01-13 22:59:48143 // Unlike in clone, clone_flags may not contain a custom termination signal
144 // that is sent to the parent when the child dies. The termination signal will
145 // always be set to SIGCHLD.
gab21691da2016-08-02 20:19:58146 int clone_flags = 0;
[email protected]d0786912014-04-09 20:06:26147
148 // By default, child processes will have the PR_SET_NO_NEW_PRIVS bit set. If
149 // true, then this bit will not be set in the new child process.
gab21691da2016-08-02 20:19:58150 bool allow_new_privs = false;
phajdan.jred5ed8f42015-03-13 21:40:13151
152 // Sets parent process death signal to SIGKILL.
gab21691da2016-08-02 20:19:58153 bool kill_on_parent_death = false;
[email protected]300c3862013-07-17 18:12:40154#endif // defined(OS_LINUX)
155
rickyza0b860b2015-01-16 18:19:34156#if defined(OS_POSIX)
rkjnsn732f03d2016-10-03 17:59:54157 // If not empty, launch the specified executable instead of
158 // cmdline.GetProgram(). This is useful when it is necessary to pass a custom
159 // argv[0].
160 base::FilePath real_path;
161
rickyza0b860b2015-01-16 18:19:34162 // If non-null, a delegate to be run immediately prior to executing the new
163 // program in the child process.
164 //
165 // WARNING: If LaunchProcess is called in the presence of multiple threads,
166 // code running in this delegate essentially needs to be async-signal safe
167 // (see man 7 signal for a list of allowed functions).
gab21691da2016-08-02 20:19:58168 PreExecDelegate* pre_exec_delegate = nullptr;
rickyza0b860b2015-01-16 18:19:34169#endif // defined(OS_POSIX)
170
[email protected]300c3862013-07-17 18:12:40171#if defined(OS_CHROMEOS)
172 // If non-negative, the specified file descriptor will be set as the launched
173 // process' controlling terminal.
gab21691da2016-08-02 20:19:58174 int ctrl_terminal_fd = -1;
[email protected]300c3862013-07-17 18:12:40175#endif // defined(OS_CHROMEOS)
[email protected]300c3862013-07-17 18:12:40176#endif // !defined(OS_WIN)
177};
178
179// Launch a process via the command line |cmdline|.
180// See the documentation of LaunchOptions for details on |options|.
181//
rvargasc40cfc62014-12-02 02:46:36182// Returns a valid Process upon success.
[email protected]300c3862013-07-17 18:12:40183//
184// Unix-specific notes:
185// - All file descriptors open in the parent process will be closed in the
186// child process except for any preserved by options::fds_to_remap, and
187// stdin, stdout, and stderr. If not remapped by options::fds_to_remap,
188// stdin is reopened as /dev/null, and the child is allowed to inherit its
189// parent's stdout and stderr.
190// - If the first argument on the command line does not contain a slash,
191// PATH will be searched. (See man execvp.)
rvargasc40cfc62014-12-02 02:46:36192BASE_EXPORT Process LaunchProcess(const CommandLine& cmdline,
193 const LaunchOptions& options);
194
[email protected]300c3862013-07-17 18:12:40195#if defined(OS_WIN)
196// Windows-specific LaunchProcess that takes the command line as a
197// string. Useful for situations where you need to control the
198// command line arguments directly, but prefer the CommandLine version
199// if launching Chrome itself.
200//
201// The first command line argument should be the path to the process,
202// and don't forget to quote it.
203//
204// Example (including literal quotes)
205// cmdline = "c:\windows\explorer.exe" -foo "c:\bar\"
rvargas61812772014-12-05 03:14:54206BASE_EXPORT Process LaunchProcess(const string16& cmdline,
207 const LaunchOptions& options);
[email protected]300c3862013-07-17 18:12:40208
[email protected]fa01e472014-02-11 14:45:35209// Launches a process with elevated privileges. This does not behave exactly
210// like LaunchProcess as it uses ShellExecuteEx instead of CreateProcess to
211// create the process. This means the process will have elevated privileges
rvargas6293e5b2014-12-01 22:53:09212// and thus some common operations like OpenProcess will fail. Currently the
213// only supported LaunchOptions are |start_hidden| and |wait|.
214BASE_EXPORT Process LaunchElevatedProcess(const CommandLine& cmdline,
215 const LaunchOptions& options);
[email protected]fa01e472014-02-11 14:45:35216
[email protected]300c3862013-07-17 18:12:40217#elif defined(OS_POSIX)
218// A POSIX-specific version of LaunchProcess that takes an argv array
219// instead of a CommandLine. Useful for situations where you need to
220// control the command line arguments directly, but prefer the
221// CommandLine version if launching Chrome itself.
rvargas02a99862015-01-10 00:46:12222BASE_EXPORT Process LaunchProcess(const std::vector<std::string>& argv,
223 const LaunchOptions& options);
224
[email protected]300c3862013-07-17 18:12:40225// Close all file descriptors, except those which are a destination in the
226// given multimap. Only call this function in a child process where you know
227// that there aren't any other threads.
228BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map);
229#endif // defined(OS_POSIX)
230
231#if defined(OS_WIN)
[email protected]15db0822013-09-13 21:24:47232// Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION
233// BasicLimitInformation.LimitFlags to |limit_flags|.
234BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags);
[email protected]300c3862013-07-17 18:12:40235
236// Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran
237// chrome. This is not thread-safe: only call from main thread.
jam79dc59a2015-08-17 03:38:16238BASE_EXPORT void RouteStdioToConsole(bool create_console_if_not_found);
[email protected]300c3862013-07-17 18:12:40239#endif // defined(OS_WIN)
240
241// Executes the application specified by |cl| and wait for it to exit. Stores
242// the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true
243// on success (application launched and exited cleanly, with exit code
244// indicating success).
245BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output);
246
jam79dc59a2015-08-17 03:38:16247// Like GetAppOutput, but also includes stderr.
248BASE_EXPORT bool GetAppOutputAndError(const CommandLine& cl,
249 std::string* output);
250
[email protected]7eb6bec62013-12-05 22:41:04251#if defined(OS_WIN)
252// A Windows-specific version of GetAppOutput that takes a command line string
253// instead of a CommandLine object. Useful for situations where you need to
254// control the command line arguments directly.
255BASE_EXPORT bool GetAppOutput(const StringPiece16& cl, std::string* output);
256#endif
257
[email protected]300c3862013-07-17 18:12:40258#if defined(OS_POSIX)
259// A POSIX-specific version of GetAppOutput that takes an argv array
260// instead of a CommandLine. Useful for situations where you need to
261// control the command line arguments directly.
262BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv,
263 std::string* output);
264
[email protected]300c3862013-07-17 18:12:40265// A version of |GetAppOutput()| which also returns the exit code of the
266// executed command. Returns true if the application runs and exits cleanly. If
267// this is the case the exit code of the application is available in
268// |*exit_code|.
269BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl,
270 std::string* output, int* exit_code);
271#endif // defined(OS_POSIX)
272
273// If supported on the platform, and the user has sufficent rights, increase
274// the current process's scheduling priority to a high priority.
275BASE_EXPORT void RaiseProcessToHighPriority();
276
277#if defined(OS_MACOSX)
278// Restore the default exception handler, setting it to Apple Crash Reporter
279// (ReportCrash). When forking and execing a new process, the child will
280// inherit the parent's exception ports, which may be set to the Breakpad
281// instance running inside the parent. The parent's Breakpad instance should
282// not handle the child's exceptions. Calling RestoreDefaultExceptionHandler
283// in the child after forking will restore the standard exception handler.
284// See http://crbug.com/20371/ for more details.
285void RestoreDefaultExceptionHandler();
286#endif // defined(OS_MACOSX)
287
[email protected]d0786912014-04-09 20:06:26288// Creates a LaunchOptions object suitable for launching processes in a test
289// binary. This should not be called in production/released code.
290BASE_EXPORT LaunchOptions LaunchOptionsForTest();
291
rickyz179aeb7b2015-06-19 00:18:46292#if defined(OS_LINUX) || defined(OS_NACL_NONSFI)
rickyza2f6d742015-01-21 21:57:34293// A wrapper for clone with fork-like behavior, meaning that it returns the
294// child's pid in the parent and 0 in the child. |flags|, |ptid|, and |ctid| are
295// as in the clone system call (the CLONE_VM flag is not supported).
296//
297// This function uses the libc clone wrapper (which updates libc's pid cache)
298// internally, so callers may expect things like getpid() to work correctly
299// after in both the child and parent. An exception is when this code is run
300// under Valgrind. Valgrind does not support the libc clone wrapper, so the libc
301// pid cache may be incorrect after this function is called under Valgrind.
302//
303// As with fork(), callers should be extremely careful when calling this while
304// multiple threads are running, since at the time the fork happened, the
305// threads could have been in any state (potentially holding locks, etc.).
306// Callers should most likely call execve() in the child soon after calling
307// this.
308BASE_EXPORT pid_t ForkWithFlags(unsigned long flags, pid_t* ptid, pid_t* ctid);
309#endif
310
[email protected]300c3862013-07-17 18:12:40311} // namespace base
312
313#endif // BASE_PROCESS_LAUNCH_H_