| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 1 | // Copyright (c) 2014 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 | |
| siggi | 420541c | 2014-11-18 23:16:32 | [diff] [blame] | 5 | #include "components/browser_watcher/exit_code_watcher_win.h" |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 6 | |
| Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 7 | #include <windows.h> |
| 8 | |
| dcheng | 70c4942 | 2016-03-02 23:20:34 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 11 | #include "base/logging.h" |
| Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 12 | #include "base/metrics/sparse_histogram.h" |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 13 | #include "base/process/kill.h" |
| Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 14 | #include "base/process/process.h" |
| 15 | #include "base/process/process_handle.h" |
| Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame^] | 16 | #include "base/task/sequenced_task_runner.h" |
| Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
| 18 | #include "base/threading/thread_task_runner_handle.h" |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 19 | |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 20 | namespace browser_watcher { |
| 21 | |
| Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 22 | const char kBrowserExitCodeHistogramName[] = "Stability.BrowserExitCodes"; |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 23 | |
| Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 24 | ExitCodeWatcher::ExitCodeWatcher() |
| Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 25 | : background_thread_("ExitCodeWatcherThread"), |
| 26 | exit_code_(STILL_ACTIVE), |
| 27 | stop_watching_handle_(CreateEvent(nullptr, TRUE, FALSE, nullptr)) { |
| 28 | DCHECK(stop_watching_handle_.IsValid()); |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 29 | } |
| 30 | |
| Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 31 | ExitCodeWatcher::~ExitCodeWatcher() {} |
| 32 | |
| erikwright | 7c4a426 | 2015-01-09 18:32:33 | [diff] [blame] | 33 | bool ExitCodeWatcher::Initialize(base::Process process) { |
| rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame] | 34 | if (!process.IsValid()) { |
| 35 | LOG(ERROR) << "Invalid parent handle, can't get parent process ID."; |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | DWORD process_pid = process.Pid(); |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 40 | if (process_pid == 0) { |
| erikwright | 7c4a426 | 2015-01-09 18:32:33 | [diff] [blame] | 41 | LOG(ERROR) << "Invalid parent handle, can't get parent process ID."; |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | |
| 45 | FILETIME creation_time = {}; |
| 46 | FILETIME dummy = {}; |
| erikwright | 7c4a426 | 2015-01-09 18:32:33 | [diff] [blame] | 47 | if (!::GetProcessTimes(process.Handle(), &creation_time, &dummy, &dummy, |
| 48 | &dummy)) { |
| 49 | PLOG(ERROR) << "Invalid parent handle, can't get parent process times."; |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | |
| rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame] | 53 | // Success, take ownership of the process. |
| dcheng | 70c4942 | 2016-03-02 23:20:34 | [diff] [blame] | 54 | process_ = std::move(process); |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 55 | |
| Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | |
| 59 | bool ExitCodeWatcher::StartWatching() { |
| 60 | if (!background_thread_.StartWithOptions( |
| 61 | base::Thread::Options(base::MessagePumpType::IO, 0))) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if (!background_thread_.task_runner()->PostTask( |
| 66 | FROM_HERE, base::BindOnce(&ExitCodeWatcher::WaitForExit, |
| 67 | base::Unretained(this)))) { |
| 68 | background_thread_.Stop(); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return true; |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 73 | } |
| 74 | |
| Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 75 | void ExitCodeWatcher::StopWatching() { |
| 76 | if (stop_watching_handle_.IsValid()) { |
| 77 | SetEvent(stop_watching_handle_.Get()); |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 78 | } |
| Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 79 | } |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 80 | |
| Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 81 | void ExitCodeWatcher::WaitForExit() { |
| 82 | base::Process::WaitExitStatus wait_result = |
| 83 | process_.WaitForExitOrEvent(stop_watching_handle_, &exit_code_); |
| 84 | if (wait_result == base::Process::WaitExitStatus::PROCESS_EXITED) { |
| 85 | WriteProcessExitCode(exit_code_); |
| 86 | } else if (wait_result == base::Process::WaitExitStatus::FAILED) { |
| 87 | LOG(ERROR) << "Failed to wait for process exit or stop event"; |
| 88 | } |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) { |
| Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 92 | if (exit_code != STILL_ACTIVE) { |
| 93 | // Record the exit codes in a sparse stability histogram, as the range of |
| 94 | // values used to report failures is large. |
| 95 | base::HistogramBase* exit_code_histogram = |
| 96 | base::SparseHistogram::FactoryGet( |
| 97 | kBrowserExitCodeHistogramName, |
| 98 | base::HistogramBase::kUmaStabilityHistogramFlag); |
| 99 | exit_code_histogram->Add(exit_code); |
| 100 | return true; |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 101 | } |
| Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 102 | return false; |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } // namespace browser_watcher |