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" 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 } // namespace win 28 29 #endif // OS_WIN 30 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 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 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 51 TERMINATION_STATUS_LAUNCH_FAILED, // child process never launched 52 TERMINATION_STATUS_OOM, // Process died due to oom 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 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 71 // Get the termination status of the process by interpreting the 72 // circumstances of the child process' death. |exit_code| is set to 73 // the status returned by waitpid() on POSIX, and from 74 // GetExitCodeProcess() on Windows. |exit_code| may be NULL if the 75 // caller is not interested in it. Note that on Linux, this function 76 // will only return a useful result the first time it is called after 77 // the child exits (because it will reap the child and the information 78 // will no longer be available). 79 BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle, 80 int* exit_code); 81 82 #if defined(OS_POSIX) 83 // Send a kill signal to the process and then wait for the process to exit 84 // and get the termination status. 85 // 86 // This is used in situations where it is believed that the process is dead 87 // or dying (because communication with the child process has been cut). 88 // In order to avoid erroneously returning that the process is still running 89 // because the kernel is still cleaning it up, this will wait for the process 90 // to terminate. In order to avoid the risk of hanging while waiting for the 91 // process to terminate, send a SIGKILL to the process before waiting for the 92 // termination status. 93 // 94 // Note that it is not an option to call WaitForExitCode and then 95 // GetTerminationStatus as the child will be reaped when WaitForExitCode 96 // returns, and this information will be lost. 97 // 98 BASE_EXPORT TerminationStatus GetKnownDeadTerminationStatus( 99 ProcessHandle handle, int* exit_code); 100 #endif // defined(OS_POSIX) 101 102 // Wait for all the processes based on the named executable to exit. If filter 103 // is non-null, then only processes selected by the filter are waited on. 104 // Returns after all processes have exited or wait_milliseconds have expired. 105 // Returns true if all the processes exited, false otherwise. 106 BASE_EXPORT bool WaitForProcessesToExit( 107 const FilePath::StringType& executable_name, 108 base::TimeDelta wait, 109 const ProcessFilter* filter); 110 111 // Waits a certain amount of time (can be 0) for all the processes with a given 112 // executable name to exit, then kills off any of them that are still around. 113 // If filter is non-null, then only processes selected by the filter are waited 114 // on. Killed processes are ended with the given exit code. Returns false if 115 // any processes needed to be killed, true if they all exited cleanly within 116 // the wait_milliseconds delay. 117 BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name, 118 base::TimeDelta wait, 119 int exit_code, 120 const ProcessFilter* filter); 121 122 // This method ensures that the specified process eventually terminates, and 123 // then it closes the given process handle. 124 // 125 // It assumes that the process has already been signalled to exit, and it 126 // begins by waiting a small amount of time for it to exit. If the process 127 // does not appear to have exited, then this function starts to become 128 // aggressive about ensuring that the process terminates. 129 // 130 // On Linux this method does not block the calling thread. 131 // On OS X this method may block for up to 2 seconds. 132 // 133 // NOTE: The process must have been opened with the PROCESS_TERMINATE and 134 // SYNCHRONIZE permissions. 135 // 136 BASE_EXPORT void EnsureProcessTerminated(Process process); 137 138 #if defined(OS_POSIX) && !defined(OS_MACOSX) 139 // The nicer version of EnsureProcessTerminated() that is patient and will 140 // wait for |pid| to finish and then reap it. 141 BASE_EXPORT void EnsureProcessGetsReaped(ProcessId pid); 142 #endif 143 144 } // namespace base 145 146 #endif // BASE_PROCESS_KILL_H_ 147