Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file contains functions for launching subprocesses. | 5 // This file contains functions for launching subprocesses. |
| 6 | 6 |
| 7 #ifndef BASE_PROCESS_LAUNCH_H_ | 7 #ifndef BASE_PROCESS_LAUNCH_H_ |
| 8 #define BASE_PROCESS_LAUNCH_H_ | 8 #define BASE_PROCESS_LAUNCH_H_ |
| 9 | 9 |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/base_export.h" | 16 #include "base/base_export.h" |
| 17 #include "base/environment.h" | 17 #include "base/environment.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/optional.h" | |
|
grt (UTC plus 2)
2017/06/27 08:01:44
unused?
brettw
2017/06/28 21:42:21
Oh yeah, Optional was my first attempt :/
| |
| 19 #include "base/process/process.h" | 20 #include "base/process/process.h" |
| 20 #include "base/process/process_handle.h" | 21 #include "base/process/process_handle.h" |
| 21 #include "base/strings/string_piece.h" | 22 #include "base/strings/string_piece.h" |
| 22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 23 | 24 |
| 24 #if defined(OS_POSIX) | 25 #if defined(OS_POSIX) |
| 25 #include "base/posix/file_descriptor_shuffle.h" | 26 #include "base/posix/file_descriptor_shuffle.h" |
| 26 #elif defined(OS_WIN) | 27 #elif defined(OS_WIN) |
| 27 #include <windows.h> | 28 #include <windows.h> |
| 28 #endif | 29 #endif |
| 29 | 30 |
| 30 #if defined(OS_FUCHSIA) | 31 #if defined(OS_FUCHSIA) |
| 31 #include <magenta/types.h> | 32 #include <magenta/types.h> |
| 32 #endif | 33 #endif |
| 33 | 34 |
| 34 namespace base { | 35 namespace base { |
| 35 | 36 |
| 36 class CommandLine; | 37 class CommandLine; |
| 37 | 38 |
| 38 #if defined(OS_WIN) | 39 #if defined(OS_POSIX) |
| 39 typedef std::vector<HANDLE> HandlesToInheritVector; | 40 typedef std::vector<std::pair<int, int>> FileHandleMappingVector; |
| 40 #endif | 41 #endif |
| 41 // TODO(viettrungluu): Only define this on POSIX? | |
| 42 typedef std::vector<std::pair<int, int> > FileHandleMappingVector; | |
| 43 | 42 |
| 44 // Options for launching a subprocess that are passed to LaunchProcess(). | 43 // Options for launching a subprocess that are passed to LaunchProcess(). |
| 45 // The default constructor constructs the object with default options. | 44 // The default constructor constructs the object with default options. |
| 46 struct BASE_EXPORT LaunchOptions { | 45 struct BASE_EXPORT LaunchOptions { |
| 47 #if defined(OS_POSIX) | 46 #if defined(OS_POSIX) |
| 48 // Delegate to be run in between fork and exec in the subprocess (see | 47 // Delegate to be run in between fork and exec in the subprocess (see |
| 49 // pre_exec_delegate below) | 48 // pre_exec_delegate below) |
| 50 class BASE_EXPORT PreExecDelegate { | 49 class BASE_EXPORT PreExecDelegate { |
| 51 public: | 50 public: |
| 52 PreExecDelegate() {} | 51 PreExecDelegate() {} |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 68 | 67 |
| 69 // If true, wait for the process to complete. | 68 // If true, wait for the process to complete. |
| 70 bool wait = false; | 69 bool wait = false; |
| 71 | 70 |
| 72 // If not empty, change to this directory before executing the new process. | 71 // If not empty, change to this directory before executing the new process. |
| 73 base::FilePath current_directory; | 72 base::FilePath current_directory; |
| 74 | 73 |
| 75 #if defined(OS_WIN) | 74 #if defined(OS_WIN) |
| 76 bool start_hidden = false; | 75 bool start_hidden = false; |
| 77 | 76 |
| 78 // If non-null, inherit exactly the list of handles in this vector (these | 77 // Windows can inherit handles when it launches child processes. |
| 79 // handles must be inheritable). | 78 // See https://blogs.msdn.microsoft.com/oldnewthing/20111216-00/?p=8873 |
| 80 HandlesToInheritVector* handles_to_inherit = nullptr; | 79 // for a good overview of Windows handle inheritance. |
| 80 // | |
| 81 // Implementation note: it might be nice to implement in terms of | |
| 82 // base::Optional<>, but then the natural default state (vector not present) | |
| 83 // would be "all inheritable handles" while we want "no inheritance." | |
| 84 enum InheritMode { | |
|
grt (UTC plus 2)
2017/06/27 08:01:44
nit: enum class?
brettw
2017/06/28 21:42:21
Done.
| |
| 85 // Only those handles in |handles_to_inherit| vector are inherited. If the | |
| 86 // vector is empty, no handles are inherited. The handles in the vector must | |
| 87 // all be inheritable. | |
| 88 INHERIT_SPECIFIC, | |
|
grt (UTC plus 2)
2017/06/27 08:01:44
nit: kFoo is now preferred (https://groups.google.
brettw
2017/06/28 21:42:21
Done.
| |
| 81 | 89 |
| 82 // If true, the new process inherits handles from the parent. In production | 90 // All handles in the current process which are inheritable are inherited. |
| 83 // code this flag should be used only when running short-lived, trusted | 91 // In production code this flag should be used only when running |
| 84 // binaries, because open handles from other libraries and subsystems will | 92 // short-lived, trusted binaries, because open handles from other libraries |
| 85 // leak to the child process, causing errors such as open socket hangs. | 93 // and subsystems will leak to the child process, causing errors such as |
| 86 // Note: If |handles_to_inherit| is non-null, this flag is ignored and only | 94 // open socket hangs. There are also race conditions that can cause handle |
| 87 // those handles will be inherited. | 95 // over-sharing. |
| 88 bool inherit_handles = false; | 96 // |
| 97 // |handles_to_inherit| must be null. | |
| 98 INHERIT_ALL | |
| 99 }; | |
| 100 InheritMode inherit_mode = INHERIT_SPECIFIC; | |
| 101 std::vector<HANDLE> handles_to_inherit; | |
| 89 | 102 |
| 90 // If non-null, runs as if the user represented by the token had launched it. | 103 // If non-null, runs as if the user represented by the token had launched it. |
| 91 // Whether the application is visible on the interactive desktop depends on | 104 // Whether the application is visible on the interactive desktop depends on |
| 92 // the token belonging to an interactive logon session. | 105 // the token belonging to an interactive logon session. |
| 93 // | 106 // |
| 94 // To avoid hard to diagnose problems, when specified this loads the | 107 // To avoid hard to diagnose problems, when specified this loads the |
| 95 // environment variables associated with the user and if this operation fails | 108 // environment variables associated with the user and if this operation fails |
| 96 // the entire call fails as well. | 109 // the entire call fails as well. |
| 97 UserTokenHandle as_user = nullptr; | 110 UserTokenHandle as_user = nullptr; |
| 98 | 111 |
| 99 // If true, use an empty string for the desktop name. | 112 // If true, use an empty string for the desktop name. |
| 100 bool empty_desktop_name = false; | 113 bool empty_desktop_name = false; |
| 101 | 114 |
| 102 // If non-null, launches the application in that job object. The process will | 115 // If non-null, launches the application in that job object. The process will |
| 103 // be terminated immediately and LaunchProcess() will fail if assignment to | 116 // be terminated immediately and LaunchProcess() will fail if assignment to |
| 104 // the job object fails. | 117 // the job object fails. |
| 105 HANDLE job_handle = nullptr; | 118 HANDLE job_handle = nullptr; |
| 106 | 119 |
| 107 // Handles for the redirection of stdin, stdout and stderr. The handles must | 120 // Handles for the redirection of stdin, stdout and stderr. The caller should |
| 108 // be inheritable. Caller should either set all three of them or none (i.e. | 121 // either set all three of them or none (i.e. there is no way to redirect |
| 109 // there is no way to redirect stderr without redirecting stdin). The | 122 // stderr without redirecting stdin). |
| 110 // |inherit_handles| flag must be set to true when redirecting stdio stream. | 123 // |
| 124 // The handles must be inheritable. Pseudo handles are used when stdout and | |
| 125 // stderr redirect to the console. In that case, GetFileType() will return | |
| 126 // FILE_TYPE_CHAR and they're automatically inherited by child processes. See | |
| 127 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075.aspx | |
| 128 // Otherwise, the caller must ensure that the |inherit_mode| and/or | |
| 129 // |handles_to_inherit| set so that the handles are inherited. | |
| 111 HANDLE stdin_handle = nullptr; | 130 HANDLE stdin_handle = nullptr; |
| 112 HANDLE stdout_handle = nullptr; | 131 HANDLE stdout_handle = nullptr; |
| 113 HANDLE stderr_handle = nullptr; | 132 HANDLE stderr_handle = nullptr; |
| 114 | 133 |
| 115 // If set to true, ensures that the child process is launched with the | 134 // If set to true, ensures that the child process is launched with the |
| 116 // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent | 135 // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent |
| 117 // job if any. | 136 // job if any. |
| 118 bool force_breakaway_from_job_ = false; | 137 bool force_breakaway_from_job_ = false; |
| 119 #else // !defined(OS_WIN) | 138 #else // !defined(OS_WIN) |
| 120 // Set/unset environment variables. These are applied on top of the parent | 139 // Set/unset environment variables. These are applied on top of the parent |
| 121 // process environment. Empty (the default) means to inherit the same | 140 // process environment. Empty (the default) means to inherit the same |
| 122 // environment. See AlterEnvironment(). | 141 // environment. See AlterEnvironment(). |
| 123 EnvironmentMap environ; | 142 EnvironmentMap environ; |
| 124 | 143 |
| 125 // Clear the environment for the new process before processing changes from | 144 // Clear the environment for the new process before processing changes from |
| 126 // |environ|. | 145 // |environ|. |
| 127 bool clear_environ = false; | 146 bool clear_environ = false; |
| 128 | 147 |
| 129 // If non-null, remap file descriptors according to the mapping of | 148 // Remap file descriptors according to the mapping of src_fd->dest_fd to |
| 130 // src fd->dest fd to propagate FDs into the child process. | 149 // propagate FDs into the child process. |
| 131 // This pointer is owned by the caller and must live through the | 150 FileHandleMappingVector fds_to_remap; |
| 132 // call to LaunchProcess(). | |
| 133 const FileHandleMappingVector* fds_to_remap = nullptr; | |
| 134 | 151 |
| 135 // Each element is an RLIMIT_* constant that should be raised to its | 152 // Each element is an RLIMIT_* constant that should be raised to its |
| 136 // rlim_max. This pointer is owned by the caller and must live through | 153 // rlim_max. This pointer is owned by the caller and must live through |
| 137 // the call to LaunchProcess(). | 154 // the call to LaunchProcess(). |
| 138 const std::vector<int>* maximize_rlimits = nullptr; | 155 const std::vector<int>* maximize_rlimits = nullptr; |
| 139 | 156 |
| 140 // If true, start the process in a new process group, instead of | 157 // If true, start the process in a new process group, instead of |
| 141 // inheriting the parent's process group. The pgid of the child process | 158 // inheriting the parent's process group. The pgid of the child process |
| 142 // will be the same as its pid. | 159 // will be the same as its pid. |
| 143 bool new_process_group = false; | 160 bool new_process_group = false; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 // multiple threads are running, since at the time the fork happened, the | 341 // multiple threads are running, since at the time the fork happened, the |
| 325 // threads could have been in any state (potentially holding locks, etc.). | 342 // threads could have been in any state (potentially holding locks, etc.). |
| 326 // Callers should most likely call execve() in the child soon after calling | 343 // Callers should most likely call execve() in the child soon after calling |
| 327 // this. | 344 // this. |
| 328 BASE_EXPORT pid_t ForkWithFlags(unsigned long flags, pid_t* ptid, pid_t* ctid); | 345 BASE_EXPORT pid_t ForkWithFlags(unsigned long flags, pid_t* ptid, pid_t* ctid); |
| 329 #endif | 346 #endif |
| 330 | 347 |
| 331 } // namespace base | 348 } // namespace base |
| 332 | 349 |
| 333 #endif // BASE_PROCESS_LAUNCH_H_ | 350 #endif // BASE_PROCESS_LAUNCH_H_ |
| OLD | NEW |