OLD | NEW |
(Empty) | |
| 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. |
| 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.h" |
| 14 #include "base/process/process_iterator.h" |
| 15 #include "base/process_util.h" |
| 16 #include "base/win/object_watcher.h" |
| 17 |
| 18 namespace base { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Exit codes with special meanings on Windows. |
| 23 const DWORD kNormalTerminationExitCode = 0; |
| 24 const DWORD kDebuggerInactiveExitCode = 0xC0000354; |
| 25 const DWORD kKeyboardInterruptExitCode = 0xC000013A; |
| 26 const DWORD kDebuggerTerminatedExitCode = 0x40010004; |
| 27 |
| 28 // This exit code is used by the Windows task manager when it kills a |
| 29 // process. It's value is obviously not that unique, and it's |
| 30 // surprising to me that the task manager uses this value, but it |
| 31 // seems to be common practice on Windows to test for it as an |
| 32 // indication that the task manager has killed something if the |
| 33 // process goes away. |
| 34 const DWORD kProcessKilledExitCode = 1; |
| 35 |
| 36 // Maximum amount of time (in milliseconds) to wait for the process to exit. |
| 37 static const int kWaitInterval = 2000; |
| 38 |
| 39 class TimerExpiredTask : public win::ObjectWatcher::Delegate { |
| 40 public: |
| 41 explicit TimerExpiredTask(ProcessHandle process); |
| 42 ~TimerExpiredTask(); |
| 43 |
| 44 void TimedOut(); |
| 45 |
| 46 // MessageLoop::Watcher ----------------------------------------------------- |
| 47 virtual void OnObjectSignaled(HANDLE object); |
| 48 |
| 49 private: |
| 50 void KillProcess(); |
| 51 |
| 52 // The process that we are watching. |
| 53 ProcessHandle process_; |
| 54 |
| 55 win::ObjectWatcher watcher_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask); |
| 58 }; |
| 59 |
| 60 TimerExpiredTask::TimerExpiredTask(ProcessHandle process) : process_(process) { |
| 61 watcher_.StartWatching(process_, this); |
| 62 } |
| 63 |
| 64 TimerExpiredTask::~TimerExpiredTask() { |
| 65 TimedOut(); |
| 66 DCHECK(!process_) << "Make sure to close the handle."; |
| 67 } |
| 68 |
| 69 void TimerExpiredTask::TimedOut() { |
| 70 if (process_) |
| 71 KillProcess(); |
| 72 } |
| 73 |
| 74 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { |
| 75 CloseHandle(process_); |
| 76 process_ = NULL; |
| 77 } |
| 78 |
| 79 void TimerExpiredTask::KillProcess() { |
| 80 // Stop watching the process handle since we're killing it. |
| 81 watcher_.StopWatching(); |
| 82 |
| 83 // OK, time to get frisky. We don't actually care when the process |
| 84 // terminates. We just care that it eventually terminates, and that's what |
| 85 // TerminateProcess should do for us. Don't check for the result code since |
| 86 // it fails quite often. This should be investigated eventually. |
| 87 base::KillProcess(process_, kProcessKilledExitCode, false); |
| 88 |
| 89 // Now, just cleanup as if the process exited normally. |
| 90 OnObjectSignaled(process_); |
| 91 } |
| 92 |
| 93 } // namespace |
| 94 |
| 95 bool KillProcess(ProcessHandle process, int exit_code, bool wait) { |
| 96 bool result = (TerminateProcess(process, exit_code) != FALSE); |
| 97 if (result && wait) { |
| 98 // The process may not end immediately due to pending I/O |
| 99 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000)) |
| 100 DLOG_GETLASTERROR(ERROR) << "Error waiting for process exit"; |
| 101 } else if (!result) { |
| 102 DLOG_GETLASTERROR(ERROR) << "Unable to terminate process"; |
| 103 } |
| 104 return result; |
| 105 } |
| 106 |
| 107 // Attempts to kill the process identified by the given process |
| 108 // entry structure, giving it the specified exit code. |
| 109 // Returns true if this is successful, false otherwise. |
| 110 bool KillProcessById(ProcessId process_id, int exit_code, bool wait) { |
| 111 HANDLE process = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, |
| 112 FALSE, // Don't inherit handle |
| 113 process_id); |
| 114 if (!process) { |
| 115 DLOG_GETLASTERROR(ERROR) << "Unable to open process " << process_id; |
| 116 return false; |
| 117 } |
| 118 bool ret = KillProcess(process, exit_code, wait); |
| 119 CloseHandle(process); |
| 120 return ret; |
| 121 } |
| 122 |
| 123 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { |
| 124 DWORD tmp_exit_code = 0; |
| 125 |
| 126 if (!::GetExitCodeProcess(handle, &tmp_exit_code)) { |
| 127 DLOG_GETLASTERROR(FATAL) << "GetExitCodeProcess() failed"; |
| 128 if (exit_code) { |
| 129 // This really is a random number. We haven't received any |
| 130 // information about the exit code, presumably because this |
| 131 // process doesn't have permission to get the exit code, or |
| 132 // because of some other cause for GetExitCodeProcess to fail |
| 133 // (MSDN docs don't give the possible failure error codes for |
| 134 // this function, so it could be anything). But we don't want |
| 135 // to leave exit_code uninitialized, since that could cause |
| 136 // random interpretations of the exit code. So we assume it |
| 137 // terminated "normally" in this case. |
| 138 *exit_code = kNormalTerminationExitCode; |
| 139 } |
| 140 // Assume the child has exited normally if we can't get the exit |
| 141 // code. |
| 142 return TERMINATION_STATUS_NORMAL_TERMINATION; |
| 143 } |
| 144 if (tmp_exit_code == STILL_ACTIVE) { |
| 145 DWORD wait_result = WaitForSingleObject(handle, 0); |
| 146 if (wait_result == WAIT_TIMEOUT) { |
| 147 if (exit_code) |
| 148 *exit_code = wait_result; |
| 149 return TERMINATION_STATUS_STILL_RUNNING; |
| 150 } |
| 151 |
| 152 if (wait_result == WAIT_FAILED) { |
| 153 DLOG_GETLASTERROR(ERROR) << "WaitForSingleObject() failed"; |
| 154 } else { |
| 155 DCHECK_EQ(WAIT_OBJECT_0, wait_result); |
| 156 |
| 157 // Strange, the process used 0x103 (STILL_ACTIVE) as exit code. |
| 158 NOTREACHED(); |
| 159 } |
| 160 |
| 161 return TERMINATION_STATUS_ABNORMAL_TERMINATION; |
| 162 } |
| 163 |
| 164 if (exit_code) |
| 165 *exit_code = tmp_exit_code; |
| 166 |
| 167 switch (tmp_exit_code) { |
| 168 case kNormalTerminationExitCode: |
| 169 return TERMINATION_STATUS_NORMAL_TERMINATION; |
| 170 case kDebuggerInactiveExitCode: // STATUS_DEBUGGER_INACTIVE. |
| 171 case kKeyboardInterruptExitCode: // Control-C/end session. |
| 172 case kDebuggerTerminatedExitCode: // Debugger terminated process. |
| 173 case kProcessKilledExitCode: // Task manager kill. |
| 174 return TERMINATION_STATUS_PROCESS_WAS_KILLED; |
| 175 default: |
| 176 // All other exit codes indicate crashes. |
| 177 return TERMINATION_STATUS_PROCESS_CRASHED; |
| 178 } |
| 179 } |
| 180 |
| 181 bool WaitForExitCode(ProcessHandle handle, int* exit_code) { |
| 182 bool success = WaitForExitCodeWithTimeout( |
| 183 handle, exit_code, base::TimeDelta::FromMilliseconds(INFINITE)); |
| 184 CloseProcessHandle(handle); |
| 185 return success; |
| 186 } |
| 187 |
| 188 bool WaitForExitCodeWithTimeout(ProcessHandle handle, |
| 189 int* exit_code, |
| 190 base::TimeDelta timeout) { |
| 191 if (::WaitForSingleObject(handle, timeout.InMilliseconds()) != WAIT_OBJECT_0) |
| 192 return false; |
| 193 DWORD temp_code; // Don't clobber out-parameters in case of failure. |
| 194 if (!::GetExitCodeProcess(handle, &temp_code)) |
| 195 return false; |
| 196 |
| 197 *exit_code = temp_code; |
| 198 return true; |
| 199 } |
| 200 |
| 201 bool WaitForProcessesToExit(const FilePath::StringType& executable_name, |
| 202 base::TimeDelta wait, |
| 203 const ProcessFilter* filter) { |
| 204 const ProcessEntry* entry; |
| 205 bool result = true; |
| 206 DWORD start_time = GetTickCount(); |
| 207 |
| 208 NamedProcessIterator iter(executable_name, filter); |
| 209 while ((entry = iter.NextProcessEntry())) { |
| 210 DWORD remaining_wait = std::max<int64>( |
| 211 0, wait.InMilliseconds() - (GetTickCount() - start_time)); |
| 212 HANDLE process = OpenProcess(SYNCHRONIZE, |
| 213 FALSE, |
| 214 entry->th32ProcessID); |
| 215 DWORD wait_result = WaitForSingleObject(process, remaining_wait); |
| 216 CloseHandle(process); |
| 217 result = result && (wait_result == WAIT_OBJECT_0); |
| 218 } |
| 219 |
| 220 return result; |
| 221 } |
| 222 |
| 223 bool WaitForSingleProcess(ProcessHandle handle, base::TimeDelta wait) { |
| 224 int exit_code; |
| 225 if (!WaitForExitCodeWithTimeout(handle, &exit_code, wait)) |
| 226 return false; |
| 227 return exit_code == 0; |
| 228 } |
| 229 |
| 230 bool CleanupProcesses(const FilePath::StringType& executable_name, |
| 231 base::TimeDelta wait, |
| 232 int exit_code, |
| 233 const ProcessFilter* filter) { |
| 234 bool exited_cleanly = WaitForProcessesToExit(executable_name, wait, filter); |
| 235 if (!exited_cleanly) |
| 236 KillProcesses(executable_name, exit_code, filter); |
| 237 return exited_cleanly; |
| 238 } |
| 239 |
| 240 void EnsureProcessTerminated(ProcessHandle process) { |
| 241 DCHECK(process != GetCurrentProcess()); |
| 242 |
| 243 // If already signaled, then we are done! |
| 244 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { |
| 245 CloseHandle(process); |
| 246 return; |
| 247 } |
| 248 |
| 249 MessageLoop::current()->PostDelayedTask( |
| 250 FROM_HERE, |
| 251 base::Bind(&TimerExpiredTask::TimedOut, |
| 252 base::Owned(new TimerExpiredTask(process))), |
| 253 base::TimeDelta::FromMilliseconds(kWaitInterval)); |
| 254 } |
| 255 |
| 256 } // namespace base |
OLD | NEW |