blob: 61db31c434a1299ad65aff2c06b649da7a0b4b49 [file] [log] [blame]
[email protected]307af212013-07-10 18:36:091// 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"
rvargas61812772014-12-05 03:14:5412#include "base/process/process.h"
[email protected]dd4b51262013-07-25 21:38:2313#include "base/process/process_handle.h"
[email protected]307af212013-07-10 18:36:0914#include "base/time/time.h"
avibeced7c2015-12-24 06:47:5915#include "build/build_config.h"
[email protected]307af212013-07-10 18:36:0916
17namespace base {
18
19class ProcessFilter;
20
wfh48c487e62016-07-27 22:48:4721#if defined(OS_WIN)
22namespace win {
23
24// See definition in sandbox/win/src/sandbox_types.h
25const DWORD kSandboxFatalMemoryExceeded = 7012;
26
27} // namespace win
28
29#endif // OS_WIN
30
[email protected]307af212013-07-10 18:36:0931// Return status values from GetTerminationStatus. Don't use these as
32// exit code arguments to KillProcess*(), use platform/application
33// specific values instead.
34enum 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
oshima620225722015-06-04 19:45:2740#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]6bf2f782013-11-08 15:52:5344#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
wfh0d9532a2015-09-02 23:18:5851 TERMINATION_STATUS_LAUNCH_FAILED, // child process never launched
wfhfa40c2722016-07-26 01:12:2852 TERMINATION_STATUS_OOM, // Process died due to oom
[email protected]307af212013-07-10 18:36:0953 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.
61BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name,
62 int exit_code,
63 const ProcessFilter* filter);
64
[email protected]307af212013-07-10 18:36:0965#if defined(OS_POSIX)
66// Attempts to kill the process group identified by |process_group_id|. Returns
67// true on success.
68BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id);
69#endif // defined(OS_POSIX)
70
[email protected]307af212013-07-10 18:36:0971// Get the termination status of the process by interpreting the
72// circumstances of the child process' death. |exit_code| is set to
Wez05c2c682017-08-17 16:20:1173// 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]307af212013-07-10 18:36:0975// 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).
78BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle,
79 int* exit_code);
80
scottmg7d80f9752017-05-25 20:33:2481#if defined(OS_POSIX) && !defined(OS_FUCHSIA)
[email protected]0dac68b42013-09-17 03:50:2282// 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//
97BASE_EXPORT TerminationStatus GetKnownDeadTerminationStatus(
98 ProcessHandle handle, int* exit_code);
scottmg7d80f9752017-05-25 20:33:2499#endif // defined(OS_POSIX) && !defined(OS_FUCHSIA)
[email protected]307af212013-07-10 18:36:09100
scottmg7d80f9752017-05-25 20:33:24101// These are only sparingly used, and not needed on Fuchsia. They could be
102// implemented if necessary.
103#if !defined(OS_FUCHSIA)
[email protected]307af212013-07-10 18:36:09104// 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.
108BASE_EXPORT bool WaitForProcessesToExit(
109 const FilePath::StringType& executable_name,
110 base::TimeDelta wait,
111 const ProcessFilter* filter);
112
[email protected]307af212013-07-10 18:36:09113// 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.
119BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name,
120 base::TimeDelta wait,
121 int exit_code,
122 const ProcessFilter* filter);
scottmg7d80f9752017-05-25 20:33:24123#endif // !defined(OS_FUCHSIA)
[email protected]307af212013-07-10 18:36:09124
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 Graham19168b82017-06-15 19:33:16134// On OS X and Fuchsia, this method may block for up to 2 seconds.
[email protected]307af212013-07-10 18:36:09135//
rvargas61812772014-12-05 03:14:54136// NOTE: The process must have been opened with the PROCESS_TERMINATE and
137// SYNCHRONIZE permissions.
[email protected]307af212013-07-10 18:36:09138//
rvargas61812772014-12-05 03:14:54139BASE_EXPORT void EnsureProcessTerminated(Process process);
[email protected]307af212013-07-10 18:36:09140
scottmg7d80f9752017-05-25 20:33:24141#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_FUCHSIA)
[email protected]307af212013-07-10 18:36:09142// The nicer version of EnsureProcessTerminated() that is patient and will
rvargasc7b523fa2015-01-14 01:13:23143// wait for |pid| to finish and then reap it.
144BASE_EXPORT void EnsureProcessGetsReaped(ProcessId pid);
[email protected]307af212013-07-10 18:36:09145#endif
146
147} // namespace base
148
149#endif // BASE_PROCESS_KILL_H_