[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 | |
| 27 | } // namespace win |
| 28 | |
| 29 | #endif // OS_WIN |
| 30 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 31 | // Return status values from GetTerminationStatus. Don't use these as |
| 32 | // exit code arguments to KillProcess*(), use platform/application |
| 33 | // specific values instead. |
| 34 | enum TerminationStatus { |
| 35 | TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status |
| 36 | TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status |
| 37 | TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill |
| 38 | TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault |
| 39 | TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 40 | #if defined(OS_CHROMEOS) |
| 41 | // Used for the case when oom-killer kills a process on ChromeOS. |
| 42 | TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM, |
| 43 | #endif |
[email protected] | 6bf2f78 | 2013-11-08 15:52:53 | [diff] [blame] | 44 | #if defined(OS_ANDROID) |
| 45 | // On Android processes are spawned from the system Zygote and we do not get |
| 46 | // the termination status. We can't know if the termination was a crash or an |
| 47 | // oom kill for sure, but we can use status of the strong process bindings as |
| 48 | // a hint. |
| 49 | TERMINATION_STATUS_OOM_PROTECTED, // child was protected from oom kill |
| 50 | #endif |
wfh | 0d9532a | 2015-09-02 23:18:58 | [diff] [blame] | 51 | TERMINATION_STATUS_LAUNCH_FAILED, // child process never launched |
wfh | fa40c272 | 2016-07-26 01:12:28 | [diff] [blame] | 52 | TERMINATION_STATUS_OOM, // Process died due to oom |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 53 | TERMINATION_STATUS_MAX_ENUM |
| 54 | }; |
| 55 | |
| 56 | // Attempts to kill all the processes on the current machine that were launched |
| 57 | // from the given executable name, ending them with the given exit code. If |
| 58 | // filter is non-null, then only processes selected by the filter are killed. |
| 59 | // Returns true if all processes were able to be killed off, false if at least |
| 60 | // one couldn't be killed. |
| 61 | BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name, |
| 62 | int exit_code, |
| 63 | const ProcessFilter* filter); |
| 64 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 65 | #if defined(OS_POSIX) |
| 66 | // Attempts to kill the process group identified by |process_group_id|. Returns |
| 67 | // true on success. |
| 68 | BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id); |
| 69 | #endif // defined(OS_POSIX) |
| 70 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 71 | // Get the termination status of the process by interpreting the |
| 72 | // circumstances of the child process' death. |exit_code| is set to |
Wez | 05c2c68 | 2017-08-17 16:20:11 | [diff] [blame^] | 73 | // the status returned by waitpid() on POSIX, and from GetExitCodeProcess() on |
| 74 | // Windows, and may not be null. Note that on Linux, this function |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 75 | // will only return a useful result the first time it is called after |
| 76 | // the child exits (because it will reap the child and the information |
| 77 | // will no longer be available). |
| 78 | BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle, |
| 79 | int* exit_code); |
| 80 | |
scottmg | 7d80f975 | 2017-05-25 20:33:24 | [diff] [blame] | 81 | #if defined(OS_POSIX) && !defined(OS_FUCHSIA) |
[email protected] | 0dac68b4 | 2013-09-17 03:50:22 | [diff] [blame] | 82 | // Send a kill signal to the process and then wait for the process to exit |
| 83 | // and get the termination status. |
| 84 | // |
| 85 | // This is used in situations where it is believed that the process is dead |
| 86 | // or dying (because communication with the child process has been cut). |
| 87 | // In order to avoid erroneously returning that the process is still running |
| 88 | // because the kernel is still cleaning it up, this will wait for the process |
| 89 | // to terminate. In order to avoid the risk of hanging while waiting for the |
| 90 | // process to terminate, send a SIGKILL to the process before waiting for the |
| 91 | // termination status. |
| 92 | // |
| 93 | // Note that it is not an option to call WaitForExitCode and then |
| 94 | // GetTerminationStatus as the child will be reaped when WaitForExitCode |
| 95 | // returns, and this information will be lost. |
| 96 | // |
| 97 | BASE_EXPORT TerminationStatus GetKnownDeadTerminationStatus( |
| 98 | ProcessHandle handle, int* exit_code); |
scottmg | 7d80f975 | 2017-05-25 20:33:24 | [diff] [blame] | 99 | #endif // defined(OS_POSIX) && !defined(OS_FUCHSIA) |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 100 | |
scottmg | 7d80f975 | 2017-05-25 20:33:24 | [diff] [blame] | 101 | // These are only sparingly used, and not needed on Fuchsia. They could be |
| 102 | // implemented if necessary. |
| 103 | #if !defined(OS_FUCHSIA) |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 104 | // Wait for all the processes based on the named executable to exit. If filter |
| 105 | // is non-null, then only processes selected by the filter are waited on. |
| 106 | // Returns after all processes have exited or wait_milliseconds have expired. |
| 107 | // Returns true if all the processes exited, false otherwise. |
| 108 | BASE_EXPORT bool WaitForProcessesToExit( |
| 109 | const FilePath::StringType& executable_name, |
| 110 | base::TimeDelta wait, |
| 111 | const ProcessFilter* filter); |
| 112 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 113 | // Waits a certain amount of time (can be 0) for all the processes with a given |
| 114 | // executable name to exit, then kills off any of them that are still around. |
| 115 | // If filter is non-null, then only processes selected by the filter are waited |
| 116 | // on. Killed processes are ended with the given exit code. Returns false if |
| 117 | // any processes needed to be killed, true if they all exited cleanly within |
| 118 | // the wait_milliseconds delay. |
| 119 | BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name, |
| 120 | base::TimeDelta wait, |
| 121 | int exit_code, |
| 122 | const ProcessFilter* filter); |
scottmg | 7d80f975 | 2017-05-25 20:33:24 | [diff] [blame] | 123 | #endif // !defined(OS_FUCHSIA) |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 124 | |
| 125 | // This method ensures that the specified process eventually terminates, and |
| 126 | // then it closes the given process handle. |
| 127 | // |
| 128 | // It assumes that the process has already been signalled to exit, and it |
| 129 | // begins by waiting a small amount of time for it to exit. If the process |
| 130 | // does not appear to have exited, then this function starts to become |
| 131 | // aggressive about ensuring that the process terminates. |
| 132 | // |
| 133 | // On Linux this method does not block the calling thread. |
Scott Graham | 19168b8 | 2017-06-15 19:33:16 | [diff] [blame] | 134 | // On OS X and Fuchsia, this method may block for up to 2 seconds. |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 135 | // |
rvargas | 6181277 | 2014-12-05 03:14:54 | [diff] [blame] | 136 | // NOTE: The process must have been opened with the PROCESS_TERMINATE and |
| 137 | // SYNCHRONIZE permissions. |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 138 | // |
rvargas | 6181277 | 2014-12-05 03:14:54 | [diff] [blame] | 139 | BASE_EXPORT void EnsureProcessTerminated(Process process); |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 140 | |
scottmg | 7d80f975 | 2017-05-25 20:33:24 | [diff] [blame] | 141 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_FUCHSIA) |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 142 | // The nicer version of EnsureProcessTerminated() that is patient and will |
rvargas | c7b523fa | 2015-01-14 01:13:23 | [diff] [blame] | 143 | // wait for |pid| to finish and then reap it. |
| 144 | BASE_EXPORT void EnsureProcessGetsReaped(ProcessId pid); |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 145 | #endif |
| 146 | |
| 147 | } // namespace base |
| 148 | |
| 149 | #endif // BASE_PROCESS_KILL_H_ |