launch.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright (c) 2013 The Chromium Authors. All rights reserved. 2// 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 10#include <set> 11#include <string> 12#include <utility> 13#include <vector> 14 15#include "base/base_export.h" 16#include "base/basictypes.h" 17#include "base/environment.h" 18#include "base/process/process_handle.h" 19#include "base/strings/string_piece.h" 20 21#if defined(OS_POSIX) 22#include "base/posix/file_descriptor_shuffle.h" 23#elif defined(OS_WIN) 24#include <windows.h> 25#include "base/win/scoped_handle.h" 26#endif 27 28class CommandLine; 29 30namespace base { 31 32typedef std::vector<std::pair<int, int> > FileHandleMappingVector; 33 34// Options for launching a subprocess that are passed to LaunchProcess(). 35// The default constructor constructs the object with default options. 36struct BASE_EXPORT LaunchOptions { 37 LaunchOptions(); 38 ~LaunchOptions(); 39 40 // If true, wait for the process to complete. 41 bool wait; 42 43#if defined(OS_WIN) 44 bool start_hidden; 45 46 // If true, the new process inherits handles from the parent. In production 47 // code this flag should be used only when running short-lived, trusted 48 // binaries, because open handles from other libraries and subsystems will 49 // leak to the child process, causing errors such as open socket hangs. 50 bool inherit_handles; 51 52 // If non-NULL, runs as if the user represented by the token had launched it. 53 // Whether the application is visible on the interactive desktop depends on 54 // the token belonging to an interactive logon session. 55 // 56 // To avoid hard to diagnose problems, when specified this loads the 57 // environment variables associated with the user and if this operation fails 58 // the entire call fails as well. 59 UserTokenHandle as_user; 60 61 // If true, use an empty string for the desktop name. 62 bool empty_desktop_name; 63 64 // If non-NULL, launches the application in that job object. The process will 65 // be terminated immediately and LaunchProcess() will fail if assignment to 66 // the job object fails. 67 HANDLE job_handle; 68 69 // Handles for the redirection of stdin, stdout and stderr. The handles must 70 // be inheritable. Caller should either set all three of them or none (i.e. 71 // there is no way to redirect stderr without redirecting stdin). The 72 // |inherit_handles| flag must be set to true when redirecting stdio stream. 73 HANDLE stdin_handle; 74 HANDLE stdout_handle; 75 HANDLE stderr_handle; 76 77 // If set to true, ensures that the child process is launched with the 78 // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent 79 // job if any. 80 bool force_breakaway_from_job_; 81#else 82 // Set/unset environment variables. Empty (the default) means to inherit 83 // the same environment. See AlterEnvironment(). 84 EnvironmentMap environ; 85 86 // If non-NULL, remap file descriptors according to the mapping of 87 // src fd->dest fd to propagate FDs into the child process. 88 // This pointer is owned by the caller and must live through the 89 // call to LaunchProcess(). 90 const FileHandleMappingVector* fds_to_remap; 91 92 // Each element is an RLIMIT_* constant that should be raised to its 93 // rlim_max. This pointer is owned by the caller and must live through 94 // the call to LaunchProcess(). 95 const std::set<int>* maximize_rlimits; 96 97 // If true, start the process in a new process group, instead of 98 // inheriting the parent's process group. The pgid of the child process 99 // will be the same as its pid. 100 bool new_process_group; 101 102#if defined(OS_LINUX) 103 // If non-zero, start the process using clone(), using flags as provided. 104 int clone_flags; 105#endif // defined(OS_LINUX) 106 107#if defined(OS_CHROMEOS) 108 // If non-negative, the specified file descriptor will be set as the launched 109 // process' controlling terminal. 110 int ctrl_terminal_fd; 111#endif // defined(OS_CHROMEOS) 112 113#endif // !defined(OS_WIN) 114}; 115 116// Launch a process via the command line |cmdline|. 117// See the documentation of LaunchOptions for details on |options|. 118// 119// Returns true upon success. 120// 121// Upon success, if |process_handle| is non-NULL, it will be filled in with the 122// handle of the launched process. NOTE: In this case, the caller is 123// responsible for closing the handle so that it doesn't leak! 124// Otherwise, the process handle will be implicitly closed. 125// 126// Unix-specific notes: 127// - All file descriptors open in the parent process will be closed in the 128// child process except for any preserved by options::fds_to_remap, and 129// stdin, stdout, and stderr. If not remapped by options::fds_to_remap, 130// stdin is reopened as /dev/null, and the child is allowed to inherit its 131// parent's stdout and stderr. 132// - If the first argument on the command line does not contain a slash, 133// PATH will be searched. (See man execvp.) 134BASE_EXPORT bool LaunchProcess(const CommandLine& cmdline, 135 const LaunchOptions& options, 136 ProcessHandle* process_handle); 137 138#if defined(OS_WIN) 139// Windows-specific LaunchProcess that takes the command line as a 140// string. Useful for situations where you need to control the 141// command line arguments directly, but prefer the CommandLine version 142// if launching Chrome itself. 143// 144// The first command line argument should be the path to the process, 145// and don't forget to quote it. 146// 147// Example (including literal quotes) 148// cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" 149BASE_EXPORT bool LaunchProcess(const string16& cmdline, 150 const LaunchOptions& options, 151 win::ScopedHandle* process_handle); 152 153#elif defined(OS_POSIX) 154// A POSIX-specific version of LaunchProcess that takes an argv array 155// instead of a CommandLine. Useful for situations where you need to 156// control the command line arguments directly, but prefer the 157// CommandLine version if launching Chrome itself. 158BASE_EXPORT bool LaunchProcess(const std::vector<std::string>& argv, 159 const LaunchOptions& options, 160 ProcessHandle* process_handle); 161 162// Close all file descriptors, except those which are a destination in the 163// given multimap. Only call this function in a child process where you know 164// that there aren't any other threads. 165BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); 166#endif // defined(OS_POSIX) 167 168#if defined(OS_WIN) 169// Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION 170// BasicLimitInformation.LimitFlags to |limit_flags|. 171BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags); 172 173// Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran 174// chrome. This is not thread-safe: only call from main thread. 175BASE_EXPORT void RouteStdioToConsole(); 176#endif // defined(OS_WIN) 177 178// Executes the application specified by |cl| and wait for it to exit. Stores 179// the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true 180// on success (application launched and exited cleanly, with exit code 181// indicating success). 182BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output); 183 184#if defined(OS_WIN) 185// A Windows-specific version of GetAppOutput that takes a command line string 186// instead of a CommandLine object. Useful for situations where you need to 187// control the command line arguments directly. 188BASE_EXPORT bool GetAppOutput(const StringPiece16& cl, std::string* output); 189#endif 190 191#if defined(OS_POSIX) 192// A POSIX-specific version of GetAppOutput that takes an argv array 193// instead of a CommandLine. Useful for situations where you need to 194// control the command line arguments directly. 195BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv, 196 std::string* output); 197 198// A restricted version of |GetAppOutput()| which (a) clears the environment, 199// and (b) stores at most |max_output| bytes; also, it doesn't search the path 200// for the command. 201BASE_EXPORT bool GetAppOutputRestricted(const CommandLine& cl, 202 std::string* output, size_t max_output); 203 204// A version of |GetAppOutput()| which also returns the exit code of the 205// executed command. Returns true if the application runs and exits cleanly. If 206// this is the case the exit code of the application is available in 207// |*exit_code|. 208BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl, 209 std::string* output, int* exit_code); 210#endif // defined(OS_POSIX) 211 212// If supported on the platform, and the user has sufficent rights, increase 213// the current process's scheduling priority to a high priority. 214BASE_EXPORT void RaiseProcessToHighPriority(); 215 216#if defined(OS_MACOSX) 217// Restore the default exception handler, setting it to Apple Crash Reporter 218// (ReportCrash). When forking and execing a new process, the child will 219// inherit the parent's exception ports, which may be set to the Breakpad 220// instance running inside the parent. The parent's Breakpad instance should 221// not handle the child's exceptions. Calling RestoreDefaultExceptionHandler 222// in the child after forking will restore the standard exception handler. 223// See http://crbug.com/20371/ for more details. 224void RestoreDefaultExceptionHandler(); 225#endif // defined(OS_MACOSX) 226 227} // namespace base 228 229#endif // BASE_PROCESS_LAUNCH_H_ 230