| 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 | |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 7 | #include "base/logging.h" |
| 8 | #include "base/process/kill.h" |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 9 | #include "base/strings/stringprintf.h" |
| 10 | #include "base/win/registry.h" |
| 11 | |
| 12 | namespace browser_watcher { |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | base::string16 GetValueName(const base::Time creation_time, |
| 17 | base::ProcessId pid) { |
| 18 | // Convert the PID and creation time to a string value unique to this |
| 19 | // process instance. |
| 20 | return base::StringPrintf(L"%d-%lld", pid, creation_time.ToInternalValue()); |
| 21 | } |
| 22 | |
| 23 | } // namespace |
| 24 | |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 25 | ExitCodeWatcher::ExitCodeWatcher(const base::char16* registry_path) : |
| siggi | d497a1a | 2014-12-19 15:20:51 | [diff] [blame] | 26 | registry_path_(registry_path), exit_code_(STILL_ACTIVE) { |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ExitCodeWatcher::~ExitCodeWatcher() { |
| 30 | } |
| 31 | |
| erikwright | 7c4a426 | 2015-01-09 18:32:33 | [diff] [blame] | 32 | bool ExitCodeWatcher::Initialize(base::Process process) { |
| rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame^] | 33 | if (!process.IsValid()) { |
| 34 | LOG(ERROR) << "Invalid parent handle, can't get parent process ID."; |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | DWORD process_pid = process.Pid(); |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 39 | if (process_pid == 0) { |
| erikwright | 7c4a426 | 2015-01-09 18:32:33 | [diff] [blame] | 40 | LOG(ERROR) << "Invalid parent handle, can't get parent process ID."; |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | |
| 44 | FILETIME creation_time = {}; |
| 45 | FILETIME dummy = {}; |
| erikwright | 7c4a426 | 2015-01-09 18:32:33 | [diff] [blame] | 46 | if (!::GetProcessTimes(process.Handle(), &creation_time, &dummy, &dummy, |
| 47 | &dummy)) { |
| 48 | PLOG(ERROR) << "Invalid parent handle, can't get parent process times."; |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
| rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame^] | 52 | // Success, take ownership of the process. |
| erikwright | 7c4a426 | 2015-01-09 18:32:33 | [diff] [blame] | 53 | process_ = process.Pass(); |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 54 | process_creation_time_ = base::Time::FromFileTime(creation_time); |
| 55 | |
| 56 | // Start by writing the value STILL_ACTIVE to registry, to allow detection |
| 57 | // of the case where the watcher itself is somehow terminated before it can |
| 58 | // write the process' actual exit code. |
| 59 | return WriteProcessExitCode(STILL_ACTIVE); |
| 60 | } |
| 61 | |
| 62 | void ExitCodeWatcher::WaitForExit() { |
| siggi | d497a1a | 2014-12-19 15:20:51 | [diff] [blame] | 63 | if (!process_.WaitForExit(&exit_code_)) { |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 64 | LOG(ERROR) << "Failed to wait for process."; |
| 65 | return; |
| 66 | } |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 67 | |
| siggi | d497a1a | 2014-12-19 15:20:51 | [diff] [blame] | 68 | WriteProcessExitCode(exit_code_); |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) { |
| 72 | base::win::RegKey key(HKEY_CURRENT_USER, |
| 73 | registry_path_.c_str(), |
| 74 | KEY_WRITE); |
| siggi | 581938f | 2014-12-18 21:17:15 | [diff] [blame] | 75 | base::string16 value_name( |
| rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame^] | 76 | GetValueName(process_creation_time_, process_.Pid())); |
| siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 77 | |
| 78 | ULONG result = key.WriteValue(value_name.c_str(), exit_code); |
| 79 | if (result != ERROR_SUCCESS) { |
| 80 | LOG(ERROR) << "Unable to write to registry, error " << result; |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | } // namespace browser_watcher |