blob: a316c36b0c1ea94baddad6746b5e265e1b7f2f9a [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]307af212013-07-10 18:36:092// 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
David Sanders6e709942022-04-05 06:49:2611#include "base/base_export.h"
[email protected]307af212013-07-10 18:36:0912#include "base/files/file_path.h"
rvargas61812772014-12-05 03:14:5413#include "base/process/process.h"
[email protected]dd4b51262013-07-25 21:38:2314#include "base/process/process_handle.h"
[email protected]307af212013-07-10 18:36:0915#include "base/time/time.h"
avibeced7c2015-12-24 06:47:5916#include "build/build_config.h"
[email protected]307af212013-07-10 18:36:0917
18namespace base {
19
20class ProcessFilter;
21
Xiaohan Wang37e81612022-01-15 18:27:0022#if BUILDFLAG(IS_WIN)
wfh48c487e62016-07-27 22:48:4723namespace win {
24
25// See definition in sandbox/win/src/sandbox_types.h
26const DWORD kSandboxFatalMemoryExceeded = 7012;
27
Bruce Dawsonec0158512017-11-16 09:10:4028// Exit codes with special meanings on Windows.
29const DWORD kNormalTerminationExitCode = 0;
30const DWORD kDebuggerInactiveExitCode = 0xC0000354;
31const DWORD kKeyboardInterruptExitCode = 0xC000013A;
32const DWORD kDebuggerTerminatedExitCode = 0x40010004;
Will Harris07925d12019-10-31 03:03:0533const DWORD kStatusInvalidImageHashExitCode = 0xC0000428;
Bruce Dawsonec0158512017-11-16 09:10:4034
35// This exit code is used by the Windows task manager when it kills a
36// process. It's value is obviously not that unique, and it's
37// surprising to me that the task manager uses this value, but it
38// seems to be common practice on Windows to test for it as an
39// indication that the task manager has killed something if the
40// process goes away.
41const DWORD kProcessKilledExitCode = 1;
42
wfh48c487e62016-07-27 22:48:4743} // namespace win
44
Xiaohan Wang37e81612022-01-15 18:27:0045#endif // BUILDFLAG(IS_WIN)
wfh48c487e62016-07-27 22:48:4746
[email protected]307af212013-07-10 18:36:0947// Return status values from GetTerminationStatus. Don't use these as
48// exit code arguments to KillProcess*(), use platform/application
49// specific values instead.
50enum TerminationStatus {
Will Harris07925d12019-10-31 03:03:0551 // clang-format off
[email protected]307af212013-07-10 18:36:0952 TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status
53 TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status
54 TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill
55 TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault
56 TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet
Xiaohan Wang37e81612022-01-15 18:27:0057#if BUILDFLAG(IS_CHROMEOS)
oshima620225722015-06-04 19:45:2758 // Used for the case when oom-killer kills a process on ChromeOS.
59 TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM,
60#endif
Xiaohan Wang37e81612022-01-15 18:27:0061#if BUILDFLAG(IS_ANDROID)
[email protected]6bf2f782013-11-08 15:52:5362 // On Android processes are spawned from the system Zygote and we do not get
63 // the termination status. We can't know if the termination was a crash or an
64 // oom kill for sure, but we can use status of the strong process bindings as
65 // a hint.
66 TERMINATION_STATUS_OOM_PROTECTED, // child was protected from oom kill
67#endif
wfh0d9532a2015-09-02 23:18:5868 TERMINATION_STATUS_LAUNCH_FAILED, // child process never launched
wfhfa40c2722016-07-26 01:12:2869 TERMINATION_STATUS_OOM, // Process died due to oom
Xiaohan Wang37e81612022-01-15 18:27:0070#if BUILDFLAG(IS_WIN)
Will Harris07925d12019-10-31 03:03:0571 // On Windows, the OS terminated process due to code integrity failure.
72 TERMINATION_STATUS_INTEGRITY_FAILURE,
73#endif
[email protected]307af212013-07-10 18:36:0974 TERMINATION_STATUS_MAX_ENUM
Will Harris07925d12019-10-31 03:03:0575 // clang-format on
[email protected]307af212013-07-10 18:36:0976};
77
78// Attempts to kill all the processes on the current machine that were launched
79// from the given executable name, ending them with the given exit code. If
80// filter is non-null, then only processes selected by the filter are killed.
81// Returns true if all processes were able to be killed off, false if at least
82// one couldn't be killed.
83BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name,
84 int exit_code,
85 const ProcessFilter* filter);
86
[email protected]307af212013-07-10 18:36:0987// Get the termination status of the process by interpreting the
88// circumstances of the child process' death. |exit_code| is set to
Wez05c2c682017-08-17 16:20:1189// the status returned by waitpid() on POSIX, and from GetExitCodeProcess() on
90// Windows, and may not be null. Note that on Linux, this function
[email protected]307af212013-07-10 18:36:0991// will only return a useful result the first time it is called after
92// the child exits (because it will reap the child and the information
93// will no longer be available).
94BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle,
95 int* exit_code);
96
Xiaohan Wang37e81612022-01-15 18:27:0097#if BUILDFLAG(IS_POSIX)
[email protected]0dac68b42013-09-17 03:50:2298// Send a kill signal to the process and then wait for the process to exit
99// and get the termination status.
100//
101// This is used in situations where it is believed that the process is dead
102// or dying (because communication with the child process has been cut).
103// In order to avoid erroneously returning that the process is still running
104// because the kernel is still cleaning it up, this will wait for the process
105// to terminate. In order to avoid the risk of hanging while waiting for the
106// process to terminate, send a SIGKILL to the process before waiting for the
107// termination status.
108//
109// Note that it is not an option to call WaitForExitCode and then
110// GetTerminationStatus as the child will be reaped when WaitForExitCode
111// returns, and this information will be lost.
112//
113BASE_EXPORT TerminationStatus GetKnownDeadTerminationStatus(
114 ProcessHandle handle, int* exit_code);
Wezc18a57c2018-04-02 20:20:14115
Xiaohan Wang37e81612022-01-15 18:27:00116#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Wezc18a57c2018-04-02 20:20:14117// Spawns a thread to wait asynchronously for the child |process| to exit
118// and then reaps it.
119BASE_EXPORT void EnsureProcessGetsReaped(Process process);
Xiaohan Wang37e81612022-01-15 18:27:00120#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
121#endif // BUILDFLAG(IS_POSIX)
[email protected]307af212013-07-10 18:36:09122
Wezc18a57c2018-04-02 20:20:14123// Registers |process| to be asynchronously monitored for termination, forcibly
124// terminated if necessary, and reaped on exit. The caller should have signalled
125// |process| to exit before calling this API. The API will allow a couple of
126// seconds grace period before forcibly terminating |process|.
127// TODO(https://crbug.com/806451): The Mac implementation currently blocks the
128// calling thread for up to two seconds.
129BASE_EXPORT void EnsureProcessTerminated(Process process);
130
scottmg7d80f9752017-05-25 20:33:24131// These are only sparingly used, and not needed on Fuchsia. They could be
132// implemented if necessary.
Xiaohan Wang37e81612022-01-15 18:27:00133#if !BUILDFLAG(IS_FUCHSIA)
[email protected]307af212013-07-10 18:36:09134// Wait for all the processes based on the named executable to exit. If filter
135// is non-null, then only processes selected by the filter are waited on.
136// Returns after all processes have exited or wait_milliseconds have expired.
137// Returns true if all the processes exited, false otherwise.
138BASE_EXPORT bool WaitForProcessesToExit(
139 const FilePath::StringType& executable_name,
140 base::TimeDelta wait,
141 const ProcessFilter* filter);
142
[email protected]307af212013-07-10 18:36:09143// Waits a certain amount of time (can be 0) for all the processes with a given
144// executable name to exit, then kills off any of them that are still around.
145// If filter is non-null, then only processes selected by the filter are waited
146// on. Killed processes are ended with the given exit code. Returns false if
147// any processes needed to be killed, true if they all exited cleanly within
148// the wait_milliseconds delay.
149BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name,
150 base::TimeDelta wait,
151 int exit_code,
152 const ProcessFilter* filter);
Xiaohan Wang37e81612022-01-15 18:27:00153#endif // !BUILDFLAG(IS_FUCHSIA)
[email protected]307af212013-07-10 18:36:09154
[email protected]307af212013-07-10 18:36:09155} // namespace base
156
157#endif // BASE_PROCESS_KILL_H_