| OLD | NEW |
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process/process.h" | 5 #include "base/process/process.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <stdint.h> |
| 8 #include <sys/resource.h> | 9 #include <sys/resource.h> |
| 9 #include <sys/wait.h> | 10 #include <sys/wait.h> |
| 10 | 11 |
| 11 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
| 14 #include "base/process/kill.h" | 15 #include "base/process/kill.h" |
| 15 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 16 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 17 #include "build/build_config.h" |
| 16 | 18 |
| 17 #if defined(OS_MACOSX) | 19 #if defined(OS_MACOSX) |
| 18 #include <sys/event.h> | 20 #include <sys/event.h> |
| 19 #endif | 21 #endif |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 #if !defined(OS_NACL_NONSFI) | 25 #if !defined(OS_NACL_NONSFI) |
| 24 | 26 |
| 25 bool WaitpidWithTimeout(base::ProcessHandle handle, | 27 bool WaitpidWithTimeout(base::ProcessHandle handle, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 47 // depending on behavior external to this function. | 49 // depending on behavior external to this function. |
| 48 // | 50 // |
| 49 // This function is used primarily for unit tests, if we want to use it in | 51 // 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. | 52 // the application itself it would probably be best to examine other routes. |
| 51 | 53 |
| 52 if (wait == base::TimeDelta::Max()) { | 54 if (wait == base::TimeDelta::Max()) { |
| 53 return HANDLE_EINTR(waitpid(handle, status, 0)) > 0; | 55 return HANDLE_EINTR(waitpid(handle, status, 0)) > 0; |
| 54 } | 56 } |
| 55 | 57 |
| 56 pid_t ret_pid = HANDLE_EINTR(waitpid(handle, status, WNOHANG)); | 58 pid_t ret_pid = HANDLE_EINTR(waitpid(handle, status, WNOHANG)); |
| 57 static const int64 kMaxSleepInMicroseconds = 1 << 18; // ~256 milliseconds. | 59 static const int64_t kMaxSleepInMicroseconds = 1 << 18; // ~256 milliseconds. |
| 58 int64 max_sleep_time_usecs = 1 << 10; // ~1 milliseconds. | 60 int64_t max_sleep_time_usecs = 1 << 10; // ~1 milliseconds. |
| 59 int64 double_sleep_time = 0; | 61 int64_t double_sleep_time = 0; |
| 60 | 62 |
| 61 // If the process hasn't exited yet, then sleep and try again. | 63 // If the process hasn't exited yet, then sleep and try again. |
| 62 base::TimeTicks wakeup_time = base::TimeTicks::Now() + wait; | 64 base::TimeTicks wakeup_time = base::TimeTicks::Now() + wait; |
| 63 while (ret_pid == 0) { | 65 while (ret_pid == 0) { |
| 64 base::TimeTicks now = base::TimeTicks::Now(); | 66 base::TimeTicks now = base::TimeTicks::Now(); |
| 65 if (now > wakeup_time) | 67 if (now > wakeup_time) |
| 66 break; | 68 break; |
| 67 // Guaranteed to be non-negative! | 69 // Guaranteed to be non-negative! |
| 68 int64 sleep_time_usecs = (wakeup_time - now).InMicroseconds(); | 70 int64_t sleep_time_usecs = (wakeup_time - now).InMicroseconds(); |
| 69 // Sleep for a bit while we wait for the process to finish. | 71 // Sleep for a bit while we wait for the process to finish. |
| 70 if (sleep_time_usecs > max_sleep_time_usecs) | 72 if (sleep_time_usecs > max_sleep_time_usecs) |
| 71 sleep_time_usecs = max_sleep_time_usecs; | 73 sleep_time_usecs = max_sleep_time_usecs; |
| 72 | 74 |
| 73 // usleep() will return 0 and set errno to EINTR on receipt of a signal | 75 // usleep() will return 0 and set errno to EINTR on receipt of a signal |
| 74 // such as SIGCHLD. | 76 // such as SIGCHLD. |
| 75 usleep(sleep_time_usecs); | 77 usleep(sleep_time_usecs); |
| 76 ret_pid = HANDLE_EINTR(waitpid(handle, status, WNOHANG)); | 78 ret_pid = HANDLE_EINTR(waitpid(handle, status, WNOHANG)); |
| 77 | 79 |
| 78 if ((max_sleep_time_usecs < kMaxSleepInMicroseconds) && | 80 if ((max_sleep_time_usecs < kMaxSleepInMicroseconds) && |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 return false; | 372 return false; |
| 371 } | 373 } |
| 372 #endif // !defined(OS_LINUX) | 374 #endif // !defined(OS_LINUX) |
| 373 | 375 |
| 374 int Process::GetPriority() const { | 376 int Process::GetPriority() const { |
| 375 DCHECK(IsValid()); | 377 DCHECK(IsValid()); |
| 376 return getpriority(PRIO_PROCESS, process_); | 378 return getpriority(PRIO_PROCESS, process_); |
| 377 } | 379 } |
| 378 | 380 |
| 379 } // namespace base | 381 } // namespace base |
| OLD | NEW |