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() { |
| 139 TimedOut(); |
| 140 DCHECK(!process_) << "Make sure to close the handle."; |
| 141 } |
| 142 |
| 143 void TimerExpiredTask::TimedOut() { |
| 144 if (process_) |
| 145 KillProcess(); |
| 146 } |
| 147 |
| 148 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { |
| 149 CloseHandle(process_); |
| 150 process_ = NULL; |
| 151 } |
| 152 |
| 153 void TimerExpiredTask::KillProcess() { |
| 154 // Stop watching the process handle since we're killing it. |
| 155 watcher_.StopWatching(); |
| 156 |
| 157 // OK, time to get frisky. We don't actually care when the process |
| 158 // terminates. We just care that it eventually terminates, and that's what |
| 159 // TerminateProcess should do for us. Don't check for the result code since |
| 160 // it fails quite often. This should be investigated eventually. |
| 161 base::KillProcess(process_, kProcessKilledExitCode, false); |
| 162 |
| 163 // Now, just cleanup as if the process exited normally. |
| 164 OnObjectSignaled(process_); |
| 165 } |
| 166 |
163 } // namespace | 167 } // namespace |
164 | 168 |
165 ProcessId GetCurrentProcId() { | 169 ProcessId GetCurrentProcId() { |
166 return ::GetCurrentProcessId(); | 170 return ::GetCurrentProcessId(); |
167 } | 171 } |
168 | 172 |
169 ProcessHandle GetCurrentProcessHandle() { | 173 ProcessHandle GetCurrentProcessHandle() { |
170 return ::GetCurrentProcess(); | 174 return ::GetCurrentProcess(); |
171 } | 175 } |
172 | 176 |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 | 625 |
622 void EnsureProcessTerminated(ProcessHandle process) { | 626 void EnsureProcessTerminated(ProcessHandle process) { |
623 DCHECK(process != GetCurrentProcess()); | 627 DCHECK(process != GetCurrentProcess()); |
624 | 628 |
625 // If already signaled, then we are done! | 629 // If already signaled, then we are done! |
626 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { | 630 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { |
627 CloseHandle(process); | 631 CloseHandle(process); |
628 return; | 632 return; |
629 } | 633 } |
630 | 634 |
631 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 635 MessageLoop::current()->PostDelayedTask( |
632 new TimerExpiredTask(process), | 636 FROM_HERE, |
633 kWaitInterval); | 637 base::Bind(&TimerExpiredTask::TimedOut, |
| 638 base::Owned(new TimerExpiredTask(process))), |
| 639 kWaitInterval); |
634 } | 640 } |
635 | 641 |
636 /////////////////////////////////////////////////////////////////////////////// | 642 /////////////////////////////////////////////////////////////////////////////// |
637 // ProcesMetrics | 643 // ProcesMetrics |
638 | 644 |
639 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 645 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
640 : process_(process), | 646 : process_(process), |
641 processor_count_(base::SysInfo::NumberOfProcessors()), | 647 processor_count_(base::SysInfo::NumberOfProcessors()), |
642 last_time_(0), | 648 last_time_(0), |
643 last_system_time_(0) { | 649 last_system_time_(0) { |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
984 | 990 |
985 PERFORMANCE_INFORMATION info; | 991 PERFORMANCE_INFORMATION info; |
986 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { | 992 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { |
987 DLOG(ERROR) << "Failed to fetch internal performance info."; | 993 DLOG(ERROR) << "Failed to fetch internal performance info."; |
988 return 0; | 994 return 0; |
989 } | 995 } |
990 return (info.CommitTotal * system_info.dwPageSize) / 1024; | 996 return (info.CommitTotal * system_info.dwPageSize) / 1024; |
991 } | 997 } |
992 | 998 |
993 } // namespace base | 999 } // namespace base |
OLD | NEW |