xref: /aosp_15_r20/external/cronet/base/process/kill.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker // This file contains routines to kill processes and get the exit code and
6*6777b538SAndroid Build Coastguard Worker // termination status.
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #ifndef BASE_PROCESS_KILL_H_
9*6777b538SAndroid Build Coastguard Worker #define BASE_PROCESS_KILL_H_
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
12*6777b538SAndroid Build Coastguard Worker #include "base/files/file_path.h"
13*6777b538SAndroid Build Coastguard Worker #include "base/process/process.h"
14*6777b538SAndroid Build Coastguard Worker #include "base/process/process_handle.h"
15*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
16*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
17*6777b538SAndroid Build Coastguard Worker 
18*6777b538SAndroid Build Coastguard Worker namespace base {
19*6777b538SAndroid Build Coastguard Worker 
20*6777b538SAndroid Build Coastguard Worker class ProcessFilter;
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
23*6777b538SAndroid Build Coastguard Worker namespace win {
24*6777b538SAndroid Build Coastguard Worker 
25*6777b538SAndroid Build Coastguard Worker // See definition in sandbox/win/src/sandbox_types.h
26*6777b538SAndroid Build Coastguard Worker const DWORD kSandboxFatalMemoryExceeded = 7012;
27*6777b538SAndroid Build Coastguard Worker 
28*6777b538SAndroid Build Coastguard Worker // Exit codes with special meanings on Windows.
29*6777b538SAndroid Build Coastguard Worker const DWORD kNormalTerminationExitCode = 0;
30*6777b538SAndroid Build Coastguard Worker const DWORD kDebuggerInactiveExitCode = 0xC0000354;
31*6777b538SAndroid Build Coastguard Worker const DWORD kKeyboardInterruptExitCode = 0xC000013A;
32*6777b538SAndroid Build Coastguard Worker const DWORD kDebuggerTerminatedExitCode = 0x40010004;
33*6777b538SAndroid Build Coastguard Worker const DWORD kStatusInvalidImageHashExitCode = 0xC0000428;
34*6777b538SAndroid Build Coastguard Worker 
35*6777b538SAndroid Build Coastguard Worker // This exit code is used by the Windows task manager when it kills a
36*6777b538SAndroid Build Coastguard Worker // process.  It's value is obviously not that unique, and it's
37*6777b538SAndroid Build Coastguard Worker // surprising to me that the task manager uses this value, but it
38*6777b538SAndroid Build Coastguard Worker // seems to be common practice on Windows to test for it as an
39*6777b538SAndroid Build Coastguard Worker // indication that the task manager has killed something if the
40*6777b538SAndroid Build Coastguard Worker // process goes away.
41*6777b538SAndroid Build Coastguard Worker const DWORD kProcessKilledExitCode = 1;
42*6777b538SAndroid Build Coastguard Worker 
43*6777b538SAndroid Build Coastguard Worker }  // namespace win
44*6777b538SAndroid Build Coastguard Worker 
45*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_WIN)
46*6777b538SAndroid Build Coastguard Worker 
47*6777b538SAndroid Build Coastguard Worker // Return status values from GetTerminationStatus. Don't use these as exit code
48*6777b538SAndroid Build Coastguard Worker // arguments to KillProcess*(), use platform/application specific values
49*6777b538SAndroid Build Coastguard Worker // instead.
50*6777b538SAndroid Build Coastguard Worker //
51*6777b538SAndroid Build Coastguard Worker // Used for metrics. Keep in sync with the "TerminationStatus" histogram enum.
52*6777b538SAndroid Build Coastguard Worker // Do not repurpose previously used indexes.
53*6777b538SAndroid Build Coastguard Worker enum TerminationStatus {
54*6777b538SAndroid Build Coastguard Worker   // Zero exit status.
55*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_NORMAL_TERMINATION = 0,
56*6777b538SAndroid Build Coastguard Worker   // Other abnormal termination reason.
57*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_ABNORMAL_TERMINATION = 1,
58*6777b538SAndroid Build Coastguard Worker   // E.g. SIGKILL or task manager kill.
59*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_PROCESS_WAS_KILLED = 2,
60*6777b538SAndroid Build Coastguard Worker   // E.g. Segmentation fault.
61*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_PROCESS_CRASHED = 3,
62*6777b538SAndroid Build Coastguard Worker   // Child hasn't exited yet.
63*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_STILL_RUNNING = 4,
64*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS)
65*6777b538SAndroid Build Coastguard Worker   // OOM-killer killed the process on ChromeOS.
66*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM = 5,
67*6777b538SAndroid Build Coastguard Worker #endif
68*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_ANDROID)
69*6777b538SAndroid Build Coastguard Worker   // On Android processes are spawned from the system Zygote and we do not get
70*6777b538SAndroid Build Coastguard Worker   // the termination status. We can't know if the termination was a crash or an
71*6777b538SAndroid Build Coastguard Worker   // oom kill for sure, but we can use status of the strong process bindings as
72*6777b538SAndroid Build Coastguard Worker   // a hint.
73*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_OOM_PROTECTED = 6,
74*6777b538SAndroid Build Coastguard Worker #endif
75*6777b538SAndroid Build Coastguard Worker   // Child process never launched.
76*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_LAUNCH_FAILED = 7,
77*6777b538SAndroid Build Coastguard Worker   // Out of memory.
78*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_OOM = 8,
79*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN)
80*6777b538SAndroid Build Coastguard Worker   // On Windows, the OS terminated process due to code integrity failure.
81*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_INTEGRITY_FAILURE = 9,
82*6777b538SAndroid Build Coastguard Worker #endif
83*6777b538SAndroid Build Coastguard Worker   TERMINATION_STATUS_MAX_ENUM = 10,
84*6777b538SAndroid Build Coastguard Worker };
85*6777b538SAndroid Build Coastguard Worker 
86*6777b538SAndroid Build Coastguard Worker // Attempts to kill all the processes on the current machine that were launched
87*6777b538SAndroid Build Coastguard Worker // from the given executable name, ending them with the given exit code.  If
88*6777b538SAndroid Build Coastguard Worker // filter is non-null, then only processes selected by the filter are killed.
89*6777b538SAndroid Build Coastguard Worker // Returns true if all processes were able to be killed off, false if at least
90*6777b538SAndroid Build Coastguard Worker // one couldn't be killed.
91*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name,
92*6777b538SAndroid Build Coastguard Worker                                int exit_code,
93*6777b538SAndroid Build Coastguard Worker                                const ProcessFilter* filter);
94*6777b538SAndroid Build Coastguard Worker 
95*6777b538SAndroid Build Coastguard Worker // Get the termination status of the process by interpreting the
96*6777b538SAndroid Build Coastguard Worker // circumstances of the child process' death. |exit_code| is set to
97*6777b538SAndroid Build Coastguard Worker // the status returned by waitpid() on POSIX, and from GetExitCodeProcess() on
98*6777b538SAndroid Build Coastguard Worker // Windows, and may not be null.  Note that on Linux, this function
99*6777b538SAndroid Build Coastguard Worker // will only return a useful result the first time it is called after
100*6777b538SAndroid Build Coastguard Worker // the child exits (because it will reap the child and the information
101*6777b538SAndroid Build Coastguard Worker // will no longer be available).
102*6777b538SAndroid Build Coastguard Worker BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle,
103*6777b538SAndroid Build Coastguard Worker                                                    int* exit_code);
104*6777b538SAndroid Build Coastguard Worker 
105*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_POSIX)
106*6777b538SAndroid Build Coastguard Worker // Send a kill signal to the process and then wait for the process to exit
107*6777b538SAndroid Build Coastguard Worker // and get the termination status.
108*6777b538SAndroid Build Coastguard Worker //
109*6777b538SAndroid Build Coastguard Worker // This is used in situations where it is believed that the process is dead
110*6777b538SAndroid Build Coastguard Worker // or dying (because communication with the child process has been cut).
111*6777b538SAndroid Build Coastguard Worker // In order to avoid erroneously returning that the process is still running
112*6777b538SAndroid Build Coastguard Worker // because the kernel is still cleaning it up, this will wait for the process
113*6777b538SAndroid Build Coastguard Worker // to terminate. In order to avoid the risk of hanging while waiting for the
114*6777b538SAndroid Build Coastguard Worker // process to terminate, send a SIGKILL to the process before waiting for the
115*6777b538SAndroid Build Coastguard Worker // termination status.
116*6777b538SAndroid Build Coastguard Worker //
117*6777b538SAndroid Build Coastguard Worker // Note that it is not an option to call WaitForExitCode and then
118*6777b538SAndroid Build Coastguard Worker // GetTerminationStatus as the child will be reaped when WaitForExitCode
119*6777b538SAndroid Build Coastguard Worker // returns, and this information will be lost.
120*6777b538SAndroid Build Coastguard Worker //
121*6777b538SAndroid Build Coastguard Worker BASE_EXPORT TerminationStatus GetKnownDeadTerminationStatus(
122*6777b538SAndroid Build Coastguard Worker     ProcessHandle handle, int* exit_code);
123*6777b538SAndroid Build Coastguard Worker 
124*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
125*6777b538SAndroid Build Coastguard Worker // Spawns a thread to wait asynchronously for the child |process| to exit
126*6777b538SAndroid Build Coastguard Worker // and then reaps it.
127*6777b538SAndroid Build Coastguard Worker BASE_EXPORT void EnsureProcessGetsReaped(Process process);
128*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
129*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_POSIX)
130*6777b538SAndroid Build Coastguard Worker 
131*6777b538SAndroid Build Coastguard Worker // Registers |process| to be asynchronously monitored for termination, forcibly
132*6777b538SAndroid Build Coastguard Worker // terminated if necessary, and reaped on exit. The caller should have signalled
133*6777b538SAndroid Build Coastguard Worker // |process| to exit before calling this API. The API will allow a couple of
134*6777b538SAndroid Build Coastguard Worker // seconds grace period before forcibly terminating |process|.
135*6777b538SAndroid Build Coastguard Worker // TODO(https://crbug.com/806451): The Mac implementation currently blocks the
136*6777b538SAndroid Build Coastguard Worker // calling thread for up to two seconds.
137*6777b538SAndroid Build Coastguard Worker BASE_EXPORT void EnsureProcessTerminated(Process process);
138*6777b538SAndroid Build Coastguard Worker 
139*6777b538SAndroid Build Coastguard Worker // These are only sparingly used, and not needed on Fuchsia or iOS. They could
140*6777b538SAndroid Build Coastguard Worker // be implemented if necessary.
141*6777b538SAndroid Build Coastguard Worker #if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
142*6777b538SAndroid Build Coastguard Worker // Wait for all the processes based on the named executable to exit.  If filter
143*6777b538SAndroid Build Coastguard Worker // is non-null, then only processes selected by the filter are waited on.
144*6777b538SAndroid Build Coastguard Worker // Returns after all processes have exited or wait_milliseconds have expired.
145*6777b538SAndroid Build Coastguard Worker // Returns true if all the processes exited, false otherwise.
146*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool WaitForProcessesToExit(
147*6777b538SAndroid Build Coastguard Worker     const FilePath::StringType& executable_name,
148*6777b538SAndroid Build Coastguard Worker     base::TimeDelta wait,
149*6777b538SAndroid Build Coastguard Worker     const ProcessFilter* filter);
150*6777b538SAndroid Build Coastguard Worker 
151*6777b538SAndroid Build Coastguard Worker // Waits a certain amount of time (can be 0) for all the processes with a given
152*6777b538SAndroid Build Coastguard Worker // executable name to exit, then kills off any of them that are still around.
153*6777b538SAndroid Build Coastguard Worker // If filter is non-null, then only processes selected by the filter are waited
154*6777b538SAndroid Build Coastguard Worker // on.  Killed processes are ended with the given exit code.  Returns false if
155*6777b538SAndroid Build Coastguard Worker // any processes needed to be killed, true if they all exited cleanly within
156*6777b538SAndroid Build Coastguard Worker // the wait_milliseconds delay.
157*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name,
158*6777b538SAndroid Build Coastguard Worker                                   base::TimeDelta wait,
159*6777b538SAndroid Build Coastguard Worker                                   int exit_code,
160*6777b538SAndroid Build Coastguard Worker                                   const ProcessFilter* filter);
161*6777b538SAndroid Build Coastguard Worker #endif  // !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
162*6777b538SAndroid Build Coastguard Worker 
163*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && TARGET_OS_SIMULATOR)
164*6777b538SAndroid Build Coastguard Worker // This is common code used by kill_ios.cc when building with iOS simulator it
165*6777b538SAndroid Build Coastguard Worker // does not need to be exported.
166*6777b538SAndroid Build Coastguard Worker void WaitForChildToDie(pid_t child, int timeout_seconds);
167*6777b538SAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && TARGET_OS_SIMULATOR)
168*6777b538SAndroid Build Coastguard Worker 
169*6777b538SAndroid Build Coastguard Worker }  // namespace base
170*6777b538SAndroid Build Coastguard Worker 
171*6777b538SAndroid Build Coastguard Worker #endif  // BASE_PROCESS_KILL_H_
172