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