blob: 0da3a26ae4f78e9af924609904e81db7872958e2 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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
7#include <io.h>
8#include <windows.h>
9
10#include "base/bind.h"
11#include "base/bind_helpers.h"
12#include "base/logging.h"
13#include "base/message_loop/message_loop.h"
14#include "base/process/process_iterator.h"
James Robinson646469d2014-10-03 15:33:28 -070015#include "base/win/object_watcher.h"
16
17namespace base {
18
19namespace {
20
21// Exit codes with special meanings on Windows.
22const DWORD kNormalTerminationExitCode = 0;
23const DWORD kDebuggerInactiveExitCode = 0xC0000354;
24const DWORD kKeyboardInterruptExitCode = 0xC000013A;
25const DWORD kDebuggerTerminatedExitCode = 0x40010004;
26
27// This exit code is used by the Windows task manager when it kills a
28// process. It's value is obviously not that unique, and it's
29// surprising to me that the task manager uses this value, but it
30// seems to be common practice on Windows to test for it as an
31// indication that the task manager has killed something if the
32// process goes away.
33const DWORD kProcessKilledExitCode = 1;
34
35// Maximum amount of time (in milliseconds) to wait for the process to exit.
36static const int kWaitInterval = 2000;
37
38class TimerExpiredTask : public win::ObjectWatcher::Delegate {
39 public:
James Robinsond2015d92014-12-08 13:45:40 -080040 explicit TimerExpiredTask(Process process);
James Robinson0fae0002015-05-05 16:31:51 -070041 ~TimerExpiredTask() override;
James Robinson646469d2014-10-03 15:33:28 -070042
43 void TimedOut();
44
45 // MessageLoop::Watcher -----------------------------------------------------
James Robinson0fae0002015-05-05 16:31:51 -070046 void OnObjectSignaled(HANDLE object) override;
James Robinson646469d2014-10-03 15:33:28 -070047
48 private:
49 void KillProcess();
50
51 // The process that we are watching.
James Robinsond2015d92014-12-08 13:45:40 -080052 Process process_;
James Robinson646469d2014-10-03 15:33:28 -070053
54 win::ObjectWatcher watcher_;
55
56 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask);
57};
58
James Robinsond2015d92014-12-08 13:45:40 -080059TimerExpiredTask::TimerExpiredTask(Process process) : process_(process.Pass()) {
60 watcher_.StartWatching(process_.Handle(), this);
James Robinson646469d2014-10-03 15:33:28 -070061}
62
63TimerExpiredTask::~TimerExpiredTask() {
64 TimedOut();
James Robinson646469d2014-10-03 15:33:28 -070065}
66
67void TimerExpiredTask::TimedOut() {
James Robinsond2015d92014-12-08 13:45:40 -080068 if (process_.IsValid())
James Robinson646469d2014-10-03 15:33:28 -070069 KillProcess();
70}
71
72void TimerExpiredTask::OnObjectSignaled(HANDLE object) {
James Robinsond2015d92014-12-08 13:45:40 -080073 process_.Close();
James Robinson646469d2014-10-03 15:33:28 -070074}
75
76void TimerExpiredTask::KillProcess() {
77 // Stop watching the process handle since we're killing it.
78 watcher_.StopWatching();
79
80 // OK, time to get frisky. We don't actually care when the process
81 // terminates. We just care that it eventually terminates, and that's what
82 // TerminateProcess should do for us. Don't check for the result code since
83 // it fails quite often. This should be investigated eventually.
James Robinson0fae0002015-05-05 16:31:51 -070084 process_.Terminate(kProcessKilledExitCode, false);
James Robinson646469d2014-10-03 15:33:28 -070085
86 // Now, just cleanup as if the process exited normally.
James Robinsond2015d92014-12-08 13:45:40 -080087 OnObjectSignaled(process_.Handle());
James Robinson646469d2014-10-03 15:33:28 -070088}
89
90} // namespace
91
James Robinson646469d2014-10-03 15:33:28 -070092TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) {
93 DWORD tmp_exit_code = 0;
94
95 if (!::GetExitCodeProcess(handle, &tmp_exit_code)) {
96 DPLOG(FATAL) << "GetExitCodeProcess() failed";
97 if (exit_code) {
98 // This really is a random number. We haven't received any
99 // information about the exit code, presumably because this
100 // process doesn't have permission to get the exit code, or
101 // because of some other cause for GetExitCodeProcess to fail
102 // (MSDN docs don't give the possible failure error codes for
103 // this function, so it could be anything). But we don't want
104 // to leave exit_code uninitialized, since that could cause
105 // random interpretations of the exit code. So we assume it
106 // terminated "normally" in this case.
107 *exit_code = kNormalTerminationExitCode;
108 }
109 // Assume the child has exited normally if we can't get the exit
110 // code.
111 return TERMINATION_STATUS_NORMAL_TERMINATION;
112 }
113 if (tmp_exit_code == STILL_ACTIVE) {
114 DWORD wait_result = WaitForSingleObject(handle, 0);
115 if (wait_result == WAIT_TIMEOUT) {
116 if (exit_code)
117 *exit_code = wait_result;
118 return TERMINATION_STATUS_STILL_RUNNING;
119 }
120
121 if (wait_result == WAIT_FAILED) {
122 DPLOG(ERROR) << "WaitForSingleObject() failed";
123 } else {
124 DCHECK_EQ(WAIT_OBJECT_0, wait_result);
125
126 // Strange, the process used 0x103 (STILL_ACTIVE) as exit code.
127 NOTREACHED();
128 }
129
130 return TERMINATION_STATUS_ABNORMAL_TERMINATION;
131 }
132
133 if (exit_code)
134 *exit_code = tmp_exit_code;
135
136 switch (tmp_exit_code) {
137 case kNormalTerminationExitCode:
138 return TERMINATION_STATUS_NORMAL_TERMINATION;
139 case kDebuggerInactiveExitCode: // STATUS_DEBUGGER_INACTIVE.
140 case kKeyboardInterruptExitCode: // Control-C/end session.
141 case kDebuggerTerminatedExitCode: // Debugger terminated process.
142 case kProcessKilledExitCode: // Task manager kill.
143 return TERMINATION_STATUS_PROCESS_WAS_KILLED;
144 default:
145 // All other exit codes indicate crashes.
146 return TERMINATION_STATUS_PROCESS_CRASHED;
147 }
148}
149
James Robinson646469d2014-10-03 15:33:28 -0700150bool WaitForProcessesToExit(const FilePath::StringType& executable_name,
Benjamin Lerman507bb1a2015-02-26 13:15:17 +0100151 TimeDelta wait,
James Robinson646469d2014-10-03 15:33:28 -0700152 const ProcessFilter* filter) {
153 bool result = true;
154 DWORD start_time = GetTickCount();
155
156 NamedProcessIterator iter(executable_name, filter);
157 for (const ProcessEntry* entry = iter.NextProcessEntry(); entry;
158 entry = iter.NextProcessEntry()) {
159 DWORD remaining_wait = static_cast<DWORD>(std::max(
160 static_cast<int64>(0),
161 wait.InMilliseconds() - (GetTickCount() - start_time)));
162 HANDLE process = OpenProcess(SYNCHRONIZE,
163 FALSE,
164 entry->th32ProcessID);
165 DWORD wait_result = WaitForSingleObject(process, remaining_wait);
166 CloseHandle(process);
167 result &= (wait_result == WAIT_OBJECT_0);
168 }
169
170 return result;
171}
172
James Robinson646469d2014-10-03 15:33:28 -0700173bool CleanupProcesses(const FilePath::StringType& executable_name,
Benjamin Lerman507bb1a2015-02-26 13:15:17 +0100174 TimeDelta wait,
James Robinson646469d2014-10-03 15:33:28 -0700175 int exit_code,
176 const ProcessFilter* filter) {
177 if (WaitForProcessesToExit(executable_name, wait, filter))
178 return true;
179 KillProcesses(executable_name, exit_code, filter);
180 return false;
181}
182
James Robinsond2015d92014-12-08 13:45:40 -0800183void EnsureProcessTerminated(Process process) {
184 DCHECK(!process.is_current());
James Robinson646469d2014-10-03 15:33:28 -0700185
186 // If already signaled, then we are done!
James Robinsond2015d92014-12-08 13:45:40 -0800187 if (WaitForSingleObject(process.Handle(), 0) == WAIT_OBJECT_0) {
James Robinson646469d2014-10-03 15:33:28 -0700188 return;
189 }
190
191 MessageLoop::current()->PostDelayedTask(
192 FROM_HERE,
Benjamin Lerman507bb1a2015-02-26 13:15:17 +0100193 Bind(&TimerExpiredTask::TimedOut,
194 Owned(new TimerExpiredTask(process.Pass()))),
195 TimeDelta::FromMilliseconds(kWaitInterval));
James Robinson646469d2014-10-03 15:33:28 -0700196}
197
198} // namespace base