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> |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 return true; | 277 return true; |
278 } | 278 } |
279 | 279 |
280 // If it didn't exit cleanly, it must have been signaled. | 280 // If it didn't exit cleanly, it must have been signaled. |
281 DCHECK(WIFSIGNALED(status)); | 281 DCHECK(WIFSIGNALED(status)); |
282 return false; | 282 return false; |
283 } | 283 } |
284 | 284 |
285 namespace { | 285 namespace { |
286 | 286 |
287 int WaitpidWithTimeout(ProcessHandle handle, int wait_milliseconds, | 287 int WaitpidWithTimeout(ProcessHandle handle, int64 wait_milliseconds, |
288 bool* success) { | 288 bool* success) { |
289 // This POSIX version of this function only guarantees that we wait no less | 289 // This POSIX version of this function only guarantees that we wait no less |
290 // than |wait_milliseconds| for the proces to exit. The child process may | 290 // than |wait_milliseconds| for the proces to exit. The child process may |
291 // exit sometime before the timeout has ended but we may still block for | 291 // exit sometime before the timeout has ended but we may still block for |
292 // up to 0.25 seconds after the fact. | 292 // up to 0.25 seconds after the fact. |
293 // | 293 // |
294 // waitpid() has no direct support on POSIX for specifying a timeout, you can | 294 // waitpid() has no direct support on POSIX for specifying a timeout, you can |
295 // either ask it to block indefinitely or return immediately (WNOHANG). | 295 // either ask it to block indefinitely or return immediately (WNOHANG). |
296 // When a child process terminates a SIGCHLD signal is sent to the parent. | 296 // When a child process terminates a SIGCHLD signal is sent to the parent. |
297 // Catching this signal would involve installing a signal handler which may | 297 // Catching this signal would involve installing a signal handler which may |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 } | 332 } |
333 | 333 |
334 if (success) | 334 if (success) |
335 *success = (ret_pid != -1); | 335 *success = (ret_pid != -1); |
336 | 336 |
337 return status; | 337 return status; |
338 } | 338 } |
339 | 339 |
340 } // namespace | 340 } // namespace |
341 | 341 |
342 bool WaitForSingleProcess(ProcessHandle handle, int wait_milliseconds) { | 342 bool WaitForSingleProcess(ProcessHandle handle, int64 wait_milliseconds) { |
343 bool waitpid_success; | 343 bool waitpid_success; |
344 int status; | 344 int status; |
345 if (wait_milliseconds == base::kNoTimeout) | 345 if (wait_milliseconds == base::kNoTimeout) |
346 waitpid_success = (HANDLE_EINTR(waitpid(handle, &status, 0)) != -1); | 346 waitpid_success = (HANDLE_EINTR(waitpid(handle, &status, 0)) != -1); |
347 else | 347 else |
348 status = WaitpidWithTimeout(handle, wait_milliseconds, &waitpid_success); | 348 status = WaitpidWithTimeout(handle, wait_milliseconds, &waitpid_success); |
349 if (status != -1) { | 349 if (status != -1) { |
350 DCHECK(waitpid_success); | 350 DCHECK(waitpid_success); |
351 return WIFEXITED(status); | 351 return WIFEXITED(status); |
352 } else { | 352 } else { |
353 return false; | 353 return false; |
354 } | 354 } |
355 } | 355 } |
356 | 356 |
357 bool CrashAwareSleep(ProcessHandle handle, int wait_milliseconds) { | 357 bool CrashAwareSleep(ProcessHandle handle, int64 wait_milliseconds) { |
358 bool waitpid_success; | 358 bool waitpid_success; |
359 int status = WaitpidWithTimeout(handle, wait_milliseconds, &waitpid_success); | 359 int status = WaitpidWithTimeout(handle, wait_milliseconds, &waitpid_success); |
360 if (status != -1) { | 360 if (status != -1) { |
361 DCHECK(waitpid_success); | 361 DCHECK(waitpid_success); |
362 return !(WIFEXITED(status) || WIFSIGNALED(status)); | 362 return !(WIFEXITED(status) || WIFSIGNALED(status)); |
363 } else { | 363 } else { |
364 // If waitpid returned with an error, then the process doesn't exist | 364 // If waitpid returned with an error, then the process doesn't exist |
365 // (which most probably means it didn't exist before our call). | 365 // (which most probably means it didn't exist before our call). |
366 return waitpid_success; | 366 return waitpid_success; |
367 } | 367 } |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 const ProcessEntry* entry; | 496 const ProcessEntry* entry; |
497 | 497 |
498 NamedProcessIterator iter(executable_name, filter); | 498 NamedProcessIterator iter(executable_name, filter); |
499 while ((entry = iter.NextProcessEntry()) != NULL) | 499 while ((entry = iter.NextProcessEntry()) != NULL) |
500 result = KillProcess((*entry).pid, exit_code, true) && result; | 500 result = KillProcess((*entry).pid, exit_code, true) && result; |
501 | 501 |
502 return result; | 502 return result; |
503 } | 503 } |
504 | 504 |
505 bool WaitForProcessesToExit(const std::wstring& executable_name, | 505 bool WaitForProcessesToExit(const std::wstring& executable_name, |
506 int wait_milliseconds, | 506 int64 wait_milliseconds, |
507 const ProcessFilter* filter) { | 507 const ProcessFilter* filter) { |
508 bool result = false; | 508 bool result = false; |
509 | 509 |
510 // TODO(port): This is inefficient, but works if there are multiple procs. | 510 // TODO(port): This is inefficient, but works if there are multiple procs. |
511 // TODO(port): use waitpid to avoid leaving zombies around | 511 // TODO(port): use waitpid to avoid leaving zombies around |
512 | 512 |
513 base::Time end_time = base::Time::Now() + | 513 base::Time end_time = base::Time::Now() + |
514 base::TimeDelta::FromMilliseconds(wait_milliseconds); | 514 base::TimeDelta::FromMilliseconds(wait_milliseconds); |
515 do { | 515 do { |
516 NamedProcessIterator iter(executable_name, filter); | 516 NamedProcessIterator iter(executable_name, filter); |
517 if (!iter.NextProcessEntry()) { | 517 if (!iter.NextProcessEntry()) { |
518 result = true; | 518 result = true; |
519 break; | 519 break; |
520 } | 520 } |
521 PlatformThread::Sleep(100); | 521 PlatformThread::Sleep(100); |
522 } while ((base::Time::Now() - end_time) > base::TimeDelta()); | 522 } while ((base::Time::Now() - end_time) > base::TimeDelta()); |
523 | 523 |
524 return result; | 524 return result; |
525 } | 525 } |
526 | 526 |
527 bool CleanupProcesses(const std::wstring& executable_name, | 527 bool CleanupProcesses(const std::wstring& executable_name, |
528 int wait_milliseconds, | 528 int64 wait_milliseconds, |
529 int exit_code, | 529 int exit_code, |
530 const ProcessFilter* filter) { | 530 const ProcessFilter* filter) { |
531 bool exited_cleanly = | 531 bool exited_cleanly = |
532 WaitForProcessesToExit(executable_name, wait_milliseconds, | 532 WaitForProcessesToExit(executable_name, wait_milliseconds, |
533 filter); | 533 filter); |
534 if (!exited_cleanly) | 534 if (!exited_cleanly) |
535 KillProcesses(executable_name, exit_code, filter); | 535 KillProcesses(executable_name, exit_code, filter); |
536 return exited_cleanly; | 536 return exited_cleanly; |
537 } | 537 } |
538 | 538 |
539 } // namespace base | 539 } // namespace base |
OLD | NEW |