OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/browser_watcher/exit_code_watcher_win.h" | 5 #include "components/browser_watcher/exit_code_watcher_win.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/process/kill.h" | 9 #include "base/process/kill.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/win/registry.h" | 12 #include "base/win/registry.h" |
13 | 13 |
14 namespace browser_watcher { | 14 namespace browser_watcher { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 base::string16 GetValueName(const base::Time creation_time, | 18 base::string16 GetValueName(const base::Time creation_time, |
19 base::ProcessId pid) { | 19 base::ProcessId pid) { |
20 // Convert the PID and creation time to a string value unique to this | 20 // Convert the PID and creation time to a string value unique to this |
21 // process instance. | 21 // process instance. |
22 return base::StringPrintf(L"%d-%lld", pid, creation_time.ToInternalValue()); | 22 return base::StringPrintf(L"%d-%lld", pid, creation_time.ToInternalValue()); |
23 } | 23 } |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 const char ExitCodeWatcher::kParenthHandleSwitch[] = "parent-handle"; | 27 const char ExitCodeWatcher::kParenthHandleSwitch[] = "parent-handle"; |
28 | 28 |
29 ExitCodeWatcher::ExitCodeWatcher(const base::char16* registry_path) : | 29 ExitCodeWatcher::ExitCodeWatcher(const base::char16* registry_path) : |
30 registry_path_(registry_path), | 30 registry_path_(registry_path) { |
31 process_pid_(0) { | |
32 } | 31 } |
33 | 32 |
34 ExitCodeWatcher::~ExitCodeWatcher() { | 33 ExitCodeWatcher::~ExitCodeWatcher() { |
35 } | 34 } |
36 | 35 |
37 bool ExitCodeWatcher::ParseArguments(const base::CommandLine& cmd_line) { | 36 bool ExitCodeWatcher::ParseArguments(const base::CommandLine& cmd_line) { |
38 std::string process_handle_str = | 37 std::string process_handle_str = |
39 cmd_line.GetSwitchValueASCII(kParenthHandleSwitch); | 38 cmd_line.GetSwitchValueASCII(kParenthHandleSwitch); |
40 unsigned process_handle_uint = 0; | 39 unsigned process_handle_uint = 0; |
41 if (process_handle_str.empty() || | 40 if (process_handle_str.empty() || |
(...skipping 14 matching lines...) Expand all Loading... |
56 | 55 |
57 FILETIME creation_time = {}; | 56 FILETIME creation_time = {}; |
58 FILETIME dummy = {}; | 57 FILETIME dummy = {}; |
59 if (!::GetProcessTimes(process_handle, &creation_time, | 58 if (!::GetProcessTimes(process_handle, &creation_time, |
60 &dummy, &dummy, &dummy)) { | 59 &dummy, &dummy, &dummy)) { |
61 LOG(ERROR) << "Invalid parent handle, can't get parent process times."; | 60 LOG(ERROR) << "Invalid parent handle, can't get parent process times."; |
62 return false; | 61 return false; |
63 } | 62 } |
64 | 63 |
65 // Success, take ownership of the process handle. | 64 // Success, take ownership of the process handle. |
66 process_.Set(process_handle); | 65 process_ = base::Process(process_handle); |
67 process_pid_ = process_pid; | |
68 process_creation_time_ = base::Time::FromFileTime(creation_time); | 66 process_creation_time_ = base::Time::FromFileTime(creation_time); |
69 | 67 |
70 // Start by writing the value STILL_ACTIVE to registry, to allow detection | 68 // Start by writing the value STILL_ACTIVE to registry, to allow detection |
71 // of the case where the watcher itself is somehow terminated before it can | 69 // of the case where the watcher itself is somehow terminated before it can |
72 // write the process' actual exit code. | 70 // write the process' actual exit code. |
73 return WriteProcessExitCode(STILL_ACTIVE); | 71 return WriteProcessExitCode(STILL_ACTIVE); |
74 } | 72 } |
75 | 73 |
76 void ExitCodeWatcher::WaitForExit() { | 74 void ExitCodeWatcher::WaitForExit() { |
77 int exit_code = 0; | 75 int exit_code = 0; |
78 if (!base::WaitForExitCode(process_.Get(), &exit_code)) { | 76 if (!process_.WaitForExit(&exit_code)) { |
79 LOG(ERROR) << "Failed to wait for process."; | 77 LOG(ERROR) << "Failed to wait for process."; |
80 return; | 78 return; |
81 } | 79 } |
82 // WaitForExitCode closes the handle on success. | |
83 process_.Take(); | |
84 | 80 |
85 WriteProcessExitCode(exit_code); | 81 WriteProcessExitCode(exit_code); |
86 } | 82 } |
87 | 83 |
88 bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) { | 84 bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) { |
89 base::win::RegKey key(HKEY_CURRENT_USER, | 85 base::win::RegKey key(HKEY_CURRENT_USER, |
90 registry_path_.c_str(), | 86 registry_path_.c_str(), |
91 KEY_WRITE); | 87 KEY_WRITE); |
92 base::string16 value_name(GetValueName(process_creation_time_, process_pid_)); | 88 base::string16 value_name( |
| 89 GetValueName(process_creation_time_, process_.pid())); |
93 | 90 |
94 ULONG result = key.WriteValue(value_name.c_str(), exit_code); | 91 ULONG result = key.WriteValue(value_name.c_str(), exit_code); |
95 if (result != ERROR_SUCCESS) { | 92 if (result != ERROR_SUCCESS) { |
96 LOG(ERROR) << "Unable to write to registry, error " << result; | 93 LOG(ERROR) << "Unable to write to registry, error " << result; |
97 return false; | 94 return false; |
98 } | 95 } |
99 | 96 |
100 return true; | 97 return true; |
101 } | 98 } |
102 | 99 |
103 } // namespace browser_watcher | 100 } // namespace browser_watcher |
OLD | NEW |