kill.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This file contains routines to kill processes and get the exit code and
  5. // termination status.
  6. #ifndef BASE_PROCESS_KILL_H_
  7. #define BASE_PROCESS_KILL_H_
  8. #include "base/files/file_path.h"
  9. #include "base/process/process.h"
  10. #include "base/process/process_handle.h"
  11. #include "base/time/time.h"
  12. #include "build/build_config.h"
  13. namespace base {
  14. class ProcessFilter;
  15. #if defined(OS_WIN)
  16. namespace win {
  17. // See definition in sandbox/win/src/sandbox_types.h
  18. const DWORD kSandboxFatalMemoryExceeded = 7012;
  19. // Exit codes with special meanings on Windows.
  20. const DWORD kNormalTerminationExitCode = 0;
  21. const DWORD kDebuggerInactiveExitCode = 0xC0000354;
  22. const DWORD kKeyboardInterruptExitCode = 0xC000013A;
  23. const DWORD kDebuggerTerminatedExitCode = 0x40010004;
  24. const DWORD kStatusInvalidImageHashExitCode = 0xC0000428;
  25. // This exit code is used by the Windows task manager when it kills a
  26. // process. It's value is obviously not that unique, and it's
  27. // surprising to me that the task manager uses this value, but it
  28. // seems to be common practice on Windows to test for it as an
  29. // indication that the task manager has killed something if the
  30. // process goes away.
  31. const DWORD kProcessKilledExitCode = 1;
  32. } // namespace win
  33. #endif // OS_WIN
  34. // Return status values from GetTerminationStatus. Don't use these as
  35. // exit code arguments to KillProcess*(), use platform/application
  36. // specific values instead.
  37. enum TerminationStatus {
  38. // clang-format off
  39. TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status
  40. TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status
  41. TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill
  42. TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault
  43. TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet
  44. #if defined(OS_CHROMEOS)
  45. // Used for the case when oom-killer kills a process on ChromeOS.
  46. TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM,
  47. #endif
  48. #if defined(OS_ANDROID)
  49. // On Android processes are spawned from the system Zygote and we do not get
  50. // the termination status. We can't know if the termination was a crash or an
  51. // oom kill for sure, but we can use status of the strong process bindings as
  52. // a hint.
  53. TERMINATION_STATUS_OOM_PROTECTED, // child was protected from oom kill
  54. #endif
  55. TERMINATION_STATUS_LAUNCH_FAILED, // child process never launched
  56. TERMINATION_STATUS_OOM, // Process died due to oom
  57. #if defined(OS_WIN)
  58. // On Windows, the OS terminated process due to code integrity failure.
  59. TERMINATION_STATUS_INTEGRITY_FAILURE,
  60. #endif
  61. TERMINATION_STATUS_MAX_ENUM
  62. // clang-format on
  63. };
  64. // Attempts to kill all the processes on the current machine that were launched
  65. // from the given executable name, ending them with the given exit code. If
  66. // filter is non-null, then only processes selected by the filter are killed.
  67. // Returns true if all processes were able to be killed off, false if at least
  68. // one couldn't be killed.
  69. BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name,
  70. int exit_code,
  71. const ProcessFilter* filter);
  72. #if defined(OS_POSIX)
  73. // Attempts to kill the process group identified by |process_group_id|. Returns
  74. // true on success.
  75. BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id);
  76. #endif // defined(OS_POSIX)
  77. // Get the termination status of the process by interpreting the
  78. // circumstances of the child process' death. |exit_code| is set to
  79. // the status returned by waitpid() on POSIX, and from GetExitCodeProcess() on
  80. // Windows, and may not be null. Note that on Linux, this function
  81. // will only return a useful result the first time it is called after
  82. // the child exits (because it will reap the child and the information
  83. // will no longer be available).
  84. BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle,
  85. int* exit_code);
  86. #if defined(OS_POSIX)
  87. // Send a kill signal to the process and then wait for the process to exit
  88. // and get the termination status.
  89. //
  90. // This is used in situations where it is believed that the process is dead
  91. // or dying (because communication with the child process has been cut).
  92. // In order to avoid erroneously returning that the process is still running
  93. // because the kernel is still cleaning it up, this will wait for the process
  94. // to terminate. In order to avoid the risk of hanging while waiting for the
  95. // process to terminate, send a SIGKILL to the process before waiting for the
  96. // termination status.
  97. //
  98. // Note that it is not an option to call WaitForExitCode and then
  99. // GetTerminationStatus as the child will be reaped when WaitForExitCode
  100. // returns, and this information will be lost.
  101. //
  102. BASE_EXPORT TerminationStatus GetKnownDeadTerminationStatus(
  103. ProcessHandle handle, int* exit_code);
  104. #if defined(OS_LINUX)
  105. // Spawns a thread to wait asynchronously for the child |process| to exit
  106. // and then reaps it.
  107. BASE_EXPORT void EnsureProcessGetsReaped(Process process);
  108. #endif // defined(OS_LINUX)
  109. #endif // defined(OS_POSIX)
  110. // Registers |process| to be asynchronously monitored for termination, forcibly
  111. // terminated if necessary, and reaped on exit. The caller should have signalled
  112. // |process| to exit before calling this API. The API will allow a couple of
  113. // seconds grace period before forcibly terminating |process|.
  114. // TODO(https://crbug.com/806451): The Mac implementation currently blocks the
  115. // calling thread for up to two seconds.
  116. BASE_EXPORT void EnsureProcessTerminated(Process process);
  117. // These are only sparingly used, and not needed on Fuchsia. They could be
  118. // implemented if necessary.
  119. #if !defined(OS_FUCHSIA)
  120. // Wait for all the processes based on the named executable to exit. If filter
  121. // is non-null, then only processes selected by the filter are waited on.
  122. // Returns after all processes have exited or wait_milliseconds have expired.
  123. // Returns true if all the processes exited, false otherwise.
  124. BASE_EXPORT bool WaitForProcessesToExit(
  125. const FilePath::StringType& executable_name,
  126. base::TimeDelta wait,
  127. const ProcessFilter* filter);
  128. // Waits a certain amount of time (can be 0) for all the processes with a given
  129. // executable name to exit, then kills off any of them that are still around.
  130. // If filter is non-null, then only processes selected by the filter are waited
  131. // on. Killed processes are ended with the given exit code. Returns false if
  132. // any processes needed to be killed, true if they all exited cleanly within
  133. // the wait_milliseconds delay.
  134. BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name,
  135. base::TimeDelta wait,
  136. int exit_code,
  137. const ProcessFilter* filter);
  138. #endif // !defined(OS_FUCHSIA)
  139. } // namespace base
  140. #endif // BASE_PROCESS_KILL_H_