Plugin Power Saver: Record PPS UMAs only for users with PPS enabled.
[chromium-blink-merge.git] / base / process / kill_win.cc
blob3c93047f5b04b3d0d5eb747f0021912c717a2ce1
1 // 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.
5 #include "base/process/kill.h"
7 #include <io.h>
8 #include <windows.h>
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"
15 #include "base/win/object_watcher.h"
17 namespace base {
19 namespace {
21 // Exit codes with special meanings on Windows.
22 const DWORD kNormalTerminationExitCode = 0;
23 const DWORD kDebuggerInactiveExitCode = 0xC0000354;
24 const DWORD kKeyboardInterruptExitCode = 0xC000013A;
25 const DWORD kDebuggerTerminatedExitCode = 0x40010004;
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.
33 const DWORD kProcessKilledExitCode = 1;
35 // Maximum amount of time (in milliseconds) to wait for the process to exit.
36 static const int kWaitInterval = 2000;
38 class TimerExpiredTask : public win::ObjectWatcher::Delegate {
39 public:
40 explicit TimerExpiredTask(Process process);
41 ~TimerExpiredTask();
43 void TimedOut();
45 // MessageLoop::Watcher -----------------------------------------------------
46 virtual void OnObjectSignaled(HANDLE object);
48 private:
49 void KillProcess();
51 // The process that we are watching.
52 Process process_;
54 win::ObjectWatcher watcher_;
56 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask);
59 TimerExpiredTask::TimerExpiredTask(Process process) : process_(process.Pass()) {
60 watcher_.StartWatching(process_.Handle(), this);
63 TimerExpiredTask::~TimerExpiredTask() {
64 TimedOut();
67 void TimerExpiredTask::TimedOut() {
68 if (process_.IsValid())
69 KillProcess();
72 void TimerExpiredTask::OnObjectSignaled(HANDLE object) {
73 process_.Close();
76 void TimerExpiredTask::KillProcess() {
77 // Stop watching the process handle since we're killing it.
78 watcher_.StopWatching();
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.
84 base::KillProcess(process_.Handle(), kProcessKilledExitCode, false);
86 // Now, just cleanup as if the process exited normally.
87 OnObjectSignaled(process_.Handle());
90 } // namespace
92 bool KillProcess(ProcessHandle process, int exit_code, bool wait) {
93 bool result = (TerminateProcess(process, exit_code) != FALSE);
94 if (result && wait) {
95 // The process may not end immediately due to pending I/O
96 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000))
97 DPLOG(ERROR) << "Error waiting for process exit";
98 } else if (!result) {
99 DPLOG(ERROR) << "Unable to terminate process";
101 return result;
104 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) {
105 DWORD tmp_exit_code = 0;
107 if (!::GetExitCodeProcess(handle, &tmp_exit_code)) {
108 DPLOG(FATAL) << "GetExitCodeProcess() failed";
109 if (exit_code) {
110 // This really is a random number. We haven't received any
111 // information about the exit code, presumably because this
112 // process doesn't have permission to get the exit code, or
113 // because of some other cause for GetExitCodeProcess to fail
114 // (MSDN docs don't give the possible failure error codes for
115 // this function, so it could be anything). But we don't want
116 // to leave exit_code uninitialized, since that could cause
117 // random interpretations of the exit code. So we assume it
118 // terminated "normally" in this case.
119 *exit_code = kNormalTerminationExitCode;
121 // Assume the child has exited normally if we can't get the exit
122 // code.
123 return TERMINATION_STATUS_NORMAL_TERMINATION;
125 if (tmp_exit_code == STILL_ACTIVE) {
126 DWORD wait_result = WaitForSingleObject(handle, 0);
127 if (wait_result == WAIT_TIMEOUT) {
128 if (exit_code)
129 *exit_code = wait_result;
130 return TERMINATION_STATUS_STILL_RUNNING;
133 if (wait_result == WAIT_FAILED) {
134 DPLOG(ERROR) << "WaitForSingleObject() failed";
135 } else {
136 DCHECK_EQ(WAIT_OBJECT_0, wait_result);
138 // Strange, the process used 0x103 (STILL_ACTIVE) as exit code.
139 NOTREACHED();
142 return TERMINATION_STATUS_ABNORMAL_TERMINATION;
145 if (exit_code)
146 *exit_code = tmp_exit_code;
148 switch (tmp_exit_code) {
149 case kNormalTerminationExitCode:
150 return TERMINATION_STATUS_NORMAL_TERMINATION;
151 case kDebuggerInactiveExitCode: // STATUS_DEBUGGER_INACTIVE.
152 case kKeyboardInterruptExitCode: // Control-C/end session.
153 case kDebuggerTerminatedExitCode: // Debugger terminated process.
154 case kProcessKilledExitCode: // Task manager kill.
155 return TERMINATION_STATUS_PROCESS_WAS_KILLED;
156 default:
157 // All other exit codes indicate crashes.
158 return TERMINATION_STATUS_PROCESS_CRASHED;
162 bool WaitForProcessesToExit(const FilePath::StringType& executable_name,
163 TimeDelta wait,
164 const ProcessFilter* filter) {
165 bool result = true;
166 DWORD start_time = GetTickCount();
168 NamedProcessIterator iter(executable_name, filter);
169 for (const ProcessEntry* entry = iter.NextProcessEntry(); entry;
170 entry = iter.NextProcessEntry()) {
171 DWORD remaining_wait = static_cast<DWORD>(std::max(
172 static_cast<int64>(0),
173 wait.InMilliseconds() - (GetTickCount() - start_time)));
174 HANDLE process = OpenProcess(SYNCHRONIZE,
175 FALSE,
176 entry->th32ProcessID);
177 DWORD wait_result = WaitForSingleObject(process, remaining_wait);
178 CloseHandle(process);
179 result &= (wait_result == WAIT_OBJECT_0);
182 return result;
185 bool CleanupProcesses(const FilePath::StringType& executable_name,
186 TimeDelta wait,
187 int exit_code,
188 const ProcessFilter* filter) {
189 if (WaitForProcessesToExit(executable_name, wait, filter))
190 return true;
191 KillProcesses(executable_name, exit_code, filter);
192 return false;
195 void EnsureProcessTerminated(Process process) {
196 DCHECK(!process.is_current());
198 // If already signaled, then we are done!
199 if (WaitForSingleObject(process.Handle(), 0) == WAIT_OBJECT_0) {
200 return;
203 MessageLoop::current()->PostDelayedTask(
204 FROM_HERE,
205 Bind(&TimerExpiredTask::TimedOut,
206 Owned(new TimerExpiredTask(process.Pass()))),
207 TimeDelta::FromMilliseconds(kWaitInterval));
210 } // namespace base