shrike | 08e576f | 2015-06-03 04:53:57 +0900 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors. All rights reserved. |
evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rsesek@chromium.org | c2b81bd | 2013-07-20 01:48:56 +0900 | [diff] [blame] | 5 | #include "base/process/process.h" |
pvalchev@google.com | 1d919db | 2010-03-10 16:46:43 +0900 | [diff] [blame] | 6 | |
mostynb | 8b7b117 | 2015-11-07 09:57:14 +0900 | [diff] [blame] | 7 | #include <errno.h> |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 8 | #include <sys/resource.h> |
rvargas | c66501f | 2015-03-04 05:46:19 +0900 | [diff] [blame] | 9 | #include <sys/wait.h> |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 10 | |
rvargas | c66501f | 2015-03-04 05:46:19 +0900 | [diff] [blame] | 11 | #include "base/files/scoped_file.h" |
erg@google.com | 6e67c1d | 2010-07-29 02:25:28 +0900 | [diff] [blame] | 12 | #include "base/logging.h" |
rvargas | c66501f | 2015-03-04 05:46:19 +0900 | [diff] [blame] | 13 | #include "base/posix/eintr_wrapper.h" |
rsesek@chromium.org | 687756f | 2013-07-26 06:38:23 +0900 | [diff] [blame] | 14 | #include "base/process/kill.h" |
rvargas | eb3cd35 | 2015-04-02 10:36:06 +0900 | [diff] [blame] | 15 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 16 | |
rvargas | c66501f | 2015-03-04 05:46:19 +0900 | [diff] [blame] | 17 | #if defined(OS_MACOSX) |
| 18 | #include <sys/event.h> |
| 19 | #endif |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | #if !defined(OS_NACL_NONSFI) |
| 24 | |
| 25 | bool WaitpidWithTimeout(base::ProcessHandle handle, |
| 26 | int* status, |
| 27 | base::TimeDelta wait) { |
| 28 | // This POSIX version of this function only guarantees that we wait no less |
| 29 | // than |wait| for the process to exit. The child process may |
| 30 | // exit sometime before the timeout has ended but we may still block for up |
| 31 | // to 256 milliseconds after the fact. |
| 32 | // |
| 33 | // waitpid() has no direct support on POSIX for specifying a timeout, you can |
| 34 | // either ask it to block indefinitely or return immediately (WNOHANG). |
| 35 | // When a child process terminates a SIGCHLD signal is sent to the parent. |
| 36 | // Catching this signal would involve installing a signal handler which may |
| 37 | // affect other parts of the application and would be difficult to debug. |
| 38 | // |
| 39 | // Our strategy is to call waitpid() once up front to check if the process |
| 40 | // has already exited, otherwise to loop for |wait|, sleeping for |
| 41 | // at most 256 milliseconds each time using usleep() and then calling |
| 42 | // waitpid(). The amount of time we sleep starts out at 1 milliseconds, and |
| 43 | // we double it every 4 sleep cycles. |
| 44 | // |
| 45 | // usleep() is speced to exit if a signal is received for which a handler |
| 46 | // has been installed. This means that when a SIGCHLD is sent, it will exit |
| 47 | // depending on behavior external to this function. |
| 48 | // |
| 49 | // This function is used primarily for unit tests, if we want to use it in |
| 50 | // the application itself it would probably be best to examine other routes. |
| 51 | |
| 52 | if (wait == base::TimeDelta::Max()) { |
| 53 | return HANDLE_EINTR(waitpid(handle, status, 0)) > 0; |
| 54 | } |
| 55 | |
| 56 | pid_t ret_pid = HANDLE_EINTR(waitpid(handle, status, WNOHANG)); |
| 57 | static const int64 kMaxSleepInMicroseconds = 1 << 18; // ~256 milliseconds. |
| 58 | int64 max_sleep_time_usecs = 1 << 10; // ~1 milliseconds. |
| 59 | int64 double_sleep_time = 0; |
| 60 | |
| 61 | // If the process hasn't exited yet, then sleep and try again. |
| 62 | base::TimeTicks wakeup_time = base::TimeTicks::Now() + wait; |
| 63 | while (ret_pid == 0) { |
| 64 | base::TimeTicks now = base::TimeTicks::Now(); |
| 65 | if (now > wakeup_time) |
| 66 | break; |
| 67 | // Guaranteed to be non-negative! |
| 68 | int64 sleep_time_usecs = (wakeup_time - now).InMicroseconds(); |
| 69 | // Sleep for a bit while we wait for the process to finish. |
| 70 | if (sleep_time_usecs > max_sleep_time_usecs) |
| 71 | sleep_time_usecs = max_sleep_time_usecs; |
| 72 | |
| 73 | // usleep() will return 0 and set errno to EINTR on receipt of a signal |
| 74 | // such as SIGCHLD. |
| 75 | usleep(sleep_time_usecs); |
| 76 | ret_pid = HANDLE_EINTR(waitpid(handle, status, WNOHANG)); |
| 77 | |
| 78 | if ((max_sleep_time_usecs < kMaxSleepInMicroseconds) && |
| 79 | (double_sleep_time++ % 4 == 0)) { |
| 80 | max_sleep_time_usecs *= 2; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return ret_pid > 0; |
| 85 | } |
| 86 | |
| 87 | #if defined(OS_MACOSX) |
| 88 | // Using kqueue on Mac so that we can wait on non-child processes. |
| 89 | // We can't use kqueues on child processes because we need to reap |
| 90 | // our own children using wait. |
| 91 | static bool WaitForSingleNonChildProcess(base::ProcessHandle handle, |
| 92 | base::TimeDelta wait) { |
| 93 | DCHECK_GT(handle, 0); |
| 94 | DCHECK_GT(wait, base::TimeDelta()); |
| 95 | |
| 96 | base::ScopedFD kq(kqueue()); |
| 97 | if (!kq.is_valid()) { |
| 98 | DPLOG(ERROR) << "kqueue"; |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | struct kevent change = {0}; |
| 103 | EV_SET(&change, handle, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); |
| 104 | int result = HANDLE_EINTR(kevent(kq.get(), &change, 1, NULL, 0, NULL)); |
| 105 | if (result == -1) { |
| 106 | if (errno == ESRCH) { |
| 107 | // If the process wasn't found, it must be dead. |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | DPLOG(ERROR) << "kevent (setup " << handle << ")"; |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | // Keep track of the elapsed time to be able to restart kevent if it's |
| 116 | // interrupted. |
| 117 | bool wait_forever = (wait == base::TimeDelta::Max()); |
| 118 | base::TimeDelta remaining_delta; |
| 119 | base::TimeTicks deadline; |
| 120 | if (!wait_forever) { |
| 121 | remaining_delta = wait; |
| 122 | deadline = base::TimeTicks::Now() + remaining_delta; |
| 123 | } |
| 124 | |
| 125 | result = -1; |
| 126 | struct kevent event = {0}; |
| 127 | |
| 128 | while (wait_forever || remaining_delta > base::TimeDelta()) { |
| 129 | struct timespec remaining_timespec; |
| 130 | struct timespec* remaining_timespec_ptr; |
| 131 | if (wait_forever) { |
| 132 | remaining_timespec_ptr = NULL; |
| 133 | } else { |
| 134 | remaining_timespec = remaining_delta.ToTimeSpec(); |
| 135 | remaining_timespec_ptr = &remaining_timespec; |
| 136 | } |
| 137 | |
| 138 | result = kevent(kq.get(), NULL, 0, &event, 1, remaining_timespec_ptr); |
| 139 | |
| 140 | if (result == -1 && errno == EINTR) { |
| 141 | if (!wait_forever) { |
| 142 | remaining_delta = deadline - base::TimeTicks::Now(); |
| 143 | } |
| 144 | result = 0; |
| 145 | } else { |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (result < 0) { |
| 151 | DPLOG(ERROR) << "kevent (wait " << handle << ")"; |
| 152 | return false; |
| 153 | } else if (result > 1) { |
| 154 | DLOG(ERROR) << "kevent (wait " << handle << "): unexpected result " |
| 155 | << result; |
| 156 | return false; |
| 157 | } else if (result == 0) { |
| 158 | // Timed out. |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | DCHECK_EQ(result, 1); |
| 163 | |
| 164 | if (event.filter != EVFILT_PROC || |
| 165 | (event.fflags & NOTE_EXIT) == 0 || |
| 166 | event.ident != static_cast<uintptr_t>(handle)) { |
| 167 | DLOG(ERROR) << "kevent (wait " << handle |
| 168 | << "): unexpected event: filter=" << event.filter |
| 169 | << ", fflags=" << event.fflags |
| 170 | << ", ident=" << event.ident; |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | #endif // OS_MACOSX |
| 177 | |
| 178 | bool WaitForExitWithTimeoutImpl(base::ProcessHandle handle, |
| 179 | int* exit_code, |
| 180 | base::TimeDelta timeout) { |
| 181 | base::ProcessHandle parent_pid = base::GetParentProcessId(handle); |
| 182 | base::ProcessHandle our_pid = base::GetCurrentProcessHandle(); |
| 183 | if (parent_pid != our_pid) { |
| 184 | #if defined(OS_MACOSX) |
| 185 | // On Mac we can wait on non child processes. |
| 186 | return WaitForSingleNonChildProcess(handle, timeout); |
| 187 | #else |
| 188 | // Currently on Linux we can't handle non child processes. |
| 189 | NOTIMPLEMENTED(); |
| 190 | #endif // OS_MACOSX |
| 191 | } |
| 192 | |
| 193 | int status; |
| 194 | if (!WaitpidWithTimeout(handle, &status, timeout)) |
| 195 | return false; |
| 196 | if (WIFSIGNALED(status)) { |
g.mehndiratt | 6acf5ca | 2015-05-29 17:17:14 +0900 | [diff] [blame] | 197 | if (exit_code) |
| 198 | *exit_code = -1; |
rvargas | c66501f | 2015-03-04 05:46:19 +0900 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | if (WIFEXITED(status)) { |
g.mehndiratt | 6acf5ca | 2015-05-29 17:17:14 +0900 | [diff] [blame] | 202 | if (exit_code) |
| 203 | *exit_code = WEXITSTATUS(status); |
rvargas | c66501f | 2015-03-04 05:46:19 +0900 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | #endif // !defined(OS_NACL_NONSFI) |
| 209 | |
| 210 | } // namespace |
| 211 | |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 212 | namespace base { |
| 213 | |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 214 | Process::Process(ProcessHandle handle) : process_(handle) { |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 215 | } |
| 216 | |
thakis | b1e9a07 | 2015-04-21 01:44:48 +0900 | [diff] [blame] | 217 | Process::~Process() { |
| 218 | } |
| 219 | |
dcheng | 8e5e0e6 | 2015-12-01 21:09:52 +0900 | [diff] [blame^] | 220 | Process::Process(Process&& other) : process_(other.process_) { |
| 221 | other.Close(); |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 222 | } |
| 223 | |
dcheng | 8e5e0e6 | 2015-12-01 21:09:52 +0900 | [diff] [blame^] | 224 | Process& Process::operator=(Process&& other) { |
| 225 | DCHECK_NE(this, &other); |
| 226 | process_ = other.process_; |
| 227 | other.Close(); |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 228 | return *this; |
| 229 | } |
| 230 | |
erg@google.com | d00c64e | 2011-01-26 02:29:39 +0900 | [diff] [blame] | 231 | // static |
| 232 | Process Process::Current() { |
rvargas | be05ac2 | 2015-02-06 04:09:24 +0900 | [diff] [blame] | 233 | return Process(GetCurrentProcessHandle()); |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 234 | } |
| 235 | |
rvargas | c016566 | 2014-11-19 05:44:11 +0900 | [diff] [blame] | 236 | // static |
rvargas | 2b74c8f | 2015-02-05 06:11:29 +0900 | [diff] [blame] | 237 | Process Process::Open(ProcessId pid) { |
rvargas | 27f4756 | 2015-01-17 11:46:47 +0900 | [diff] [blame] | 238 | if (pid == GetCurrentProcId()) |
| 239 | return Current(); |
| 240 | |
rvargas | 2b74c8f | 2015-02-05 06:11:29 +0900 | [diff] [blame] | 241 | // On POSIX process handles are the same as PIDs. |
rvargas | 27f4756 | 2015-01-17 11:46:47 +0900 | [diff] [blame] | 242 | return Process(pid); |
| 243 | } |
| 244 | |
| 245 | // static |
rvargas | 1a11448 | 2015-03-17 08:03:52 +0900 | [diff] [blame] | 246 | Process Process::OpenWithExtraPrivileges(ProcessId pid) { |
rvargas | 2b74c8f | 2015-02-05 06:11:29 +0900 | [diff] [blame] | 247 | // On POSIX there are no privileges to set. |
| 248 | return Open(pid); |
| 249 | } |
| 250 | |
| 251 | // static |
rvargas | c016566 | 2014-11-19 05:44:11 +0900 | [diff] [blame] | 252 | Process Process::DeprecatedGetProcessFromHandle(ProcessHandle handle) { |
| 253 | DCHECK_NE(handle, GetCurrentProcessHandle()); |
| 254 | return Process(handle); |
| 255 | } |
| 256 | |
shrike | 6e8e11d | 2015-09-25 03:38:00 +0900 | [diff] [blame] | 257 | #if !defined(OS_LINUX) |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 258 | // static |
| 259 | bool Process::CanBackgroundProcesses() { |
| 260 | return false; |
| 261 | } |
shrike | 6e8e11d | 2015-09-25 03:38:00 +0900 | [diff] [blame] | 262 | #endif // !defined(OS_LINUX) |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 263 | |
| 264 | bool Process::IsValid() const { |
| 265 | return process_ != kNullProcessHandle; |
| 266 | } |
| 267 | |
| 268 | ProcessHandle Process::Handle() const { |
| 269 | return process_; |
| 270 | } |
| 271 | |
| 272 | Process Process::Duplicate() const { |
| 273 | if (is_current()) |
| 274 | return Current(); |
| 275 | |
| 276 | return Process(process_); |
erg@google.com | d00c64e | 2011-01-26 02:29:39 +0900 | [diff] [blame] | 277 | } |
| 278 | |
rvargas | c5a6f98 | 2015-01-24 09:27:25 +0900 | [diff] [blame] | 279 | ProcessId Process::Pid() const { |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 280 | DCHECK(IsValid()); |
erg@google.com | d00c64e | 2011-01-26 02:29:39 +0900 | [diff] [blame] | 281 | return GetProcId(process_); |
| 282 | } |
| 283 | |
| 284 | bool Process::is_current() const { |
| 285 | return process_ == GetCurrentProcessHandle(); |
| 286 | } |
| 287 | |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 288 | void Process::Close() { |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 289 | process_ = kNullProcessHandle; |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 290 | // if the process wasn't terminated (so we waited) or the state |
thomasvl@chromium.org | 570d744 | 2009-02-06 04:22:01 +0900 | [diff] [blame] | 291 | // wasn't already collected w/ a wait from process_utils, we're gonna |
| 292 | // end up w/ a zombie when it does finally exit. |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 293 | } |
| 294 | |
rvargas | eb3cd35 | 2015-04-02 10:36:06 +0900 | [diff] [blame] | 295 | #if !defined(OS_NACL_NONSFI) |
| 296 | bool Process::Terminate(int exit_code, bool wait) const { |
rickyz | 593f78c | 2015-07-02 07:50:03 +0900 | [diff] [blame] | 297 | // exit_code isn't supportable. |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 298 | DCHECK(IsValid()); |
rickyz | 593f78c | 2015-07-02 07:50:03 +0900 | [diff] [blame] | 299 | CHECK_GT(process_, 0); |
| 300 | |
rvargas | eb3cd35 | 2015-04-02 10:36:06 +0900 | [diff] [blame] | 301 | bool result = kill(process_, SIGTERM) == 0; |
| 302 | if (result && wait) { |
| 303 | int tries = 60; |
| 304 | |
| 305 | if (RunningOnValgrind()) { |
| 306 | // Wait for some extra time when running under Valgrind since the child |
| 307 | // processes may take some time doing leak checking. |
| 308 | tries *= 2; |
| 309 | } |
| 310 | |
| 311 | unsigned sleep_ms = 4; |
| 312 | |
| 313 | // The process may not end immediately due to pending I/O |
| 314 | bool exited = false; |
| 315 | while (tries-- > 0) { |
| 316 | pid_t pid = HANDLE_EINTR(waitpid(process_, NULL, WNOHANG)); |
| 317 | if (pid == process_) { |
| 318 | exited = true; |
| 319 | break; |
| 320 | } |
| 321 | if (pid == -1) { |
| 322 | if (errno == ECHILD) { |
| 323 | // The wait may fail with ECHILD if another process also waited for |
| 324 | // the same pid, causing the process state to get cleaned up. |
| 325 | exited = true; |
| 326 | break; |
| 327 | } |
| 328 | DPLOG(ERROR) << "Error waiting for process " << process_; |
| 329 | } |
| 330 | |
| 331 | usleep(sleep_ms * 1000); |
| 332 | const unsigned kMaxSleepMs = 1000; |
| 333 | if (sleep_ms < kMaxSleepMs) |
| 334 | sleep_ms *= 2; |
| 335 | } |
| 336 | |
| 337 | // If we're waiting and the child hasn't died by now, force it |
| 338 | // with a SIGKILL. |
| 339 | if (!exited) |
| 340 | result = kill(process_, SIGKILL) == 0; |
| 341 | } |
| 342 | |
| 343 | if (!result) |
| 344 | DPLOG(ERROR) << "Unable to terminate process " << process_; |
| 345 | |
| 346 | return result; |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 347 | } |
rvargas | eb3cd35 | 2015-04-02 10:36:06 +0900 | [diff] [blame] | 348 | #endif // !defined(OS_NACL_NONSFI) |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 349 | |
rvargas | 74cc137 | 2014-12-12 09:25:14 +0900 | [diff] [blame] | 350 | bool Process::WaitForExit(int* exit_code) { |
rvargas | c66501f | 2015-03-04 05:46:19 +0900 | [diff] [blame] | 351 | return WaitForExitWithTimeout(TimeDelta::Max(), exit_code); |
rvargas | 74cc137 | 2014-12-12 09:25:14 +0900 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) { |
rvargas | c66501f | 2015-03-04 05:46:19 +0900 | [diff] [blame] | 355 | return WaitForExitWithTimeoutImpl(Handle(), exit_code, timeout); |
rvargas | 74cc137 | 2014-12-12 09:25:14 +0900 | [diff] [blame] | 356 | } |
| 357 | |
shrike | 6e8e11d | 2015-09-25 03:38:00 +0900 | [diff] [blame] | 358 | #if !defined(OS_LINUX) |
brettw@google.com | ba3b5a3 | 2008-11-12 07:35:19 +0900 | [diff] [blame] | 359 | bool Process::IsProcessBackgrounded() const { |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 360 | // See SetProcessBackgrounded(). |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 361 | DCHECK(IsValid()); |
evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
| 365 | bool Process::SetProcessBackgrounded(bool value) { |
shrike | 00a05f7 | 2015-10-02 11:46:58 +0900 | [diff] [blame] | 366 | // Not implemented for POSIX systems other than Linux. With POSIX, if we were |
| 367 | // to lower the process priority we wouldn't be able to raise it back to its |
| 368 | // initial priority. |
shrike | 08e576f | 2015-06-03 04:53:57 +0900 | [diff] [blame] | 369 | NOTIMPLEMENTED(); |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 370 | return false; |
evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 371 | } |
shrike | 6e8e11d | 2015-09-25 03:38:00 +0900 | [diff] [blame] | 372 | #endif // !defined(OS_LINUX) |
evanm@google.com | 0c9f1eb | 2008-10-15 05:49:16 +0900 | [diff] [blame] | 373 | |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 374 | int Process::GetPriority() const { |
rvargas | a9aa671 | 2014-10-18 07:32:16 +0900 | [diff] [blame] | 375 | DCHECK(IsValid()); |
davemoore@chromium.org | ad59fcb | 2009-10-30 02:43:44 +0900 | [diff] [blame] | 376 | return getpriority(PRIO_PROCESS, process_); |
| 377 | } |
| 378 | |
danakj | 651c3e2 | 2015-03-07 10:51:42 +0900 | [diff] [blame] | 379 | } // namespace base |