xref: /libevent-2.1.12/kqueue.c (revision cb9da0bf)
1 /*	$OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $	*/
2 
3 /*
4  * Copyright 2000-2007 Niels Provos <[email protected]>
5  * Copyright 2007-2012 Niels Provos and Nick Mathewson
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "event2/event-config.h"
30 #include "evconfig-private.h"
31 
32 #ifdef EVENT__HAVE_KQUEUE
33 
34 #include <sys/types.h>
35 #ifdef EVENT__HAVE_SYS_TIME_H
36 #include <sys/time.h>
37 #endif
38 #include <sys/queue.h>
39 #include <sys/event.h>
40 #include <signal.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #ifdef EVENT__HAVE_INTTYPES_H
47 #include <inttypes.h>
48 #endif
49 
50 /* Some platforms apparently define the udata field of struct kevent as
51  * intptr_t, whereas others define it as void*.  There doesn't seem to be an
52  * easy way to tell them apart via autoconf, so we need to use OS macros. */
53 #if defined(EVENT__HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__)
54 #define PTR_TO_UDATA(x)	((intptr_t)(x))
55 #define INT_TO_UDATA(x) ((intptr_t)(x))
56 #else
57 #define PTR_TO_UDATA(x)	(x)
58 #define INT_TO_UDATA(x) ((void*)(x))
59 #endif
60 
61 #include "event-internal.h"
62 #include "log-internal.h"
63 #include "evmap-internal.h"
64 #include "event2/thread.h"
65 #include "evthread-internal.h"
66 #include "changelist-internal.h"
67 
68 #define NEVENT		64
69 
70 struct kqop {
71 	struct kevent *changes;
72 	int changes_size;
73 
74 	struct kevent *events;
75 	int events_size;
76 	int kq;
77 	pid_t pid;
78 };
79 
80 static void kqop_free(struct kqop *kqop);
81 
82 static void *kq_init(struct event_base *);
83 static int kq_sig_add(struct event_base *, int, short, short, void *);
84 static int kq_sig_del(struct event_base *, int, short, short, void *);
85 static int kq_dispatch(struct event_base *, struct timeval *);
86 static void kq_dealloc(struct event_base *);
87 
88 const struct eventop kqops = {
89 	"kqueue",
90 	kq_init,
91 	event_changelist_add,
92 	event_changelist_del,
93 	kq_dispatch,
94 	kq_dealloc,
95 	1 /* need reinit */,
96     EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS,
97 	EVENT_CHANGELIST_FDINFO_SIZE
98 };
99 
100 static const struct eventop kqsigops = {
101 	"kqueue_signal",
102 	NULL,
103 	kq_sig_add,
104 	kq_sig_del,
105 	NULL,
106 	NULL,
107 	1 /* need reinit */,
108 	0,
109 	0
110 };
111 
112 static void *
113 kq_init(struct event_base *base)
114 {
115 	int kq = -1;
116 	struct kqop *kqueueop = NULL;
117 
118 	if (!(kqueueop = mm_calloc(1, sizeof(struct kqop))))
119 		return (NULL);
120 
121 /* Initialize the kernel queue */
122 
123 	if ((kq = kqueue()) == -1) {
124 		event_warn("kqueue");
125 		goto err;
126 	}
127 
128 	kqueueop->kq = kq;
129 
130 	kqueueop->pid = getpid();
131 
132 	/* Initialize fields */
133 	kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent));
134 	if (kqueueop->changes == NULL)
135 		goto err;
136 	kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent));
137 	if (kqueueop->events == NULL)
138 		goto err;
139 	kqueueop->events_size = kqueueop->changes_size = NEVENT;
140 
141 	/* Check for Mac OS X kqueue bug. */
142 	memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]);
143 	kqueueop->changes[0].ident = -1;
144 	kqueueop->changes[0].filter = EVFILT_READ;
145 	kqueueop->changes[0].flags = EV_ADD;
146 	/*
147 	 * If kqueue works, then kevent will succeed, and it will
148 	 * stick an error in events[0].  If kqueue is broken, then
149 	 * kevent will fail.
150 	 */
151 	if (kevent(kq,
152 		kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
153 	    (int)kqueueop->events[0].ident != -1 ||
154 	    kqueueop->events[0].flags != EV_ERROR) {
155 		event_warn("%s: detected broken kqueue; not using.", __func__);
156 		goto err;
157 	}
158 
159 	base->evsigsel = &kqsigops;
160 
161 	return (kqueueop);
162 err:
163 	if (kqueueop)
164 		kqop_free(kqueueop);
165 
166 	return (NULL);
167 }
168 
169 #define ADD_UDATA 0x30303
170 
171 static void
172 kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change)
173 {
174 	memset(out, 0, sizeof(out));
175 	out->ident = fd;
176 	out->filter = filter;
177 
178 	if (change & EV_CHANGE_ADD) {
179 		out->flags = EV_ADD;
180 		/* We set a magic number here so that we can tell 'add'
181 		 * errors from 'del' errors. */
182 		out->udata = INT_TO_UDATA(ADD_UDATA);
183 		if (change & EV_ET)
184 			out->flags |= EV_CLEAR;
185 #ifdef NOTE_EOF
186 		/* Make it behave like select() and poll() */
187 		if (filter == EVFILT_READ)
188 			out->fflags = NOTE_EOF;
189 #endif
190 	} else {
191 		EVUTIL_ASSERT(change & EV_CHANGE_DEL);
192 		out->flags = EV_DELETE;
193 	}
194 }
195 
196 static int
197 kq_build_changes_list(const struct event_changelist *changelist,
198     struct kqop *kqop)
199 {
200 	int i;
201 	int n_changes = 0;
202 
203 	for (i = 0; i < changelist->n_changes; ++i) {
204 		struct event_change *in_ch = &changelist->changes[i];
205 		struct kevent *out_ch;
206 		if (n_changes >= kqop->changes_size - 1) {
207 			int newsize = kqop->changes_size * 2;
208 			struct kevent *newchanges;
209 
210 			newchanges = mm_realloc(kqop->changes,
211 			    newsize * sizeof(struct kevent));
212 			if (newchanges == NULL) {
213 				event_warn("%s: realloc", __func__);
214 				return (-1);
215 			}
216 			kqop->changes = newchanges;
217 			kqop->changes_size = newsize;
218 		}
219 		if (in_ch->read_change) {
220 			out_ch = &kqop->changes[n_changes++];
221 			kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ,
222 			    in_ch->read_change);
223 		}
224 		if (in_ch->write_change) {
225 			out_ch = &kqop->changes[n_changes++];
226 			kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE,
227 			    in_ch->write_change);
228 		}
229 	}
230 	return n_changes;
231 }
232 
233 static int
234 kq_grow_events(struct kqop *kqop, size_t new_size)
235 {
236 	struct kevent *newresult;
237 
238 	newresult = mm_realloc(kqop->events,
239 	    new_size * sizeof(struct kevent));
240 
241 	if (newresult) {
242 		kqop->events = newresult;
243 		kqop->events_size = new_size;
244 		return 0;
245 	} else {
246 		return -1;
247 	}
248 }
249 
250 static int
251 kq_dispatch(struct event_base *base, struct timeval *tv)
252 {
253 	struct kqop *kqop = base->evbase;
254 	struct kevent *events = kqop->events;
255 	struct kevent *changes;
256 	struct timespec ts, *ts_p = NULL;
257 	int i, n_changes, res;
258 
259 	if (tv != NULL) {
260 		TIMEVAL_TO_TIMESPEC(tv, &ts);
261 		ts_p = &ts;
262 	}
263 
264 	/* Build "changes" from "base->changes" */
265 	EVUTIL_ASSERT(kqop->changes);
266 	n_changes = kq_build_changes_list(&base->changelist, kqop);
267 	if (n_changes < 0)
268 		return -1;
269 
270 	event_changelist_remove_all(&base->changelist, base);
271 
272 	/* steal the changes array in case some broken code tries to call
273 	 * dispatch twice at once. */
274 	changes = kqop->changes;
275 	kqop->changes = NULL;
276 
277 	/* Make sure that 'events' is at least as long as the list of changes:
278 	 * otherwise errors in the changes can get reported as a -1 return
279 	 * value from kevent() rather than as EV_ERROR events in the events
280 	 * array.
281 	 *
282 	 * (We could instead handle -1 return values from kevent() by
283 	 * retrying with a smaller changes array or a larger events array,
284 	 * but this approach seems less risky for now.)
285 	 */
286 	if (kqop->events_size < n_changes) {
287 		int new_size = kqop->events_size;
288 		do {
289 			new_size *= 2;
290 		} while (new_size < n_changes);
291 
292 		kq_grow_events(kqop, new_size);
293 		events = kqop->events;
294 	}
295 
296 	EVBASE_RELEASE_LOCK(base, th_base_lock);
297 
298 	res = kevent(kqop->kq, changes, n_changes,
299 	    events, kqop->events_size, ts_p);
300 
301 	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
302 
303 	EVUTIL_ASSERT(kqop->changes == NULL);
304 	kqop->changes = changes;
305 
306 	if (res == -1) {
307 		if (errno != EINTR) {
308 			event_warn("kevent");
309 			return (-1);
310 		}
311 
312 		return (0);
313 	}
314 
315 	event_debug(("%s: kevent reports %d", __func__, res));
316 
317 	for (i = 0; i < res; i++) {
318 		int which = 0;
319 
320 		if (events[i].flags & EV_ERROR) {
321 			switch (events[i].data) {
322 
323 			/* Can occur on delete if we are not currently
324 			 * watching any events on this fd.  That can
325 			 * happen when the fd was closed and another
326 			 * file was opened with that fd. */
327 			case ENOENT:
328 			/* Can occur for reasons not fully understood
329 			 * on FreeBSD. */
330 			case EINVAL:
331 				continue;
332 
333 			/* Can occur on a delete if the fd is closed. */
334 			case EBADF:
335 				/* XXXX On NetBSD, we can also get EBADF if we
336 				 * try to add the write side of a pipe, but
337 				 * the read side has already been closed.
338 				 * Other BSDs call this situation 'EPIPE'. It
339 				 * would be good if we had a way to report
340 				 * this situation. */
341 				continue;
342 			/* These two can occur on an add if the fd was one side
343 			 * of a pipe, and the other side was closed. */
344 			case EPERM:
345 			case EPIPE:
346 				/* Report read events, if we're listening for
347 				 * them, so that the user can learn about any
348 				 * add errors.  (If the operation was a
349 				 * delete, then udata should be cleared.) */
350 				if (events[i].udata) {
351 					/* The operation was an add:
352 					 * report the error as a read. */
353 					which |= EV_READ;
354 					break;
355 				} else {
356 					/* The operation was a del:
357 					 * report nothing. */
358 					continue;
359 				}
360 
361 			/* Other errors shouldn't occur. */
362 			default:
363 				errno = events[i].data;
364 				return (-1);
365 			}
366 		} else if (events[i].filter == EVFILT_READ) {
367 			which |= EV_READ;
368 		} else if (events[i].filter == EVFILT_WRITE) {
369 			which |= EV_WRITE;
370 		} else if (events[i].filter == EVFILT_SIGNAL) {
371 			which |= EV_SIGNAL;
372 		}
373 
374 		if (!which)
375 			continue;
376 
377 		if (events[i].filter == EVFILT_SIGNAL) {
378 			evmap_signal_active(base, events[i].ident, 1);
379 		} else {
380 			evmap_io_active(base, events[i].ident, which | EV_ET);
381 		}
382 	}
383 
384 	if (res == kqop->events_size) {
385 		/* We used all the events space that we have. Maybe we should
386 		   make it bigger. */
387 		kq_grow_events(kqop, kqop->events_size * 2);
388 	}
389 
390 	return (0);
391 }
392 
393 static void
394 kqop_free(struct kqop *kqop)
395 {
396 	if (kqop->changes)
397 		mm_free(kqop->changes);
398 	if (kqop->events)
399 		mm_free(kqop->events);
400 	if (kqop->kq >= 0 && kqop->pid == getpid())
401 		close(kqop->kq);
402 	memset(kqop, 0, sizeof(struct kqop));
403 	mm_free(kqop);
404 }
405 
406 static void
407 kq_dealloc(struct event_base *base)
408 {
409 	struct kqop *kqop = base->evbase;
410 	evsig_dealloc(base);
411 	kqop_free(kqop);
412 }
413 
414 /* signal handling */
415 static int
416 kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p)
417 {
418 	struct kqop *kqop = base->evbase;
419 	struct kevent kev;
420 	struct timespec timeout = { 0, 0 };
421 	(void)p;
422 
423 	EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
424 
425 	memset(&kev, 0, sizeof(kev));
426 	kev.ident = nsignal;
427 	kev.filter = EVFILT_SIGNAL;
428 	kev.flags = EV_ADD;
429 
430 	/* Be ready for the signal if it is sent any
431 	 * time between now and the next call to
432 	 * kq_dispatch. */
433 	if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
434 		return (-1);
435 
436         /* We can set the handler for most signals to SIG_IGN and
437          * still have them reported to us in the queue.  However,
438          * if the handler for SIGCHLD is SIG_IGN, the system reaps
439          * zombie processes for us, and we don't get any notification.
440          * This appears to be the only signal with this quirk. */
441 	if (evsig_set_handler_(base, nsignal,
442                                nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1)
443 		return (-1);
444 
445 	return (0);
446 }
447 
448 static int
449 kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p)
450 {
451 	struct kqop *kqop = base->evbase;
452 	struct kevent kev;
453 
454 	struct timespec timeout = { 0, 0 };
455 	(void)p;
456 
457 	EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
458 
459 	memset(&kev, 0, sizeof(kev));
460 	kev.ident = nsignal;
461 	kev.filter = EVFILT_SIGNAL;
462 	kev.flags = EV_DELETE;
463 
464 	/* Because we insert signal events
465 	 * immediately, we need to delete them
466 	 * immediately, too */
467 	if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
468 		return (-1);
469 
470 	if (evsig_restore_handler_(base, nsignal) == -1)
471 		return (-1);
472 
473 	return (0);
474 }
475 
476 #endif /* EVENT__HAVE_KQUEUE */
477