1 // Copyright 2011 The Chromium Authors
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/process/process.h"
6
7 #include <windows.h>
8
9 #include "base/clang_profiling_buildflags.h"
10 #include "base/logging.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "base/process/kill.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "base/trace_event/base_tracing.h"
15 #include "base/win/windows_version.h"
16
17 #if BUILDFLAG(CLANG_PROFILING)
18 #include "base/test/clang_profiling.h"
19 #endif
20
21 namespace {
22
23 DWORD kBasicProcessAccess =
24 PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE;
25
26 } // namespace
27
28 namespace base {
29
Process(ProcessHandle handle)30 Process::Process(ProcessHandle handle)
31 : process_(handle), is_current_process_(false) {
32 CHECK_NE(handle, ::GetCurrentProcess());
33 }
34
Process(Process && other)35 Process::Process(Process&& other)
36 : process_(other.process_.release()),
37 is_current_process_(other.is_current_process_) {
38 other.Close();
39 }
40
~Process()41 Process::~Process() {
42 }
43
operator =(Process && other)44 Process& Process::operator=(Process&& other) {
45 DCHECK_NE(this, &other);
46 process_.Set(other.process_.release());
47 is_current_process_ = other.is_current_process_;
48 other.Close();
49 return *this;
50 }
51
52 // static
Current()53 Process Process::Current() {
54 Process process;
55 process.is_current_process_ = true;
56 return process;
57 }
58
59 // static
Open(ProcessId pid)60 Process Process::Open(ProcessId pid) {
61 return Process(::OpenProcess(kBasicProcessAccess, FALSE, pid));
62 }
63
64 // static
OpenWithExtraPrivileges(ProcessId pid)65 Process Process::OpenWithExtraPrivileges(ProcessId pid) {
66 DWORD access = kBasicProcessAccess | PROCESS_DUP_HANDLE | PROCESS_VM_READ;
67 return Process(::OpenProcess(access, FALSE, pid));
68 }
69
70 // static
OpenWithAccess(ProcessId pid,DWORD desired_access)71 Process Process::OpenWithAccess(ProcessId pid, DWORD desired_access) {
72 return Process(::OpenProcess(desired_access, FALSE, pid));
73 }
74
75 // static
CanSetPriority()76 bool Process::CanSetPriority() {
77 return true;
78 }
79
80 // static
TerminateCurrentProcessImmediately(int exit_code)81 void Process::TerminateCurrentProcessImmediately(int exit_code) {
82 #if BUILDFLAG(CLANG_PROFILING)
83 WriteClangProfilingProfile();
84 #endif
85 ::TerminateProcess(GetCurrentProcess(), static_cast<UINT>(exit_code));
86 // There is some ambiguity over whether the call above can return. Rather than
87 // hitting confusing crashes later on we should crash right here.
88 ImmediateCrash();
89 }
90
IsValid() const91 bool Process::IsValid() const {
92 return process_.is_valid() || is_current();
93 }
94
Handle() const95 ProcessHandle Process::Handle() const {
96 return is_current_process_ ? GetCurrentProcess() : process_.get();
97 }
98
Duplicate() const99 Process Process::Duplicate() const {
100 if (is_current())
101 return Current();
102
103 ProcessHandle out_handle;
104 if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(),
105 Handle(),
106 GetCurrentProcess(),
107 &out_handle,
108 0,
109 FALSE,
110 DUPLICATE_SAME_ACCESS)) {
111 return Process();
112 }
113 return Process(out_handle);
114 }
115
Release()116 ProcessHandle Process::Release() {
117 if (is_current())
118 return ::GetCurrentProcess();
119 return process_.release();
120 }
121
Pid() const122 ProcessId Process::Pid() const {
123 DCHECK(IsValid());
124 return GetProcId(Handle());
125 }
126
CreationTime() const127 Time Process::CreationTime() const {
128 FILETIME creation_time = {};
129 FILETIME ignore1 = {};
130 FILETIME ignore2 = {};
131 FILETIME ignore3 = {};
132 if (!::GetProcessTimes(Handle(), &creation_time, &ignore1, &ignore2,
133 &ignore3)) {
134 return Time();
135 }
136 return Time::FromFileTime(creation_time);
137 }
138
is_current() const139 bool Process::is_current() const {
140 return is_current_process_;
141 }
142
Close()143 void Process::Close() {
144 is_current_process_ = false;
145 if (!process_.is_valid())
146 return;
147
148 process_.Close();
149 }
150
Terminate(int exit_code,bool wait) const151 bool Process::Terminate(int exit_code, bool wait) const {
152 constexpr DWORD kWaitMs = 60 * 1000;
153
154 DCHECK(IsValid());
155 bool result =
156 ::TerminateProcess(Handle(), static_cast<UINT>(exit_code)) != FALSE;
157 if (result) {
158 // The process may not end immediately due to pending I/O
159 if (wait && ::WaitForSingleObject(Handle(), kWaitMs) != WAIT_OBJECT_0)
160 DPLOG(ERROR) << "Error waiting for process exit";
161 Exited(exit_code);
162 } else {
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.
171 if (::WaitForSingleObject(Handle(), kWaitMs) == WAIT_OBJECT_0) {
172 DWORD actual_exit;
173 Exited(::GetExitCodeProcess(Handle(), &actual_exit)
174 ? static_cast<int>(actual_exit)
175 : exit_code);
176 result = true;
177 }
178 }
179 return result;
180 }
181
WaitForExitOrEvent(const base::win::ScopedHandle & stop_event_handle,int * exit_code) const182 Process::WaitExitStatus Process::WaitForExitOrEvent(
183 const base::win::ScopedHandle& stop_event_handle,
184 int* exit_code) const {
185 HANDLE events[] = {Handle(), stop_event_handle.get()};
186 DWORD wait_result =
187 ::WaitForMultipleObjects(std::size(events), events, FALSE, INFINITE);
188
189 if (wait_result == WAIT_OBJECT_0) {
190 DWORD temp_code; // Don't clobber out-parameters in case of failure.
191 if (!::GetExitCodeProcess(Handle(), &temp_code))
192 return Process::WaitExitStatus::FAILED;
193
194 if (exit_code)
195 *exit_code = static_cast<int>(temp_code);
196
197 Exited(static_cast<int>(temp_code));
198 return Process::WaitExitStatus::PROCESS_EXITED;
199 }
200
201 if (wait_result == WAIT_OBJECT_0 + 1) {
202 return Process::WaitExitStatus::STOP_EVENT_SIGNALED;
203 }
204
205 return Process::WaitExitStatus::FAILED;
206 }
207
WaitForExit(int * exit_code) const208 bool Process::WaitForExit(int* exit_code) const {
209 return WaitForExitWithTimeout(TimeDelta::Max(), exit_code);
210 }
211
WaitForExitWithTimeout(TimeDelta timeout,int * exit_code) const212 bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const {
213 TRACE_EVENT0("base", "Process::WaitForExitWithTimeout");
214
215 if (!timeout.is_zero()) {
216 // Assert that this thread is allowed to wait below. This intentionally
217 // doesn't use ScopedBlockingCallWithBaseSyncPrimitives because the process
218 // being waited upon tends to itself be using the CPU and considering this
219 // thread non-busy causes more issue than it fixes: http://crbug.com/905788
220 internal::AssertBaseSyncPrimitivesAllowed();
221 }
222
223 // Limit timeout to INFINITE.
224 DWORD timeout_ms = saturated_cast<DWORD>(timeout.InMilliseconds());
225 if (::WaitForSingleObject(Handle(), timeout_ms) != WAIT_OBJECT_0)
226 return false;
227
228 DWORD temp_code; // Don't clobber out-parameters in case of failure.
229 if (!::GetExitCodeProcess(Handle(), &temp_code))
230 return false;
231
232 if (exit_code)
233 *exit_code = static_cast<int>(temp_code);
234
235 Exited(static_cast<int>(temp_code));
236 return true;
237 }
238
Exited(int exit_code) const239 void Process::Exited(int exit_code) const {}
240
GetPriority() const241 Process::Priority Process::GetPriority() const {
242 DCHECK(IsValid());
243 int priority = GetOSPriority();
244 if (priority == 0)
245 return Priority::kUserBlocking; // Failure case. Use default value.
246 if ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
247 (priority == IDLE_PRIORITY_CLASS)) {
248 return Priority::kBestEffort;
249 }
250
251 PROCESS_POWER_THROTTLING_STATE power_throttling = {
252 .Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION,
253 .ControlMask = 0ul,
254 .StateMask = 0ul,
255 };
256 const bool ret =
257 ::GetProcessInformation(Handle(), ProcessPowerThrottling,
258 &power_throttling, sizeof(power_throttling));
259
260 // Return Priority::kUserVisible if EcoQoS read & write supported and level
261 // set.
262 if (ret != 0 &&
263 power_throttling.ControlMask ==
264 PROCESS_POWER_THROTTLING_EXECUTION_SPEED &&
265 power_throttling.StateMask == PROCESS_POWER_THROTTLING_EXECUTION_SPEED) {
266 return Priority::kUserVisible;
267 }
268
269 return Priority::kUserBlocking;
270 }
271
SetPriority(Priority priority)272 bool Process::SetPriority(Priority priority) {
273 DCHECK(IsValid());
274 // Having a process remove itself from background mode is a potential
275 // priority inversion, and having a process put itself in background mode is
276 // broken in Windows 11 22H2. So, it is no longer supported. See
277 // https://crbug.com/1396155 for details.
278 DCHECK(!is_current());
279 const DWORD priority_class = priority == Priority::kBestEffort
280 ? IDLE_PRIORITY_CLASS
281 : NORMAL_PRIORITY_CLASS;
282
283 auto* os_info = base::win::OSInfo::GetInstance();
284 if (os_info->version() >= win::Version::WIN11) {
285 PROCESS_POWER_THROTTLING_STATE power_throttling;
286 RtlZeroMemory(&power_throttling, sizeof(power_throttling));
287 power_throttling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
288
289 // EcoQoS is a Windows 11 only feature, but before 22H2, there is no way to
290 // query its current QoS state, GetProcessInformation API to read
291 // PROCESS_POWER_THROTTLING_STATE would fail. For kUserVisible, we
292 // intentionally exclude clients before 22H2 so that GetPriority() is
293 // consistent with SetPriority().
294 if (priority == Priority::kBestEffort ||
295 (priority == Priority::kUserVisible &&
296 os_info->version() >= win::Version::WIN11_22H2)) {
297 // Sets Eco QoS level.
298 power_throttling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
299 power_throttling.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
300 } else {
301 // Uses system default.
302 power_throttling.ControlMask = 0;
303 power_throttling.StateMask = 0;
304 }
305 bool ret =
306 ::SetProcessInformation(Handle(), ProcessPowerThrottling,
307 &power_throttling, sizeof(power_throttling));
308 if (ret == 0) {
309 DPLOG(ERROR) << "Setting process QoS policy fails";
310 }
311 }
312
313 return (::SetPriorityClass(Handle(), priority_class) != 0);
314 }
315
GetOSPriority() const316 int Process::GetOSPriority() const {
317 DCHECK(IsValid());
318 return static_cast<int>(::GetPriorityClass(Handle()));
319 }
320
321 } // namespace base
322