kill.h 7.5 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 "butil/files/file_path.h"
  9. #include "butil/process/process_handle.h"
  10. #include "butil/time/time.h"
  11. namespace butil {
  12. class ProcessFilter;
  13. // Return status values from GetTerminationStatus. Don't use these as
  14. // exit code arguments to KillProcess*(), use platform/application
  15. // specific values instead.
  16. enum TerminationStatus {
  17. TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status
  18. TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status
  19. TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill
  20. TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault
  21. TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet
  22. #if defined(OS_ANDROID)
  23. // On Android processes are spawned from the system Zygote and we do not get
  24. // the termination status. We can't know if the termination was a crash or an
  25. // oom kill for sure, but we can use status of the strong process bindings as
  26. // a hint.
  27. TERMINATION_STATUS_OOM_PROTECTED, // child was protected from oom kill
  28. #endif
  29. TERMINATION_STATUS_MAX_ENUM
  30. };
  31. // Attempts to kill all the processes on the current machine that were launched
  32. // from the given executable name, ending them with the given exit code. If
  33. // filter is non-null, then only processes selected by the filter are killed.
  34. // Returns true if all processes were able to be killed off, false if at least
  35. // one couldn't be killed.
  36. BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name,
  37. int exit_code,
  38. const ProcessFilter* filter);
  39. // Attempts to kill the process identified by the given process
  40. // entry structure, giving it the specified exit code. If |wait| is true, wait
  41. // for the process to be actually terminated before returning.
  42. // Returns true if this is successful, false otherwise.
  43. BASE_EXPORT bool KillProcess(ProcessHandle process, int exit_code, bool wait);
  44. #if defined(OS_POSIX)
  45. // Attempts to kill the process group identified by |process_group_id|. Returns
  46. // true on success.
  47. BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id);
  48. #endif // defined(OS_POSIX)
  49. #if defined(OS_WIN)
  50. BASE_EXPORT bool KillProcessById(ProcessId process_id,
  51. int exit_code,
  52. bool wait);
  53. #endif // defined(OS_WIN)
  54. // Get the termination status of the process by interpreting the
  55. // circumstances of the child process' death. |exit_code| is set to
  56. // the status returned by waitpid() on POSIX, and from
  57. // GetExitCodeProcess() on Windows. |exit_code| may be NULL if the
  58. // caller is not interested in it. Note that on Linux, this function
  59. // will only return a useful result the first time it is called after
  60. // the child exits (because it will reap the child and the information
  61. // will no longer be available).
  62. BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle,
  63. int* exit_code);
  64. #if defined(OS_POSIX)
  65. // Send a kill signal to the process and then wait for the process to exit
  66. // and get the termination status.
  67. //
  68. // This is used in situations where it is believed that the process is dead
  69. // or dying (because communication with the child process has been cut).
  70. // In order to avoid erroneously returning that the process is still running
  71. // because the kernel is still cleaning it up, this will wait for the process
  72. // to terminate. In order to avoid the risk of hanging while waiting for the
  73. // process to terminate, send a SIGKILL to the process before waiting for the
  74. // termination status.
  75. //
  76. // Note that it is not an option to call WaitForExitCode and then
  77. // GetTerminationStatus as the child will be reaped when WaitForExitCode
  78. // returns, and this information will be lost.
  79. //
  80. BASE_EXPORT TerminationStatus GetKnownDeadTerminationStatus(
  81. ProcessHandle handle, int* exit_code);
  82. #endif // defined(OS_POSIX)
  83. // Waits for process to exit. On POSIX systems, if the process hasn't been
  84. // signaled then puts the exit code in |exit_code|; otherwise it's considered
  85. // a failure. On Windows |exit_code| is always filled. Returns true on success,
  86. // and closes |handle| in any case.
  87. BASE_EXPORT bool WaitForExitCode(ProcessHandle handle, int* exit_code);
  88. // Waits for process to exit. If it did exit within |timeout_milliseconds|,
  89. // then puts the exit code in |exit_code|, and returns true.
  90. // In POSIX systems, if the process has been signaled then |exit_code| is set
  91. // to -1. Returns false on failure (the caller is then responsible for closing
  92. // |handle|).
  93. // The caller is always responsible for closing the |handle|.
  94. BASE_EXPORT bool WaitForExitCodeWithTimeout(ProcessHandle handle,
  95. int* exit_code,
  96. butil::TimeDelta timeout);
  97. // Wait for all the processes based on the named executable to exit. If filter
  98. // is non-null, then only processes selected by the filter are waited on.
  99. // Returns after all processes have exited or wait_milliseconds have expired.
  100. // Returns true if all the processes exited, false otherwise.
  101. BASE_EXPORT bool WaitForProcessesToExit(
  102. const FilePath::StringType& executable_name,
  103. butil::TimeDelta wait,
  104. const ProcessFilter* filter);
  105. // Wait for a single process to exit. Return true if it exited cleanly within
  106. // the given time limit. On Linux |handle| must be a child process, however
  107. // on Mac and Windows it can be any process.
  108. BASE_EXPORT bool WaitForSingleProcess(ProcessHandle handle,
  109. butil::TimeDelta wait);
  110. // Waits a certain amount of time (can be 0) for all the processes with a given
  111. // executable name to exit, then kills off any of them that are still around.
  112. // If filter is non-null, then only processes selected by the filter are waited
  113. // on. Killed processes are ended with the given exit code. Returns false if
  114. // any processes needed to be killed, true if they all exited cleanly within
  115. // the wait_milliseconds delay.
  116. BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name,
  117. butil::TimeDelta wait,
  118. int exit_code,
  119. const ProcessFilter* filter);
  120. // This method ensures that the specified process eventually terminates, and
  121. // then it closes the given process handle.
  122. //
  123. // It assumes that the process has already been signalled to exit, and it
  124. // begins by waiting a small amount of time for it to exit. If the process
  125. // does not appear to have exited, then this function starts to become
  126. // aggressive about ensuring that the process terminates.
  127. //
  128. // On Linux this method does not block the calling thread.
  129. // On OS X this method may block for up to 2 seconds.
  130. //
  131. // NOTE: The process handle must have been opened with the PROCESS_TERMINATE
  132. // and SYNCHRONIZE permissions.
  133. //
  134. BASE_EXPORT void EnsureProcessTerminated(ProcessHandle process_handle);
  135. #if defined(OS_POSIX) && !defined(OS_MACOSX)
  136. // The nicer version of EnsureProcessTerminated() that is patient and will
  137. // wait for |process_handle| to finish and then reap it.
  138. BASE_EXPORT void EnsureProcessGetsReaped(ProcessHandle process_handle);
  139. #endif
  140. } // namespace butil
  141. #endif // BASE_PROCESS_KILL_H_