[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 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 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 7 | #include <windows.h> |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 8 | #include <io.h> |
| 9 | #include <stdint.h> |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 10 | |
fdoray | 7b67e033 | 2016-09-27 12:23:33 | [diff] [blame] | 11 | #include <algorithm> |
dcheng | 70c4942 | 2016-03-02 23:20:34 | [diff] [blame] | 12 | #include <utility> |
| 13 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 14 | #include "base/bind.h" |
| 15 | #include "base/bind_helpers.h" |
| 16 | #include "base/logging.h" |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 17 | #include "base/macros.h" |
wfh | fa40c272 | 2016-07-26 01:12:28 | [diff] [blame] | 18 | #include "base/process/memory.h" |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 19 | #include "base/process/process_iterator.h" |
fdoray | c407aee | 2017-01-10 14:06:26 | [diff] [blame] | 20 | #include "base/task_scheduler/post_task.h" |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // Exit codes with special meanings on Windows. |
| 27 | const DWORD kNormalTerminationExitCode = 0; |
| 28 | const DWORD kDebuggerInactiveExitCode = 0xC0000354; |
| 29 | const DWORD kKeyboardInterruptExitCode = 0xC000013A; |
| 30 | const 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. |
| 38 | const DWORD kProcessKilledExitCode = 1; |
| 39 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 40 | } // namespace |
| 41 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 42 | TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { |
Wez | 05c2c68 | 2017-08-17 16:20:11 | [diff] [blame^] | 43 | DCHECK(exit_code); |
| 44 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 45 | DWORD tmp_exit_code = 0; |
| 46 | |
| 47 | if (!::GetExitCodeProcess(handle, &tmp_exit_code)) { |
[email protected] | ad8cfa9 | 2014-05-21 20:06:23 | [diff] [blame] | 48 | DPLOG(FATAL) << "GetExitCodeProcess() failed"; |
Wez | 05c2c68 | 2017-08-17 16:20:11 | [diff] [blame^] | 49 | |
| 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] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 61 | // 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) { |
Wez | 05c2c68 | 2017-08-17 16:20:11 | [diff] [blame^] | 68 | *exit_code = wait_result; |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 69 | return TERMINATION_STATUS_STILL_RUNNING; |
| 70 | } |
| 71 | |
| 72 | if (wait_result == WAIT_FAILED) { |
[email protected] | ad8cfa9 | 2014-05-21 20:06:23 | [diff] [blame] | 73 | DPLOG(ERROR) << "WaitForSingleObject() failed"; |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 74 | } 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 | |
Wez | 05c2c68 | 2017-08-17 16:20:11 | [diff] [blame^] | 84 | *exit_code = tmp_exit_code; |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 85 | |
| 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; |
wfh | 48c487e6 | 2016-07-27 22:48:47 | [diff] [blame] | 94 | case base::win::kSandboxFatalMemoryExceeded: // Terminated process due to |
| 95 | // exceeding the sandbox job |
| 96 | // object memory limits. |
wfh | fa40c272 | 2016-07-26 01:12:28 | [diff] [blame] | 97 | case base::win::kOomExceptionCode: // Ran out of memory. |
| 98 | return TERMINATION_STATUS_OOM; |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 99 | default: |
| 100 | // All other exit codes indicate crashes. |
| 101 | return TERMINATION_STATUS_PROCESS_CRASHED; |
| 102 | } |
| 103 | } |
| 104 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 105 | bool WaitForProcessesToExit(const FilePath::StringType& executable_name, |
rvargas | 2f70a15 | 2015-02-24 00:28:11 | [diff] [blame] | 106 | TimeDelta wait, |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 107 | const ProcessFilter* filter) { |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 108 | bool result = true; |
| 109 | DWORD start_time = GetTickCount(); |
| 110 | |
| 111 | NamedProcessIterator iter(executable_name, filter); |
[email protected] | ed8e57da | 2014-07-03 07:03:39 | [diff] [blame] | 112 | for (const ProcessEntry* entry = iter.NextProcessEntry(); entry; |
| 113 | entry = iter.NextProcessEntry()) { |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 114 | DWORD remaining_wait = static_cast<DWORD>( |
| 115 | std::max(static_cast<int64_t>(0), |
| 116 | wait.InMilliseconds() - (GetTickCount() - start_time))); |
brucedawson | d350943 | 2015-09-18 18:28:13 | [diff] [blame] | 117 | HANDLE process = OpenProcess(SYNCHRONIZE, |
| 118 | FALSE, |
| 119 | entry->th32ProcessID); |
| 120 | DWORD wait_result = WaitForSingleObject(process, remaining_wait); |
| 121 | CloseHandle(process); |
[email protected] | ed8e57da | 2014-07-03 07:03:39 | [diff] [blame] | 122 | result &= (wait_result == WAIT_OBJECT_0); |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return result; |
| 126 | } |
| 127 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 128 | bool CleanupProcesses(const FilePath::StringType& executable_name, |
rvargas | 2f70a15 | 2015-02-24 00:28:11 | [diff] [blame] | 129 | TimeDelta wait, |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 130 | int exit_code, |
| 131 | const ProcessFilter* filter) { |
[email protected] | ed8e57da | 2014-07-03 07:03:39 | [diff] [blame] | 132 | if (WaitForProcessesToExit(executable_name, wait, filter)) |
| 133 | return true; |
| 134 | KillProcesses(executable_name, exit_code, filter); |
| 135 | return false; |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 136 | } |
| 137 | |
rvargas | 6181277 | 2014-12-05 03:14:54 | [diff] [blame] | 138 | void EnsureProcessTerminated(Process process) { |
| 139 | DCHECK(!process.is_current()); |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 140 | |
| 141 | // If already signaled, then we are done! |
fdoray | c407aee | 2017-01-10 14:06:26 | [diff] [blame] | 142 | if (WaitForSingleObject(process.Handle(), 0) == WAIT_OBJECT_0) |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 143 | return; |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 144 | |
fdoray | c407aee | 2017-01-10 14:06:26 | [diff] [blame] | 145 | PostDelayedTaskWithTraits( |
| 146 | FROM_HERE, |
fdoray | f7d6f760 | 2017-05-09 04:18:32 | [diff] [blame] | 147 | {TaskPriority::BACKGROUND, TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
fdoray | c407aee | 2017-01-10 14:06:26 | [diff] [blame] | 148 | 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] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace base |