michael@0: /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */ michael@0: michael@0: /* michael@0: * Copyright 2000-2007 Niels Provos michael@0: * Copyright 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #include "event2/event-config.h" michael@0: michael@0: #define _GNU_SOURCE michael@0: michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_TIME_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_INTTYPES_H michael@0: #include michael@0: #endif michael@0: michael@0: /* Some platforms apparently define the udata field of struct kevent as michael@0: * intptr_t, whereas others define it as void*. There doesn't seem to be an michael@0: * easy way to tell them apart via autoconf, so we need to use OS macros. */ michael@0: #if defined(_EVENT_HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) michael@0: #define PTR_TO_UDATA(x) ((intptr_t)(x)) michael@0: #define INT_TO_UDATA(x) ((intptr_t)(x)) michael@0: #else michael@0: #define PTR_TO_UDATA(x) (x) michael@0: #define INT_TO_UDATA(x) ((void*)(x)) michael@0: #endif michael@0: michael@0: #include "event-internal.h" michael@0: #include "log-internal.h" michael@0: #include "evmap-internal.h" michael@0: #include "event2/thread.h" michael@0: #include "evthread-internal.h" michael@0: #include "changelist-internal.h" michael@0: michael@0: #define NEVENT 64 michael@0: michael@0: struct kqop { michael@0: struct kevent *changes; michael@0: int changes_size; michael@0: michael@0: struct kevent *events; michael@0: int events_size; michael@0: int kq; michael@0: pid_t pid; michael@0: }; michael@0: michael@0: static void kqop_free(struct kqop *kqop); michael@0: michael@0: static void *kq_init(struct event_base *); michael@0: static int kq_sig_add(struct event_base *, int, short, short, void *); michael@0: static int kq_sig_del(struct event_base *, int, short, short, void *); michael@0: static int kq_dispatch(struct event_base *, struct timeval *); michael@0: static void kq_dealloc(struct event_base *); michael@0: michael@0: const struct eventop kqops = { michael@0: "kqueue", michael@0: kq_init, michael@0: event_changelist_add, michael@0: event_changelist_del, michael@0: kq_dispatch, michael@0: kq_dealloc, michael@0: 1 /* need reinit */, michael@0: EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS, michael@0: EVENT_CHANGELIST_FDINFO_SIZE michael@0: }; michael@0: michael@0: static const struct eventop kqsigops = { michael@0: "kqueue_signal", michael@0: NULL, michael@0: kq_sig_add, michael@0: kq_sig_del, michael@0: NULL, michael@0: NULL, michael@0: 1 /* need reinit */, michael@0: 0, michael@0: 0 michael@0: }; michael@0: michael@0: static void * michael@0: kq_init(struct event_base *base) michael@0: { michael@0: int kq = -1; michael@0: struct kqop *kqueueop = NULL; michael@0: michael@0: if (!(kqueueop = mm_calloc(1, sizeof(struct kqop)))) michael@0: return (NULL); michael@0: michael@0: /* Initialize the kernel queue */ michael@0: michael@0: if ((kq = kqueue()) == -1) { michael@0: event_warn("kqueue"); michael@0: goto err; michael@0: } michael@0: michael@0: kqueueop->kq = kq; michael@0: michael@0: kqueueop->pid = getpid(); michael@0: michael@0: /* Initialize fields */ michael@0: kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent)); michael@0: if (kqueueop->changes == NULL) michael@0: goto err; michael@0: kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent)); michael@0: if (kqueueop->events == NULL) michael@0: goto err; michael@0: kqueueop->events_size = kqueueop->changes_size = NEVENT; michael@0: michael@0: /* Check for Mac OS X kqueue bug. */ michael@0: memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]); michael@0: kqueueop->changes[0].ident = -1; michael@0: kqueueop->changes[0].filter = EVFILT_READ; michael@0: kqueueop->changes[0].flags = EV_ADD; michael@0: /* michael@0: * If kqueue works, then kevent will succeed, and it will michael@0: * stick an error in events[0]. If kqueue is broken, then michael@0: * kevent will fail. michael@0: */ michael@0: if (kevent(kq, michael@0: kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 || michael@0: (int)kqueueop->events[0].ident != -1 || michael@0: kqueueop->events[0].flags != EV_ERROR) { michael@0: event_warn("%s: detected broken kqueue; not using.", __func__); michael@0: goto err; michael@0: } michael@0: michael@0: base->evsigsel = &kqsigops; michael@0: michael@0: return (kqueueop); michael@0: err: michael@0: if (kqueueop) michael@0: kqop_free(kqueueop); michael@0: michael@0: return (NULL); michael@0: } michael@0: michael@0: #define ADD_UDATA 0x30303 michael@0: michael@0: static void michael@0: kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change) michael@0: { michael@0: memset(out, 0, sizeof(struct kevent)); michael@0: out->ident = fd; michael@0: out->filter = filter; michael@0: michael@0: if (change & EV_CHANGE_ADD) { michael@0: out->flags = EV_ADD; michael@0: /* We set a magic number here so that we can tell 'add' michael@0: * errors from 'del' errors. */ michael@0: out->udata = INT_TO_UDATA(ADD_UDATA); michael@0: if (change & EV_ET) michael@0: out->flags |= EV_CLEAR; michael@0: #ifdef NOTE_EOF michael@0: /* Make it behave like select() and poll() */ michael@0: if (filter == EVFILT_READ) michael@0: out->fflags = NOTE_EOF; michael@0: #endif michael@0: } else { michael@0: EVUTIL_ASSERT(change & EV_CHANGE_DEL); michael@0: out->flags = EV_DELETE; michael@0: } michael@0: } michael@0: michael@0: static int michael@0: kq_build_changes_list(const struct event_changelist *changelist, michael@0: struct kqop *kqop) michael@0: { michael@0: int i; michael@0: int n_changes = 0; michael@0: michael@0: for (i = 0; i < changelist->n_changes; ++i) { michael@0: struct event_change *in_ch = &changelist->changes[i]; michael@0: struct kevent *out_ch; michael@0: if (n_changes >= kqop->changes_size - 1) { michael@0: int newsize = kqop->changes_size * 2; michael@0: struct kevent *newchanges; michael@0: michael@0: newchanges = mm_realloc(kqop->changes, michael@0: newsize * sizeof(struct kevent)); michael@0: if (newchanges == NULL) { michael@0: event_warn("%s: realloc", __func__); michael@0: return (-1); michael@0: } michael@0: kqop->changes = newchanges; michael@0: kqop->changes_size = newsize; michael@0: } michael@0: if (in_ch->read_change) { michael@0: out_ch = &kqop->changes[n_changes++]; michael@0: kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ, michael@0: in_ch->read_change); michael@0: } michael@0: if (in_ch->write_change) { michael@0: out_ch = &kqop->changes[n_changes++]; michael@0: kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE, michael@0: in_ch->write_change); michael@0: } michael@0: } michael@0: return n_changes; michael@0: } michael@0: michael@0: static int michael@0: kq_grow_events(struct kqop *kqop, size_t new_size) michael@0: { michael@0: struct kevent *newresult; michael@0: michael@0: newresult = mm_realloc(kqop->events, michael@0: new_size * sizeof(struct kevent)); michael@0: michael@0: if (newresult) { michael@0: kqop->events = newresult; michael@0: kqop->events_size = new_size; michael@0: return 0; michael@0: } else { michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: static int michael@0: kq_dispatch(struct event_base *base, struct timeval *tv) michael@0: { michael@0: struct kqop *kqop = base->evbase; michael@0: struct kevent *events = kqop->events; michael@0: struct kevent *changes; michael@0: struct timespec ts, *ts_p = NULL; michael@0: int i, n_changes, res; michael@0: michael@0: if (tv != NULL) { michael@0: TIMEVAL_TO_TIMESPEC(tv, &ts); michael@0: ts_p = &ts; michael@0: } michael@0: michael@0: /* Build "changes" from "base->changes" */ michael@0: EVUTIL_ASSERT(kqop->changes); michael@0: n_changes = kq_build_changes_list(&base->changelist, kqop); michael@0: if (n_changes < 0) michael@0: return -1; michael@0: michael@0: event_changelist_remove_all(&base->changelist, base); michael@0: michael@0: /* steal the changes array in case some broken code tries to call michael@0: * dispatch twice at once. */ michael@0: changes = kqop->changes; michael@0: kqop->changes = NULL; michael@0: michael@0: /* Make sure that 'events' is at least as long as the list of changes: michael@0: * otherwise errors in the changes can get reported as a -1 return michael@0: * value from kevent() rather than as EV_ERROR events in the events michael@0: * array. michael@0: * michael@0: * (We could instead handle -1 return values from kevent() by michael@0: * retrying with a smaller changes array or a larger events array, michael@0: * but this approach seems less risky for now.) michael@0: */ michael@0: if (kqop->events_size < n_changes) { michael@0: int new_size = kqop->events_size; michael@0: do { michael@0: new_size *= 2; michael@0: } while (new_size < n_changes); michael@0: michael@0: kq_grow_events(kqop, new_size); michael@0: events = kqop->events; michael@0: } michael@0: michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: michael@0: res = kevent(kqop->kq, changes, n_changes, michael@0: events, kqop->events_size, ts_p); michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: michael@0: EVUTIL_ASSERT(kqop->changes == NULL); michael@0: kqop->changes = changes; michael@0: michael@0: if (res == -1) { michael@0: if (errno != EINTR) { michael@0: event_warn("kevent"); michael@0: return (-1); michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: event_debug(("%s: kevent reports %d", __func__, res)); michael@0: michael@0: for (i = 0; i < res; i++) { michael@0: int which = 0; michael@0: michael@0: if (events[i].flags & EV_ERROR) { michael@0: switch (events[i].data) { michael@0: michael@0: /* Can occur on delete if we are not currently michael@0: * watching any events on this fd. That can michael@0: * happen when the fd was closed and another michael@0: * file was opened with that fd. */ michael@0: case ENOENT: michael@0: /* Can occur for reasons not fully understood michael@0: * on FreeBSD. */ michael@0: case EINVAL: michael@0: continue; michael@0: michael@0: /* Can occur on a delete if the fd is closed. */ michael@0: case EBADF: michael@0: /* XXXX On NetBSD, we can also get EBADF if we michael@0: * try to add the write side of a pipe, but michael@0: * the read side has already been closed. michael@0: * Other BSDs call this situation 'EPIPE'. It michael@0: * would be good if we had a way to report michael@0: * this situation. */ michael@0: continue; michael@0: /* These two can occur on an add if the fd was one side michael@0: * of a pipe, and the other side was closed. */ michael@0: case EPERM: michael@0: case EPIPE: michael@0: /* Report read events, if we're listening for michael@0: * them, so that the user can learn about any michael@0: * add errors. (If the operation was a michael@0: * delete, then udata should be cleared.) */ michael@0: if (events[i].udata) { michael@0: /* The operation was an add: michael@0: * report the error as a read. */ michael@0: which |= EV_READ; michael@0: break; michael@0: } else { michael@0: /* The operation was a del: michael@0: * report nothing. */ michael@0: continue; michael@0: } michael@0: michael@0: /* Other errors shouldn't occur. */ michael@0: default: michael@0: errno = events[i].data; michael@0: return (-1); michael@0: } michael@0: } else if (events[i].filter == EVFILT_READ) { michael@0: which |= EV_READ; michael@0: } else if (events[i].filter == EVFILT_WRITE) { michael@0: which |= EV_WRITE; michael@0: } else if (events[i].filter == EVFILT_SIGNAL) { michael@0: which |= EV_SIGNAL; michael@0: } michael@0: michael@0: if (!which) michael@0: continue; michael@0: michael@0: if (events[i].filter == EVFILT_SIGNAL) { michael@0: evmap_signal_active(base, events[i].ident, 1); michael@0: } else { michael@0: evmap_io_active(base, events[i].ident, which | EV_ET); michael@0: } michael@0: } michael@0: michael@0: if (res == kqop->events_size) { michael@0: /* We used all the events space that we have. Maybe we should michael@0: make it bigger. */ michael@0: kq_grow_events(kqop, kqop->events_size * 2); michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: static void michael@0: kqop_free(struct kqop *kqop) michael@0: { michael@0: if (kqop->changes) michael@0: mm_free(kqop->changes); michael@0: if (kqop->events) michael@0: mm_free(kqop->events); michael@0: if (kqop->kq >= 0 && kqop->pid == getpid()) michael@0: close(kqop->kq); michael@0: memset(kqop, 0, sizeof(struct kqop)); michael@0: mm_free(kqop); michael@0: } michael@0: michael@0: static void michael@0: kq_dealloc(struct event_base *base) michael@0: { michael@0: struct kqop *kqop = base->evbase; michael@0: evsig_dealloc(base); michael@0: kqop_free(kqop); michael@0: } michael@0: michael@0: /* signal handling */ michael@0: static int michael@0: kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p) michael@0: { michael@0: struct kqop *kqop = base->evbase; michael@0: struct kevent kev; michael@0: struct timespec timeout = { 0, 0 }; michael@0: (void)p; michael@0: michael@0: EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG); michael@0: michael@0: memset(&kev, 0, sizeof(kev)); michael@0: kev.ident = nsignal; michael@0: kev.filter = EVFILT_SIGNAL; michael@0: kev.flags = EV_ADD; michael@0: michael@0: /* Be ready for the signal if it is sent any michael@0: * time between now and the next call to michael@0: * kq_dispatch. */ michael@0: if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) michael@0: return (-1); michael@0: michael@0: /* Backported from michael@0: * https://github.com/nmathewson/Libevent/commit/148458e0a1fd25e167aa2ef229d1c9a70b27c3e9 */ michael@0: /* We can set the handler for most signals to SIG_IGN and michael@0: * still have them reported to us in the queue. However, michael@0: * if the handler for SIGCHLD is SIG_IGN, the system reaps michael@0: * zombie processes for us, and we don't get any notification. michael@0: * This appears to be the only signal with this quirk. */ michael@0: if (_evsig_set_handler(base, nsignal, michael@0: nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1) { michael@0: return (-1); michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: static int michael@0: kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p) michael@0: { michael@0: struct kqop *kqop = base->evbase; michael@0: struct kevent kev; michael@0: michael@0: struct timespec timeout = { 0, 0 }; michael@0: (void)p; michael@0: michael@0: EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG); michael@0: michael@0: memset(&kev, 0, sizeof(kev)); michael@0: kev.ident = nsignal; michael@0: kev.filter = EVFILT_SIGNAL; michael@0: kev.flags = EV_DELETE; michael@0: michael@0: /* Because we insert signal events michael@0: * immediately, we need to delete them michael@0: * immediately, too */ michael@0: if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) michael@0: return (-1); michael@0: michael@0: if (_evsig_restore_handler(base, nsignal) == -1) michael@0: return (-1); michael@0: michael@0: return (0); michael@0: }