1 /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */
4 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5 * Copyright 2007-2012 Niels Provos and Nick Mathewson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include "event2/event-config.h"
30 #include "evconfig-private.h"
32 #ifdef EVENT__HAVE_KQUEUE
34 #include <sys/types.h>
35 #ifdef EVENT__HAVE_SYS_TIME_H
38 #include <sys/queue.h>
39 #include <sys/event.h>
47 #ifdef EVENT__HAVE_INTTYPES_H
51 /* Some platforms apparently define the udata field of struct kevent as
52 * intptr_t, whereas others define it as void*. There doesn't seem to be an
53 * easy way to tell them apart via autoconf, so we need to use OS macros. */
54 #if defined(EVENT__HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) && !defined(__CloudABI__)
55 #define PTR_TO_UDATA(x) ((intptr_t)(x))
56 #define INT_TO_UDATA(x) ((intptr_t)(x))
58 #define PTR_TO_UDATA(x) (x)
59 #define INT_TO_UDATA(x) ((void*)(x))
62 #include "event-internal.h"
63 #include "log-internal.h"
64 #include "evmap-internal.h"
65 #include "event2/thread.h"
66 #include "event2/util.h"
67 #include "evthread-internal.h"
68 #include "changelist-internal.h"
70 #include "kqueue-internal.h"
75 struct kevent *changes;
78 struct kevent *events;
81 int notify_event_added;
85 static void kqop_free(struct kqop *kqop);
87 static void *kq_init(struct event_base *);
88 static int kq_sig_add(struct event_base *, int, short, short, void *);
89 static int kq_sig_del(struct event_base *, int, short, short, void *);
90 static int kq_dispatch(struct event_base *, struct timeval *);
91 static void kq_dealloc(struct event_base *);
93 const struct eventop kqops = {
96 event_changelist_add_,
97 event_changelist_del_,
101 EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS,
102 EVENT_CHANGELIST_FDINFO_SIZE
105 static const struct eventop kqsigops = {
118 kq_init(struct event_base *base)
121 struct kqop *kqueueop = NULL;
123 if (!(kqueueop = mm_calloc(1, sizeof(struct kqop))))
126 /* Initialize the kernel queue */
128 if ((kq = kqueue()) == -1) {
129 event_warn("kqueue");
135 kqueueop->pid = getpid();
137 /* Initialize fields */
138 kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent));
139 if (kqueueop->changes == NULL)
141 kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent));
142 if (kqueueop->events == NULL)
144 kqueueop->events_size = kqueueop->changes_size = NEVENT;
146 /* Check for Mac OS X kqueue bug. */
147 memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]);
148 kqueueop->changes[0].ident = -1;
149 kqueueop->changes[0].filter = EVFILT_READ;
150 kqueueop->changes[0].flags = EV_ADD;
152 * If kqueue works, then kevent will succeed, and it will
153 * stick an error in events[0]. If kqueue is broken, then
157 kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
158 (int)kqueueop->events[0].ident != -1 ||
159 !(kqueueop->events[0].flags & EV_ERROR)) {
160 event_warn("%s: detected broken kqueue; not using.", __func__);
164 base->evsigsel = &kqsigops;
174 #define ADD_UDATA 0x30303
177 kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change)
179 memset(out, 0, sizeof(struct kevent));
181 out->filter = filter;
183 if (change & EV_CHANGE_ADD) {
185 /* We set a magic number here so that we can tell 'add'
186 * errors from 'del' errors. */
187 out->udata = INT_TO_UDATA(ADD_UDATA);
189 out->flags |= EV_CLEAR;
191 /* Make it behave like select() and poll() */
192 if (filter == EVFILT_READ)
193 out->fflags = NOTE_EOF;
196 EVUTIL_ASSERT(change & EV_CHANGE_DEL);
197 out->flags = EV_DELETE;
202 kq_build_changes_list(const struct event_changelist *changelist,
208 for (i = 0; i < changelist->n_changes; ++i) {
209 struct event_change *in_ch = &changelist->changes[i];
210 struct kevent *out_ch;
211 if (n_changes >= kqop->changes_size - 1) {
213 struct kevent *newchanges;
215 if (kqop->changes_size > INT_MAX / 2 ||
216 (size_t)kqop->changes_size * 2 > EV_SIZE_MAX /
217 sizeof(struct kevent)) {
218 event_warnx("%s: int overflow", __func__);
222 newsize = kqop->changes_size * 2;
223 newchanges = mm_realloc(kqop->changes,
224 newsize * sizeof(struct kevent));
225 if (newchanges == NULL) {
226 event_warn("%s: realloc", __func__);
229 kqop->changes = newchanges;
230 kqop->changes_size = newsize;
232 if (in_ch->read_change) {
233 out_ch = &kqop->changes[n_changes++];
234 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ,
237 if (in_ch->write_change) {
238 out_ch = &kqop->changes[n_changes++];
239 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE,
240 in_ch->write_change);
247 kq_grow_events(struct kqop *kqop, size_t new_size)
249 struct kevent *newresult;
251 newresult = mm_realloc(kqop->events,
252 new_size * sizeof(struct kevent));
255 kqop->events = newresult;
256 kqop->events_size = new_size;
264 kq_dispatch(struct event_base *base, struct timeval *tv)
266 struct kqop *kqop = base->evbase;
267 struct kevent *events = kqop->events;
268 struct kevent *changes;
269 struct timespec ts, *ts_p = NULL;
270 int i, n_changes, res;
273 ts.tv_sec = tv->tv_sec;
274 ts.tv_nsec = tv->tv_usec * 1000;
278 /* Build "changes" from "base->changes" */
279 EVUTIL_ASSERT(kqop->changes);
280 n_changes = kq_build_changes_list(&base->changelist, kqop);
284 event_changelist_remove_all_(&base->changelist, base);
286 /* steal the changes array in case some broken code tries to call
287 * dispatch twice at once. */
288 changes = kqop->changes;
289 kqop->changes = NULL;
291 /* Make sure that 'events' is at least as long as the list of changes:
292 * otherwise errors in the changes can get reported as a -1 return
293 * value from kevent() rather than as EV_ERROR events in the events
296 * (We could instead handle -1 return values from kevent() by
297 * retrying with a smaller changes array or a larger events array,
298 * but this approach seems less risky for now.)
300 if (kqop->events_size < n_changes) {
301 int new_size = kqop->events_size;
304 } while (new_size < n_changes);
306 kq_grow_events(kqop, new_size);
307 events = kqop->events;
310 EVBASE_RELEASE_LOCK(base, th_base_lock);
312 res = kevent(kqop->kq, changes, n_changes,
313 events, kqop->events_size, ts_p);
315 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
317 EVUTIL_ASSERT(kqop->changes == NULL);
318 kqop->changes = changes;
321 if (errno != EINTR) {
322 event_warn("kevent");
329 event_debug(("%s: kevent reports %d", __func__, res));
331 for (i = 0; i < res; i++) {
334 if (events[i].flags & EV_ERROR) {
335 switch (events[i].data) {
337 /* Can occur on delete if we are not currently
338 * watching any events on this fd. That can
339 * happen when the fd was closed and another
340 * file was opened with that fd. */
342 /* Can occur for reasons not fully understood
346 #if defined(__FreeBSD__)
348 * This currently occurs if an FD is closed
349 * before the EV_DELETE makes it out via kevent().
350 * The FreeBSD capabilities code sees the blank
351 * capability set and rejects the request to
354 * To be strictly correct - when an FD is closed,
355 * all the registered events are also removed.
356 * Queuing EV_DELETE to a closed FD is wrong.
357 * The event(s) should just be deleted from
358 * the pending changelist.
364 /* Can occur on a delete if the fd is closed. */
366 /* XXXX On NetBSD, we can also get EBADF if we
367 * try to add the write side of a pipe, but
368 * the read side has already been closed.
369 * Other BSDs call this situation 'EPIPE'. It
370 * would be good if we had a way to report
373 /* These two can occur on an add if the fd was one side
374 * of a pipe, and the other side was closed. */
377 /* Report read events, if we're listening for
378 * them, so that the user can learn about any
379 * add errors. (If the operation was a
380 * delete, then udata should be cleared.) */
381 if (events[i].udata) {
382 /* The operation was an add:
383 * report the error as a read. */
387 /* The operation was a del:
392 /* Other errors shouldn't occur. */
394 errno = events[i].data;
397 } else if (events[i].filter == EVFILT_READ) {
399 } else if (events[i].filter == EVFILT_WRITE) {
401 } else if (events[i].filter == EVFILT_SIGNAL) {
404 } else if (events[i].filter == EVFILT_USER) {
405 base->is_notify_pending = 0;
412 if (events[i].filter == EVFILT_SIGNAL) {
413 evmap_signal_active_(base, events[i].ident, 1);
415 evmap_io_active_(base, events[i].ident, which | EV_ET);
419 if (res == kqop->events_size) {
420 /* We used all the events space that we have. Maybe we should
422 kq_grow_events(kqop, kqop->events_size * 2);
429 kqop_free(struct kqop *kqop)
432 mm_free(kqop->changes);
434 mm_free(kqop->events);
435 if (kqop->kq >= 0 && kqop->pid == getpid())
437 memset(kqop, 0, sizeof(struct kqop));
442 kq_dealloc(struct event_base *base)
444 struct kqop *kqop = base->evbase;
445 evsig_dealloc_(base);
449 /* signal handling */
451 kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p)
453 struct kqop *kqop = base->evbase;
455 struct timespec timeout = { 0, 0 };
458 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
460 memset(&kev, 0, sizeof(kev));
462 kev.filter = EVFILT_SIGNAL;
465 /* Be ready for the signal if it is sent any
466 * time between now and the next call to
468 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
471 /* We can set the handler for most signals to SIG_IGN and
472 * still have them reported to us in the queue. However,
473 * if the handler for SIGCHLD is SIG_IGN, the system reaps
474 * zombie processes for us, and we don't get any notification.
475 * This appears to be the only signal with this quirk. */
476 if (evsig_set_handler_(base, nsignal,
477 nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1)
484 kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p)
486 struct kqop *kqop = base->evbase;
489 struct timespec timeout = { 0, 0 };
492 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
494 memset(&kev, 0, sizeof(kev));
496 kev.filter = EVFILT_SIGNAL;
497 kev.flags = EV_DELETE;
499 /* Because we insert signal events
500 * immediately, we need to delete them
501 * immediately, too */
502 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
505 if (evsig_restore_handler_(base, nsignal) == -1)
512 /* OSX 10.6 and FreeBSD 8.1 add support for EVFILT_USER, which we can use
513 * to wake up the event loop from another thread. */
515 /* Magic number we use for our filter ID. */
516 #define NOTIFY_IDENT 42
519 event_kq_add_notify_event_(struct event_base *base)
521 struct kqop *kqop = base->evbase;
522 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
524 struct timespec timeout = { 0, 0 };
527 if (kqop->notify_event_added)
530 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
531 memset(&kev, 0, sizeof(kev));
532 kev.ident = NOTIFY_IDENT;
533 kev.filter = EVFILT_USER;
534 kev.flags = EV_ADD | EV_CLEAR;
536 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
537 event_warn("kevent: adding EVFILT_USER event");
541 kqop->notify_event_added = 1;
550 event_kq_notify_base_(struct event_base *base)
552 struct kqop *kqop = base->evbase;
553 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
555 struct timespec timeout = { 0, 0 };
557 if (! kqop->notify_event_added)
560 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
561 memset(&kev, 0, sizeof(kev));
562 kev.ident = NOTIFY_IDENT;
563 kev.filter = EVFILT_USER;
564 kev.fflags = NOTE_TRIGGER;
566 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
567 event_warn("kevent: triggering EVFILT_USER event");
577 #endif /* EVENT__HAVE_KQUEUE */