OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_util.h" | 5 #include "base/process_util.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <io.h> | 8 #include <io.h> |
9 #include <windows.h> | 9 #include <windows.h> |
10 #include <userenv.h> | 10 #include <userenv.h> |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 case kProcessKilledExitCode: // Task manager kill. | 457 case kProcessKilledExitCode: // Task manager kill. |
458 return TERMINATION_STATUS_PROCESS_WAS_KILLED; | 458 return TERMINATION_STATUS_PROCESS_WAS_KILLED; |
459 default: | 459 default: |
460 // All other exit codes indicate crashes. | 460 // All other exit codes indicate crashes. |
461 return TERMINATION_STATUS_PROCESS_CRASHED; | 461 return TERMINATION_STATUS_PROCESS_CRASHED; |
462 } | 462 } |
463 } | 463 } |
464 | 464 |
465 bool WaitForExitCode(ProcessHandle handle, int* exit_code) { | 465 bool WaitForExitCode(ProcessHandle handle, int* exit_code) { |
466 bool success = WaitForExitCodeWithTimeout(handle, exit_code, INFINITE); | 466 bool success = WaitForExitCodeWithTimeout(handle, exit_code, INFINITE); |
467 CloseProcessHandle(handle); | 467 if (!success) |
| 468 CloseProcessHandle(handle); |
468 return success; | 469 return success; |
469 } | 470 } |
470 | 471 |
471 bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, | 472 bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, |
472 int64 timeout_milliseconds) { | 473 int64 timeout_milliseconds) { |
473 if (::WaitForSingleObject(handle, timeout_milliseconds) != WAIT_OBJECT_0) | 474 if (::WaitForSingleObject(handle, timeout_milliseconds) != WAIT_OBJECT_0) |
474 return false; | 475 return false; |
475 DWORD temp_code; // Don't clobber out-parameters in case of failure. | 476 DWORD temp_code; // Don't clobber out-parameters in case of failure. |
476 if (!::GetExitCodeProcess(handle, &temp_code)) | 477 if (!::GetExitCodeProcess(handle, &temp_code)) |
477 return false; | 478 return false; |
478 | 479 |
| 480 // Only close the handle on success, to give the caller a chance to forcefully |
| 481 // terminate the process if he wants to. |
| 482 CloseProcessHandle(handle); |
| 483 |
479 *exit_code = temp_code; | 484 *exit_code = temp_code; |
480 return true; | 485 return true; |
481 } | 486 } |
482 | 487 |
483 ProcessIterator::ProcessIterator(const ProcessFilter* filter) | 488 ProcessIterator::ProcessIterator(const ProcessFilter* filter) |
484 : started_iteration_(false), | 489 : started_iteration_(false), |
485 filter_(filter) { | 490 filter_(filter) { |
486 snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | 491 snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
487 } | 492 } |
488 | 493 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 } | 537 } |
533 | 538 |
534 return result; | 539 return result; |
535 } | 540 } |
536 | 541 |
537 bool WaitForSingleProcess(ProcessHandle handle, int64 wait_milliseconds) { | 542 bool WaitForSingleProcess(ProcessHandle handle, int64 wait_milliseconds) { |
538 bool retval = WaitForSingleObject(handle, wait_milliseconds) == WAIT_OBJECT_0; | 543 bool retval = WaitForSingleObject(handle, wait_milliseconds) == WAIT_OBJECT_0; |
539 return retval; | 544 return retval; |
540 } | 545 } |
541 | 546 |
| 547 bool CrashAwareSleep(ProcessHandle handle, int64 wait_milliseconds) { |
| 548 bool retval = WaitForSingleObject(handle, wait_milliseconds) == WAIT_TIMEOUT; |
| 549 return retval; |
| 550 } |
| 551 |
542 bool CleanupProcesses(const std::wstring& executable_name, | 552 bool CleanupProcesses(const std::wstring& executable_name, |
543 int64 wait_milliseconds, | 553 int64 wait_milliseconds, |
544 int exit_code, | 554 int exit_code, |
545 const ProcessFilter* filter) { | 555 const ProcessFilter* filter) { |
546 bool exited_cleanly = WaitForProcessesToExit(executable_name, | 556 bool exited_cleanly = WaitForProcessesToExit(executable_name, |
547 wait_milliseconds, | 557 wait_milliseconds, |
548 filter); | 558 filter); |
549 if (!exited_cleanly) | 559 if (!exited_cleanly) |
550 KillProcesses(executable_name, exit_code, filter); | 560 KillProcesses(executable_name, exit_code, filter); |
551 return exited_cleanly; | 561 return exited_cleanly; |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 | 909 |
900 PERFORMANCE_INFORMATION info; | 910 PERFORMANCE_INFORMATION info; |
901 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { | 911 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { |
902 LOG(ERROR) << "Failed to fetch internal performance info."; | 912 LOG(ERROR) << "Failed to fetch internal performance info."; |
903 return 0; | 913 return 0; |
904 } | 914 } |
905 return (info.CommitTotal * system_info.dwPageSize) / 1024; | 915 return (info.CommitTotal * system_info.dwPageSize) / 1024; |
906 } | 916 } |
907 | 917 |
908 } // namespace base | 918 } // namespace base |
OLD | NEW |