OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <dirent.h> | 5 #include <dirent.h> |
6 #include <errno.h> | 6 #include <errno.h> |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <signal.h> | 8 #include <signal.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <sys/resource.h> | 10 #include <sys/resource.h> |
11 #include <sys/time.h> | 11 #include <sys/time.h> |
12 #include <sys/types.h> | 12 #include <sys/types.h> |
13 #include <sys/wait.h> | 13 #include <sys/wait.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 | 15 |
16 #include <limits> | 16 #include <limits> |
17 | 17 |
18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
20 #include "base/platform_thread.h" | 20 #include "base/platform_thread.h" |
21 #include "base/process_util.h" | 21 #include "base/process_util.h" |
22 #include "base/scoped_ptr.h" | 22 #include "base/scoped_ptr.h" |
23 #include "base/sys_info.h" | 23 #include "base/sys_info.h" |
24 #include "base/time.h" | 24 #include "base/time.h" |
| 25 #include "base/waitable_event.h" |
25 | 26 |
26 const int kMicrosecondsPerSecond = 1000000; | 27 const int kMicrosecondsPerSecond = 1000000; |
27 | 28 |
28 namespace base { | 29 namespace base { |
29 | 30 |
30 ProcessId GetCurrentProcId() { | 31 ProcessId GetCurrentProcId() { |
31 return getpid(); | 32 return getpid(); |
32 } | 33 } |
33 | 34 |
34 ProcessHandle GetCurrentProcessHandle() { | 35 ProcessHandle GetCurrentProcessHandle() { |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 // affect other parts of the application and would be difficult to debug. | 201 // affect other parts of the application and would be difficult to debug. |
201 // | 202 // |
202 // Our strategy is to call waitpid() once up front to check if the process | 203 // Our strategy is to call waitpid() once up front to check if the process |
203 // has already exited, otherwise to loop for wait_milliseconds, sleeping for | 204 // has already exited, otherwise to loop for wait_milliseconds, sleeping for |
204 // at most 0.25 secs each time using usleep() and then calling waitpid(). | 205 // at most 0.25 secs each time using usleep() and then calling waitpid(). |
205 // | 206 // |
206 // usleep() is speced to exit if a signal is received for which a handler | 207 // usleep() is speced to exit if a signal is received for which a handler |
207 // has been installed. This means that when a SIGCHLD is sent, it will exit | 208 // has been installed. This means that when a SIGCHLD is sent, it will exit |
208 // depending on behavior external to this function. | 209 // depending on behavior external to this function. |
209 // | 210 // |
210 // This function is used primarilly for unit tests, if we want to use it in | 211 // This function is used primarily for unit tests, if we want to use it in |
211 // the application itself it would probably be best to examine other routes. | 212 // the application itself it would probably be best to examine other routes. |
212 int status = -1; | 213 int status = -1; |
213 pid_t ret_pid = waitpid(handle, &status, WNOHANG); | 214 pid_t ret_pid = waitpid(handle, &status, WNOHANG); |
214 static const int64 kQuarterSecondInMicroseconds = kMicrosecondsPerSecond/4; | 215 static const int64 kQuarterSecondInMicroseconds = kMicrosecondsPerSecond/4; |
215 | 216 |
216 // If the process hasn't exited yet, then sleep and try again. | 217 // If the process hasn't exited yet, then sleep and try again. |
217 Time wakeup_time = Time::Now() + TimeDelta::FromMilliseconds( | 218 Time wakeup_time = Time::Now() + TimeDelta::FromMilliseconds( |
218 wait_milliseconds); | 219 wait_milliseconds); |
219 while (ret_pid == 0) { | 220 while (ret_pid == 0) { |
220 Time now = Time::Now(); | 221 Time now = Time::Now(); |
(...skipping 15 matching lines...) Expand all Loading... |
236 if (success) | 237 if (success) |
237 *success = (ret_pid != -1); | 238 *success = (ret_pid != -1); |
238 | 239 |
239 return status; | 240 return status; |
240 } | 241 } |
241 | 242 |
242 } // namespace | 243 } // namespace |
243 | 244 |
244 bool WaitForSingleProcess(ProcessHandle handle, int wait_milliseconds) { | 245 bool WaitForSingleProcess(ProcessHandle handle, int wait_milliseconds) { |
245 bool waitpid_success; | 246 bool waitpid_success; |
246 int status = WaitpidWithTimeout(handle, wait_milliseconds, &waitpid_success); | 247 int status; |
| 248 if (wait_milliseconds == base::kNoTimeout) |
| 249 waitpid_success = (waitpid(handle, &status, 0) != -1); |
| 250 else |
| 251 status = WaitpidWithTimeout(handle, wait_milliseconds, &waitpid_success); |
247 if (status != -1) { | 252 if (status != -1) { |
248 DCHECK(waitpid_success); | 253 DCHECK(waitpid_success); |
249 return WIFEXITED(status); | 254 return WIFEXITED(status); |
250 } else { | 255 } else { |
251 return false; | 256 return false; |
252 } | 257 } |
253 } | 258 } |
254 | 259 |
255 bool CrashAwareSleep(ProcessHandle handle, int wait_milliseconds) { | 260 bool CrashAwareSleep(ProcessHandle handle, int wait_milliseconds) { |
256 bool waitpid_success; | 261 bool waitpid_success; |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 const ProcessFilter* filter) { | 441 const ProcessFilter* filter) { |
437 bool exited_cleanly = | 442 bool exited_cleanly = |
438 WaitForProcessesToExit(executable_name, wait_milliseconds, | 443 WaitForProcessesToExit(executable_name, wait_milliseconds, |
439 filter); | 444 filter); |
440 if (!exited_cleanly) | 445 if (!exited_cleanly) |
441 KillProcesses(executable_name, exit_code, filter); | 446 KillProcesses(executable_name, exit_code, filter); |
442 return exited_cleanly; | 447 return exited_cleanly; |
443 } | 448 } |
444 | 449 |
445 } // namespace base | 450 } // namespace base |
OLD | NEW |