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