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 // indication that the task manager has killed something if the | 31 // indication that the task manager has killed something if the |
32 // process goes away. | 32 // process goes away. |
33 const DWORD kProcessKilledExitCode = 1; | 33 const DWORD kProcessKilledExitCode = 1; |
34 | 34 |
35 // Maximum amount of time (in milliseconds) to wait for the process to exit. | 35 // Maximum amount of time (in milliseconds) to wait for the process to exit. |
36 static const int kWaitInterval = 2000; | 36 static const int kWaitInterval = 2000; |
37 | 37 |
38 class TimerExpiredTask : public win::ObjectWatcher::Delegate { | 38 class TimerExpiredTask : public win::ObjectWatcher::Delegate { |
39 public: | 39 public: |
40 explicit TimerExpiredTask(Process process); | 40 explicit TimerExpiredTask(Process process); |
41 ~TimerExpiredTask(); | 41 ~TimerExpiredTask() override; |
42 | 42 |
43 void TimedOut(); | 43 void TimedOut(); |
44 | 44 |
45 // MessageLoop::Watcher ----------------------------------------------------- | 45 // MessageLoop::Watcher ----------------------------------------------------- |
46 virtual void OnObjectSignaled(HANDLE object); | 46 void OnObjectSignaled(HANDLE object) override; |
47 | 47 |
48 private: | 48 private: |
49 void KillProcess(); | 49 void KillProcess(); |
50 | 50 |
51 // The process that we are watching. | 51 // The process that we are watching. |
52 Process process_; | 52 Process process_; |
53 | 53 |
54 win::ObjectWatcher watcher_; | 54 win::ObjectWatcher watcher_; |
55 | 55 |
56 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask); | 56 DISALLOW_COPY_AND_ASSIGN(TimerExpiredTask); |
(...skipping 17 matching lines...) Expand all Loading... |
74 } | 74 } |
75 | 75 |
76 void TimerExpiredTask::KillProcess() { | 76 void TimerExpiredTask::KillProcess() { |
77 // Stop watching the process handle since we're killing it. | 77 // Stop watching the process handle since we're killing it. |
78 watcher_.StopWatching(); | 78 watcher_.StopWatching(); |
79 | 79 |
80 // OK, time to get frisky. We don't actually care when the process | 80 // OK, time to get frisky. We don't actually care when the process |
81 // terminates. We just care that it eventually terminates, and that's what | 81 // terminates. We just care that it eventually terminates, and that's what |
82 // TerminateProcess should do for us. Don't check for the result code since | 82 // TerminateProcess should do for us. Don't check for the result code since |
83 // it fails quite often. This should be investigated eventually. | 83 // it fails quite often. This should be investigated eventually. |
84 base::KillProcess(process_.Handle(), kProcessKilledExitCode, false); | 84 process_.Terminate(kProcessKilledExitCode, false); |
85 | 85 |
86 // Now, just cleanup as if the process exited normally. | 86 // Now, just cleanup as if the process exited normally. |
87 OnObjectSignaled(process_.Handle()); | 87 OnObjectSignaled(process_.Handle()); |
88 } | 88 } |
89 | 89 |
90 } // namespace | 90 } // namespace |
91 | 91 |
92 bool KillProcess(ProcessHandle process, int exit_code, bool wait) { | |
93 bool result = (TerminateProcess(process, exit_code) != FALSE); | |
94 if (result && wait) { | |
95 // The process may not end immediately due to pending I/O | |
96 if (WAIT_OBJECT_0 != WaitForSingleObject(process, 60 * 1000)) | |
97 DPLOG(ERROR) << "Error waiting for process exit"; | |
98 } else if (!result) { | |
99 DPLOG(ERROR) << "Unable to terminate process"; | |
100 } | |
101 return result; | |
102 } | |
103 | |
104 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { | 92 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { |
105 DWORD tmp_exit_code = 0; | 93 DWORD tmp_exit_code = 0; |
106 | 94 |
107 if (!::GetExitCodeProcess(handle, &tmp_exit_code)) { | 95 if (!::GetExitCodeProcess(handle, &tmp_exit_code)) { |
108 DPLOG(FATAL) << "GetExitCodeProcess() failed"; | 96 DPLOG(FATAL) << "GetExitCodeProcess() failed"; |
109 if (exit_code) { | 97 if (exit_code) { |
110 // This really is a random number. We haven't received any | 98 // This really is a random number. We haven't received any |
111 // information about the exit code, presumably because this | 99 // information about the exit code, presumably because this |
112 // process doesn't have permission to get the exit code, or | 100 // process doesn't have permission to get the exit code, or |
113 // because of some other cause for GetExitCodeProcess to fail | 101 // because of some other cause for GetExitCodeProcess to fail |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 } | 189 } |
202 | 190 |
203 MessageLoop::current()->PostDelayedTask( | 191 MessageLoop::current()->PostDelayedTask( |
204 FROM_HERE, | 192 FROM_HERE, |
205 Bind(&TimerExpiredTask::TimedOut, | 193 Bind(&TimerExpiredTask::TimedOut, |
206 Owned(new TimerExpiredTask(process.Pass()))), | 194 Owned(new TimerExpiredTask(process.Pass()))), |
207 TimeDelta::FromMilliseconds(kWaitInterval)); | 195 TimeDelta::FromMilliseconds(kWaitInterval)); |
208 } | 196 } |
209 | 197 |
210 } // namespace base | 198 } // namespace base |
OLD | NEW |