| 6 |
| 7 | #include <algorithm> |
| 8 |
| 9 | #include <windows.h> |
| 10 | #include <io.h> |
| 11 | #include <stdint.h> |
| 12 |
| 13 | #include "base/logging.h" |
| 14 | #include "base/macros.h" |
| 15 | #include "base/process/memory.h" |
| 16 | #include "base/process/process_iterator.h" |
| 17 |
| 18 | namespace base { |
| 19 |
| 20 | TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { |
| 21 | DCHECK(exit_code); |
| 22 |
| 23 | DWORD tmp_exit_code = 0; |
| 24 |
| 25 | if (!::GetExitCodeProcess(handle, &tmp_exit_code)) { |
| 26 | DPLOG(FATAL) << "GetExitCodeProcess() failed"; |
| 27 |
| 28 | // This really is a random number. We haven't received any |
| 29 | // information about the exit code, presumably because this |
| 30 | // process doesn't have permission to get the exit code, or |
| 31 | // because of some other cause for GetExitCodeProcess to fail |
| 32 | // (MSDN docs don't give the possible failure error codes for |
| 33 | // this function, so it could be anything). But we don't want |
| 34 | // to leave exit_code uninitialized, since that could cause |
| 35 | // random interpretations of the exit code. So we assume it |
| 36 | // terminated "normally" in this case. |
| 37 | *exit_code = win::kNormalTerminationExitCode; |
| 38 |
| 39 | // Assume the child has exited normally if we can't get the exit |
| 40 | // code. |
| 41 | return TERMINATION_STATUS_NORMAL_TERMINATION; |
| 42 | } |
| 43 | if (tmp_exit_code == STILL_ACTIVE) { |
| 44 | DWORD wait_result = WaitForSingleObject(handle, 0); |
| 45 | if (wait_result == WAIT_TIMEOUT) { |
| 46 | *exit_code = wait_result; |
| 47 | return TERMINATION_STATUS_STILL_RUNNING; |
| 48 | } |
| 49 |
| 50 | if (wait_result == WAIT_FAILED) { |
| 51 | DPLOG(ERROR) << "WaitForSingleObject() failed"; |
| 52 | } else { |
| 53 | DCHECK_EQ(WAIT_OBJECT_0, wait_result); |
| 54 |
| 55 | // Strange, the process used 0x103 (STILL_ACTIVE) as exit code. |
| 56 | NOTREACHED(); |
| 57 | } |
| 58 |
| 59 | return TERMINATION_STATUS_ABNORMAL_TERMINATION; |
| 60 | } |
| 61 |
| 62 | *exit_code = tmp_exit_code; |
| 63 |
| 64 | // clang-format off |
| 65 | switch (tmp_exit_code) { |
| 66 | case win::kNormalTerminationExitCode: |
| 67 | return TERMINATION_STATUS_NORMAL_TERMINATION; |
| 68 | case win::kDebuggerInactiveExitCode: // STATUS_DEBUGGER_INACTIVE. |
| 69 | case win::kKeyboardInterruptExitCode: // Control-C/end session. |
| 70 | case win::kDebuggerTerminatedExitCode: // Debugger terminated process. |
| 71 | case win::kProcessKilledExitCode: // Task manager kill. |
| 72 | return TERMINATION_STATUS_PROCESS_WAS_KILLED; |
| 73 | case win::kSandboxFatalMemoryExceeded: // Terminated process due to |
| 74 | // exceeding the sandbox job |
| 75 | // object memory limits. |
| 76 | case win::kOomExceptionCode: // Ran out of memory. |
| 77 | return TERMINATION_STATUS_OOM; |
| 78 | // This exit code means the process failed an OS integrity check. |
| 79 | // This is tested in ProcessMitigationsTest.* in sandbox. |
| 80 | case win::kStatusInvalidImageHashExitCode: |
| 81 | return TERMINATION_STATUS_INTEGRITY_FAILURE; |
| 82 | default: |
| 83 | // All other exit codes indicate crashes. |
| 84 | return TERMINATION_STATUS_PROCESS_CRASHED; |
| 85 | } |
| 86 | // clang-format on |
| 87 | } |
| 88 |
| 89 | bool WaitForProcessesToExit(const FilePath::StringType& executable_name, |
| 90 | TimeDelta wait, |
| 91 | const ProcessFilter* filter) { |
| 92 | bool result = true; |
| 93 | DWORD start_time = GetTickCount(); |
| 94 |
| 95 | NamedProcessIterator iter(executable_name, filter); |
| 96 | for (const ProcessEntry* entry = iter.NextProcessEntry(); entry; |
| 97 | entry = iter.NextProcessEntry()) { |
| 98 | DWORD remaining_wait = static_cast<DWORD>( |
| 99 | std::max(static_cast<int64_t>(0), |
| 100 | wait.InMilliseconds() - (GetTickCount() - start_time))); |
| 101 | HANDLE process = OpenProcess(SYNCHRONIZE, |
| 102 | FALSE, |
| 103 | entry->th32ProcessID); |
| 104 | DWORD wait_result = WaitForSingleObject(process, remaining_wait); |
| 105 | CloseHandle(process); |
| 106 | result &= (wait_result == WAIT_OBJECT_0); |
| 107 | } |
| 108 |
| 109 | return result; |
| 110 | } |
| 111 |
| 112 | bool CleanupProcesses(const FilePath::StringType& executable_name, |
| 113 | TimeDelta wait, |
| 114 | int exit_code, |
| 115 | const ProcessFilter* filter) { |
| 116 | if (WaitForProcessesToExit(executable_name, wait, filter)) |
| 117 | return true; |
| 118 | KillProcesses(executable_name, exit_code, filter); |
| 119 | return false; |
| 120 | } |
| 121 |
| 122 | } // namespace base |
| 123 |