OLD | NEW |
(Empty) | |
| 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 "base/command_line.h" |
| 6 #include "base/process/kill.h" |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/win/registry.h" |
| 10 #include "components/browser_watcher/watcher_win.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 |
| 25 ExitCodeWatcher::ExitCodeWatcher(const wchar_t* registry_path) : |
| 26 registry_path_(registry_path), |
| 27 process_(base::kNullProcessHandle), |
| 28 process_pid_(0) { |
| 29 } |
| 30 |
| 31 bool ExitCodeWatcher::ParseArguments(const base::CommandLine& cmd_line) { |
| 32 std::string process_handle_str = |
| 33 cmd_line.GetSwitchValueASCII("parent-handle"); |
| 34 unsigned process_handle_uint = 0; |
| 35 if (process_handle_str.empty() || |
| 36 !base::StringToUint(process_handle_str, &process_handle_uint)) { |
| 37 LOG(ERROR) << "Missing or invalid parent-handle argument."; |
| 38 return false; |
| 39 } |
| 40 |
| 41 HANDLE process_handle = reinterpret_cast<HANDLE>(process_handle_uint); |
| 42 // Initial test of the handle, a zero PID indicates invalid handle, or not |
| 43 // a process handle. In this case, bail immediately and avoid closing the |
| 44 // handle. |
| 45 DWORD process_pid = ::GetProcessId(process_handle); |
| 46 if (process_pid == 0) { |
| 47 LOG(ERROR) << "Invalid parent-handle, can't get parent PID."; |
| 48 return false; |
| 49 } |
| 50 |
| 51 FILETIME creation_time = {}; |
| 52 FILETIME dummy = {}; |
| 53 if (!::GetProcessTimes(process_handle, &creation_time, |
| 54 &dummy, &dummy, &dummy)) { |
| 55 LOG(ERROR) << "Invalid parent handle, can't get parent process times."; |
| 56 return false; |
| 57 } |
| 58 |
| 59 // Success, take ownership of the process handle. |
| 60 process_.Set(process_handle); |
| 61 process_pid_ = process_pid; |
| 62 process_creation_time_ = base::Time::FromFileTime(creation_time); |
| 63 |
| 64 // Start by writing the value STILL_ACTIVE to registry, to allow detection |
| 65 // of the case where the watcher itself is somehow terminated before it can |
| 66 // write the process' actual exit code. |
| 67 base::win::RegKey key(HKEY_CURRENT_USER, |
| 68 registry_path_.c_str(), |
| 69 KEY_WRITE); |
| 70 base::string16 value_name(GetValueName(process_creation_time_, process_pid_)); |
| 71 |
| 72 ULONG result = key.WriteValue(value_name.c_str(), STILL_ACTIVE); |
| 73 if (result != ERROR_SUCCESS) { |
| 74 LOG(ERROR) << "Unable to write to registry, error " << result; |
| 75 return false; |
| 76 } |
| 77 |
| 78 return true; |
| 79 } |
| 80 |
| 81 void ExitCodeWatcher::WaitForExit() { |
| 82 int exit_code = 0; |
| 83 if (!base::WaitForExitCode(process_.Get(), &exit_code)) { |
| 84 LOG(ERROR) << "Failed to wait for process."; |
| 85 return; |
| 86 } |
| 87 // WaitForExitCode closes the handle on success. |
| 88 process_.Take(); |
| 89 |
| 90 base::win::RegKey key(HKEY_CURRENT_USER, |
| 91 registry_path_.c_str(), |
| 92 KEY_WRITE); |
| 93 base::string16 value_name(GetValueName(process_creation_time_, process_pid_)); |
| 94 |
| 95 ULONG result = key.WriteValue(value_name.c_str(), exit_code); |
| 96 if (result != ERROR_SUCCESS) |
| 97 LOG(ERROR) << "Unable to write exit code, error: " << result; |
| 98 } |
| 99 |
| 100 } // namespace browser_watcher |
OLD | NEW |