blob: 6a0038e2c00d7dac6fe0bcda4251283caf8accf3 [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#include "base/process/kill.h"
6
[email protected]307af212013-07-10 18:36:097#include <windows.h>
avibeced7c2015-12-24 06:47:598#include <io.h>
9#include <stdint.h>
[email protected]307af212013-07-10 18:36:0910
fdoray7b67e0332016-09-27 12:23:3311#include <algorithm>
dcheng70c49422016-03-02 23:20:3412#include <utility>
13
[email protected]307af212013-07-10 18:36:0914#include "base/bind.h"
15#include "base/bind_helpers.h"
16#include "base/logging.h"
avibeced7c2015-12-24 06:47:5917#include "base/macros.h"
wfhfa40c2722016-07-26 01:12:2818#include "base/process/memory.h"
[email protected]307af212013-07-10 18:36:0919#include "base/process/process_iterator.h"
fdorayc407aee2017-01-10 14:06:2620#include "base/task_scheduler/post_task.h"
[email protected]307af212013-07-10 18:36:0921
22namespace base {
23
24namespace {
25
26// Exit codes with special meanings on Windows.
27const DWORD kNormalTerminationExitCode = 0;
28const DWORD kDebuggerInactiveExitCode = 0xC0000354;
29const DWORD kKeyboardInterruptExitCode = 0xC000013A;
30const DWORD kDebuggerTerminatedExitCode = 0x40010004;
31
32// This exit code is used by the Windows task manager when it kills a
33// process. It's value is obviously not that unique, and it's
34// surprising to me that the task manager uses this value, but it
35// seems to be common practice on Windows to test for it as an
36// indication that the task manager has killed something if the
37// process goes away.
38const DWORD kProcessKilledExitCode = 1;
39
[email protected]307af212013-07-10 18:36:0940} // namespace
41
[email protected]307af212013-07-10 18:36:0942TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) {
Wez05c2c682017-08-17 16:20:1143 DCHECK(exit_code);
44
[email protected]307af212013-07-10 18:36:0945 DWORD tmp_exit_code = 0;
46
47 if (!::GetExitCodeProcess(handle, &tmp_exit_code)) {
[email protected]ad8cfa92014-05-21 20:06:2348 DPLOG(FATAL) << "GetExitCodeProcess() failed";
Wez05c2c682017-08-17 16:20:1149
50 // This really is a random number. We haven't received any
51 // information about the exit code, presumably because this
52 // process doesn't have permission to get the exit code, or
53 // because of some other cause for GetExitCodeProcess to fail
54 // (MSDN docs don't give the possible failure error codes for
55 // this function, so it could be anything). But we don't want
56 // to leave exit_code uninitialized, since that could cause
57 // random interpretations of the exit code. So we assume it
58 // terminated "normally" in this case.
59 *exit_code = kNormalTerminationExitCode;
60
[email protected]307af212013-07-10 18:36:0961 // Assume the child has exited normally if we can't get the exit
62 // code.
63 return TERMINATION_STATUS_NORMAL_TERMINATION;
64 }
65 if (tmp_exit_code == STILL_ACTIVE) {
66 DWORD wait_result = WaitForSingleObject(handle, 0);
67 if (wait_result == WAIT_TIMEOUT) {
Wez05c2c682017-08-17 16:20:1168 *exit_code = wait_result;
[email protected]307af212013-07-10 18:36:0969 return TERMINATION_STATUS_STILL_RUNNING;
70 }
71
72 if (wait_result == WAIT_FAILED) {
[email protected]ad8cfa92014-05-21 20:06:2373 DPLOG(ERROR) << "WaitForSingleObject() failed";
[email protected]307af212013-07-10 18:36:0974 } else {
75 DCHECK_EQ(WAIT_OBJECT_0, wait_result);
76
77 // Strange, the process used 0x103 (STILL_ACTIVE) as exit code.
78 NOTREACHED();
79 }
80
81 return TERMINATION_STATUS_ABNORMAL_TERMINATION;
82 }
83
Wez05c2c682017-08-17 16:20:1184 *exit_code = tmp_exit_code;
[email protected]307af212013-07-10 18:36:0985
86 switch (tmp_exit_code) {
87 case kNormalTerminationExitCode:
88 return TERMINATION_STATUS_NORMAL_TERMINATION;
89 case kDebuggerInactiveExitCode: // STATUS_DEBUGGER_INACTIVE.
90 case kKeyboardInterruptExitCode: // Control-C/end session.
91 case kDebuggerTerminatedExitCode: // Debugger terminated process.
92 case kProcessKilledExitCode: // Task manager kill.
93 return TERMINATION_STATUS_PROCESS_WAS_KILLED;
wfh48c487e62016-07-27 22:48:4794 case base::win::kSandboxFatalMemoryExceeded: // Terminated process due to
95 // exceeding the sandbox job
96 // object memory limits.
wfhfa40c2722016-07-26 01:12:2897 case base::win::kOomExceptionCode: // Ran out of memory.
98 return TERMINATION_STATUS_OOM;
[email protected]307af212013-07-10 18:36:0999 default:
100 // All other exit codes indicate crashes.
101 return TERMINATION_STATUS_PROCESS_CRASHED;
102 }
103}
104
[email protected]307af212013-07-10 18:36:09105bool WaitForProcessesToExit(const FilePath::StringType& executable_name,
rvargas2f70a152015-02-24 00:28:11106 TimeDelta wait,
[email protected]307af212013-07-10 18:36:09107 const ProcessFilter* filter) {
[email protected]307af212013-07-10 18:36:09108 bool result = true;
109 DWORD start_time = GetTickCount();
110
111 NamedProcessIterator iter(executable_name, filter);
[email protected]ed8e57da2014-07-03 07:03:39112 for (const ProcessEntry* entry = iter.NextProcessEntry(); entry;
113 entry = iter.NextProcessEntry()) {
avibeced7c2015-12-24 06:47:59114 DWORD remaining_wait = static_cast<DWORD>(
115 std::max(static_cast<int64_t>(0),
116 wait.InMilliseconds() - (GetTickCount() - start_time)));
brucedawsond3509432015-09-18 18:28:13117 HANDLE process = OpenProcess(SYNCHRONIZE,
118 FALSE,
119 entry->th32ProcessID);
120 DWORD wait_result = WaitForSingleObject(process, remaining_wait);
121 CloseHandle(process);
[email protected]ed8e57da2014-07-03 07:03:39122 result &= (wait_result == WAIT_OBJECT_0);
[email protected]307af212013-07-10 18:36:09123 }
124
125 return result;
126}
127
[email protected]307af212013-07-10 18:36:09128bool CleanupProcesses(const FilePath::StringType& executable_name,
rvargas2f70a152015-02-24 00:28:11129 TimeDelta wait,
[email protected]307af212013-07-10 18:36:09130 int exit_code,
131 const ProcessFilter* filter) {
[email protected]ed8e57da2014-07-03 07:03:39132 if (WaitForProcessesToExit(executable_name, wait, filter))
133 return true;
134 KillProcesses(executable_name, exit_code, filter);
135 return false;
[email protected]307af212013-07-10 18:36:09136}
137
rvargas61812772014-12-05 03:14:54138void EnsureProcessTerminated(Process process) {
139 DCHECK(!process.is_current());
[email protected]307af212013-07-10 18:36:09140
141 // If already signaled, then we are done!
fdorayc407aee2017-01-10 14:06:26142 if (WaitForSingleObject(process.Handle(), 0) == WAIT_OBJECT_0)
[email protected]307af212013-07-10 18:36:09143 return;
[email protected]307af212013-07-10 18:36:09144
fdorayc407aee2017-01-10 14:06:26145 PostDelayedTaskWithTraits(
146 FROM_HERE,
fdorayf7d6f7602017-05-09 04:18:32147 {TaskPriority::BACKGROUND, TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
fdorayc407aee2017-01-10 14:06:26148 Bind(
149 [](Process process) {
150 if (WaitForSingleObject(process.Handle(), 0) == WAIT_OBJECT_0)
151 return;
152 process.Terminate(kProcessKilledExitCode, false);
153 },
154 Passed(&process)),
155 TimeDelta::FromSeconds(2));
[email protected]307af212013-07-10 18:36:09156}
157
158} // namespace base