OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/kill.h" | 5 #include "base/process/kill.h" |
6 | 6 |
7 #include <io.h> | 7 #include <io.h> |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 // seems to be common practice on Windows to test for it as an | 31 // seems to be common practice on Windows to test for it as an |
32 // indication that the task manager has killed something if the | 32 // indication that the task manager has killed something if the |
33 // process goes away. | 33 // process goes away. |
34 const DWORD kProcessKilledExitCode = 1; | 34 const DWORD kProcessKilledExitCode = 1; |
35 | 35 |
36 // Maximum amount of time (in milliseconds) to wait for the process to exit. | 36 // Maximum amount of time (in milliseconds) to wait for the process to exit. |
37 static const int kWaitInterval = 2000; | 37 static const int kWaitInterval = 2000; |
38 | 38 |
39 class TimerExpiredTask : public win::ObjectWatcher::Delegate { | 39 class TimerExpiredTask : public win::ObjectWatcher::Delegate { |
40 public: | 40 public: |
41 explicit TimerExpiredTask(ProcessHandle process); | 41 explicit TimerExpiredTask(Process process); |
42 ~TimerExpiredTask(); | 42 ~TimerExpiredTask(); |
43 | 43 |
44 void TimedOut(); | 44 void TimedOut(); |
45 | 45 |
46 // MessageLoop::Watcher ----------------------------------------------------- | 46 // MessageLoop::Watcher ----------------------------------------------------- |
47 virtual void OnObjectSignaled(HANDLE object); | 47 virtual void OnObjectSignaled(HANDLE object); |
48 | 48 |
49 private: | 49 private: |
50 void KillProcess(); | 50 void KillProcess(); |
51 | 51 |
52 // The process that we are watching. | 52 // The process that we are watching. |
53 ProcessHandle process_; | 53 Process process_; |
gab
2014/11/27 13:28:44
If I'm reading this correctly, the changes in this
rvargas (doing something else)
2014/12/01 20:50:36
You're right, there is no need to extend the handl
gab
2014/12/01 21:31:05
Indeed, I meant reset(), not release() (my bad) --
rvargas (doing something else)
2014/12/02 21:07:47
It would be better to not have a Handle method at
| |
54 | 54 |
55 win::ObjectWatcher watcher_; | 55 win::ObjectWatcher watcher_; |
56 | 56 |
57 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask); | 57 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask); |
58 }; | 58 }; |
59 | 59 |
60 TimerExpiredTask::TimerExpiredTask(ProcessHandle process) : process_(process) { | 60 TimerExpiredTask::TimerExpiredTask(Process process) : process_(process.Pass()) { |
61 watcher_.StartWatching(process_, this); | 61 watcher_.StartWatching(process_.Handle(), this); |
62 } | 62 } |
63 | 63 |
64 TimerExpiredTask::~TimerExpiredTask() { | 64 TimerExpiredTask::~TimerExpiredTask() { |
65 TimedOut(); | 65 TimedOut(); |
66 DCHECK(!process_) << "Make sure to close the handle."; | |
67 } | 66 } |
68 | 67 |
69 void TimerExpiredTask::TimedOut() { | 68 void TimerExpiredTask::TimedOut() { |
70 if (process_) | 69 if (process_.IsValid()) |
71 KillProcess(); | 70 KillProcess(); |
72 } | 71 } |
73 | 72 |
74 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { | 73 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { |
75 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. | 74 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. |
76 tracked_objects::ScopedTracker tracking_profile( | 75 tracked_objects::ScopedTracker tracking_profile( |
77 FROM_HERE_WITH_EXPLICIT_FUNCTION("TimerExpiredTask_OnObjectSignaled")); | 76 FROM_HERE_WITH_EXPLICIT_FUNCTION("TimerExpiredTask_OnObjectSignaled")); |
78 | |
79 CloseHandle(process_); | |
80 process_ = NULL; | |
81 } | 77 } |
82 | 78 |
83 void TimerExpiredTask::KillProcess() { | 79 void TimerExpiredTask::KillProcess() { |
84 // Stop watching the process handle since we're killing it. | 80 // Stop watching the process handle since we're killing it. |
85 watcher_.StopWatching(); | 81 watcher_.StopWatching(); |
86 | 82 |
87 // OK, time to get frisky. We don't actually care when the process | 83 // OK, time to get frisky. We don't actually care when the process |
88 // terminates. We just care that it eventually terminates, and that's what | 84 // terminates. We just care that it eventually terminates, and that's what |
89 // TerminateProcess should do for us. Don't check for the result code since | 85 // TerminateProcess should do for us. Don't check for the result code since |
90 // it fails quite often. This should be investigated eventually. | 86 // it fails quite often. This should be investigated eventually. |
91 base::KillProcess(process_, kProcessKilledExitCode, false); | 87 base::KillProcess(process_.Handle(), kProcessKilledExitCode, false); |
92 | 88 |
93 // Now, just cleanup as if the process exited normally. | 89 // Now, just cleanup as if the process exited normally. |
94 OnObjectSignaled(process_); | 90 OnObjectSignaled(process_.Handle()); |
95 } | 91 } |
96 | 92 |
97 } // namespace | 93 } // namespace |
98 | 94 |
99 bool KillProcess(ProcessHandle process, int exit_code, bool wait) { | 95 bool KillProcess(ProcessHandle process, int exit_code, bool wait) { |
100 bool result = (TerminateProcess(process, exit_code) != FALSE); | 96 bool result = (TerminateProcess(process, exit_code) != FALSE); |
101 if (result && wait) { | 97 if (result && wait) { |
102 // The process may not end immediately due to pending I/O | 98 // The process may not end immediately due to pending I/O |
103 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000)) | 99 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000)) |
104 DPLOG(ERROR) << "Error waiting for process exit"; | 100 DPLOG(ERROR) << "Error waiting for process exit"; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 bool CleanupProcesses(const FilePath::StringType& executable_name, | 230 bool CleanupProcesses(const FilePath::StringType& executable_name, |
235 base::TimeDelta wait, | 231 base::TimeDelta wait, |
236 int exit_code, | 232 int exit_code, |
237 const ProcessFilter* filter) { | 233 const ProcessFilter* filter) { |
238 if (WaitForProcessesToExit(executable_name, wait, filter)) | 234 if (WaitForProcessesToExit(executable_name, wait, filter)) |
239 return true; | 235 return true; |
240 KillProcesses(executable_name, exit_code, filter); | 236 KillProcesses(executable_name, exit_code, filter); |
241 return false; | 237 return false; |
242 } | 238 } |
243 | 239 |
244 void EnsureProcessTerminated(ProcessHandle process) { | 240 void EnsureProcessTerminated(Process process) { |
245 DCHECK(process != GetCurrentProcess()); | 241 DCHECK(!process.is_current()); |
246 | 242 |
247 // If already signaled, then we are done! | 243 // If already signaled, then we are done! |
248 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { | 244 if (WaitForSingleObject(process.Handle(), 0) == WAIT_OBJECT_0) { |
249 CloseHandle(process); | |
250 return; | 245 return; |
251 } | 246 } |
252 | 247 |
253 MessageLoop::current()->PostDelayedTask( | 248 MessageLoop::current()->PostDelayedTask( |
254 FROM_HERE, | 249 FROM_HERE, |
255 base::Bind(&TimerExpiredTask::TimedOut, | 250 base::Bind(&TimerExpiredTask::TimedOut, |
256 base::Owned(new TimerExpiredTask(process))), | 251 base::Owned(new TimerExpiredTask(process.Pass()))), |
257 base::TimeDelta::FromMilliseconds(kWaitInterval)); | 252 base::TimeDelta::FromMilliseconds(kWaitInterval)); |
258 } | 253 } |
259 | 254 |
260 } // namespace base | 255 } // namespace base |
OLD | NEW |