15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
29ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
39ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// found in the LICENSE file.
49ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
59ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// This file contains functions for launching subprocesses.
69ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
79ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#ifndef BASE_PROCESS_LAUNCH_H_
89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#define BASE_PROCESS_LAUNCH_H_
99ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include <string>
119ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include <utility>
129ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include <vector>
139ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
149ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/base_export.h"
159ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/basictypes.h"
1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/environment.h"
179ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/process/process_handle.h"
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/strings/string_piece.h"
199ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
209ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_POSIX)
219ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/posix/file_descriptor_shuffle.h"
229ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#elif defined(OS_WIN)
239ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include <windows.h>
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/win/scoped_handle.h"
259ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif
269ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
279ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochnamespace base {
289ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class CommandLine;
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_WIN)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef std::vector<HANDLE> HandlesToInheritVector;
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// TODO(viettrungluu): Only define this on POSIX?
359ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochtypedef std::vector<std::pair<int, int> > FileHandleMappingVector;
369ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
379ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Options for launching a subprocess that are passed to LaunchProcess().
389ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// The default constructor constructs the object with default options.
3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)struct BASE_EXPORT LaunchOptions {
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  LaunchOptions();
4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ~LaunchOptions();
429ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
439ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // If true, wait for the process to complete.
449ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  bool wait;
459ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
469ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_WIN)
479ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  bool start_hidden;
489ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If non-null, inherit exactly the list of handles in this vector (these
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // handles must be inheritable). This is only supported on Vista and higher.
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  HandlesToInheritVector* handles_to_inherit;
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
539ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // If true, the new process inherits handles from the parent. In production
549ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // code this flag should be used only when running short-lived, trusted
559ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // binaries, because open handles from other libraries and subsystems will
569ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // leak to the child process, causing errors such as open socket hangs.
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Note: If |handles_to_inherit| is non-null, this flag is ignored and only
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // those handles will be inherited (on Vista and higher).
599ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  bool inherit_handles;
609ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If non-null, runs as if the user represented by the token had launched it.
629ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // Whether the application is visible on the interactive desktop depends on
639ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // the token belonging to an interactive logon session.
649ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  //
659ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // To avoid hard to diagnose problems, when specified this loads the
669ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // environment variables associated with the user and if this operation fails
679ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // the entire call fails as well.
689ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  UserTokenHandle as_user;
699ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
709ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // If true, use an empty string for the desktop name.
719ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  bool empty_desktop_name;
729ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If non-null, launches the application in that job object. The process will
749ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // be terminated immediately and LaunchProcess() will fail if assignment to
759ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // the job object fails.
769ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  HANDLE job_handle;
779ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
789ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // Handles for the redirection of stdin, stdout and stderr. The handles must
799ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // be inheritable. Caller should either set all three of them or none (i.e.
809ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // there is no way to redirect stderr without redirecting stdin). The
819ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // |inherit_handles| flag must be set to true when redirecting stdio stream.
829ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  HANDLE stdin_handle;
839ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  HANDLE stdout_handle;
849ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  HANDLE stderr_handle;
859ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
869ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // If set to true, ensures that the child process is launched with the
879ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent
889ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // job if any.
899ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  bool force_breakaway_from_job_;
909ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#else
9146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Set/unset environment variables. These are applied on top of the parent
9246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // process environment.  Empty (the default) means to inherit the same
9346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // environment. See AlterEnvironment().
9458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EnvironmentMap environ;
959ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
9646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Clear the environment for the new process before processing changes from
9746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // |environ|.
9846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool clear_environ;
9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If non-null, remap file descriptors according to the mapping of
1019ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // src fd->dest fd to propagate FDs into the child process.
1029ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // This pointer is owned by the caller and must live through the
1039ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // call to LaunchProcess().
1049ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  const FileHandleMappingVector* fds_to_remap;
1059ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1069ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // Each element is an RLIMIT_* constant that should be raised to its
1079ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // rlim_max.  This pointer is owned by the caller and must live through
1089ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // the call to LaunchProcess().
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::vector<int>* maximize_rlimits;
1109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1119ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // If true, start the process in a new process group, instead of
1129ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // inheriting the parent's process group.  The pgid of the child process
1139ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // will be the same as its pid.
1149ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  bool new_process_group;
1159ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1169ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_LINUX)
1179ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // If non-zero, start the process using clone(), using flags as provided.
1189ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  int clone_flags;
119c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
120c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // By default, child processes will have the PR_SET_NO_NEW_PRIVS bit set. If
121c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // true, then this bit will not be set in the new child process.
122c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool allow_new_privs;
1239ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif  // defined(OS_LINUX)
1249ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1259ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_CHROMEOS)
1269ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // If non-negative, the specified file descriptor will be set as the launched
1279ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // process' controlling terminal.
1289ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  int ctrl_terminal_fd;
1299ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif  // defined(OS_CHROMEOS)
1309ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#if defined(OS_MACOSX)
1326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // If this name is non-empty, the new child, after fork() but before exec(),
1336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // will look up this server name in the bootstrap namespace. The resulting
1346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // service port will be replaced as the bootstrap port in the child. Because
1356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // the process's IPC space is cleared on exec(), any rights to the old
1366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // bootstrap port will not be transferred to the new process.
1376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  std::string replacement_bootstrap_name;
1386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#endif
1396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1409ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif  // !defined(OS_WIN)
1419ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch};
1429ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1439ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Launch a process via the command line |cmdline|.
1449ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// See the documentation of LaunchOptions for details on |options|.
1459ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//
1469ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Returns true upon success.
1479ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Upon success, if |process_handle| is non-null, it will be filled in with the
1499ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// handle of the launched process.  NOTE: In this case, the caller is
1509ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// responsible for closing the handle so that it doesn't leak!
1519ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Otherwise, the process handle will be implicitly closed.
1529ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//
1539ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Unix-specific notes:
1549ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// - All file descriptors open in the parent process will be closed in the
1559ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//   child process except for any preserved by options::fds_to_remap, and
1569ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//   stdin, stdout, and stderr. If not remapped by options::fds_to_remap,
1579ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//   stdin is reopened as /dev/null, and the child is allowed to inherit its
1589ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//   parent's stdout and stderr.
1599ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// - If the first argument on the command line does not contain a slash,
1609ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//   PATH will be searched.  (See man execvp.)
1619ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT bool LaunchProcess(const CommandLine& cmdline,
1629ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch                               const LaunchOptions& options,
1639ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch                               ProcessHandle* process_handle);
1649ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1659ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_WIN)
1669ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Windows-specific LaunchProcess that takes the command line as a
1679ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// string.  Useful for situations where you need to control the
1689ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// command line arguments directly, but prefer the CommandLine version
1699ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// if launching Chrome itself.
1709ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//
1719ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// The first command line argument should be the path to the process,
1729ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// and don't forget to quote it.
1739ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//
1749ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Example (including literal quotes)
1759ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//  cmdline = "c:\windows\explorer.exe" -foo "c:\bar\"
1769ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT bool LaunchProcess(const string16& cmdline,
1779ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch                               const LaunchOptions& options,
178f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                               win::ScopedHandle* process_handle);
1799ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Launches a process with elevated privileges.  This does not behave exactly
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// like LaunchProcess as it uses ShellExecuteEx instead of CreateProcess to
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// create the process.  This means the process will have elevated privileges
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// and thus some common operations like OpenProcess will fail. The process will
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// be available through the |process_handle| argument.  Currently the only
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// supported LaunchOptions are |start_hidden| and |wait|.
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)BASE_EXPORT bool LaunchElevatedProcess(const CommandLine& cmdline,
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       const LaunchOptions& options,
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       ProcessHandle* process_handle);
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1909ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#elif defined(OS_POSIX)
1919ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// A POSIX-specific version of LaunchProcess that takes an argv array
1929ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// instead of a CommandLine.  Useful for situations where you need to
1939ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// control the command line arguments directly, but prefer the
1949ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// CommandLine version if launching Chrome itself.
1959ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT bool LaunchProcess(const std::vector<std::string>& argv,
1969ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch                               const LaunchOptions& options,
1979ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch                               ProcessHandle* process_handle);
1989ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1999ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Close all file descriptors, except those which are a destination in the
2009ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// given multimap. Only call this function in a child process where you know
2019ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// that there aren't any other threads.
2029ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map);
2039ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif  // defined(OS_POSIX)
2049ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
2059ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_WIN)
206d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION
207d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// BasicLimitInformation.LimitFlags to |limit_flags|.
208d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags);
2099ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
2109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran
2119ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// chrome. This is not thread-safe: only call from main thread.
2129ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT void RouteStdioToConsole();
2139ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif  // defined(OS_WIN)
2149ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
2159ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Executes the application specified by |cl| and wait for it to exit. Stores
2169ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true
2179ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// on success (application launched and exited cleanly, with exit code
2189ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// indicating success).
2199ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output);
2209ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
221a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#if defined(OS_WIN)
222a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// A Windows-specific version of GetAppOutput that takes a command line string
223a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// instead of a CommandLine object. Useful for situations where you need to
224a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// control the command line arguments directly.
225a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)BASE_EXPORT bool GetAppOutput(const StringPiece16& cl, std::string* output);
226a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
227a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
2289ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_POSIX)
2299ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// A POSIX-specific version of GetAppOutput that takes an argv array
2309ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// instead of a CommandLine.  Useful for situations where you need to
2319ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// control the command line arguments directly.
2329ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv,
2339ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch                              std::string* output);
2349ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
2359ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// A restricted version of |GetAppOutput()| which (a) clears the environment,
2369ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// and (b) stores at most |max_output| bytes; also, it doesn't search the path
2379ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// for the command.
2389ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT bool GetAppOutputRestricted(const CommandLine& cl,
2399ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch                                        std::string* output, size_t max_output);
2409ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
2419ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// A version of |GetAppOutput()| which also returns the exit code of the
2429ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// executed command. Returns true if the application runs and exits cleanly. If
2439ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// this is the case the exit code of the application is available in
2449ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// |*exit_code|.
2459ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl,
2469ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch                                          std::string* output, int* exit_code);
2479ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif  // defined(OS_POSIX)
2489ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
2499ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// If supported on the platform, and the user has sufficent rights, increase
2509ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// the current process's scheduling priority to a high priority.
2519ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochBASE_EXPORT void RaiseProcessToHighPriority();
2529ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
2539ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_MACOSX)
2549ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Restore the default exception handler, setting it to Apple Crash Reporter
2559ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// (ReportCrash).  When forking and execing a new process, the child will
2569ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// inherit the parent's exception ports, which may be set to the Breakpad
2579ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// instance running inside the parent.  The parent's Breakpad instance should
2589ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// not handle the child's exceptions.  Calling RestoreDefaultExceptionHandler
2599ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// in the child after forking will restore the standard exception handler.
2609ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// See http://crbug.com/20371/ for more details.
2619ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochvoid RestoreDefaultExceptionHandler();
2626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Look up the bootstrap server named |replacement_bootstrap_name| via the
2646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// current |bootstrap_port|. Then replace the task's bootstrap port with the
2656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// received right.
2666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void ReplaceBootstrapPort(const std::string& replacement_bootstrap_name);
2679ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif  // defined(OS_MACOSX)
2689ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
269c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch// Creates a LaunchOptions object suitable for launching processes in a test
270c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch// binary. This should not be called in production/released code.
271c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen MurdochBASE_EXPORT LaunchOptions LaunchOptionsForTest();
272c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
2739ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}  // namespace base
2749ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
2759ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif  // BASE_PROCESS_LAUNCH_H_
276