[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 5 | #include "base/process/process.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 6 | |
bcwhite | d970596 | 2016-08-10 03:10:03 | [diff] [blame] | 7 | #include "base/debug/activity_tracker.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include "base/logging.h" |
rvargas | 8572897 | 2015-03-03 20:46:19 | [diff] [blame] | 9 | #include "base/numerics/safe_conversions.h" |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 10 | #include "base/process/kill.h" |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 11 | #include "base/win/windows_version.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 13 | namespace { |
| 14 | |
| 15 | DWORD kBasicProcessAccess = |
| 16 | PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE; |
| 17 | |
| 18 | } // namespace |
| 19 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 20 | namespace base { |
| 21 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 22 | Process::Process(ProcessHandle handle) |
| 23 | : is_current_process_(false), |
| 24 | process_(handle) { |
| 25 | CHECK_NE(handle, ::GetCurrentProcess()); |
| 26 | } |
| 27 | |
dcheng | e1b0277c | 2015-12-01 12:09:52 | [diff] [blame] | 28 | Process::Process(Process&& other) |
| 29 | : is_current_process_(other.is_current_process_), |
| 30 | process_(other.process_.Take()) { |
| 31 | other.Close(); |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 32 | } |
| 33 | |
thakis | 3096dac | 2015-04-20 16:44:48 | [diff] [blame] | 34 | Process::~Process() { |
| 35 | } |
| 36 | |
dcheng | e1b0277c | 2015-12-01 12:09:52 | [diff] [blame] | 37 | Process& Process::operator=(Process&& other) { |
| 38 | DCHECK_NE(this, &other); |
| 39 | process_.Set(other.process_.Take()); |
| 40 | is_current_process_ = other.is_current_process_; |
| 41 | other.Close(); |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 42 | return *this; |
| 43 | } |
| 44 | |
| 45 | // static |
| 46 | Process Process::Current() { |
| 47 | Process process; |
| 48 | process.is_current_process_ = true; |
dcheng | 70c4942 | 2016-03-02 23:20:34 | [diff] [blame] | 49 | return process; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // static |
rvargas | 6b039c37 | 2015-02-04 21:11:29 | [diff] [blame] | 53 | Process Process::Open(ProcessId pid) { |
| 54 | return Process(::OpenProcess(kBasicProcessAccess, FALSE, pid)); |
| 55 | } |
| 56 | |
| 57 | // static |
rvargas | 1c376a8 | 2015-03-16 23:03:52 | [diff] [blame] | 58 | Process Process::OpenWithExtraPrivileges(ProcessId pid) { |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 59 | DWORD access = kBasicProcessAccess | PROCESS_DUP_HANDLE | PROCESS_VM_READ; |
rvargas | 6b039c37 | 2015-02-04 21:11:29 | [diff] [blame] | 60 | return Process(::OpenProcess(access, FALSE, pid)); |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // static |
rvargas | 17a407d | 2015-01-23 20:36:44 | [diff] [blame] | 64 | Process Process::OpenWithAccess(ProcessId pid, DWORD desired_access) { |
| 65 | return Process(::OpenProcess(desired_access, FALSE, pid)); |
| 66 | } |
| 67 | |
| 68 | // static |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 69 | Process Process::DeprecatedGetProcessFromHandle(ProcessHandle handle) { |
| 70 | DCHECK_NE(handle, ::GetCurrentProcess()); |
| 71 | ProcessHandle out_handle; |
| 72 | if (!::DuplicateHandle(GetCurrentProcess(), handle, |
| 73 | GetCurrentProcess(), &out_handle, |
| 74 | 0, FALSE, DUPLICATE_SAME_ACCESS)) { |
| 75 | return Process(); |
| 76 | } |
| 77 | return Process(out_handle); |
| 78 | } |
| 79 | |
| 80 | // static |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 81 | bool Process::CanBackgroundProcesses() { |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool Process::IsValid() const { |
| 86 | return process_.IsValid() || is_current(); |
| 87 | } |
| 88 | |
| 89 | ProcessHandle Process::Handle() const { |
| 90 | return is_current_process_ ? GetCurrentProcess() : process_.Get(); |
| 91 | } |
| 92 | |
| 93 | Process Process::Duplicate() const { |
| 94 | if (is_current()) |
| 95 | return Current(); |
| 96 | |
| 97 | ProcessHandle out_handle; |
| 98 | if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(), |
| 99 | Handle(), |
| 100 | GetCurrentProcess(), |
| 101 | &out_handle, |
| 102 | 0, |
| 103 | FALSE, |
| 104 | DUPLICATE_SAME_ACCESS)) { |
| 105 | return Process(); |
| 106 | } |
| 107 | return Process(out_handle); |
| 108 | } |
| 109 | |
rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame] | 110 | ProcessId Process::Pid() const { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 111 | DCHECK(IsValid()); |
| 112 | return GetProcId(Handle()); |
| 113 | } |
| 114 | |
| 115 | bool Process::is_current() const { |
| 116 | return is_current_process_; |
| 117 | } |
| 118 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 119 | void Process::Close() { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 120 | is_current_process_ = false; |
| 121 | if (!process_.IsValid()) |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 122 | return; |
[email protected] | b987e90e | 2011-08-15 19:22:44 | [diff] [blame] | 123 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 124 | process_.Close(); |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 125 | } |
| 126 | |
rvargas | 02ad783 | 2015-04-02 01:36:06 | [diff] [blame] | 127 | bool Process::Terminate(int exit_code, bool wait) const { |
rvargas | eedb763e | 2015-03-09 23:53:45 | [diff] [blame] | 128 | DCHECK(IsValid()); |
rvargas | 02ad783 | 2015-04-02 01:36:06 | [diff] [blame] | 129 | bool result = (::TerminateProcess(Handle(), exit_code) != FALSE); |
| 130 | if (result && wait) { |
| 131 | // The process may not end immediately due to pending I/O |
| 132 | if (::WaitForSingleObject(Handle(), 60 * 1000) != WAIT_OBJECT_0) |
| 133 | DPLOG(ERROR) << "Error waiting for process exit"; |
| 134 | } else if (!result) { |
| 135 | DPLOG(ERROR) << "Unable to terminate process"; |
| 136 | } |
| 137 | return result; |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 138 | } |
| 139 | |
jcivelli | f4462a35 | 2017-01-10 04:45:59 | [diff] [blame^] | 140 | bool Process::WaitForExit(int* exit_code) const { |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 141 | return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(INFINITE), |
| 142 | exit_code); |
| 143 | } |
| 144 | |
jcivelli | f4462a35 | 2017-01-10 04:45:59 | [diff] [blame^] | 145 | bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const { |
bcwhite | d970596 | 2016-08-10 03:10:03 | [diff] [blame] | 146 | // Record the event that this thread is blocking upon (for hang diagnosis). |
| 147 | base::debug::ScopedProcessWaitActivity process_activity(this); |
| 148 | |
rvargas | 8572897 | 2015-03-03 20:46:19 | [diff] [blame] | 149 | // Limit timeout to INFINITE. |
| 150 | DWORD timeout_ms = saturated_cast<DWORD>(timeout.InMilliseconds()); |
| 151 | if (::WaitForSingleObject(Handle(), timeout_ms) != WAIT_OBJECT_0) |
| 152 | return false; |
| 153 | |
| 154 | DWORD temp_code; // Don't clobber out-parameters in case of failure. |
| 155 | if (!::GetExitCodeProcess(Handle(), &temp_code)) |
| 156 | return false; |
| 157 | |
g.mehndiratt | fd19e23 | 2015-05-29 08:17:14 | [diff] [blame] | 158 | if (exit_code) |
| 159 | *exit_code = temp_code; |
rvargas | 8572897 | 2015-03-03 20:46:19 | [diff] [blame] | 160 | return true; |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 2f15de4 | 2008-11-11 22:35:19 | [diff] [blame] | 163 | bool Process::IsProcessBackgrounded() const { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 164 | DCHECK(IsValid()); |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 165 | DWORD priority = GetPriority(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | if (priority == 0) |
| 167 | return false; // Failure case. |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 168 | return ((priority == BELOW_NORMAL_PRIORITY_CLASS) || |
| 169 | (priority == IDLE_PRIORITY_CLASS)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | bool Process::SetProcessBackgrounded(bool value) { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 173 | DCHECK(IsValid()); |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 174 | // Vista and above introduce a real background mode, which not only |
| 175 | // sets the priority class on the threads but also on the IO generated |
| 176 | // by it. Unfortunately it can only be set for the calling process. |
| 177 | DWORD priority; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 178 | if ((base::win::GetVersion() >= base::win::VERSION_VISTA) && (is_current())) { |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 179 | priority = value ? PROCESS_MODE_BACKGROUND_BEGIN : |
| 180 | PROCESS_MODE_BACKGROUND_END; |
| 181 | } else { |
gab | 3d47343 | 2015-07-21 17:20:20 | [diff] [blame] | 182 | priority = value ? IDLE_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 183 | } |
| 184 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 185 | return (::SetPriorityClass(Handle(), priority) != 0); |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 188 | int Process::GetPriority() const { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 189 | DCHECK(IsValid()); |
| 190 | return ::GetPriorityClass(Handle()); |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 193 | } // namespace base |