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 
5 #include "components/browser_watcher/exit_code_watcher_win.h"
6 
7 #include <windows.h>
8 
9 #include <utility>
10 
11 #include "base/logging.h"
12 #include "base/metrics/sparse_histogram.h"
13 #include "base/process/kill.h"
14 #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"
19 
20 namespace browser_watcher {
21 
22 const char kBrowserExitCodeHistogramName[] = "Stability.BrowserExitCodes";
23 
ExitCodeWatcher()24 ExitCodeWatcher::ExitCodeWatcher()
25     : background_thread_("ExitCodeWatcherThread"),
26       exit_code_(STILL_ACTIVE),
27       stop_watching_handle_(CreateEvent(nullptr, TRUE, FALSE, nullptr)) {
28   DCHECK(stop_watching_handle_.IsValid());
29 }
30 
~ExitCodeWatcher()31 ExitCodeWatcher::~ExitCodeWatcher() {}
32 
Initialize(base::Process process)33 bool ExitCodeWatcher::Initialize(base::Process process) {
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();
40   if (process_pid == 0) {
41     LOG(ERROR) << "Invalid parent handle, can't get parent process ID.";
42     return false;
43   }
44 
45   FILETIME creation_time = {};
46   FILETIME dummy = {};
47   if (!::GetProcessTimes(process.Handle(), &creation_time, &dummy, &dummy,
48                          &dummy)) {
49     PLOG(ERROR) << "Invalid parent handle, can't get parent process times.";
50     return false;
51   }
52 
53   // Success, take ownership of the process.
54   process_ = std::move(process);
55 
56   return true;
57 }
58 
StartWatching()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;
73 }
74 
StopWatching()75 void ExitCodeWatcher::StopWatching() {
76   if (stop_watching_handle_.IsValid()) {
77     SetEvent(stop_watching_handle_.Get());
78   }
79 }
80 
WaitForExit()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   }
89 }
90 
WriteProcessExitCode(int exit_code)91 bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) {
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;
101   }
102   return false;
103 }
104 
105 }  // namespace browser_watcher
106