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 "base/clang_profiling_buildflags.h"
8 #include "base/logging.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "base/process/kill.h"
11 #include "base/threading/thread_restrictions.h"
12 #include "base/trace_event/base_tracing.h"
13 #include "base/win/windows_version.h"
14
15 #include <windows.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
30 // Sets Eco QoS (Quality of Service) level for background process which would
31 // select efficient CPU frequency and schedule the process to efficient cores
32 // (available on hybrid CPUs).
33 // QoS is a scheduling Win API which indicates the desired performance and power
34 // efficiency of a process/thread. EcoQoS is introduced since Windows 11.
35 BASE_FEATURE(kUseEcoQoSForBackgroundProcess,
36 "UseEcoQoSForBackgroundProcess",
37 FEATURE_DISABLED_BY_DEFAULT);
38
Process(ProcessHandle handle)39 Process::Process(ProcessHandle handle)
40 : process_(handle), is_current_process_(false) {
41 CHECK_NE(handle, ::GetCurrentProcess());
42 }
43
Process(Process && other)44 Process::Process(Process&& other)
45 : process_(other.process_.release()),
46 is_current_process_(other.is_current_process_) {
47 other.Close();
48 }
49
~Process()50 Process::~Process() {
51 }
52
operator =(Process && other)53 Process& Process::operator=(Process&& other) {
54 DCHECK_NE(this, &other);
55 process_.Set(other.process_.release());
56 is_current_process_ = other.is_current_process_;
57 other.Close();
58 return *this;
59 }
60
61 // static
Current()62 Process Process::Current() {
63 Process process;
64 process.is_current_process_ = true;
65 return process;
66 }
67
68 // static
Open(ProcessId pid)69 Process Process::Open(ProcessId pid) {
70 return Process(::OpenProcess(kBasicProcessAccess, FALSE, pid));
71 }
72
73 // static
OpenWithExtraPrivileges(ProcessId pid)74 Process Process::OpenWithExtraPrivileges(ProcessId pid) {
75 DWORD access = kBasicProcessAccess | PROCESS_DUP_HANDLE | PROCESS_VM_READ;
76 return Process(::OpenProcess(access, FALSE, pid));
77 }
78
79 // static
OpenWithAccess(ProcessId pid,DWORD desired_access)80 Process Process::OpenWithAccess(ProcessId pid, DWORD desired_access) {
81 return Process(::OpenProcess(desired_access, FALSE, pid));
82 }
83
84 // static
CanBackgroundProcesses()85 bool Process::CanBackgroundProcesses() {
86 return true;
87 }
88
89 // static
TerminateCurrentProcessImmediately(int exit_code)90 void Process::TerminateCurrentProcessImmediately(int exit_code) {
91 #if BUILDFLAG(CLANG_PROFILING)
92 WriteClangProfilingProfile();
93 #endif
94 ::TerminateProcess(GetCurrentProcess(), static_cast<UINT>(exit_code));
95 // There is some ambiguity over whether the call above can return. Rather than
96 // hitting confusing crashes later on we should crash right here.
97 ImmediateCrash();
98 }
99
IsValid() const100 bool Process::IsValid() const {
101 return process_.is_valid() || is_current();
102 }
103
Handle() const104 ProcessHandle Process::Handle() const {
105 return is_current_process_ ? GetCurrentProcess() : process_.get();
106 }
107
Duplicate() const108 Process Process::Duplicate() const {
109 if (is_current())
110 return Current();
111
112 ProcessHandle out_handle;
113 if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(),
114 Handle(),
115 GetCurrentProcess(),
116 &out_handle,
117 0,
118 FALSE,
119 DUPLICATE_SAME_ACCESS)) {
120 return Process();
121 }
122 return Process(out_handle);
123 }
124
Release()125 ProcessHandle Process::Release() {
126 if (is_current())
127 return ::GetCurrentProcess();
128 return process_.release();
129 }
130
Pid() const131 ProcessId Process::Pid() const {
132 DCHECK(IsValid());
133 return GetProcId(Handle());
134 }
135
CreationTime() const136 Time Process::CreationTime() const {
137 FILETIME creation_time = {};
138 FILETIME ignore1 = {};
139 FILETIME ignore2 = {};
140 FILETIME ignore3 = {};
141 if (!::GetProcessTimes(Handle(), &creation_time, &ignore1, &ignore2,
142 &ignore3)) {
143 return Time();
144 }
145 return Time::FromFileTime(creation_time);
146 }
147
is_current() const148 bool Process::is_current() const {
149 return is_current_process_;
150 }
151
Close()152 void Process::Close() {
153 is_current_process_ = false;
154 if (!process_.is_valid())
155 return;
156
157 process_.Close();
158 }
159
Terminate(int exit_code,bool wait) const160 bool Process::Terminate(int exit_code, bool wait) const {
161 constexpr DWORD kWaitMs = 60 * 1000;
162
163 DCHECK(IsValid());
164 bool result =
165 ::TerminateProcess(Handle(), static_cast<UINT>(exit_code)) != FALSE;
166 if (result) {
167 // The process may not end immediately due to pending I/O
168 if (wait && ::WaitForSingleObject(Handle(), kWaitMs) != WAIT_OBJECT_0)
169 DPLOG(ERROR) << "Error waiting for process exit";
170 Exited(exit_code);
171 } else {
172 // The process can't be terminated, perhaps because it has already exited or
173 // is in the process of exiting. An error code of ERROR_ACCESS_DENIED is the
174 // undocumented-but-expected result if the process has already exited or
175 // started exiting when TerminateProcess is called, so don't print an error
176 // message in that case.
177 if (GetLastError() != ERROR_ACCESS_DENIED)
178 DPLOG(ERROR) << "Unable to terminate process";
179 // A non-zero timeout is necessary here for the same reasons as above.
180 if (::WaitForSingleObject(Handle(), kWaitMs) == WAIT_OBJECT_0) {
181 DWORD actual_exit;
182 Exited(::GetExitCodeProcess(Handle(), &actual_exit)
183 ? static_cast<int>(actual_exit)
184 : exit_code);
185 result = true;
186 }
187 }
188 return result;
189 }
190
WaitForExitOrEvent(const base::win::ScopedHandle & stop_event_handle,int * exit_code) const191 Process::WaitExitStatus Process::WaitForExitOrEvent(
192 const base::win::ScopedHandle& stop_event_handle,
193 int* exit_code) const {
194 HANDLE events[] = {Handle(), stop_event_handle.get()};
195 DWORD wait_result =
196 ::WaitForMultipleObjects(std::size(events), events, FALSE, INFINITE);
197
198 if (wait_result == WAIT_OBJECT_0) {
199 DWORD temp_code; // Don't clobber out-parameters in case of failure.
200 if (!::GetExitCodeProcess(Handle(), &temp_code))
201 return Process::WaitExitStatus::FAILED;
202
203 if (exit_code)
204 *exit_code = static_cast<int>(temp_code);
205
206 Exited(static_cast<int>(temp_code));
207 return Process::WaitExitStatus::PROCESS_EXITED;
208 }
209
210 if (wait_result == WAIT_OBJECT_0 + 1) {
211 return Process::WaitExitStatus::STOP_EVENT_SIGNALED;
212 }
213
214 return Process::WaitExitStatus::FAILED;
215 }
216
WaitForExit(int * exit_code) const217 bool Process::WaitForExit(int* exit_code) const {
218 return WaitForExitWithTimeout(TimeDelta::Max(), exit_code);
219 }
220
WaitForExitWithTimeout(TimeDelta timeout,int * exit_code) const221 bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const {
222 TRACE_EVENT0("base", "Process::WaitForExitWithTimeout");
223
224 if (!timeout.is_zero()) {
225 // Assert that this thread is allowed to wait below. This intentionally
226 // doesn't use ScopedBlockingCallWithBaseSyncPrimitives because the process
227 // being waited upon tends to itself be using the CPU and considering this
228 // thread non-busy causes more issue than it fixes: http://crbug.com/905788
229 internal::AssertBaseSyncPrimitivesAllowed();
230 }
231
232 // Limit timeout to INFINITE.
233 DWORD timeout_ms = saturated_cast<DWORD>(timeout.InMilliseconds());
234 if (::WaitForSingleObject(Handle(), timeout_ms) != WAIT_OBJECT_0)
235 return false;
236
237 DWORD temp_code; // Don't clobber out-parameters in case of failure.
238 if (!::GetExitCodeProcess(Handle(), &temp_code))
239 return false;
240
241 if (exit_code)
242 *exit_code = static_cast<int>(temp_code);
243
244 Exited(static_cast<int>(temp_code));
245 return true;
246 }
247
Exited(int exit_code) const248 void Process::Exited(int exit_code) const {}
249
IsProcessBackgrounded() const250 bool Process::IsProcessBackgrounded() const {
251 DCHECK(IsValid());
252 int priority = GetPriority();
253 if (priority == 0)
254 return false; // Failure case.
255 return ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
256 (priority == IDLE_PRIORITY_CLASS));
257 }
258
SetProcessBackgrounded(bool value)259 bool Process::SetProcessBackgrounded(bool value) {
260 DCHECK(IsValid());
261 // Having a process remove itself from background mode is a potential
262 // priority inversion, and having a process put itself in background mode is
263 // broken in Windows 11 22H2. So, it is no longer supported. See
264 // https://crbug.com/1396155 for details.
265 DCHECK(!is_current());
266 const DWORD priority = value ? IDLE_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
267
268 if (base::win::OSInfo::GetInstance()->version() >=
269 base::win::Version::WIN11 &&
270 FeatureList::IsEnabled(kUseEcoQoSForBackgroundProcess)) {
271 PROCESS_POWER_THROTTLING_STATE power_throttling;
272 RtlZeroMemory(&power_throttling, sizeof(power_throttling));
273 power_throttling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
274
275 if (value) {
276 // Sets Eco QoS level.
277 power_throttling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
278 power_throttling.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
279 } else {
280 // Uses system default.
281 power_throttling.ControlMask = 0;
282 power_throttling.StateMask = 0;
283 }
284 bool ret =
285 ::SetProcessInformation(Handle(), ProcessPowerThrottling,
286 &power_throttling, sizeof(power_throttling));
287 if (ret == 0) {
288 DPLOG(ERROR) << "Setting process QoS policy fails";
289 }
290 }
291
292 return (::SetPriorityClass(Handle(), priority) != 0);
293 }
294
GetPriority() const295 int Process::GetPriority() const {
296 DCHECK(IsValid());
297 return static_cast<int>(::GetPriorityClass(Handle()));
298 }
299
300 } // namespace base
301