| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/namespace contains utility functions for enumerating, ending and | 5 // This file/namespace contains utility functions for enumerating, ending and |
| 6 // computing statistics of processes. | 6 // computing statistics of processes. |
| 7 | 7 |
| 8 #ifndef BASE_PROCESS_UTIL_H_ | 8 #ifndef BASE_PROCESS_UTIL_H_ |
| 9 #define BASE_PROCESS_UTIL_H_ | 9 #define BASE_PROCESS_UTIL_H_ |
| 10 #pragma once | 10 #pragma once |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 // given multimap. Only call this function in a child process where you know | 188 // given multimap. Only call this function in a child process where you know |
| 189 // that there aren't any other threads. | 189 // that there aren't any other threads. |
| 190 BASE_API void CloseSuperfluousFds(const InjectiveMultimap& saved_map); | 190 BASE_API void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
| 191 #endif | 191 #endif |
| 192 | 192 |
| 193 // TODO(evan): rename these to use StudlyCaps. | 193 // TODO(evan): rename these to use StudlyCaps. |
| 194 typedef std::vector<std::pair<std::string, std::string> > environment_vector; | 194 typedef std::vector<std::pair<std::string, std::string> > environment_vector; |
| 195 typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; | 195 typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; |
| 196 | 196 |
| 197 // Options for launching a subprocess that are passed to LaunchApp(). | 197 // Options for launching a subprocess that are passed to LaunchApp(). |
| 198 // The default constructor constructs the object with default options. |
| 198 struct LaunchOptions { | 199 struct LaunchOptions { |
| 199 LaunchOptions() : wait(false), process_handle(NULL), | 200 LaunchOptions() : wait(false), |
| 200 #if defined(OS_WIN) | 201 #if defined(OS_WIN) |
| 201 start_hidden(false), inherit_handles(false), as_user(NULL), | 202 start_hidden(false), inherit_handles(false), as_user(NULL), |
| 202 empty_desktop_name(false) | 203 empty_desktop_name(false) |
| 203 #else | 204 #else |
| 204 environ(NULL), fds_to_remap(NULL), new_process_group(false) | 205 environ(NULL), fds_to_remap(NULL), new_process_group(false) |
| 205 #endif | 206 #endif |
| 206 {} | 207 {} |
| 207 | 208 |
| 208 // If true, wait for the process to complete. | 209 // If true, wait for the process to complete. |
| 209 bool wait; | 210 bool wait; |
| 210 | 211 |
| 211 // If non-NULL, will be filled in with the handle of the launched process. | |
| 212 // NOTE: In this case, the caller is responsible for closing the handle so | |
| 213 // that it doesn't leak! Otherwise, the handle will be implicitly | |
| 214 // closed. | |
| 215 // Not especially useful unless |wait| is false. | |
| 216 ProcessHandle* process_handle; | |
| 217 | |
| 218 #if defined(OS_WIN) | 212 #if defined(OS_WIN) |
| 219 bool start_hidden; | 213 bool start_hidden; |
| 220 | 214 |
| 221 // If true, the new process inherits handles from the parent. | 215 // If true, the new process inherits handles from the parent. |
| 222 bool inherit_handles; | 216 bool inherit_handles; |
| 223 | 217 |
| 224 // If non-NULL, runs as if the user represented by the token had launched it. | 218 // If non-NULL, runs as if the user represented by the token had launched it. |
| 225 // Whether the application is visible on the interactive desktop depends on | 219 // Whether the application is visible on the interactive desktop depends on |
| 226 // the token belonging to an interactive logon session. | 220 // the token belonging to an interactive logon session. |
| 227 // | 221 // |
| (...skipping 18 matching lines...) Expand all Loading... |
| 246 const file_handle_mapping_vector* fds_to_remap; | 240 const file_handle_mapping_vector* fds_to_remap; |
| 247 | 241 |
| 248 // If true, start the process in a new process group, instead of | 242 // If true, start the process in a new process group, instead of |
| 249 // inheriting the parent's process group. The pgid of the child process | 243 // inheriting the parent's process group. The pgid of the child process |
| 250 // will be the same as its pid. | 244 // will be the same as its pid. |
| 251 bool new_process_group; | 245 bool new_process_group; |
| 252 #endif | 246 #endif |
| 253 }; | 247 }; |
| 254 | 248 |
| 255 // Launch a process via the command line |cmdline|. | 249 // Launch a process via the command line |cmdline|. |
| 256 // See the documentation of LaunchOptions for details on launching options. | 250 // See the documentation of LaunchOptions for details on |options|. |
| 251 // |
| 252 // If |process_handle| is non-NULL, it will be filled in with the |
| 253 // handle of the launched process. NOTE: In this case, the caller is |
| 254 // responsible for closing the handle so that it doesn't leak! |
| 255 // Otherwise, the process handle will be implicitly closed. |
| 257 // | 256 // |
| 258 // Unix-specific notes: | 257 // Unix-specific notes: |
| 259 // - Before launching, all FDs open in the parent process will be marked as | 258 // - Before launching, all FDs open in the parent process will be marked as |
| 260 // close-on-exec. | 259 // close-on-exec. |
| 261 // - If the first argument on the command line does not contain a slash, | 260 // - If the first argument on the command line does not contain a slash, |
| 262 // PATH will be searched. (See man execvp.) | 261 // PATH will be searched. (See man execvp.) |
| 263 BASE_API bool LaunchProcess(const CommandLine& cmdline, | 262 BASE_API bool LaunchProcess(const CommandLine& cmdline, |
| 264 const LaunchOptions& options); | 263 const LaunchOptions& options, |
| 264 ProcessHandle* process_handle); |
| 265 | 265 |
| 266 #if defined(OS_WIN) | 266 #if defined(OS_WIN) |
| 267 | 267 |
| 268 enum IntegrityLevel { | 268 enum IntegrityLevel { |
| 269 INTEGRITY_UNKNOWN, | 269 INTEGRITY_UNKNOWN, |
| 270 LOW_INTEGRITY, | 270 LOW_INTEGRITY, |
| 271 MEDIUM_INTEGRITY, | 271 MEDIUM_INTEGRITY, |
| 272 HIGH_INTEGRITY, | 272 HIGH_INTEGRITY, |
| 273 }; | 273 }; |
| 274 // Determine the integrity level of the specified process. Returns false | 274 // Determine the integrity level of the specified process. Returns false |
| 275 // if the system does not support integrity levels (pre-Vista) or in the case | 275 // if the system does not support integrity levels (pre-Vista) or in the case |
| 276 // of an underlying system failure. | 276 // of an underlying system failure. |
| 277 BASE_API bool GetProcessIntegrityLevel(ProcessHandle process, | 277 BASE_API bool GetProcessIntegrityLevel(ProcessHandle process, |
| 278 IntegrityLevel *level); | 278 IntegrityLevel *level); |
| 279 | 279 |
| 280 // Windows-specific LaunchProcess that takes the command line as a | 280 // Windows-specific LaunchProcess that takes the command line as a |
| 281 // string. Useful for situations where you need to control the | 281 // string. Useful for situations where you need to control the |
| 282 // command line arguments directly, but prefer the CommandLine version | 282 // command line arguments directly, but prefer the CommandLine version |
| 283 // if launching Chrome itself. | 283 // if launching Chrome itself. |
| 284 // | 284 // |
| 285 // The first command line argument should be the path to the process, | 285 // The first command line argument should be the path to the process, |
| 286 // and don't forget to quote it. | 286 // and don't forget to quote it. |
| 287 // | 287 // |
| 288 // Example (including literal quotes) | 288 // Example (including literal quotes) |
| 289 // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" | 289 // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" |
| 290 BASE_API bool LaunchProcess(const string16& cmdline, | 290 BASE_API bool LaunchProcess(const string16& cmdline, |
| 291 const LaunchOptions& options); | 291 const LaunchOptions& options, |
| 292 ProcessHandle* process_handle); |
| 292 | 293 |
| 293 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. | 294 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
| 294 inline bool LaunchApp(const std::wstring& cmdline, | 295 inline bool LaunchApp(const std::wstring& cmdline, |
| 295 bool wait, bool start_hidden, | 296 bool wait, bool start_hidden, |
| 296 ProcessHandle* process_handle) { | 297 ProcessHandle* process_handle) { |
| 297 LaunchOptions options; | 298 LaunchOptions options; |
| 298 options.wait = wait; | 299 options.wait = wait; |
| 299 options.start_hidden = start_hidden; | 300 options.start_hidden = start_hidden; |
| 300 options.process_handle = process_handle; | 301 return LaunchProcess(cmdline, options, process_handle); |
| 301 return LaunchProcess(cmdline, options); | |
| 302 } | 302 } |
| 303 | 303 |
| 304 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. | 304 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
| 305 inline bool LaunchAppWithHandleInheritance(const std::wstring& cmdline, | 305 inline bool LaunchAppWithHandleInheritance(const std::wstring& cmdline, |
| 306 bool wait, bool start_hidden, | 306 bool wait, bool start_hidden, |
| 307 ProcessHandle* process_handle) { | 307 ProcessHandle* process_handle) { |
| 308 LaunchOptions options; | 308 LaunchOptions options; |
| 309 options.wait = wait; | 309 options.wait = wait; |
| 310 options.start_hidden = start_hidden; | 310 options.start_hidden = start_hidden; |
| 311 options.process_handle = process_handle; | |
| 312 options.inherit_handles = true; | 311 options.inherit_handles = true; |
| 313 return LaunchProcess(cmdline, options); | 312 return LaunchProcess(cmdline, options, process_handle); |
| 314 } | 313 } |
| 315 | 314 |
| 316 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. | 315 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
| 317 inline bool LaunchAppAsUser(UserTokenHandle token, | 316 inline bool LaunchAppAsUser(UserTokenHandle token, |
| 318 const std::wstring& cmdline, | 317 const std::wstring& cmdline, |
| 319 bool start_hidden, | 318 bool start_hidden, |
| 320 ProcessHandle* process_handle) { | 319 ProcessHandle* process_handle) { |
| 321 LaunchOptions options; | 320 LaunchOptions options; |
| 322 options.start_hidden = start_hidden; | 321 options.start_hidden = start_hidden; |
| 323 options.process_handle = process_handle; | |
| 324 options.as_user = token; | 322 options.as_user = token; |
| 325 return LaunchProcess(cmdline, options); | 323 return LaunchProcess(cmdline, options, process_handle); |
| 326 } | 324 } |
| 327 | 325 |
| 328 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. | 326 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
| 329 inline bool LaunchAppAsUser(UserTokenHandle token, | 327 inline bool LaunchAppAsUser(UserTokenHandle token, |
| 330 const std::wstring& cmdline, | 328 const std::wstring& cmdline, |
| 331 bool start_hidden, ProcessHandle* process_handle, | 329 bool start_hidden, ProcessHandle* process_handle, |
| 332 bool empty_desktop_name, bool inherit_handles) { | 330 bool empty_desktop_name, bool inherit_handles) { |
| 333 LaunchOptions options; | 331 LaunchOptions options; |
| 334 options.start_hidden = start_hidden; | 332 options.start_hidden = start_hidden; |
| 335 options.process_handle = process_handle; | |
| 336 options.as_user = token; | 333 options.as_user = token; |
| 337 options.empty_desktop_name = empty_desktop_name; | 334 options.empty_desktop_name = empty_desktop_name; |
| 338 options.inherit_handles = inherit_handles; | 335 options.inherit_handles = inherit_handles; |
| 339 return LaunchProcess(cmdline, options); | 336 return LaunchProcess(cmdline, options, process_handle); |
| 340 } | 337 } |
| 341 | 338 |
| 342 #elif defined(OS_POSIX) | 339 #elif defined(OS_POSIX) |
| 343 // A POSIX-specific version of LaunchProcess that takes an argv array | 340 // A POSIX-specific version of LaunchProcess that takes an argv array |
| 344 // instead of a CommandLine. Useful for situations where you need to | 341 // instead of a CommandLine. Useful for situations where you need to |
| 345 // control the command line arguments directly, but prefer the | 342 // control the command line arguments directly, but prefer the |
| 346 // CommandLine version if launching Chrome itself. | 343 // CommandLine version if launching Chrome itself. |
| 347 BASE_API bool LaunchProcess(const std::vector<std::string>& argv, | 344 BASE_API bool LaunchProcess(const std::vector<std::string>& argv, |
| 348 const LaunchOptions& options); | 345 const LaunchOptions& options, |
| 346 ProcessHandle* process_handle); |
| 349 | 347 |
| 350 // AlterEnvironment returns a modified environment vector, constructed from the | 348 // AlterEnvironment returns a modified environment vector, constructed from the |
| 351 // given environment and the list of changes given in |changes|. Each key in | 349 // given environment and the list of changes given in |changes|. Each key in |
| 352 // the environment is matched against the first element of the pairs. In the | 350 // the environment is matched against the first element of the pairs. In the |
| 353 // event of a match, the value is replaced by the second of the pair, unless | 351 // event of a match, the value is replaced by the second of the pair, unless |
| 354 // the second is empty, in which case the key-value is removed. | 352 // the second is empty, in which case the key-value is removed. |
| 355 // | 353 // |
| 356 // The returned array is allocated using new[] and must be freed by the caller. | 354 // The returned array is allocated using new[] and must be freed by the caller. |
| 357 BASE_API char** AlterEnvironment(const environment_vector& changes, | 355 BASE_API char** AlterEnvironment(const environment_vector& changes, |
| 358 const char* const* const env); | 356 const char* const* const env); |
| 359 #endif // defined(OS_POSIX) | 357 #endif // defined(OS_POSIX) |
| 360 | 358 |
| 361 #if defined(OS_WIN) | 359 #if defined(OS_WIN) |
| 362 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. | 360 // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
| 363 // Successfully deprecated on non-Windows. | 361 // Successfully deprecated on non-Windows. |
| 364 inline bool LaunchApp(const CommandLine& cl, bool wait, bool start_hidden, | 362 inline bool LaunchApp(const CommandLine& cl, bool wait, bool start_hidden, |
| 365 ProcessHandle* process_handle) { | 363 ProcessHandle* process_handle) { |
| 366 LaunchOptions options; | 364 LaunchOptions options; |
| 367 options.wait = wait; | 365 options.wait = wait; |
| 368 options.process_handle = process_handle; | 366 options.start_hidden = start_hidden; |
| 369 | 367 return LaunchProcess(cl, options, process_handle); |
| 370 return LaunchProcess(cl, options); | |
| 371 } | 368 } |
| 372 #endif | 369 #endif |
| 373 | 370 |
| 374 // Executes the application specified by |cl| and wait for it to exit. Stores | 371 // Executes the application specified by |cl| and wait for it to exit. Stores |
| 375 // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true | 372 // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true |
| 376 // on success (application launched and exited cleanly, with exit code | 373 // on success (application launched and exited cleanly, with exit code |
| 377 // indicating success). | 374 // indicating success). |
| 378 BASE_API bool GetAppOutput(const CommandLine& cl, std::string* output); | 375 BASE_API bool GetAppOutput(const CommandLine& cl, std::string* output); |
| 379 | 376 |
| 380 #if defined(OS_POSIX) | 377 #if defined(OS_POSIX) |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 // instance running inside the parent. The parent's Breakpad instance should | 748 // instance running inside the parent. The parent's Breakpad instance should |
| 752 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler | 749 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 753 // in the child after forking will restore the standard exception handler. | 750 // in the child after forking will restore the standard exception handler. |
| 754 // See http://crbug.com/20371/ for more details. | 751 // See http://crbug.com/20371/ for more details. |
| 755 void RestoreDefaultExceptionHandler(); | 752 void RestoreDefaultExceptionHandler(); |
| 756 #endif // defined(OS_MACOSX) | 753 #endif // defined(OS_MACOSX) |
| 757 | 754 |
| 758 } // namespace base | 755 } // namespace base |
| 759 | 756 |
| 760 #endif // BASE_PROCESS_UTIL_H_ | 757 #endif // BASE_PROCESS_UTIL_H_ |
| OLD | NEW |