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>
46 #ifdef EVENT__HAVE_INTTYPES_H
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__) && !defined(__CloudABI__)
54 #define PTR_TO_UDATA(x) ((intptr_t)(x))
55 #define INT_TO_UDATA(x) ((intptr_t)(x))
57 #define PTR_TO_UDATA(x) (x)
58 #define INT_TO_UDATA(x) ((void*)(x))
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"
68 #include "kqueue-internal.h"
73 struct kevent *changes;
76 struct kevent *events;
79 int notify_event_added;
83 static void kqop_free(struct kqop *kqop);
85 static void *kq_init(struct event_base *);
86 static int kq_sig_add(struct event_base *, int, short, short, void *);
87 static int kq_sig_del(struct event_base *, int, short, short, void *);
88 static int kq_dispatch(struct event_base *, struct timeval *);
89 static void kq_dealloc(struct event_base *);
91 const struct eventop kqops = {
94 event_changelist_add_,
95 event_changelist_del_,
99 EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS,
100 EVENT_CHANGELIST_FDINFO_SIZE
103 static const struct eventop kqsigops = {
116 kq_init(struct event_base *base)
119 struct kqop *kqueueop = NULL;
121 if (!(kqueueop = mm_calloc(1, sizeof(struct kqop))))
124 /* Initialize the kernel queue */
126 if ((kq = kqueue()) == -1) {
127 event_warn("kqueue");
133 kqueueop->pid = getpid();
135 /* Initialize fields */
136 kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent));
137 if (kqueueop->changes == NULL)
139 kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent));
140 if (kqueueop->events == NULL)
142 kqueueop->events_size = kqueueop->changes_size = NEVENT;
144 /* Check for Mac OS X kqueue bug. */
145 memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]);
146 kqueueop->changes[0].ident = -1;
147 kqueueop->changes[0].filter = EVFILT_READ;
148 kqueueop->changes[0].flags = EV_ADD;
150 * If kqueue works, then kevent will succeed, and it will
151 * stick an error in events[0]. If kqueue is broken, then
155 kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
156 (int)kqueueop->events[0].ident != -1 ||
157 !(kqueueop->events[0].flags & EV_ERROR)) {
158 event_warn("%s: detected broken kqueue; not using.", __func__);
162 base->evsigsel = &kqsigops;
172 #define ADD_UDATA 0x30303
175 kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change)
177 memset(out, 0, sizeof(struct kevent));
179 out->filter = filter;
181 if (change & EV_CHANGE_ADD) {
183 /* We set a magic number here so that we can tell 'add'
184 * errors from 'del' errors. */
185 out->udata = INT_TO_UDATA(ADD_UDATA);
187 out->flags |= EV_CLEAR;
189 /* Make it behave like select() and poll() */
190 if (filter == EVFILT_READ)
191 out->fflags = NOTE_EOF;
194 EVUTIL_ASSERT(change & EV_CHANGE_DEL);
195 out->flags = EV_DELETE;
200 kq_build_changes_list(const struct event_changelist *changelist,
206 for (i = 0; i < changelist->n_changes; ++i) {
207 struct event_change *in_ch = &changelist->changes[i];
208 struct kevent *out_ch;
209 if (n_changes >= kqop->changes_size - 1) {
210 int newsize = kqop->changes_size * 2;
211 struct kevent *newchanges;
213 newchanges = mm_realloc(kqop->changes,
214 newsize * sizeof(struct kevent));
215 if (newchanges == NULL) {
216 event_warn("%s: realloc", __func__);
219 kqop->changes = newchanges;
220 kqop->changes_size = newsize;
222 if (in_ch->read_change) {
223 out_ch = &kqop->changes[n_changes++];
224 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ,
227 if (in_ch->write_change) {
228 out_ch = &kqop->changes[n_changes++];
229 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE,
230 in_ch->write_change);
237 kq_grow_events(struct kqop *kqop, size_t new_size)
239 struct kevent *newresult;
241 newresult = mm_realloc(kqop->events,
242 new_size * sizeof(struct kevent));
245 kqop->events = newresult;
246 kqop->events_size = new_size;
254 kq_dispatch(struct event_base *base, struct timeval *tv)
256 struct kqop *kqop = base->evbase;
257 struct kevent *events = kqop->events;
258 struct kevent *changes;
259 struct timespec ts, *ts_p = NULL;
260 int i, n_changes, res;
263 ts.tv_sec = tv->tv_sec;
264 ts.tv_nsec = tv->tv_usec * 1000;
268 /* Build "changes" from "base->changes" */
269 EVUTIL_ASSERT(kqop->changes);
270 n_changes = kq_build_changes_list(&base->changelist, kqop);
274 event_changelist_remove_all_(&base->changelist, base);
276 /* steal the changes array in case some broken code tries to call
277 * dispatch twice at once. */
278 changes = kqop->changes;
279 kqop->changes = NULL;
281 /* Make sure that 'events' is at least as long as the list of changes:
282 * otherwise errors in the changes can get reported as a -1 return
283 * value from kevent() rather than as EV_ERROR events in the events
286 * (We could instead handle -1 return values from kevent() by
287 * retrying with a smaller changes array or a larger events array,
288 * but this approach seems less risky for now.)
290 if (kqop->events_size < n_changes) {
291 int new_size = kqop->events_size;
294 } while (new_size < n_changes);
296 kq_grow_events(kqop, new_size);
297 events = kqop->events;
300 EVBASE_RELEASE_LOCK(base, th_base_lock);
302 res = kevent(kqop->kq, changes, n_changes,
303 events, kqop->events_size, ts_p);
305 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
307 EVUTIL_ASSERT(kqop->changes == NULL);
308 kqop->changes = changes;
311 if (errno != EINTR) {
312 event_warn("kevent");
319 event_debug(("%s: kevent reports %d", __func__, res));
321 for (i = 0; i < res; i++) {
324 if (events[i].flags & EV_ERROR) {
325 switch (events[i].data) {
327 /* Can occur on delete if we are not currently
328 * watching any events on this fd. That can
329 * happen when the fd was closed and another
330 * file was opened with that fd. */
332 /* Can occur for reasons not fully understood
336 #if defined(__FreeBSD__)
338 * This currently occurs if an FD is closed
339 * before the EV_DELETE makes it out via kevent().
340 * The FreeBSD capabilities code sees the blank
341 * capability set and rejects the request to
344 * To be strictly correct - when an FD is closed,
345 * all the registered events are also removed.
346 * Queuing EV_DELETE to a closed FD is wrong.
347 * The event(s) should just be deleted from
348 * the pending changelist.
354 /* Can occur on a delete if the fd is closed. */
356 /* XXXX On NetBSD, we can also get EBADF if we
357 * try to add the write side of a pipe, but
358 * the read side has already been closed.
359 * Other BSDs call this situation 'EPIPE'. It
360 * would be good if we had a way to report
363 /* These two can occur on an add if the fd was one side
364 * of a pipe, and the other side was closed. */
367 /* Report read events, if we're listening for
368 * them, so that the user can learn about any
369 * add errors. (If the operation was a
370 * delete, then udata should be cleared.) */
371 if (events[i].udata) {
372 /* The operation was an add:
373 * report the error as a read. */
377 /* The operation was a del:
382 /* Other errors shouldn't occur. */
384 errno = events[i].data;
387 } else if (events[i].filter == EVFILT_READ) {
389 } else if (events[i].filter == EVFILT_WRITE) {
391 } else if (events[i].filter == EVFILT_SIGNAL) {
394 } else if (events[i].filter == EVFILT_USER) {
395 base->is_notify_pending = 0;
402 if (events[i].filter == EVFILT_SIGNAL) {
403 evmap_signal_active_(base, events[i].ident, 1);
405 evmap_io_active_(base, events[i].ident, which | EV_ET);
409 if (res == kqop->events_size) {
410 /* We used all the events space that we have. Maybe we should
412 kq_grow_events(kqop, kqop->events_size * 2);
419 kqop_free(struct kqop *kqop)
422 mm_free(kqop->changes);
424 mm_free(kqop->events);
425 if (kqop->kq >= 0 && kqop->pid == getpid())
427 memset(kqop, 0, sizeof(struct kqop));
432 kq_dealloc(struct event_base *base)
434 struct kqop *kqop = base->evbase;
435 evsig_dealloc_(base);
439 /* signal handling */
441 kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p)
443 struct kqop *kqop = base->evbase;
445 struct timespec timeout = { 0, 0 };
448 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
450 memset(&kev, 0, sizeof(kev));
452 kev.filter = EVFILT_SIGNAL;
455 /* Be ready for the signal if it is sent any
456 * time between now and the next call to
458 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
461 /* We can set the handler for most signals to SIG_IGN and
462 * still have them reported to us in the queue. However,
463 * if the handler for SIGCHLD is SIG_IGN, the system reaps
464 * zombie processes for us, and we don't get any notification.
465 * This appears to be the only signal with this quirk. */
466 if (evsig_set_handler_(base, nsignal,
467 nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1)
474 kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p)
476 struct kqop *kqop = base->evbase;
479 struct timespec timeout = { 0, 0 };
482 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
484 memset(&kev, 0, sizeof(kev));
486 kev.filter = EVFILT_SIGNAL;
487 kev.flags = EV_DELETE;
489 /* Because we insert signal events
490 * immediately, we need to delete them
491 * immediately, too */
492 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
495 if (evsig_restore_handler_(base, nsignal) == -1)
502 /* OSX 10.6 and FreeBSD 8.1 add support for EVFILT_USER, which we can use
503 * to wake up the event loop from another thread. */
505 /* Magic number we use for our filter ID. */
506 #define NOTIFY_IDENT 42
509 event_kq_add_notify_event_(struct event_base *base)
511 struct kqop *kqop = base->evbase;
512 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
514 struct timespec timeout = { 0, 0 };
517 if (kqop->notify_event_added)
520 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
521 memset(&kev, 0, sizeof(kev));
522 kev.ident = NOTIFY_IDENT;
523 kev.filter = EVFILT_USER;
524 kev.flags = EV_ADD | EV_CLEAR;
526 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
527 event_warn("kevent: adding EVFILT_USER event");
531 kqop->notify_event_added = 1;
540 event_kq_notify_base_(struct event_base *base)
542 struct kqop *kqop = base->evbase;
543 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
545 struct timespec timeout = { 0, 0 };
547 if (! kqop->notify_event_added)
550 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
551 memset(&kev, 0, sizeof(kev));
552 kev.ident = NOTIFY_IDENT;
553 kev.filter = EVFILT_USER;
554 kev.fflags = NOTE_TRIGGER;
556 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
557 event_warn("kevent: triggering EVFILT_USER event");
567 #endif /* EVENT__HAVE_KQUEUE */