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