| 6 |
| // termination status. |
| 7 |
| 8 | #ifndef BASE_PROCESS_KILL_H_ |
| 9 | #define BASE_PROCESS_KILL_H_ |
| 10 |
| 11 | #include "base/files/file_path.h" |
| 12 | #include "base/process/process.h" |
| 13 | #include "base/process/process_handle.h" |
| 14 | #include "base/time/time.h" |
| 15 | #include "build/build_config.h" |
| 16 |
| 17 | namespace base { |
| 18 |
| 19 | class ProcessFilter; |
| 20 |
| 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 | // 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; |
| 32 | const DWORD kStatusInvalidImageHashExitCode = 0xC0000428; |
| 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 |
| 42 | } // namespace win |
| 43 |
| 44 | #endif // OS_WIN |
| 45 |
| 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 { |
| 50 | // clang-format off |
| 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 |
| 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 |
| 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 |
| 67 | TERMINATION_STATUS_LAUNCH_FAILED, // child process never launched |
| 68 | TERMINATION_STATUS_OOM, // Process died due to oom |
| 69 | #if defined(OS_WIN) |
| 70 | // On Windows, the OS terminated process due to code integrity failure. |
| 71 | TERMINATION_STATUS_INTEGRITY_FAILURE, |
| 72 | #endif |
| 73 | TERMINATION_STATUS_MAX_ENUM |
| 74 | // clang-format on |
| 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 |
| 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 |
| 92 | // Get the termination status of the process by interpreting the |
| 93 | // circumstances of the child process' death. |exit_code| is set to |
| 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 |
| 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 |
| 102 | #if defined(OS_POSIX) |
| 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); |
| 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) |
| 126 | #endif // defined(OS_POSIX) |
| 127 |
| 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 |
| 136 | // These are only sparingly used, and not needed on Fuchsia. They could be |
| 137 | // implemented if necessary. |
| 138 | #if !defined(OS_FUCHSIA) |
| 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 |
| 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); |
| 158 | #endif // !defined(OS_FUCHSIA) |
| 159 |
| 160 | } // namespace base |
| 161 |
| 162 | #endif // BASE_PROCESS_KILL_H_ |
| 163 |