blob: 70a04d97e5a739aaf151fd5027a42f5bd4960b1e [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
Bruce Dawsonec0158512017-11-16 09:10:4027// Exit codes with special meanings on Windows.
28const DWORD kNormalTerminationExitCode = 0;
29const DWORD kDebuggerInactiveExitCode = 0xC0000354;
30const DWORD kKeyboardInterruptExitCode = 0xC000013A;
31const DWORD kDebuggerTerminatedExitCode = 0x40010004;
Will Harris07925d12019-10-31 03:03:0532const DWORD kStatusInvalidImageHashExitCode = 0xC0000428;
Bruce Dawsonec0158512017-11-16 09:10:4033
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.
40const DWORD kProcessKilledExitCode = 1;
41
wfh48c487e62016-07-27 22:48:4742} // namespace win
43
44#endif // OS_WIN
45
[email protected]307af212013-07-10 18:36:0946// Return status values from GetTerminationStatus. Don't use these as
47// exit code arguments to KillProcess*(), use platform/application
48// specific values instead.
49enum TerminationStatus {
Will Harris07925d12019-10-31 03:03:0550 // clang-format off
[email protected]307af212013-07-10 18:36:0951 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
oshima620225722015-06-04 19:45:2756#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
[email protected]6bf2f782013-11-08 15:52:5360#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
wfh0d9532a2015-09-02 23:18:5867 TERMINATION_STATUS_LAUNCH_FAILED, // child process never launched
wfhfa40c2722016-07-26 01:12:2868 TERMINATION_STATUS_OOM, // Process died due to oom
Will Harris07925d12019-10-31 03:03:0569#if defined(OS_WIN)
70 // On Windows, the OS terminated process due to code integrity failure.
71 TERMINATION_STATUS_INTEGRITY_FAILURE,
72#endif
[email protected]307af212013-07-10 18:36:0973 TERMINATION_STATUS_MAX_ENUM
Will Harris07925d12019-10-31 03:03:0574 // clang-format on
[email protected]307af212013-07-10 18:36:0975};
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.
82BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name,
83 int exit_code,
84 const ProcessFilter* filter);
85
[email protected]307af212013-07-10 18:36:0986#if defined(OS_POSIX)
87// Attempts to kill the process group identified by |process_group_id|. Returns
88// true on success.
89BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id);
90#endif // defined(OS_POSIX)
91
[email protected]307af212013-07-10 18:36:0992// Get the termination status of the process by interpreting the
93// circumstances of the child process' death. |exit_code| is set to
Wez05c2c682017-08-17 16:20:1194// the status returned by waitpid() on POSIX, and from GetExitCodeProcess() on
95// Windows, and may not be null. Note that on Linux, this function
[email protected]307af212013-07-10 18:36:0996// 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).
99BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle,
100 int* exit_code);
101
Wez35539132018-07-17 11:26:05102#if defined(OS_POSIX)
[email protected]0dac68b42013-09-17 03:50:22103// 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//
118BASE_EXPORT TerminationStatus GetKnownDeadTerminationStatus(
119 ProcessHandle handle, int* exit_code);
Wezc18a57c2018-04-02 20:20:14120
121#if defined(OS_LINUX)
122// Spawns a thread to wait asynchronously for the child |process| to exit
123// and then reaps it.
124BASE_EXPORT void EnsureProcessGetsReaped(Process process);
125#endif // defined(OS_LINUX)
Wez35539132018-07-17 11:26:05126#endif // defined(OS_POSIX)
[email protected]307af212013-07-10 18:36:09127
Wezc18a57c2018-04-02 20:20:14128// 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.
134BASE_EXPORT void EnsureProcessTerminated(Process process);
135
scottmg7d80f9752017-05-25 20:33:24136// These are only sparingly used, and not needed on Fuchsia. They could be
137// implemented if necessary.
138#if !defined(OS_FUCHSIA)
[email protected]307af212013-07-10 18:36:09139// 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.
143BASE_EXPORT bool WaitForProcessesToExit(
144 const FilePath::StringType& executable_name,
145 base::TimeDelta wait,
146 const ProcessFilter* filter);
147
[email protected]307af212013-07-10 18:36:09148// 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.
154BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name,
155 base::TimeDelta wait,
156 int exit_code,
157 const ProcessFilter* filter);
scottmg7d80f9752017-05-25 20:33:24158#endif // !defined(OS_FUCHSIA)
[email protected]307af212013-07-10 18:36:09159
[email protected]307af212013-07-10 18:36:09160} // namespace base
161
162#endif // BASE_PROCESS_KILL_H_