kill.h 7.0 KB

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