| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process/kill.h" | 5 #include "base/process/kill.h" |
| 6 | 6 |
| 7 #include <io.h> | 7 #include <io.h> |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/process/process_iterator.h" | 14 #include "base/process/process_iterator.h" |
| 15 #include "base/profiler/scoped_tracker.h" | |
| 16 #include "base/win/object_watcher.h" | 15 #include "base/win/object_watcher.h" |
| 17 | 16 |
| 18 namespace base { | 17 namespace base { |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 // Exit codes with special meanings on Windows. | 21 // Exit codes with special meanings on Windows. |
| 23 const DWORD kNormalTerminationExitCode = 0; | 22 const DWORD kNormalTerminationExitCode = 0; |
| 24 const DWORD kDebuggerInactiveExitCode = 0xC0000354; | 23 const DWORD kDebuggerInactiveExitCode = 0xC0000354; |
| 25 const DWORD kKeyboardInterruptExitCode = 0xC000013A; | 24 const DWORD kKeyboardInterruptExitCode = 0xC000013A; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 TimerExpiredTask::~TimerExpiredTask() { | 63 TimerExpiredTask::~TimerExpiredTask() { |
| 65 TimedOut(); | 64 TimedOut(); |
| 66 } | 65 } |
| 67 | 66 |
| 68 void TimerExpiredTask::TimedOut() { | 67 void TimerExpiredTask::TimedOut() { |
| 69 if (process_.IsValid()) | 68 if (process_.IsValid()) |
| 70 KillProcess(); | 69 KillProcess(); |
| 71 } | 70 } |
| 72 | 71 |
| 73 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { | 72 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { |
| 74 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. | |
| 75 tracked_objects::ScopedTracker tracking_profile( | |
| 76 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 77 "418183 TimerExpiredTask::OnObjectSignaled")); | |
| 78 | |
| 79 process_.Close(); | 73 process_.Close(); |
| 80 } | 74 } |
| 81 | 75 |
| 82 void TimerExpiredTask::KillProcess() { | 76 void TimerExpiredTask::KillProcess() { |
| 83 // Stop watching the process handle since we're killing it. | 77 // Stop watching the process handle since we're killing it. |
| 84 watcher_.StopWatching(); | 78 watcher_.StopWatching(); |
| 85 | 79 |
| 86 // OK, time to get frisky. We don't actually care when the process | 80 // OK, time to get frisky. We don't actually care when the process |
| 87 // terminates. We just care that it eventually terminates, and that's what | 81 // terminates. We just care that it eventually terminates, and that's what |
| 88 // TerminateProcess should do for us. Don't check for the result code since | 82 // TerminateProcess should do for us. Don't check for the result code since |
| (...skipping 11 matching lines...) Expand all Loading... |
| 100 if (result && wait) { | 94 if (result && wait) { |
| 101 // The process may not end immediately due to pending I/O | 95 // The process may not end immediately due to pending I/O |
| 102 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000)) | 96 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000)) |
| 103 DPLOG(ERROR) << "Error waiting for process exit"; | 97 DPLOG(ERROR) << "Error waiting for process exit"; |
| 104 } else if (!result) { | 98 } else if (!result) { |
| 105 DPLOG(ERROR) << "Unable to terminate process"; | 99 DPLOG(ERROR) << "Unable to terminate process"; |
| 106 } | 100 } |
| 107 return result; | 101 return result; |
| 108 } | 102 } |
| 109 | 103 |
| 110 // Attempts to kill the process identified by the given process | |
| 111 // entry structure, giving it the specified exit code. | |
| 112 // Returns true if this is successful, false otherwise. | |
| 113 bool KillProcessById(ProcessId process_id, int exit_code, bool wait) { | |
| 114 HANDLE process = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, | |
| 115 FALSE, // Don't inherit handle | |
| 116 process_id); | |
| 117 if (!process) { | |
| 118 DPLOG(ERROR) << "Unable to open process " << process_id; | |
| 119 return false; | |
| 120 } | |
| 121 bool ret = KillProcess(process, exit_code, wait); | |
| 122 CloseHandle(process); | |
| 123 return ret; | |
| 124 } | |
| 125 | |
| 126 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { | 104 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { |
| 127 DWORD tmp_exit_code = 0; | 105 DWORD tmp_exit_code = 0; |
| 128 | 106 |
| 129 if (!::GetExitCodeProcess(handle, &tmp_exit_code)) { | 107 if (!::GetExitCodeProcess(handle, &tmp_exit_code)) { |
| 130 DPLOG(FATAL) << "GetExitCodeProcess() failed"; | 108 DPLOG(FATAL) << "GetExitCodeProcess() failed"; |
| 131 if (exit_code) { | 109 if (exit_code) { |
| 132 // This really is a random number. We haven't received any | 110 // This really is a random number. We haven't received any |
| 133 // information about the exit code, presumably because this | 111 // information about the exit code, presumably because this |
| 134 // process doesn't have permission to get the exit code, or | 112 // process doesn't have permission to get the exit code, or |
| 135 // because of some other cause for GetExitCodeProcess to fail | 113 // because of some other cause for GetExitCodeProcess to fail |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 case kKeyboardInterruptExitCode: // Control-C/end session. | 152 case kKeyboardInterruptExitCode: // Control-C/end session. |
| 175 case kDebuggerTerminatedExitCode: // Debugger terminated process. | 153 case kDebuggerTerminatedExitCode: // Debugger terminated process. |
| 176 case kProcessKilledExitCode: // Task manager kill. | 154 case kProcessKilledExitCode: // Task manager kill. |
| 177 return TERMINATION_STATUS_PROCESS_WAS_KILLED; | 155 return TERMINATION_STATUS_PROCESS_WAS_KILLED; |
| 178 default: | 156 default: |
| 179 // All other exit codes indicate crashes. | 157 // All other exit codes indicate crashes. |
| 180 return TERMINATION_STATUS_PROCESS_CRASHED; | 158 return TERMINATION_STATUS_PROCESS_CRASHED; |
| 181 } | 159 } |
| 182 } | 160 } |
| 183 | 161 |
| 184 bool WaitForExitCode(ProcessHandle handle, int* exit_code) { | |
| 185 // TODO(rvargas) crbug.com/417532: Remove this function. | |
| 186 Process process(handle); | |
| 187 return process.WaitForExit(exit_code); | |
| 188 } | |
| 189 | |
| 190 bool WaitForExitCodeWithTimeout(ProcessHandle handle, | |
| 191 int* exit_code, | |
| 192 TimeDelta timeout) { | |
| 193 if (::WaitForSingleObject( | |
| 194 handle, static_cast<DWORD>(timeout.InMilliseconds())) != WAIT_OBJECT_0) | |
| 195 return false; | |
| 196 DWORD temp_code; // Don't clobber out-parameters in case of failure. | |
| 197 if (!::GetExitCodeProcess(handle, &temp_code)) | |
| 198 return false; | |
| 199 | |
| 200 *exit_code = temp_code; | |
| 201 return true; | |
| 202 } | |
| 203 | |
| 204 bool WaitForProcessesToExit(const FilePath::StringType& executable_name, | 162 bool WaitForProcessesToExit(const FilePath::StringType& executable_name, |
| 205 TimeDelta wait, | 163 TimeDelta wait, |
| 206 const ProcessFilter* filter) { | 164 const ProcessFilter* filter) { |
| 207 bool result = true; | 165 bool result = true; |
| 208 DWORD start_time = GetTickCount(); | 166 DWORD start_time = GetTickCount(); |
| 209 | 167 |
| 210 NamedProcessIterator iter(executable_name, filter); | 168 NamedProcessIterator iter(executable_name, filter); |
| 211 for (const ProcessEntry* entry = iter.NextProcessEntry(); entry; | 169 for (const ProcessEntry* entry = iter.NextProcessEntry(); entry; |
| 212 entry = iter.NextProcessEntry()) { | 170 entry = iter.NextProcessEntry()) { |
| 213 DWORD remaining_wait = static_cast<DWORD>(std::max( | 171 DWORD remaining_wait = static_cast<DWORD>(std::max( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 243 } | 201 } |
| 244 | 202 |
| 245 MessageLoop::current()->PostDelayedTask( | 203 MessageLoop::current()->PostDelayedTask( |
| 246 FROM_HERE, | 204 FROM_HERE, |
| 247 Bind(&TimerExpiredTask::TimedOut, | 205 Bind(&TimerExpiredTask::TimedOut, |
| 248 Owned(new TimerExpiredTask(process.Pass()))), | 206 Owned(new TimerExpiredTask(process.Pass()))), |
| 249 TimeDelta::FromMilliseconds(kWaitInterval)); | 207 TimeDelta::FromMilliseconds(kWaitInterval)); |
| 250 } | 208 } |
| 251 | 209 |
| 252 } // namespace base | 210 } // namespace base |
| OLD | NEW |