OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 } | 539 } |
540 | 540 |
541 bool WaitForExitCode(ProcessHandle handle, int* exit_code) { | 541 bool WaitForExitCode(ProcessHandle handle, int* exit_code) { |
542 bool success = WaitForExitCodeWithTimeout( | 542 bool success = WaitForExitCodeWithTimeout( |
543 handle, exit_code, base::TimeDelta::FromMilliseconds(INFINITE)); | 543 handle, exit_code, base::TimeDelta::FromMilliseconds(INFINITE)); |
544 CloseProcessHandle(handle); | 544 CloseProcessHandle(handle); |
545 return success; | 545 return success; |
546 } | 546 } |
547 | 547 |
548 bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, | 548 bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, |
549 int64 timeout_milliseconds) { | 549 base::TimeDelta timeout) { |
550 if (::WaitForSingleObject(handle, timeout_milliseconds) != WAIT_OBJECT_0) | 550 if (::WaitForSingleObject(handle, timeout.InMilliseconds()) != WAIT_OBJECT_0) |
551 return false; | 551 return false; |
552 DWORD temp_code; // Don't clobber out-parameters in case of failure. | 552 DWORD temp_code; // Don't clobber out-parameters in case of failure. |
553 if (!::GetExitCodeProcess(handle, &temp_code)) | 553 if (!::GetExitCodeProcess(handle, &temp_code)) |
554 return false; | 554 return false; |
555 | 555 |
556 *exit_code = temp_code; | 556 *exit_code = temp_code; |
557 return true; | 557 return true; |
558 } | 558 } |
559 | 559 |
560 bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, | |
561 base::TimeDelta timeout) { | |
562 return WaitForExitCodeWithTimeout( | |
563 handle, exit_code, timeout.InMilliseconds()); | |
564 } | |
565 | |
566 ProcessIterator::ProcessIterator(const ProcessFilter* filter) | 560 ProcessIterator::ProcessIterator(const ProcessFilter* filter) |
567 : started_iteration_(false), | 561 : started_iteration_(false), |
568 filter_(filter) { | 562 filter_(filter) { |
569 snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | 563 snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
570 } | 564 } |
571 | 565 |
572 ProcessIterator::~ProcessIterator() { | 566 ProcessIterator::~ProcessIterator() { |
573 CloseHandle(snapshot_); | 567 CloseHandle(snapshot_); |
574 } | 568 } |
575 | 569 |
(...skipping 13 matching lines...) Expand all Loading... |
589 entry->dwSize = sizeof(*entry); | 583 entry->dwSize = sizeof(*entry); |
590 } | 584 } |
591 | 585 |
592 bool NamedProcessIterator::IncludeEntry() { | 586 bool NamedProcessIterator::IncludeEntry() { |
593 // Case insensitive. | 587 // Case insensitive. |
594 return _wcsicmp(executable_name_.c_str(), entry().exe_file()) == 0 && | 588 return _wcsicmp(executable_name_.c_str(), entry().exe_file()) == 0 && |
595 ProcessIterator::IncludeEntry(); | 589 ProcessIterator::IncludeEntry(); |
596 } | 590 } |
597 | 591 |
598 bool WaitForProcessesToExit(const FilePath::StringType& executable_name, | 592 bool WaitForProcessesToExit(const FilePath::StringType& executable_name, |
599 int64 wait_milliseconds, | 593 base::TimeDelta wait, |
600 const ProcessFilter* filter) { | 594 const ProcessFilter* filter) { |
601 const ProcessEntry* entry; | 595 const ProcessEntry* entry; |
602 bool result = true; | 596 bool result = true; |
603 DWORD start_time = GetTickCount(); | 597 DWORD start_time = GetTickCount(); |
604 | 598 |
605 NamedProcessIterator iter(executable_name, filter); | 599 NamedProcessIterator iter(executable_name, filter); |
606 while ((entry = iter.NextProcessEntry())) { | 600 while ((entry = iter.NextProcessEntry())) { |
607 DWORD remaining_wait = | 601 DWORD remaining_wait = std::max<int64>( |
608 std::max<int64>(0, wait_milliseconds - (GetTickCount() - start_time)); | 602 0, wait.InMilliseconds() - (GetTickCount() - start_time)); |
609 HANDLE process = OpenProcess(SYNCHRONIZE, | 603 HANDLE process = OpenProcess(SYNCHRONIZE, |
610 FALSE, | 604 FALSE, |
611 entry->th32ProcessID); | 605 entry->th32ProcessID); |
612 DWORD wait_result = WaitForSingleObject(process, remaining_wait); | 606 DWORD wait_result = WaitForSingleObject(process, remaining_wait); |
613 CloseHandle(process); | 607 CloseHandle(process); |
614 result = result && (wait_result == WAIT_OBJECT_0); | 608 result = result && (wait_result == WAIT_OBJECT_0); |
615 } | 609 } |
616 | 610 |
617 return result; | 611 return result; |
618 } | 612 } |
619 | 613 |
620 bool WaitForProcessesToExit(const FilePath::StringType& executable_name, | |
621 base::TimeDelta wait, | |
622 const ProcessFilter* filter) { | |
623 return WaitForProcessesToExit(executable_name, wait.InMilliseconds(), filter); | |
624 } | |
625 | |
626 bool WaitForSingleProcess(ProcessHandle handle, int64 wait_milliseconds) { | |
627 return WaitForSingleProcess( | |
628 handle, base::TimeDelta::FromMilliseconds(wait_milliseconds)); | |
629 } | |
630 | |
631 bool WaitForSingleProcess(ProcessHandle handle, base::TimeDelta wait) { | 614 bool WaitForSingleProcess(ProcessHandle handle, base::TimeDelta wait) { |
632 int exit_code; | 615 int exit_code; |
633 if (!WaitForExitCodeWithTimeout(handle, &exit_code, wait)) | 616 if (!WaitForExitCodeWithTimeout(handle, &exit_code, wait)) |
634 return false; | 617 return false; |
635 return exit_code == 0; | 618 return exit_code == 0; |
636 } | 619 } |
637 | 620 |
638 bool CleanupProcesses(const FilePath::StringType& executable_name, | 621 bool CleanupProcesses(const FilePath::StringType& executable_name, |
639 int64 wait_milliseconds, | 622 int64 wait_milliseconds, |
640 int exit_code, | 623 int exit_code, |
641 const ProcessFilter* filter) { | 624 const ProcessFilter* filter) { |
642 bool exited_cleanly = WaitForProcessesToExit(executable_name, | 625 bool exited_cleanly = WaitForProcessesToExit( |
643 wait_milliseconds, | 626 executable_name, |
644 filter); | 627 base::TimeDelta::FromMilliseconds(wait_milliseconds), |
| 628 filter); |
645 if (!exited_cleanly) | 629 if (!exited_cleanly) |
646 KillProcesses(executable_name, exit_code, filter); | 630 KillProcesses(executable_name, exit_code, filter); |
647 return exited_cleanly; | 631 return exited_cleanly; |
648 } | 632 } |
649 | 633 |
650 void EnsureProcessTerminated(ProcessHandle process) { | 634 void EnsureProcessTerminated(ProcessHandle process) { |
651 DCHECK(process != GetCurrentProcess()); | 635 DCHECK(process != GetCurrentProcess()); |
652 | 636 |
653 // If already signaled, then we are done! | 637 // If already signaled, then we are done! |
654 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { | 638 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1014 | 998 |
1015 PERFORMANCE_INFORMATION info; | 999 PERFORMANCE_INFORMATION info; |
1016 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { | 1000 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { |
1017 DLOG(ERROR) << "Failed to fetch internal performance info."; | 1001 DLOG(ERROR) << "Failed to fetch internal performance info."; |
1018 return 0; | 1002 return 0; |
1019 } | 1003 } |
1020 return (info.CommitTotal * system_info.dwPageSize) / 1024; | 1004 return (info.CommitTotal * system_info.dwPageSize) / 1024; |
1021 } | 1005 } |
1022 | 1006 |
1023 } // namespace base | 1007 } // namespace base |
OLD | NEW |