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