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