blob: 2e2ef77cfb63377bc6f92f3571c0ad18402a9096 [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"
16#include "base/sequenced_task_runner.h"
17#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 {
21
Chris Davis2e8cc3af2020-02-06 21:40:3222const char kBrowserExitCodeHistogramName[] = "Stability.BrowserExitCodes";
siggic0d0a0e2014-11-17 23:29:1923
Chris Davis2e8cc3af2020-02-06 21:40:3224ExitCodeWatcher::ExitCodeWatcher()
Mike Rorkef36971b2020-03-06 17:56:2925 : background_thread_("ExitCodeWatcherThread"),
26 exit_code_(STILL_ACTIVE),
27 stop_watching_handle_(CreateEvent(nullptr, TRUE, FALSE, nullptr)) {
28 DCHECK(stop_watching_handle_.IsValid());
siggic0d0a0e2014-11-17 23:29:1929}
30
Mike Rorkef36971b2020-03-06 17:56:2931ExitCodeWatcher::~ExitCodeWatcher() {}
32
erikwright7c4a4262015-01-09 18:32:3333bool ExitCodeWatcher::Initialize(base::Process process) {
rvargas960db882015-01-24 00:27:2534 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();
siggic0d0a0e2014-11-17 23:29:1940 if (process_pid == 0) {
erikwright7c4a4262015-01-09 18:32:3341 LOG(ERROR) << "Invalid parent handle, can't get parent process ID.";
siggic0d0a0e2014-11-17 23:29:1942 return false;
43 }
44
45 FILETIME creation_time = {};
46 FILETIME dummy = {};
erikwright7c4a4262015-01-09 18:32:3347 if (!::GetProcessTimes(process.Handle(), &creation_time, &dummy, &dummy,
48 &dummy)) {
49 PLOG(ERROR) << "Invalid parent handle, can't get parent process times.";
siggic0d0a0e2014-11-17 23:29:1950 return false;
51 }
52
rvargas960db882015-01-24 00:27:2553 // Success, take ownership of the process.
dcheng70c49422016-03-02 23:20:3454 process_ = std::move(process);
siggic0d0a0e2014-11-17 23:29:1955
Chris Davis2e8cc3af2020-02-06 21:40:3256 return true;
57}
58
59bool 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;
siggic0d0a0e2014-11-17 23:29:1973}
74
Mike Rorkef36971b2020-03-06 17:56:2975void ExitCodeWatcher::StopWatching() {
76 if (stop_watching_handle_.IsValid()) {
77 SetEvent(stop_watching_handle_.Get());
siggic0d0a0e2014-11-17 23:29:1978 }
Mike Rorkef36971b2020-03-06 17:56:2979}
siggic0d0a0e2014-11-17 23:29:1980
Mike Rorkef36971b2020-03-06 17:56:2981void 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 }
siggic0d0a0e2014-11-17 23:29:1989}
90
91bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) {
Chris Davis2e8cc3af2020-02-06 21:40:3292 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;
siggic0d0a0e2014-11-17 23:29:19101 }
Chris Davis2e8cc3af2020-02-06 21:40:32102 return false;
siggic0d0a0e2014-11-17 23:29:19103}
104
105} // namespace browser_watcher