1 // Copyright (c) 2011 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/process/process.h"
6
7 #include "base/clang_profiling_buildflags.h"
8 #include "base/debug/activity_tracker.h"
9 #include "base/logging.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/process/kill.h"
12 #include "base/threading/thread_restrictions.h"
13
14 #include <windows.h>
15
16 #if BUILDFLAG(CLANG_PROFILING)
17 #include "base/test/clang_profiling.h"
18 #endif
19
20 namespace {
21
22 DWORD kBasicProcessAccess =
23 PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE;
24
25 } // namespace
26
27 namespace base {
28
Process(ProcessHandle handle)29 Process::Process(ProcessHandle handle)
30 : process_(handle), is_current_process_(false) {
31 CHECK_NE(handle, ::GetCurrentProcess());
32 }
33
Process(Process && other)34 Process::Process(Process&& other)
35 : process_(other.process_.Take()),
36 is_current_process_(other.is_current_process_) {
37 other.Close();
38 }
39
~Process()40 Process::~Process() {
41 }
42
operator =(Process && other)43 Process& Process::operator=(Process&& other) {
44 DCHECK_NE(this, &other);
45 process_.Set(other.process_.Take());
46 is_current_process_ = other.is_current_process_;
47 other.Close();
48 return *this;
49 }
50
51 // static
Current()52 Process Process::Current() {
53 Process process;
54 process.is_current_process_ = true;
55 return process;
56 }
57
58 // static
Open(ProcessId pid)59 Process Process::Open(ProcessId pid) {
60 return Process(::OpenProcess(kBasicProcessAccess, FALSE, pid));
61 }
62
63 // static
OpenWithExtraPrivileges(ProcessId pid)64 Process Process::OpenWithExtraPrivileges(ProcessId pid) {
65 DWORD access = kBasicProcessAccess | PROCESS_DUP_HANDLE | PROCESS_VM_READ;
66 return Process(::OpenProcess(access, FALSE, pid));
67 }
68
69 // static
OpenWithAccess(ProcessId pid,DWORD desired_access)70 Process Process::OpenWithAccess(ProcessId pid, DWORD desired_access) {
71 return Process(::OpenProcess(desired_access, FALSE, pid));
72 }
73
74 // static
DeprecatedGetProcessFromHandle(ProcessHandle handle)75 Process Process::DeprecatedGetProcessFromHandle(ProcessHandle handle) {
76 DCHECK_NE(handle, ::GetCurrentProcess());
77 ProcessHandle out_handle;
78 if (!::DuplicateHandle(GetCurrentProcess(), handle,
79 GetCurrentProcess(), &out_handle,
80 0, FALSE, DUPLICATE_SAME_ACCESS)) {
81 return Process();
82 }
83 return Process(out_handle);
84 }
85
86 // static
CanBackgroundProcesses()87 bool Process::CanBackgroundProcesses() {
88 return true;
89 }
90
91 // static
TerminateCurrentProcessImmediately(int exit_code)92 void Process::TerminateCurrentProcessImmediately(int exit_code) {
93 #if BUILDFLAG(CLANG_PROFILING)
94 WriteClangProfilingProfile();
95 #endif
96 ::TerminateProcess(GetCurrentProcess(), exit_code);
97 // There is some ambiguity over whether the call above can return. Rather than
98 // hitting confusing crashes later on we should crash right here.
99 IMMEDIATE_CRASH();
100 }
101
IsValid() const102 bool Process::IsValid() const {
103 return process_.IsValid() || is_current();
104 }
105
Handle() const106 ProcessHandle Process::Handle() const {
107 return is_current_process_ ? GetCurrentProcess() : process_.Get();
108 }
109
Duplicate() const110 Process Process::Duplicate() const {
111 if (is_current())
112 return Current();
113
114 ProcessHandle out_handle;
115 if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(),
116 Handle(),
117 GetCurrentProcess(),
118 &out_handle,
119 0,
120 FALSE,
121 DUPLICATE_SAME_ACCESS)) {
122 return Process();
123 }
124 return Process(out_handle);
125 }
126
Pid() const127 ProcessId Process::Pid() const {
128 DCHECK(IsValid());
129 return GetProcId(Handle());
130 }
131
CreationTime() const132 Time Process::CreationTime() const {
133 FILETIME creation_time = {};
134 FILETIME ignore1 = {};
135 FILETIME ignore2 = {};
136 FILETIME ignore3 = {};
137 if (!::GetProcessTimes(Handle(), &creation_time, &ignore1, &ignore2,
138 &ignore3)) {
139 return Time();
140 }
141 return Time::FromFileTime(creation_time);
142 }
143
is_current() const144 bool Process::is_current() const {
145 return is_current_process_;
146 }
147
Close()148 void Process::Close() {
149 is_current_process_ = false;
150 if (!process_.IsValid())
151 return;
152
153 process_.Close();
154 }
155
Terminate(int exit_code,bool wait) const156 bool Process::Terminate(int exit_code, bool wait) const {
157 constexpr DWORD kWaitMs = 60 * 1000;
158
159 // exit_code cannot be implemented.
160 DCHECK(IsValid());
161 bool result = (::TerminateProcess(Handle(), exit_code) != FALSE);
162 if (result) {
163 // The process may not end immediately due to pending I/O
164 if (wait && ::WaitForSingleObject(Handle(), kWaitMs) != WAIT_OBJECT_0)
165 DPLOG(ERROR) << "Error waiting for process exit";
166 Exited(exit_code);
167 } else {
168 // The process can't be terminated, perhaps because it has already exited or
169 // is in the process of exiting. An error code of ERROR_ACCESS_DENIED is the
170 // undocumented-but-expected result if the process has already exited or
171 // started exiting when TerminateProcess is called, so don't print an error
172 // message in that case.
173 if (GetLastError() != ERROR_ACCESS_DENIED)
174 DPLOG(ERROR) << "Unable to terminate process";
175 // A non-zero timeout is necessary here for the same reasons as above.
176 if (::WaitForSingleObject(Handle(), kWaitMs) == WAIT_OBJECT_0) {
177 DWORD actual_exit;
178 Exited(::GetExitCodeProcess(Handle(), &actual_exit) ? actual_exit
179 : exit_code);
180 result = true;
181 }
182 }
183 return result;
184 }
185
WaitForExitOrEvent(const base::win::ScopedHandle & stop_event_handle,int * exit_code) const186 Process::WaitExitStatus Process::WaitForExitOrEvent(
187 const base::win::ScopedHandle& stop_event_handle,
188 int* exit_code) const {
189 // Record the event that this thread is blocking upon (for hang diagnosis).
190 base::debug::ScopedProcessWaitActivity process_activity(this);
191
192 HANDLE events[] = {Handle(), stop_event_handle.Get()};
193 DWORD wait_result =
194 ::WaitForMultipleObjects(base::size(events), events, FALSE, INFINITE);
195
196 if (wait_result == WAIT_OBJECT_0) {
197 DWORD temp_code; // Don't clobber out-parameters in case of failure.
198 if (!::GetExitCodeProcess(Handle(), &temp_code))
199 return Process::WaitExitStatus::FAILED;
200
201 if (exit_code)
202 *exit_code = temp_code;
203
204 Exited(temp_code);
205 return Process::WaitExitStatus::PROCESS_EXITED;
206 }
207
208 if (wait_result == WAIT_OBJECT_0 + 1) {
209 return Process::WaitExitStatus::STOP_EVENT_SIGNALED;
210 }
211
212 return Process::WaitExitStatus::FAILED;
213 }
214
WaitForExit(int * exit_code) const215 bool Process::WaitForExit(int* exit_code) const {
216 return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(INFINITE),
217 exit_code);
218 }
219
WaitForExitWithTimeout(TimeDelta timeout,int * exit_code) const220 bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const {
221 // Intentionally avoid instantiating ScopedBlockingCallWithBaseSyncPrimitives.
222 // In some cases, this function waits on a child Process doing CPU work.
223 // http://crbug.com/905788
224 if (!timeout.is_zero())
225 internal::AssertBaseSyncPrimitivesAllowed();
226
227 // Record the event that this thread is blocking upon (for hang diagnosis).
228 base::debug::ScopedProcessWaitActivity process_activity(this);
229
230 // Limit timeout to INFINITE.
231 DWORD timeout_ms = saturated_cast<DWORD>(timeout.InMilliseconds());
232 if (::WaitForSingleObject(Handle(), timeout_ms) != WAIT_OBJECT_0)
233 return false;
234
235 DWORD temp_code; // Don't clobber out-parameters in case of failure.
236 if (!::GetExitCodeProcess(Handle(), &temp_code))
237 return false;
238
239 if (exit_code)
240 *exit_code = temp_code;
241
242 Exited(temp_code);
243 return true;
244 }
245
Exited(int exit_code) const246 void Process::Exited(int exit_code) const {
247 base::debug::GlobalActivityTracker::RecordProcessExitIfEnabled(Pid(),
248 exit_code);
249 }
250
IsProcessBackgrounded() const251 bool Process::IsProcessBackgrounded() const {
252 DCHECK(IsValid());
253 DWORD priority = GetPriority();
254 if (priority == 0)
255 return false; // Failure case.
256 return ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
257 (priority == IDLE_PRIORITY_CLASS));
258 }
259
SetProcessBackgrounded(bool value)260 bool Process::SetProcessBackgrounded(bool value) {
261 DCHECK(IsValid());
262 // Vista and above introduce a real background mode, which not only
263 // sets the priority class on the threads but also on the IO generated
264 // by it. Unfortunately it can only be set for the calling process.
265 DWORD priority;
266 if (is_current()) {
267 priority = value ? PROCESS_MODE_BACKGROUND_BEGIN :
268 PROCESS_MODE_BACKGROUND_END;
269 } else {
270 priority = value ? IDLE_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
271 }
272
273 return (::SetPriorityClass(Handle(), priority) != 0);
274 }
275
GetPriority() const276 int Process::GetPriority() const {
277 DCHECK(IsValid());
278 return ::GetPriorityClass(Handle());
279 }
280
281 } // namespace base
282