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 <signal.h> | 7 #include <signal.h> |
8 #include <sys/event.h> | 8 #include <sys/event.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #include <sys/wait.h> | 10 #include <sys/wait.h> |
11 | 11 |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/files/scoped_file.h" | |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
16 | 15 |
17 namespace base { | 16 namespace base { |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 const int kWaitBeforeKillSeconds = 2; | 20 const int kWaitBeforeKillSeconds = 2; |
22 | 21 |
23 // Reap |child| process. This call blocks until completion. | 22 // Reap |child| process. This call blocks until completion. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 DCHECK(timeout > 0); | 69 DCHECK(timeout > 0); |
71 | 70 |
72 // DON'T ADD ANY EARLY RETURNS TO THIS FUNCTION without ensuring that | 71 // DON'T ADD ANY EARLY RETURNS TO THIS FUNCTION without ensuring that |
73 // |child| has been reaped. Specifically, even if a kqueue, kevent, or other | 72 // |child| has been reaped. Specifically, even if a kqueue, kevent, or other |
74 // call fails, this function should fall back to the last resort of trying | 73 // call fails, this function should fall back to the last resort of trying |
75 // to kill and reap the process. Not observing this rule will resurrect | 74 // to kill and reap the process. Not observing this rule will resurrect |
76 // zombies. | 75 // zombies. |
77 | 76 |
78 int result; | 77 int result; |
79 | 78 |
80 ScopedFD kq(HANDLE_EINTR(kqueue())); | 79 int kq = HANDLE_EINTR(kqueue()); |
81 if (!kq.is_valid()) { | 80 if (kq == -1) { |
82 DPLOG(ERROR) << "kqueue()"; | 81 DPLOG(ERROR) << "kqueue()"; |
83 } else { | 82 } else { |
| 83 file_util::ScopedFD auto_close_kq(&kq); |
| 84 |
84 struct kevent change = {0}; | 85 struct kevent change = {0}; |
85 EV_SET(&change, child, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); | 86 EV_SET(&change, child, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); |
86 result = HANDLE_EINTR(kevent(kq.get(), &change, 1, NULL, 0, NULL)); | 87 result = HANDLE_EINTR(kevent(kq, &change, 1, NULL, 0, NULL)); |
87 | 88 |
88 if (result == -1) { | 89 if (result == -1) { |
89 if (errno != ESRCH) { | 90 if (errno != ESRCH) { |
90 DPLOG(ERROR) << "kevent (setup " << child << ")"; | 91 DPLOG(ERROR) << "kevent (setup " << child << ")"; |
91 } else { | 92 } else { |
92 // At this point, one of the following has occurred: | 93 // At this point, one of the following has occurred: |
93 // 1. The process has died but has not yet been reaped. | 94 // 1. The process has died but has not yet been reaped. |
94 // 2. The process has died and has already been reaped. | 95 // 2. The process has died and has already been reaped. |
95 // 3. The process is in the process of dying. It's no longer | 96 // 3. The process is in the process of dying. It's no longer |
96 // kqueueable, but it may not be waitable yet either. Mark calls | 97 // kqueueable, but it may not be waitable yet either. Mark calls |
(...skipping 15 matching lines...) Expand all Loading... |
112 } | 113 } |
113 } else { | 114 } else { |
114 // Keep track of the elapsed time to be able to restart kevent if it's | 115 // Keep track of the elapsed time to be able to restart kevent if it's |
115 // interrupted. | 116 // interrupted. |
116 TimeDelta remaining_delta = TimeDelta::FromSeconds(timeout); | 117 TimeDelta remaining_delta = TimeDelta::FromSeconds(timeout); |
117 TimeTicks deadline = TimeTicks::Now() + remaining_delta; | 118 TimeTicks deadline = TimeTicks::Now() + remaining_delta; |
118 result = -1; | 119 result = -1; |
119 struct kevent event = {0}; | 120 struct kevent event = {0}; |
120 while (remaining_delta.InMilliseconds() > 0) { | 121 while (remaining_delta.InMilliseconds() > 0) { |
121 const struct timespec remaining_timespec = remaining_delta.ToTimeSpec(); | 122 const struct timespec remaining_timespec = remaining_delta.ToTimeSpec(); |
122 result = kevent(kq.get(), NULL, 0, &event, 1, &remaining_timespec); | 123 result = kevent(kq, NULL, 0, &event, 1, &remaining_timespec); |
123 if (result == -1 && errno == EINTR) { | 124 if (result == -1 && errno == EINTR) { |
124 remaining_delta = deadline - TimeTicks::Now(); | 125 remaining_delta = deadline - TimeTicks::Now(); |
125 result = 0; | 126 result = 0; |
126 } else { | 127 } else { |
127 break; | 128 break; |
128 } | 129 } |
129 } | 130 } |
130 | 131 |
131 if (result == -1) { | 132 if (result == -1) { |
132 DPLOG(ERROR) << "kevent (wait " << child << ")"; | 133 DPLOG(ERROR) << "kevent (wait " << child << ")"; |
(...skipping 30 matching lines...) Expand all Loading... |
163 } | 164 } |
164 } | 165 } |
165 | 166 |
166 } // namespace | 167 } // namespace |
167 | 168 |
168 void EnsureProcessTerminated(ProcessHandle process) { | 169 void EnsureProcessTerminated(ProcessHandle process) { |
169 WaitForChildToDie(process, kWaitBeforeKillSeconds); | 170 WaitForChildToDie(process, kWaitBeforeKillSeconds); |
170 } | 171 } |
171 | 172 |
172 } // namespace base | 173 } // namespace base |
OLD | NEW |