OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 #include <crt_externs.h> | 8 #include <crt_externs.h> |
9 #include <errno.h> | 9 #include <errno.h> |
10 #include <mach/mach.h> | 10 #include <mach/mach.h> |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process }; | 63 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process }; |
64 if (sysctl(mib, 4, &info, &length, NULL, 0) < 0) { | 64 if (sysctl(mib, 4, &info, &length, NULL, 0) < 0) { |
65 DPLOG(ERROR) << "sysctl"; | 65 DPLOG(ERROR) << "sysctl"; |
66 return -1; | 66 return -1; |
67 } | 67 } |
68 if (length == 0) | 68 if (length == 0) |
69 return -1; | 69 return -1; |
70 return info.kp_eproc.e_ppid; | 70 return info.kp_eproc.e_ppid; |
71 } | 71 } |
72 | 72 |
73 namespace { | |
74 | |
75 const int kWaitBeforeKillSeconds = 2; | |
76 | |
77 // Reap |child| process. This call blocks until completion. | |
78 void BlockingReap(pid_t child) { | |
79 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, 0)); | |
80 if (result == -1) { | |
81 DPLOG(ERROR) << "waitpid(" << child << ", NULL, 0)"; | |
82 } | |
83 } | |
84 | |
85 // Waits for |timeout| seconds for the given |child| to exit and reap it. If | |
86 // the child doesn't exit within the time specified, kills it. | |
87 // | |
88 // This function takes two approaches: first, it tries to use kqueue to | |
89 // observe when the process exits. kevent can monitor a kqueue with a | |
90 // timeout, so this method is preferred to wait for a specified period of | |
91 // time. Once the kqueue indicates the process has exited, waitpid will reap | |
92 // the exited child. If the kqueue doesn't provide an exit event notification, | |
93 // before the timeout expires, or if the kqueue fails or misbehaves, the | |
94 // process will be mercilessly killed and reaped. | |
95 // | |
96 // A child process passed to this function may be in one of several states: | |
97 // running, terminated and not yet reaped, and (apparently, and unfortunately) | |
98 // terminated and already reaped. Normally, a process will at least have been | |
99 // asked to exit before this function is called, but this is not required. | |
100 // If a process is terminating and unreaped, there may be a window between the | |
101 // time that kqueue will no longer recognize it and when it becomes an actual | |
102 // zombie that a non-blocking (WNOHANG) waitpid can reap. This condition is | |
103 // detected when kqueue indicates that the process is not running and a | |
104 // non-blocking waitpid fails to reap the process but indicates that it is | |
105 // still running. In this event, a blocking attempt to reap the process | |
106 // collects the known-dying child, preventing zombies from congregating. | |
107 // | |
108 // In the event that the kqueue misbehaves entirely, as it might under a | |
109 // EMFILE condition ("too many open files", or out of file descriptors), this | |
110 // function will forcibly kill and reap the child without delay. This | |
111 // eliminates another potential zombie vector. (If you're out of file | |
112 // descriptors, you're probably deep into something else, but that doesn't | |
113 // mean that zombies be allowed to kick you while you're down.) | |
114 // | |
115 // The fact that this function seemingly can be called to wait on a child | |
116 // that's not only already terminated but already reaped is a bit of a | |
117 // problem: a reaped child's pid can be reclaimed and may refer to a distinct | |
118 // process in that case. The fact that this function can seemingly be called | |
119 // to wait on a process that's not even a child is also a problem: kqueue will | |
120 // work in that case, but waitpid won't, and killing a non-child might not be | |
121 // the best approach. | |
122 void WaitForChildToDie(pid_t child, int timeout) { | |
123 DCHECK(child > 0); | |
124 DCHECK(timeout > 0); | |
125 | |
126 // DON'T ADD ANY EARLY RETURNS TO THIS FUNCTION without ensuring that | |
127 // |child| has been reaped. Specifically, even if a kqueue, kevent, or other | |
128 // call fails, this function should fall back to the last resort of trying | |
129 // to kill and reap the process. Not observing this rule will resurrect | |
130 // zombies. | |
131 | |
132 int result; | |
133 | |
134 int kq = HANDLE_EINTR(kqueue()); | |
135 if (kq == -1) { | |
136 DPLOG(ERROR) << "kqueue()"; | |
137 } else { | |
138 file_util::ScopedFD auto_close_kq(&kq); | |
139 | |
140 struct kevent change = {0}; | |
141 EV_SET(&change, child, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); | |
142 result = HANDLE_EINTR(kevent(kq, &change, 1, NULL, 0, NULL)); | |
143 | |
144 if (result == -1) { | |
145 if (errno != ESRCH) { | |
146 DPLOG(ERROR) << "kevent (setup " << child << ")"; | |
147 } else { | |
148 // At this point, one of the following has occurred: | |
149 // 1. The process has died but has not yet been reaped. | |
150 // 2. The process has died and has already been reaped. | |
151 // 3. The process is in the process of dying. It's no longer | |
152 // kqueueable, but it may not be waitable yet either. Mark calls | |
153 // this case the "zombie death race". | |
154 | |
155 result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); | |
156 | |
157 if (result != 0) { | |
158 // A positive result indicates case 1. waitpid succeeded and reaped | |
159 // the child. A result of -1 indicates case 2. The child has already | |
160 // been reaped. In both of these cases, no further action is | |
161 // necessary. | |
162 return; | |
163 } | |
164 | |
165 // |result| is 0, indicating case 3. The process will be waitable in | |
166 // short order. Fall back out of the kqueue code to kill it (for good | |
167 // measure) and reap it. | |
168 } | |
169 } else { | |
170 // Keep track of the elapsed time to be able to restart kevent if it's | |
171 // interrupted. | |
172 TimeDelta remaining_delta = TimeDelta::FromSeconds(timeout); | |
173 TimeTicks deadline = TimeTicks::Now() + remaining_delta; | |
174 result = -1; | |
175 struct kevent event = {0}; | |
176 while (remaining_delta.InMilliseconds() > 0) { | |
177 const struct timespec remaining_timespec = remaining_delta.ToTimeSpec(); | |
178 result = kevent(kq, NULL, 0, &event, 1, &remaining_timespec); | |
179 if (result == -1 && errno == EINTR) { | |
180 remaining_delta = deadline - TimeTicks::Now(); | |
181 result = 0; | |
182 } else { | |
183 break; | |
184 } | |
185 } | |
186 | |
187 if (result == -1) { | |
188 DPLOG(ERROR) << "kevent (wait " << child << ")"; | |
189 } else if (result > 1) { | |
190 DLOG(ERROR) << "kevent (wait " << child << "): unexpected result " | |
191 << result; | |
192 } else if (result == 1) { | |
193 if ((event.fflags & NOTE_EXIT) && | |
194 (event.ident == static_cast<uintptr_t>(child))) { | |
195 // The process is dead or dying. This won't block for long, if at | |
196 // all. | |
197 BlockingReap(child); | |
198 return; | |
199 } else { | |
200 DLOG(ERROR) << "kevent (wait " << child | |
201 << "): unexpected event: fflags=" << event.fflags | |
202 << ", ident=" << event.ident; | |
203 } | |
204 } | |
205 } | |
206 } | |
207 | |
208 // The child is still alive, or is very freshly dead. Be sure by sending it | |
209 // a signal. This is safe even if it's freshly dead, because it will be a | |
210 // zombie (or on the way to zombiedom) and kill will return 0 even if the | |
211 // signal is not delivered to a live process. | |
212 result = kill(child, SIGKILL); | |
213 if (result == -1) { | |
214 DPLOG(ERROR) << "kill(" << child << ", SIGKILL)"; | |
215 } else { | |
216 // The child is definitely on the way out now. BlockingReap won't need to | |
217 // wait for long, if at all. | |
218 BlockingReap(child); | |
219 } | |
220 } | |
221 | |
222 } // namespace | |
223 | |
224 void EnsureProcessTerminated(ProcessHandle process) { | |
225 WaitForChildToDie(process, kWaitBeforeKillSeconds); | |
226 } | |
227 | |
228 } // namespace base | 73 } // namespace base |
OLD | NEW |