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 <string> | 10 #include <string> |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
32 typedef std::vector<HANDLE> HandlesToInheritVector; | 32 typedef std::vector<HANDLE> HandlesToInheritVector; |
33 #endif | 33 #endif |
34 // TODO(viettrungluu): Only define this on POSIX? | 34 // TODO(viettrungluu): Only define this on POSIX? |
35 typedef std::vector<std::pair<int, int> > FileHandleMappingVector; | 35 typedef std::vector<std::pair<int, int> > FileHandleMappingVector; |
36 | 36 |
37 // Options for launching a subprocess that are passed to LaunchProcess(). | 37 // Options for launching a subprocess that are passed to LaunchProcess(). |
38 // The default constructor constructs the object with default options. | 38 // The default constructor constructs the object with default options. |
39 struct BASE_EXPORT LaunchOptions { | 39 struct BASE_EXPORT LaunchOptions { |
| 40 #if defined(OS_POSIX) |
| 41 // Delegate to be run in between fork and exec in the subprocess (see |
| 42 // pre_exec_delegate below) |
| 43 class BASE_EXPORT PreExecDelegate { |
| 44 public: |
| 45 PreExecDelegate() {} |
| 46 virtual ~PreExecDelegate() {} |
| 47 |
| 48 // Since this is to be run between fork and exec, and fork may have happened |
| 49 // while multiple threads were running, this function needs to be async |
| 50 // safe. |
| 51 virtual void RunAsyncSafe() = 0; |
| 52 |
| 53 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(PreExecDelegate); |
| 55 }; |
| 56 #endif // defined(OS_POSIX) |
| 57 |
40 LaunchOptions(); | 58 LaunchOptions(); |
41 ~LaunchOptions(); | 59 ~LaunchOptions(); |
42 | 60 |
43 // If true, wait for the process to complete. | 61 // If true, wait for the process to complete. |
44 bool wait; | 62 bool wait; |
45 | 63 |
46 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
47 bool start_hidden; | 65 bool start_hidden; |
48 | 66 |
49 // If non-null, inherit exactly the list of handles in this vector (these | 67 // If non-null, inherit exactly the list of handles in this vector (these |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // the call to LaunchProcess(). | 126 // the call to LaunchProcess(). |
109 const std::vector<int>* maximize_rlimits; | 127 const std::vector<int>* maximize_rlimits; |
110 | 128 |
111 // If true, start the process in a new process group, instead of | 129 // If true, start the process in a new process group, instead of |
112 // inheriting the parent's process group. The pgid of the child process | 130 // inheriting the parent's process group. The pgid of the child process |
113 // will be the same as its pid. | 131 // will be the same as its pid. |
114 bool new_process_group; | 132 bool new_process_group; |
115 | 133 |
116 #if defined(OS_LINUX) | 134 #if defined(OS_LINUX) |
117 // If non-zero, start the process using clone(), using flags as provided. | 135 // If non-zero, start the process using clone(), using flags as provided. |
| 136 // Unlike in clone, clone_flags may not contain a custom termination signal |
| 137 // that is sent to the parent when the child dies. The termination signal will |
| 138 // always be set to SIGCHLD. |
118 int clone_flags; | 139 int clone_flags; |
119 | 140 |
120 // By default, child processes will have the PR_SET_NO_NEW_PRIVS bit set. If | 141 // By default, child processes will have the PR_SET_NO_NEW_PRIVS bit set. If |
121 // true, then this bit will not be set in the new child process. | 142 // true, then this bit will not be set in the new child process. |
122 bool allow_new_privs; | 143 bool allow_new_privs; |
123 #endif // defined(OS_LINUX) | 144 #endif // defined(OS_LINUX) |
124 | 145 |
| 146 #if defined(OS_POSIX) |
| 147 // If non-null, a delegate to be run immediately prior to executing the new |
| 148 // program in the child process. |
| 149 // |
| 150 // WARNING: If LaunchProcess is called in the presence of multiple threads, |
| 151 // code running in this delegate essentially needs to be async-signal safe |
| 152 // (see man 7 signal for a list of allowed functions). |
| 153 PreExecDelegate* pre_exec_delegate; |
| 154 #endif // defined(OS_POSIX) |
| 155 |
125 #if defined(OS_CHROMEOS) | 156 #if defined(OS_CHROMEOS) |
126 // If non-negative, the specified file descriptor will be set as the launched | 157 // If non-negative, the specified file descriptor will be set as the launched |
127 // process' controlling terminal. | 158 // process' controlling terminal. |
128 int ctrl_terminal_fd; | 159 int ctrl_terminal_fd; |
129 #endif // defined(OS_CHROMEOS) | 160 #endif // defined(OS_CHROMEOS) |
130 | 161 |
131 #if defined(OS_MACOSX) | 162 #if defined(OS_MACOSX) |
132 // If this name is non-empty, the new child, after fork() but before exec(), | 163 // If this name is non-empty, the new child, after fork() but before exec(), |
133 // will look up this server name in the bootstrap namespace. The resulting | 164 // will look up this server name in the bootstrap namespace. The resulting |
134 // service port will be replaced as the bootstrap port in the child. Because | 165 // service port will be replaced as the bootstrap port in the child. Because |
(...skipping 14 matching lines...) Expand all Loading... |
149 // - All file descriptors open in the parent process will be closed in the | 180 // - All file descriptors open in the parent process will be closed in the |
150 // child process except for any preserved by options::fds_to_remap, and | 181 // child process except for any preserved by options::fds_to_remap, and |
151 // stdin, stdout, and stderr. If not remapped by options::fds_to_remap, | 182 // stdin, stdout, and stderr. If not remapped by options::fds_to_remap, |
152 // stdin is reopened as /dev/null, and the child is allowed to inherit its | 183 // stdin is reopened as /dev/null, and the child is allowed to inherit its |
153 // parent's stdout and stderr. | 184 // parent's stdout and stderr. |
154 // - If the first argument on the command line does not contain a slash, | 185 // - If the first argument on the command line does not contain a slash, |
155 // PATH will be searched. (See man execvp.) | 186 // PATH will be searched. (See man execvp.) |
156 BASE_EXPORT Process LaunchProcess(const CommandLine& cmdline, | 187 BASE_EXPORT Process LaunchProcess(const CommandLine& cmdline, |
157 const LaunchOptions& options); | 188 const LaunchOptions& options); |
158 | 189 |
159 // Deprecated version. | |
160 // TODO(rvargas) crbug.com/417532: Remove this after migrating all consumers. | |
161 BASE_EXPORT bool LaunchProcess(const CommandLine& cmdline, | |
162 const LaunchOptions& options, | |
163 ProcessHandle* process_handle); | |
164 | |
165 #if defined(OS_WIN) | 190 #if defined(OS_WIN) |
166 // Windows-specific LaunchProcess that takes the command line as a | 191 // Windows-specific LaunchProcess that takes the command line as a |
167 // string. Useful for situations where you need to control the | 192 // string. Useful for situations where you need to control the |
168 // command line arguments directly, but prefer the CommandLine version | 193 // command line arguments directly, but prefer the CommandLine version |
169 // if launching Chrome itself. | 194 // if launching Chrome itself. |
170 // | 195 // |
171 // The first command line argument should be the path to the process, | 196 // The first command line argument should be the path to the process, |
172 // and don't forget to quote it. | 197 // and don't forget to quote it. |
173 // | 198 // |
174 // Example (including literal quotes) | 199 // Example (including literal quotes) |
(...skipping 10 matching lines...) Expand all Loading... |
185 const LaunchOptions& options); | 210 const LaunchOptions& options); |
186 | 211 |
187 #elif defined(OS_POSIX) | 212 #elif defined(OS_POSIX) |
188 // A POSIX-specific version of LaunchProcess that takes an argv array | 213 // A POSIX-specific version of LaunchProcess that takes an argv array |
189 // instead of a CommandLine. Useful for situations where you need to | 214 // instead of a CommandLine. Useful for situations where you need to |
190 // control the command line arguments directly, but prefer the | 215 // control the command line arguments directly, but prefer the |
191 // CommandLine version if launching Chrome itself. | 216 // CommandLine version if launching Chrome itself. |
192 BASE_EXPORT Process LaunchProcess(const std::vector<std::string>& argv, | 217 BASE_EXPORT Process LaunchProcess(const std::vector<std::string>& argv, |
193 const LaunchOptions& options); | 218 const LaunchOptions& options); |
194 | 219 |
195 // Deprecated version. | |
196 // TODO(rvargas) crbug.com/417532: Remove this after migrating all consumers. | |
197 BASE_EXPORT bool LaunchProcess(const std::vector<std::string>& argv, | |
198 const LaunchOptions& options, | |
199 ProcessHandle* process_handle); | |
200 | |
201 // Close all file descriptors, except those which are a destination in the | 220 // Close all file descriptors, except those which are a destination in the |
202 // given multimap. Only call this function in a child process where you know | 221 // given multimap. Only call this function in a child process where you know |
203 // that there aren't any other threads. | 222 // that there aren't any other threads. |
204 BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); | 223 BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
205 #endif // defined(OS_POSIX) | 224 #endif // defined(OS_POSIX) |
206 | 225 |
207 #if defined(OS_WIN) | 226 #if defined(OS_WIN) |
208 // Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION | 227 // Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION |
209 // BasicLimitInformation.LimitFlags to |limit_flags|. | 228 // BasicLimitInformation.LimitFlags to |limit_flags|. |
210 BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags); | 229 BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 void ReplaceBootstrapPort(const std::string& replacement_bootstrap_name); | 287 void ReplaceBootstrapPort(const std::string& replacement_bootstrap_name); |
269 #endif // defined(OS_MACOSX) | 288 #endif // defined(OS_MACOSX) |
270 | 289 |
271 // Creates a LaunchOptions object suitable for launching processes in a test | 290 // Creates a LaunchOptions object suitable for launching processes in a test |
272 // binary. This should not be called in production/released code. | 291 // binary. This should not be called in production/released code. |
273 BASE_EXPORT LaunchOptions LaunchOptionsForTest(); | 292 BASE_EXPORT LaunchOptions LaunchOptionsForTest(); |
274 | 293 |
275 } // namespace base | 294 } // namespace base |
276 | 295 |
277 #endif // BASE_PROCESS_LAUNCH_H_ | 296 #endif // BASE_PROCESS_LAUNCH_H_ |
OLD | NEW |