[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 | |
Sebastien Marchand | bd02bc29e | 2020-03-11 15:53:36 | [diff] [blame] | 7 | #include "base/clang_profiling_buildflags.h" |
bcwhite | d970596 | 2016-08-10 03:10:03 | [diff] [blame] | 8 | #include "base/debug/activity_tracker.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include "base/logging.h" |
rvargas | 8572897 | 2015-03-03 20:46:19 | [diff] [blame] | 10 | #include "base/numerics/safe_conversions.h" |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 11 | #include "base/process/kill.h" |
Francois Doray | a678fc1 | 2017-10-30 22:18:06 | [diff] [blame] | 12 | #include "base/threading/thread_restrictions.h" |
Gabriel Charette | 6836c0d5 | 2021-01-11 17:40:26 | [diff] [blame] | 13 | #include "base/trace_event/base_tracing.h" |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 14 | #include "third_party/abseil-cpp/absl/types/optional.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | |
Bruce Dawson | bfdc3fd | 2018-01-03 20:32:36 | [diff] [blame] | 16 | #include <windows.h> |
| 17 | |
Sebastien Marchand | bd02bc29e | 2020-03-11 15:53:36 | [diff] [blame] | 18 | #if BUILDFLAG(CLANG_PROFILING) |
| 19 | #include "base/test/clang_profiling.h" |
Yannic Bonenberger | 6801e6c | 2019-06-07 10:42:53 | [diff] [blame] | 20 | #endif |
| 21 | |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | DWORD kBasicProcessAccess = |
| 25 | PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE; |
| 26 | |
| 27 | } // namespace |
| 28 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 29 | namespace base { |
| 30 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 31 | Process::Process(ProcessHandle handle) |
scottmg | 297cc93 | 2017-05-24 03:45:58 | [diff] [blame] | 32 | : process_(handle), is_current_process_(false) { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 33 | CHECK_NE(handle, ::GetCurrentProcess()); |
| 34 | } |
| 35 | |
dcheng | e1b0277c | 2015-12-01 12:09:52 | [diff] [blame] | 36 | Process::Process(Process&& other) |
scottmg | 297cc93 | 2017-05-24 03:45:58 | [diff] [blame] | 37 | : process_(other.process_.Take()), |
| 38 | is_current_process_(other.is_current_process_) { |
dcheng | e1b0277c | 2015-12-01 12:09:52 | [diff] [blame] | 39 | other.Close(); |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 40 | } |
| 41 | |
thakis | 3096dac | 2015-04-20 16:44:48 | [diff] [blame] | 42 | Process::~Process() { |
| 43 | } |
| 44 | |
dcheng | e1b0277c | 2015-12-01 12:09:52 | [diff] [blame] | 45 | Process& Process::operator=(Process&& other) { |
| 46 | DCHECK_NE(this, &other); |
| 47 | process_.Set(other.process_.Take()); |
| 48 | is_current_process_ = other.is_current_process_; |
| 49 | other.Close(); |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 50 | return *this; |
| 51 | } |
| 52 | |
| 53 | // static |
| 54 | Process Process::Current() { |
| 55 | Process process; |
| 56 | process.is_current_process_ = true; |
dcheng | 70c4942 | 2016-03-02 23:20:34 | [diff] [blame] | 57 | return process; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // static |
rvargas | 6b039c37 | 2015-02-04 21:11:29 | [diff] [blame] | 61 | Process Process::Open(ProcessId pid) { |
| 62 | return Process(::OpenProcess(kBasicProcessAccess, FALSE, pid)); |
| 63 | } |
| 64 | |
| 65 | // static |
rvargas | 1c376a8 | 2015-03-16 23:03:52 | [diff] [blame] | 66 | Process Process::OpenWithExtraPrivileges(ProcessId pid) { |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 67 | DWORD access = kBasicProcessAccess | PROCESS_DUP_HANDLE | PROCESS_VM_READ; |
rvargas | 6b039c37 | 2015-02-04 21:11:29 | [diff] [blame] | 68 | return Process(::OpenProcess(access, FALSE, pid)); |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // static |
rvargas | 17a407d | 2015-01-23 20:36:44 | [diff] [blame] | 72 | Process Process::OpenWithAccess(ProcessId pid, DWORD desired_access) { |
| 73 | return Process(::OpenProcess(desired_access, FALSE, pid)); |
| 74 | } |
| 75 | |
| 76 | // static |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 77 | bool Process::CanBackgroundProcesses() { |
| 78 | return true; |
| 79 | } |
| 80 | |
haraken | 940efb9 | 2017-02-08 05:58:15 | [diff] [blame] | 81 | // static |
| 82 | void Process::TerminateCurrentProcessImmediately(int exit_code) { |
Sebastien Marchand | bd02bc29e | 2020-03-11 15:53:36 | [diff] [blame] | 83 | #if BUILDFLAG(CLANG_PROFILING) |
| 84 | WriteClangProfilingProfile(); |
Wez | d2031d30 | 2018-08-16 23:34:25 | [diff] [blame] | 85 | #endif |
haraken | 940efb9 | 2017-02-08 05:58:15 | [diff] [blame] | 86 | ::TerminateProcess(GetCurrentProcess(), exit_code); |
Bruce Dawson | 33ce2ff | 2017-11-28 20:47:51 | [diff] [blame] | 87 | // There is some ambiguity over whether the call above can return. Rather than |
| 88 | // hitting confusing crashes later on we should crash right here. |
Wez | 5f11741 | 2018-02-07 04:17:47 | [diff] [blame] | 89 | IMMEDIATE_CRASH(); |
haraken | 940efb9 | 2017-02-08 05:58:15 | [diff] [blame] | 90 | } |
| 91 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 92 | bool Process::IsValid() const { |
| 93 | return process_.IsValid() || is_current(); |
| 94 | } |
| 95 | |
| 96 | ProcessHandle Process::Handle() const { |
| 97 | return is_current_process_ ? GetCurrentProcess() : process_.Get(); |
| 98 | } |
| 99 | |
| 100 | Process Process::Duplicate() const { |
| 101 | if (is_current()) |
| 102 | return Current(); |
| 103 | |
| 104 | ProcessHandle out_handle; |
| 105 | if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(), |
| 106 | Handle(), |
| 107 | GetCurrentProcess(), |
| 108 | &out_handle, |
| 109 | 0, |
| 110 | FALSE, |
| 111 | DUPLICATE_SAME_ACCESS)) { |
| 112 | return Process(); |
| 113 | } |
| 114 | return Process(out_handle); |
| 115 | } |
| 116 | |
Robert Sesek | 0e7f165a | 2020-11-20 21:31:45 | [diff] [blame] | 117 | ProcessHandle Process::Release() { |
| 118 | if (is_current()) |
| 119 | return ::GetCurrentProcess(); |
| 120 | return process_.Take(); |
| 121 | } |
| 122 | |
rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame] | 123 | ProcessId Process::Pid() const { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 124 | DCHECK(IsValid()); |
| 125 | return GetProcId(Handle()); |
| 126 | } |
| 127 | |
Francois Doray | 7d131534 | 2018-10-21 03:54:47 | [diff] [blame] | 128 | Time Process::CreationTime() const { |
| 129 | FILETIME creation_time = {}; |
| 130 | FILETIME ignore1 = {}; |
| 131 | FILETIME ignore2 = {}; |
| 132 | FILETIME ignore3 = {}; |
| 133 | if (!::GetProcessTimes(Handle(), &creation_time, &ignore1, &ignore2, |
| 134 | &ignore3)) { |
| 135 | return Time(); |
| 136 | } |
| 137 | return Time::FromFileTime(creation_time); |
| 138 | } |
| 139 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 140 | bool Process::is_current() const { |
| 141 | return is_current_process_; |
| 142 | } |
| 143 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 144 | void Process::Close() { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 145 | is_current_process_ = false; |
| 146 | if (!process_.IsValid()) |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 147 | return; |
[email protected] | b987e90e | 2011-08-15 19:22:44 | [diff] [blame] | 148 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 149 | process_.Close(); |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 150 | } |
| 151 | |
rvargas | 02ad783 | 2015-04-02 01:36:06 | [diff] [blame] | 152 | bool Process::Terminate(int exit_code, bool wait) const { |
Brian White | 6c8f3b9c | 2017-12-01 18:55:31 | [diff] [blame] | 153 | constexpr DWORD kWaitMs = 60 * 1000; |
| 154 | |
rvargas | eedb763e | 2015-03-09 23:53:45 | [diff] [blame] | 155 | DCHECK(IsValid()); |
rvargas | 02ad783 | 2015-04-02 01:36:06 | [diff] [blame] | 156 | bool result = (::TerminateProcess(Handle(), exit_code) != FALSE); |
Brian White | ae2a8b9a | 2017-11-02 19:10:36 | [diff] [blame] | 157 | if (result) { |
rvargas | 02ad783 | 2015-04-02 01:36:06 | [diff] [blame] | 158 | // The process may not end immediately due to pending I/O |
Brian White | 6c8f3b9c | 2017-12-01 18:55:31 | [diff] [blame] | 159 | if (wait && ::WaitForSingleObject(Handle(), kWaitMs) != WAIT_OBJECT_0) |
rvargas | 02ad783 | 2015-04-02 01:36:06 | [diff] [blame] | 160 | DPLOG(ERROR) << "Error waiting for process exit"; |
Brian White | ae2a8b9a | 2017-11-02 19:10:36 | [diff] [blame] | 161 | Exited(exit_code); |
Brian White | ccb2c6e | 2017-11-14 19:09:44 | [diff] [blame] | 162 | } else { |
Bruce Dawson | d9111de | 2019-06-18 23:29:38 | [diff] [blame] | 163 | // The process can't be terminated, perhaps because it has already exited or |
| 164 | // is in the process of exiting. An error code of ERROR_ACCESS_DENIED is the |
| 165 | // undocumented-but-expected result if the process has already exited or |
| 166 | // started exiting when TerminateProcess is called, so don't print an error |
| 167 | // message in that case. |
| 168 | if (GetLastError() != ERROR_ACCESS_DENIED) |
| 169 | DPLOG(ERROR) << "Unable to terminate process"; |
| 170 | // A non-zero timeout is necessary here for the same reasons as above. |
Brian White | 6c8f3b9c | 2017-12-01 18:55:31 | [diff] [blame] | 171 | if (::WaitForSingleObject(Handle(), kWaitMs) == WAIT_OBJECT_0) { |
Brian White | ccb2c6e | 2017-11-14 19:09:44 | [diff] [blame] | 172 | DWORD actual_exit; |
| 173 | Exited(::GetExitCodeProcess(Handle(), &actual_exit) ? actual_exit |
| 174 | : exit_code); |
| 175 | result = true; |
| 176 | } |
rvargas | 02ad783 | 2015-04-02 01:36:06 | [diff] [blame] | 177 | } |
| 178 | return result; |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 179 | } |
| 180 | |
Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 181 | Process::WaitExitStatus Process::WaitForExitOrEvent( |
| 182 | const base::win::ScopedHandle& stop_event_handle, |
| 183 | int* exit_code) const { |
| 184 | // Record the event that this thread is blocking upon (for hang diagnosis). |
| 185 | base::debug::ScopedProcessWaitActivity process_activity(this); |
| 186 | |
| 187 | HANDLE events[] = {Handle(), stop_event_handle.Get()}; |
| 188 | DWORD wait_result = |
| 189 | ::WaitForMultipleObjects(base::size(events), events, FALSE, INFINITE); |
| 190 | |
| 191 | if (wait_result == WAIT_OBJECT_0) { |
| 192 | DWORD temp_code; // Don't clobber out-parameters in case of failure. |
| 193 | if (!::GetExitCodeProcess(Handle(), &temp_code)) |
| 194 | return Process::WaitExitStatus::FAILED; |
| 195 | |
| 196 | if (exit_code) |
| 197 | *exit_code = temp_code; |
| 198 | |
| 199 | Exited(temp_code); |
| 200 | return Process::WaitExitStatus::PROCESS_EXITED; |
| 201 | } |
| 202 | |
| 203 | if (wait_result == WAIT_OBJECT_0 + 1) { |
| 204 | return Process::WaitExitStatus::STOP_EVENT_SIGNALED; |
| 205 | } |
| 206 | |
| 207 | return Process::WaitExitStatus::FAILED; |
| 208 | } |
| 209 | |
jcivelli | f4462a35 | 2017-01-10 04:45:59 | [diff] [blame] | 210 | bool Process::WaitForExit(int* exit_code) const { |
Bruce Dawson | d3e5d6e | 2021-07-16 21:17:26 | [diff] [blame^] | 211 | return WaitForExitWithTimeout(TimeDelta::Max(), exit_code); |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 212 | } |
| 213 | |
jcivelli | f4462a35 | 2017-01-10 04:45:59 | [diff] [blame] | 214 | bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const { |
Gabriel Charette | 6836c0d5 | 2021-01-11 17:40:26 | [diff] [blame] | 215 | TRACE_EVENT0("base", "Process::WaitForExitWithTimeout"); |
Francois Doray | a678fc1 | 2017-10-30 22:18:06 | [diff] [blame] | 216 | |
bcwhite | d970596 | 2016-08-10 03:10:03 | [diff] [blame] | 217 | // Record the event that this thread is blocking upon (for hang diagnosis). |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 218 | absl::optional<debug::ScopedProcessWaitActivity> process_activity; |
Gabriel Charette | 6836c0d5 | 2021-01-11 17:40:26 | [diff] [blame] | 219 | if (!timeout.is_zero()) { |
| 220 | process_activity.emplace(this); |
| 221 | // Assert that this thread is allowed to wait below. This intentionally |
| 222 | // doesn't use ScopedBlockingCallWithBaseSyncPrimitives because the process |
| 223 | // being waited upon tends to itself be using the CPU and considering this |
| 224 | // thread non-busy causes more issue than it fixes: http://crbug.com/905788 |
| 225 | internal::AssertBaseSyncPrimitivesAllowed(); |
| 226 | } |
bcwhite | d970596 | 2016-08-10 03:10:03 | [diff] [blame] | 227 | |
rvargas | 8572897 | 2015-03-03 20:46:19 | [diff] [blame] | 228 | // Limit timeout to INFINITE. |
| 229 | DWORD timeout_ms = saturated_cast<DWORD>(timeout.InMilliseconds()); |
| 230 | if (::WaitForSingleObject(Handle(), timeout_ms) != WAIT_OBJECT_0) |
| 231 | return false; |
| 232 | |
| 233 | DWORD temp_code; // Don't clobber out-parameters in case of failure. |
| 234 | if (!::GetExitCodeProcess(Handle(), &temp_code)) |
| 235 | return false; |
| 236 | |
g.mehndiratt | fd19e23 | 2015-05-29 08:17:14 | [diff] [blame] | 237 | if (exit_code) |
| 238 | *exit_code = temp_code; |
bcwhite | 815054b5 | 2017-03-16 18:41:12 | [diff] [blame] | 239 | |
Brian White | ae2a8b9a | 2017-11-02 19:10:36 | [diff] [blame] | 240 | Exited(temp_code); |
rvargas | 8572897 | 2015-03-03 20:46:19 | [diff] [blame] | 241 | return true; |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 242 | } |
| 243 | |
Brian White | ae2a8b9a | 2017-11-02 19:10:36 | [diff] [blame] | 244 | void Process::Exited(int exit_code) const { |
| 245 | base::debug::GlobalActivityTracker::RecordProcessExitIfEnabled(Pid(), |
| 246 | exit_code); |
| 247 | } |
| 248 | |
[email protected] | 2f15de4 | 2008-11-11 22:35:19 | [diff] [blame] | 249 | bool Process::IsProcessBackgrounded() const { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 250 | DCHECK(IsValid()); |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 251 | DWORD priority = GetPriority(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 252 | if (priority == 0) |
| 253 | return false; // Failure case. |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 254 | return ((priority == BELOW_NORMAL_PRIORITY_CLASS) || |
| 255 | (priority == IDLE_PRIORITY_CLASS)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | bool Process::SetProcessBackgrounded(bool value) { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 259 | DCHECK(IsValid()); |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 260 | // Vista and above introduce a real background mode, which not only |
| 261 | // sets the priority class on the threads but also on the IO generated |
| 262 | // by it. Unfortunately it can only be set for the calling process. |
| 263 | DWORD priority; |
Patrick Monette | ab240ec | 2017-07-13 22:49:14 | [diff] [blame] | 264 | if (is_current()) { |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 265 | priority = value ? PROCESS_MODE_BACKGROUND_BEGIN : |
| 266 | PROCESS_MODE_BACKGROUND_END; |
| 267 | } else { |
gab | 3d47343 | 2015-07-21 17:20:20 | [diff] [blame] | 268 | priority = value ? IDLE_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 269 | } |
| 270 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 271 | return (::SetPriorityClass(Handle(), priority) != 0); |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 272 | } |
| 273 | |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 274 | int Process::GetPriority() const { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 275 | DCHECK(IsValid()); |
| 276 | return ::GetPriorityClass(Handle()); |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 277 | } |
| 278 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 279 | } // namespace base |