blob: c6f01baac941d5e8119eab3443e9b4a096eda99d [file] [log] [blame]
siggic0d0a0e2014-11-17 23:29:191// 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
siggi420541c2014-11-18 23:16:325#include "components/browser_watcher/exit_code_watcher_win.h"
siggic0d0a0e2014-11-17 23:29:196
Mike Rorkef36971b2020-03-06 17:56:297#include <windows.h>
8
dcheng70c49422016-03-02 23:20:349#include <utility>
10
siggic0d0a0e2014-11-17 23:29:1911#include "base/logging.h"
Chris Davis2e8cc3af2020-02-06 21:40:3212#include "base/metrics/sparse_histogram.h"
siggic0d0a0e2014-11-17 23:29:1913#include "base/process/kill.h"
Chris Davis2e8cc3af2020-02-06 21:40:3214#include "base/process/process.h"
15#include "base/process/process_handle.h"
Patrick Monette643cdf62021-10-15 19:13:4216#include "base/task/sequenced_task_runner.h"
Chris Davis2e8cc3af2020-02-06 21:40:3217#include "base/threading/thread.h"
18#include "base/threading/thread_task_runner_handle.h"
siggic0d0a0e2014-11-17 23:29:1919
siggic0d0a0e2014-11-17 23:29:1920namespace browser_watcher {
Alex Gough0599f792022-03-14 16:42:1421namespace {
22constexpr char kBrowserExitCodeHistogramName[] = "Stability.BrowserExitCodes";
23bool 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
siggic0d0a0e2014-11-17 23:29:1937
Chris Davis2e8cc3af2020-02-06 21:40:3238ExitCodeWatcher::ExitCodeWatcher()
Mike Rorkef36971b2020-03-06 17:56:2939 : background_thread_("ExitCodeWatcherThread"),
40 exit_code_(STILL_ACTIVE),
41 stop_watching_handle_(CreateEvent(nullptr, TRUE, FALSE, nullptr)) {
42 DCHECK(stop_watching_handle_.IsValid());
siggic0d0a0e2014-11-17 23:29:1943}
44
Mike Rorkef36971b2020-03-06 17:56:2945ExitCodeWatcher::~ExitCodeWatcher() {}
46
erikwright7c4a4262015-01-09 18:32:3347bool ExitCodeWatcher::Initialize(base::Process process) {
rvargas960db882015-01-24 00:27:2548 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();
siggic0d0a0e2014-11-17 23:29:1954 if (process_pid == 0) {
erikwright7c4a4262015-01-09 18:32:3355 LOG(ERROR) << "Invalid parent handle, can't get parent process ID.";
siggic0d0a0e2014-11-17 23:29:1956 return false;
57 }
58
59 FILETIME creation_time = {};
60 FILETIME dummy = {};
erikwright7c4a4262015-01-09 18:32:3361 if (!::GetProcessTimes(process.Handle(), &creation_time, &dummy, &dummy,
62 &dummy)) {
63 PLOG(ERROR) << "Invalid parent handle, can't get parent process times.";
siggic0d0a0e2014-11-17 23:29:1964 return false;
65 }
66
rvargas960db882015-01-24 00:27:2567 // Success, take ownership of the process.
dcheng70c49422016-03-02 23:20:3468 process_ = std::move(process);
siggic0d0a0e2014-11-17 23:29:1969
Chris Davis2e8cc3af2020-02-06 21:40:3270 return true;
71}
72
73bool 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;
siggic0d0a0e2014-11-17 23:29:1987}
88
Mike Rorkef36971b2020-03-06 17:56:2989void ExitCodeWatcher::StopWatching() {
90 if (stop_watching_handle_.IsValid()) {
91 SetEvent(stop_watching_handle_.Get());
siggic0d0a0e2014-11-17 23:29:1992 }
Mike Rorkef36971b2020-03-06 17:56:2993}
siggic0d0a0e2014-11-17 23:29:1994
Mike Rorkef36971b2020-03-06 17:56:2995void 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 }
siggic0d0a0e2014-11-17 23:29:19103}
104
siggic0d0a0e2014-11-17 23:29:19105} // namespace browser_watcher