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 if (!success) | 467 CloseProcessHandle(handle); |
468 CloseProcessHandle(handle); | |
469 return success; | 468 return success; |
470 } | 469 } |
471 | 470 |
472 bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, | 471 bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, |
473 int64 timeout_milliseconds) { | 472 int64 timeout_milliseconds) { |
474 if (::WaitForSingleObject(handle, timeout_milliseconds) != WAIT_OBJECT_0) | 473 if (::WaitForSingleObject(handle, timeout_milliseconds) != WAIT_OBJECT_0) |
475 return false; | 474 return false; |
476 DWORD temp_code; // Don't clobber out-parameters in case of failure. | 475 DWORD temp_code; // Don't clobber out-parameters in case of failure. |
477 if (!::GetExitCodeProcess(handle, &temp_code)) | 476 if (!::GetExitCodeProcess(handle, &temp_code)) |
478 return false; | 477 return false; |
479 | 478 |
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 | |
484 *exit_code = temp_code; | 479 *exit_code = temp_code; |
485 return true; | 480 return true; |
486 } | 481 } |
487 | 482 |
488 ProcessIterator::ProcessIterator(const ProcessFilter* filter) | 483 ProcessIterator::ProcessIterator(const ProcessFilter* filter) |
489 : started_iteration_(false), | 484 : started_iteration_(false), |
490 filter_(filter) { | 485 filter_(filter) { |
491 snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | 486 snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
492 } | 487 } |
493 | 488 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 } | 532 } |
538 | 533 |
539 return result; | 534 return result; |
540 } | 535 } |
541 | 536 |
542 bool WaitForSingleProcess(ProcessHandle handle, int64 wait_milliseconds) { | 537 bool WaitForSingleProcess(ProcessHandle handle, int64 wait_milliseconds) { |
543 bool retval = WaitForSingleObject(handle, wait_milliseconds) == WAIT_OBJECT_0; | 538 bool retval = WaitForSingleObject(handle, wait_milliseconds) == WAIT_OBJECT_0; |
544 return retval; | 539 return retval; |
545 } | 540 } |
546 | 541 |
547 bool CrashAwareSleep(ProcessHandle handle, int64 wait_milliseconds) { | |
548 bool retval = WaitForSingleObject(handle, wait_milliseconds) == WAIT_TIMEOUT; | |
549 return retval; | |
550 } | |
551 | |
552 bool CleanupProcesses(const std::wstring& executable_name, | 542 bool CleanupProcesses(const std::wstring& executable_name, |
553 int64 wait_milliseconds, | 543 int64 wait_milliseconds, |
554 int exit_code, | 544 int exit_code, |
555 const ProcessFilter* filter) { | 545 const ProcessFilter* filter) { |
556 bool exited_cleanly = WaitForProcessesToExit(executable_name, | 546 bool exited_cleanly = WaitForProcessesToExit(executable_name, |
557 wait_milliseconds, | 547 wait_milliseconds, |
558 filter); | 548 filter); |
559 if (!exited_cleanly) | 549 if (!exited_cleanly) |
560 KillProcesses(executable_name, exit_code, filter); | 550 KillProcesses(executable_name, exit_code, filter); |
561 return exited_cleanly; | 551 return exited_cleanly; |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
909 | 899 |
910 PERFORMANCE_INFORMATION info; | 900 PERFORMANCE_INFORMATION info; |
911 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { | 901 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { |
912 LOG(ERROR) << "Failed to fetch internal performance info."; | 902 LOG(ERROR) << "Failed to fetch internal performance info."; |
913 return 0; | 903 return 0; |
914 } | 904 } |
915 return (info.CommitTotal * system_info.dwPageSize) / 1024; | 905 return (info.CommitTotal * system_info.dwPageSize) / 1024; |
916 } | 906 } |
917 | 907 |
918 } // namespace base | 908 } // namespace base |
OLD | NEW |