| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include <list> | 30 #include <list> |
| 31 #include <set> | 31 #include <set> |
| 32 #include <string> | 32 #include <string> |
| 33 #include <utility> | 33 #include <utility> |
| 34 #include <vector> | 34 #include <vector> |
| 35 | 35 |
| 36 #include "base/base_export.h" | 36 #include "base/base_export.h" |
| 37 #include "base/files/file_path.h" | 37 #include "base/files/file_path.h" |
| 38 #include "base/process.h" | 38 #include "base/process.h" |
| 39 #include "base/process/memory.h" | 39 #include "base/process/memory.h" |
| 40 #include "base/process/kill.h" |
| 40 #include "base/process/process_iterator.h" | 41 #include "base/process/process_iterator.h" |
| 41 #include "base/process/process_metrics.h" | 42 #include "base/process/process_metrics.h" |
| 42 | 43 |
| 43 #if defined(OS_POSIX) | 44 #if defined(OS_POSIX) |
| 44 #include "base/posix/file_descriptor_shuffle.h" | 45 #include "base/posix/file_descriptor_shuffle.h" |
| 45 #endif | 46 #endif |
| 46 | 47 |
| 47 class CommandLine; | 48 class CommandLine; |
| 48 | 49 |
| 49 namespace base { | 50 namespace base { |
| 50 | 51 |
| 51 // Return status values from GetTerminationStatus. Don't use these as | |
| 52 // exit code arguments to KillProcess*(), use platform/application | |
| 53 // specific values instead. | |
| 54 enum TerminationStatus { | |
| 55 TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status | |
| 56 TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status | |
| 57 TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill | |
| 58 TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault | |
| 59 TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet | |
| 60 TERMINATION_STATUS_MAX_ENUM | |
| 61 }; | |
| 62 | |
| 63 #if defined(OS_WIN) | 52 #if defined(OS_WIN) |
| 64 // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran | 53 // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran |
| 65 // chrome. This is not thread-safe: only call from main thread. | 54 // chrome. This is not thread-safe: only call from main thread. |
| 66 BASE_EXPORT void RouteStdioToConsole(); | 55 BASE_EXPORT void RouteStdioToConsole(); |
| 67 #endif | 56 #endif |
| 68 | 57 |
| 69 // Returns the id of the current process. | 58 // Returns the id of the current process. |
| 70 BASE_EXPORT ProcessId GetCurrentProcId(); | 59 BASE_EXPORT ProcessId GetCurrentProcId(); |
| 71 | 60 |
| 72 // Returns the ProcessHandle of the current process. | 61 // Returns the ProcessHandle of the current process. |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 std::string* output, size_t max_output); | 327 std::string* output, size_t max_output); |
| 339 | 328 |
| 340 // A version of |GetAppOutput()| which also returns the exit code of the | 329 // A version of |GetAppOutput()| which also returns the exit code of the |
| 341 // executed command. Returns true if the application runs and exits cleanly. If | 330 // executed command. Returns true if the application runs and exits cleanly. If |
| 342 // this is the case the exit code of the application is available in | 331 // this is the case the exit code of the application is available in |
| 343 // |*exit_code|. | 332 // |*exit_code|. |
| 344 BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl, | 333 BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl, |
| 345 std::string* output, int* exit_code); | 334 std::string* output, int* exit_code); |
| 346 #endif // defined(OS_POSIX) | 335 #endif // defined(OS_POSIX) |
| 347 | 336 |
| 348 // Attempts to kill all the processes on the current machine that were launched | |
| 349 // from the given executable name, ending them with the given exit code. If | |
| 350 // filter is non-null, then only processes selected by the filter are killed. | |
| 351 // Returns true if all processes were able to be killed off, false if at least | |
| 352 // one couldn't be killed. | |
| 353 BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name, | |
| 354 int exit_code, const ProcessFilter* filter); | |
| 355 | |
| 356 // Attempts to kill the process identified by the given process | |
| 357 // entry structure, giving it the specified exit code. If |wait| is true, wait | |
| 358 // for the process to be actually terminated before returning. | |
| 359 // Returns true if this is successful, false otherwise. | |
| 360 BASE_EXPORT bool KillProcess(ProcessHandle process, int exit_code, bool wait); | |
| 361 | |
| 362 #if defined(OS_POSIX) | |
| 363 // Attempts to kill the process group identified by |process_group_id|. Returns | |
| 364 // true on success. | |
| 365 BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id); | |
| 366 #endif // defined(OS_POSIX) | |
| 367 | |
| 368 #if defined(OS_WIN) | |
| 369 BASE_EXPORT bool KillProcessById(ProcessId process_id, int exit_code, | |
| 370 bool wait); | |
| 371 #endif // defined(OS_WIN) | |
| 372 | |
| 373 // Get the termination status of the process by interpreting the | |
| 374 // circumstances of the child process' death. |exit_code| is set to | |
| 375 // the status returned by waitpid() on POSIX, and from | |
| 376 // GetExitCodeProcess() on Windows. |exit_code| may be NULL if the | |
| 377 // caller is not interested in it. Note that on Linux, this function | |
| 378 // will only return a useful result the first time it is called after | |
| 379 // the child exits (because it will reap the child and the information | |
| 380 // will no longer be available). | |
| 381 BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle, | |
| 382 int* exit_code); | |
| 383 | |
| 384 #if defined(OS_POSIX) | |
| 385 // Wait for the process to exit and get the termination status. See | |
| 386 // GetTerminationStatus for more information. On POSIX systems, we can't call | |
| 387 // WaitForExitCode and then GetTerminationStatus as the child will be reaped | |
| 388 // when WaitForExitCode return and this information will be lost. | |
| 389 BASE_EXPORT TerminationStatus WaitForTerminationStatus(ProcessHandle handle, | |
| 390 int* exit_code); | |
| 391 #endif // defined(OS_POSIX) | |
| 392 | |
| 393 // Waits for process to exit. On POSIX systems, if the process hasn't been | |
| 394 // signaled then puts the exit code in |exit_code|; otherwise it's considered | |
| 395 // a failure. On Windows |exit_code| is always filled. Returns true on success, | |
| 396 // and closes |handle| in any case. | |
| 397 BASE_EXPORT bool WaitForExitCode(ProcessHandle handle, int* exit_code); | |
| 398 | |
| 399 // Waits for process to exit. If it did exit within |timeout_milliseconds|, | |
| 400 // then puts the exit code in |exit_code|, and returns true. | |
| 401 // In POSIX systems, if the process has been signaled then |exit_code| is set | |
| 402 // to -1. Returns false on failure (the caller is then responsible for closing | |
| 403 // |handle|). | |
| 404 // The caller is always responsible for closing the |handle|. | |
| 405 BASE_EXPORT bool WaitForExitCodeWithTimeout(ProcessHandle handle, | |
| 406 int* exit_code, | |
| 407 base::TimeDelta timeout); | |
| 408 | |
| 409 // Wait for all the processes based on the named executable to exit. If filter | |
| 410 // is non-null, then only processes selected by the filter are waited on. | |
| 411 // Returns after all processes have exited or wait_milliseconds have expired. | |
| 412 // Returns true if all the processes exited, false otherwise. | |
| 413 BASE_EXPORT bool WaitForProcessesToExit( | |
| 414 const FilePath::StringType& executable_name, | |
| 415 base::TimeDelta wait, | |
| 416 const ProcessFilter* filter); | |
| 417 | |
| 418 // Wait for a single process to exit. Return true if it exited cleanly within | |
| 419 // the given time limit. On Linux |handle| must be a child process, however | |
| 420 // on Mac and Windows it can be any process. | |
| 421 BASE_EXPORT bool WaitForSingleProcess(ProcessHandle handle, | |
| 422 base::TimeDelta wait); | |
| 423 | |
| 424 // Waits a certain amount of time (can be 0) for all the processes with a given | |
| 425 // executable name to exit, then kills off any of them that are still around. | |
| 426 // If filter is non-null, then only processes selected by the filter are waited | |
| 427 // on. Killed processes are ended with the given exit code. Returns false if | |
| 428 // any processes needed to be killed, true if they all exited cleanly within | |
| 429 // the wait_milliseconds delay. | |
| 430 BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name, | |
| 431 base::TimeDelta wait, | |
| 432 int exit_code, | |
| 433 const ProcessFilter* filter); | |
| 434 | |
| 435 // This method ensures that the specified process eventually terminates, and | |
| 436 // then it closes the given process handle. | |
| 437 // | |
| 438 // It assumes that the process has already been signalled to exit, and it | |
| 439 // begins by waiting a small amount of time for it to exit. If the process | |
| 440 // does not appear to have exited, then this function starts to become | |
| 441 // aggressive about ensuring that the process terminates. | |
| 442 // | |
| 443 // On Linux this method does not block the calling thread. | |
| 444 // On OS X this method may block for up to 2 seconds. | |
| 445 // | |
| 446 // NOTE: The process handle must have been opened with the PROCESS_TERMINATE | |
| 447 // and SYNCHRONIZE permissions. | |
| 448 // | |
| 449 BASE_EXPORT void EnsureProcessTerminated(ProcessHandle process_handle); | |
| 450 | |
| 451 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 452 // The nicer version of EnsureProcessTerminated() that is patient and will | |
| 453 // wait for |process_handle| to finish and then reap it. | |
| 454 BASE_EXPORT void EnsureProcessGetsReaped(ProcessHandle process_handle); | |
| 455 #endif | |
| 456 | |
| 457 // If supported on the platform, and the user has sufficent rights, increase | 337 // If supported on the platform, and the user has sufficent rights, increase |
| 458 // the current process's scheduling priority to a high priority. | 338 // the current process's scheduling priority to a high priority. |
| 459 BASE_EXPORT void RaiseProcessToHighPriority(); | 339 BASE_EXPORT void RaiseProcessToHighPriority(); |
| 460 | 340 |
| 461 #if defined(OS_MACOSX) | 341 #if defined(OS_MACOSX) |
| 462 // Restore the default exception handler, setting it to Apple Crash Reporter | 342 // Restore the default exception handler, setting it to Apple Crash Reporter |
| 463 // (ReportCrash). When forking and execing a new process, the child will | 343 // (ReportCrash). When forking and execing a new process, the child will |
| 464 // inherit the parent's exception ports, which may be set to the Breakpad | 344 // inherit the parent's exception ports, which may be set to the Breakpad |
| 465 // instance running inside the parent. The parent's Breakpad instance should | 345 // instance running inside the parent. The parent's Breakpad instance should |
| 466 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler | 346 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 467 // in the child after forking will restore the standard exception handler. | 347 // in the child after forking will restore the standard exception handler. |
| 468 // See http://crbug.com/20371/ for more details. | 348 // See http://crbug.com/20371/ for more details. |
| 469 void RestoreDefaultExceptionHandler(); | 349 void RestoreDefaultExceptionHandler(); |
| 470 #endif // defined(OS_MACOSX) | 350 #endif // defined(OS_MACOSX) |
| 471 | 351 |
| 472 } // namespace base | 352 } // namespace base |
| 473 | 353 |
| 474 #endif // BASE_PROCESS_UTIL_H_ | 354 #endif // BASE_PROCESS_UTIL_H_ |
| OLD | NEW |