Chromium Code Reviews| 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> |
| 11 #include <psapi.h> | 11 #include <psapi.h> |
| 12 | 12 |
| 13 #include <ios> | 13 #include <ios> |
| 14 | 14 |
| 15 #include "base/bind.h" | |
| 16 #include "base/bind_helpers.h" | |
| 15 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 16 #include "base/debug/stack_trace.h" | 18 #include "base/debug/stack_trace.h" |
| 17 #include "base/logging.h" | 19 #include "base/logging.h" |
| 18 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/message_loop.h" | 21 #include "base/message_loop.h" |
| 20 #include "base/metrics/histogram.h" | 22 #include "base/metrics/histogram.h" |
| 21 #include "base/sys_info.h" | 23 #include "base/sys_info.h" |
| 22 #include "base/win/object_watcher.h" | 24 #include "base/win/object_watcher.h" |
| 23 #include "base/win/scoped_handle.h" | 25 #include "base/win/scoped_handle.h" |
| 24 #include "base/win/windows_version.h" | 26 #include "base/win/windows_version.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 | 103 |
| 102 void OnNoMemory() { | 104 void OnNoMemory() { |
| 103 // Kill the process. This is important for security, since WebKit doesn't | 105 // Kill the process. This is important for security, since WebKit doesn't |
| 104 // NULL-check many memory allocations. If a malloc fails, returns NULL, and | 106 // NULL-check many memory allocations. If a malloc fails, returns NULL, and |
| 105 // the buffer is then used, it provides a handy mapping of memory starting at | 107 // the buffer is then used, it provides a handy mapping of memory starting at |
| 106 // address 0 for an attacker to utilize. | 108 // address 0 for an attacker to utilize. |
| 107 __debugbreak(); | 109 __debugbreak(); |
| 108 _exit(1); | 110 _exit(1); |
| 109 } | 111 } |
| 110 | 112 |
| 111 class TimerExpiredTask : public Task, | 113 class TimerExpiredTask : public win::ObjectWatcher::Delegate { |
| 112 public win::ObjectWatcher::Delegate { | |
| 113 public: | 114 public: |
| 114 explicit TimerExpiredTask(ProcessHandle process) : process_(process) { | 115 explicit TimerExpiredTask(ProcessHandle process); |
| 115 watcher_.StartWatching(process_, this); | 116 ~TimerExpiredTask(); |
| 116 } | |
| 117 | 117 |
| 118 virtual ~TimerExpiredTask() { | 118 void TimedOut(); |
| 119 if (process_) { | |
| 120 KillProcess(); | |
| 121 DCHECK(!process_) << "Make sure to close the handle."; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 // Task --------------------------------------------------------------------- | |
| 126 | |
| 127 virtual void Run() { | |
| 128 if (process_) | |
| 129 KillProcess(); | |
| 130 } | |
| 131 | 119 |
| 132 // MessageLoop::Watcher ----------------------------------------------------- | 120 // MessageLoop::Watcher ----------------------------------------------------- |
| 133 | 121 virtual void OnObjectSignaled(HANDLE object); |
| 134 virtual void OnObjectSignaled(HANDLE object) { | |
| 135 // When we're called from KillProcess, the ObjectWatcher may still be | |
| 136 // watching. the process handle, so make sure it has stopped. | |
| 137 watcher_.StopWatching(); | |
| 138 | |
| 139 CloseHandle(process_); | |
| 140 process_ = NULL; | |
| 141 } | |
| 142 | 122 |
| 143 private: | 123 private: |
| 144 void KillProcess() { | 124 void KillProcess(); |
| 145 // OK, time to get frisky. We don't actually care when the process | |
| 146 // terminates. We just care that it eventually terminates, and that's what | |
| 147 // TerminateProcess should do for us. Don't check for the result code since | |
| 148 // it fails quite often. This should be investigated eventually. | |
| 149 base::KillProcess(process_, kProcessKilledExitCode, false); | |
| 150 | |
| 151 // Now, just cleanup as if the process exited normally. | |
| 152 OnObjectSignaled(process_); | |
| 153 } | |
| 154 | 125 |
| 155 // The process that we are watching. | 126 // The process that we are watching. |
| 156 ProcessHandle process_; | 127 ProcessHandle process_; |
| 157 | 128 |
| 158 win::ObjectWatcher watcher_; | 129 win::ObjectWatcher watcher_; |
| 159 | 130 |
| 160 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask); | 131 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask); |
| 161 }; | 132 }; |
| 162 | 133 |
| 134 TimerExpiredTask::TimerExpiredTask(ProcessHandle process) : process_(process) { | |
| 135 watcher_.StartWatching(process_, this); | |
| 136 } | |
| 137 | |
| 138 TimerExpiredTask::~TimerExpiredTask() { | |
|
willchan no longer on Chromium
2011/12/13 00:41:14
Should this just call TimedOut() and then DCHECK a
| |
| 139 if (process_) { | |
| 140 KillProcess(); | |
| 141 DCHECK(!process_) << "Make sure to close the handle."; | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 void TimerExpiredTask::TimedOut() { | |
| 146 if (process_) | |
| 147 KillProcess(); | |
| 148 } | |
| 149 | |
| 150 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { | |
| 151 CloseHandle(process_); | |
| 152 process_ = NULL; | |
| 153 } | |
| 154 | |
| 155 void TimerExpiredTask::KillProcess() { | |
| 156 // Stop watching the process handle since we're killing it. | |
| 157 watcher_.StopWatching(); | |
| 158 | |
| 159 // OK, time to get frisky. We don't actually care when the process | |
| 160 // terminates. We just care that it eventually terminates, and that's what | |
| 161 // TerminateProcess should do for us. Don't check for the result code since | |
| 162 // it fails quite often. This should be investigated eventually. | |
| 163 base::KillProcess(process_, kProcessKilledExitCode, false); | |
| 164 | |
| 165 // Now, just cleanup as if the process exited normally. | |
| 166 OnObjectSignaled(process_); | |
| 167 } | |
| 168 | |
| 163 } // namespace | 169 } // namespace |
| 164 | 170 |
| 165 ProcessId GetCurrentProcId() { | 171 ProcessId GetCurrentProcId() { |
| 166 return ::GetCurrentProcessId(); | 172 return ::GetCurrentProcessId(); |
| 167 } | 173 } |
| 168 | 174 |
| 169 ProcessHandle GetCurrentProcessHandle() { | 175 ProcessHandle GetCurrentProcessHandle() { |
| 170 return ::GetCurrentProcess(); | 176 return ::GetCurrentProcess(); |
| 171 } | 177 } |
| 172 | 178 |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 621 | 627 |
| 622 void EnsureProcessTerminated(ProcessHandle process) { | 628 void EnsureProcessTerminated(ProcessHandle process) { |
| 623 DCHECK(process != GetCurrentProcess()); | 629 DCHECK(process != GetCurrentProcess()); |
| 624 | 630 |
| 625 // If already signaled, then we are done! | 631 // If already signaled, then we are done! |
| 626 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { | 632 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { |
| 627 CloseHandle(process); | 633 CloseHandle(process); |
| 628 return; | 634 return; |
| 629 } | 635 } |
| 630 | 636 |
| 631 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 637 MessageLoop::current()->PostDelayedTask( |
| 632 new TimerExpiredTask(process), | 638 FROM_HERE, |
| 633 kWaitInterval); | 639 base::Bind(&TimerExpiredTask::TimedOut, |
| 640 base::Owned(new TimerExpiredTask(process))), | |
| 641 kWaitInterval); | |
| 634 } | 642 } |
| 635 | 643 |
| 636 /////////////////////////////////////////////////////////////////////////////// | 644 /////////////////////////////////////////////////////////////////////////////// |
| 637 // ProcesMetrics | 645 // ProcesMetrics |
| 638 | 646 |
| 639 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 647 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
| 640 : process_(process), | 648 : process_(process), |
| 641 processor_count_(base::SysInfo::NumberOfProcessors()), | 649 processor_count_(base::SysInfo::NumberOfProcessors()), |
| 642 last_time_(0), | 650 last_time_(0), |
| 643 last_system_time_(0) { | 651 last_system_time_(0) { |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 984 | 992 |
| 985 PERFORMANCE_INFORMATION info; | 993 PERFORMANCE_INFORMATION info; |
| 986 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { | 994 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { |
| 987 DLOG(ERROR) << "Failed to fetch internal performance info."; | 995 DLOG(ERROR) << "Failed to fetch internal performance info."; |
| 988 return 0; | 996 return 0; |
| 989 } | 997 } |
| 990 return (info.CommitTotal * system_info.dwPageSize) / 1024; | 998 return (info.CommitTotal * system_info.dwPageSize) / 1024; |
| 991 } | 999 } |
| 992 | 1000 |
| 993 } // namespace base | 1001 } // namespace base |
| OLD | NEW |