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