| 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(Process process); | 41 explicit TimerExpiredTask(ProcessHandle 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 Process process_; | 53 ProcessHandle process_; |
| 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(Process process) : process_(process.Pass()) { | 60 TimerExpiredTask::TimerExpiredTask(ProcessHandle process) : process_(process) { |
| 61 watcher_.StartWatching(process_.Handle(), this); | 61 watcher_.StartWatching(process_, this); |
| 62 } | 62 } |
| 63 | 63 |
| 64 TimerExpiredTask::~TimerExpiredTask() { | 64 TimerExpiredTask::~TimerExpiredTask() { |
| 65 TimedOut(); | 65 TimedOut(); |
| 66 DCHECK(!process_) << "Make sure to close the handle."; |
| 66 } | 67 } |
| 67 | 68 |
| 68 void TimerExpiredTask::TimedOut() { | 69 void TimerExpiredTask::TimedOut() { |
| 69 if (process_.IsValid()) | 70 if (process_) |
| 70 KillProcess(); | 71 KillProcess(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { | 74 void TimerExpiredTask::OnObjectSignaled(HANDLE object) { |
| 74 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. | 75 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed. |
| 75 tracked_objects::ScopedTracker tracking_profile( | 76 tracked_objects::ScopedTracker tracking_profile( |
| 76 FROM_HERE_WITH_EXPLICIT_FUNCTION("TimerExpiredTask_OnObjectSignaled")); | 77 FROM_HERE_WITH_EXPLICIT_FUNCTION("TimerExpiredTask_OnObjectSignaled")); |
| 77 | 78 |
| 78 process_.Close(); | 79 CloseHandle(process_); |
| 80 process_ = NULL; |
| 79 } | 81 } |
| 80 | 82 |
| 81 void TimerExpiredTask::KillProcess() { | 83 void TimerExpiredTask::KillProcess() { |
| 82 // Stop watching the process handle since we're killing it. | 84 // Stop watching the process handle since we're killing it. |
| 83 watcher_.StopWatching(); | 85 watcher_.StopWatching(); |
| 84 | 86 |
| 85 // OK, time to get frisky. We don't actually care when the process | 87 // OK, time to get frisky. We don't actually care when the process |
| 86 // terminates. We just care that it eventually terminates, and that's what | 88 // terminates. We just care that it eventually terminates, and that's what |
| 87 // TerminateProcess should do for us. Don't check for the result code since | 89 // TerminateProcess should do for us. Don't check for the result code since |
| 88 // it fails quite often. This should be investigated eventually. | 90 // it fails quite often. This should be investigated eventually. |
| 89 base::KillProcess(process_.Handle(), kProcessKilledExitCode, false); | 91 base::KillProcess(process_, kProcessKilledExitCode, false); |
| 90 | 92 |
| 91 // Now, just cleanup as if the process exited normally. | 93 // Now, just cleanup as if the process exited normally. |
| 92 OnObjectSignaled(process_.Handle()); | 94 OnObjectSignaled(process_); |
| 93 } | 95 } |
| 94 | 96 |
| 95 } // namespace | 97 } // namespace |
| 96 | 98 |
| 97 bool KillProcess(ProcessHandle process, int exit_code, bool wait) { | 99 bool KillProcess(ProcessHandle process, int exit_code, bool wait) { |
| 98 bool result = (TerminateProcess(process, exit_code) != FALSE); | 100 bool result = (TerminateProcess(process, exit_code) != FALSE); |
| 99 if (result && wait) { | 101 if (result && wait) { |
| 100 // The process may not end immediately due to pending I/O | 102 // The process may not end immediately due to pending I/O |
| 101 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000)) | 103 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000)) |
| 102 DPLOG(ERROR) << "Error waiting for process exit"; | 104 DPLOG(ERROR) << "Error waiting for process exit"; |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 bool CleanupProcesses(const FilePath::StringType& executable_name, | 234 bool CleanupProcesses(const FilePath::StringType& executable_name, |
| 233 base::TimeDelta wait, | 235 base::TimeDelta wait, |
| 234 int exit_code, | 236 int exit_code, |
| 235 const ProcessFilter* filter) { | 237 const ProcessFilter* filter) { |
| 236 if (WaitForProcessesToExit(executable_name, wait, filter)) | 238 if (WaitForProcessesToExit(executable_name, wait, filter)) |
| 237 return true; | 239 return true; |
| 238 KillProcesses(executable_name, exit_code, filter); | 240 KillProcesses(executable_name, exit_code, filter); |
| 239 return false; | 241 return false; |
| 240 } | 242 } |
| 241 | 243 |
| 242 void EnsureProcessTerminated(Process process) { | 244 void EnsureProcessTerminated(ProcessHandle process) { |
| 243 DCHECK(!process.is_current()); | 245 DCHECK(process != GetCurrentProcess()); |
| 244 | 246 |
| 245 // If already signaled, then we are done! | 247 // If already signaled, then we are done! |
| 246 if (WaitForSingleObject(process.Handle(), 0) == WAIT_OBJECT_0) { | 248 if (WaitForSingleObject(process, 0) == WAIT_OBJECT_0) { |
| 249 CloseHandle(process); |
| 247 return; | 250 return; |
| 248 } | 251 } |
| 249 | 252 |
| 250 MessageLoop::current()->PostDelayedTask( | 253 MessageLoop::current()->PostDelayedTask( |
| 251 FROM_HERE, | 254 FROM_HERE, |
| 252 base::Bind(&TimerExpiredTask::TimedOut, | 255 base::Bind(&TimerExpiredTask::TimedOut, |
| 253 base::Owned(new TimerExpiredTask(process.Pass()))), | 256 base::Owned(new TimerExpiredTask(process))), |
| 254 base::TimeDelta::FromMilliseconds(kWaitInterval)); | 257 base::TimeDelta::FromMilliseconds(kWaitInterval)); |
| 255 } | 258 } |
| 256 | 259 |
| 257 } // namespace base | 260 } // namespace base |
| OLD | NEW |