1*eabc0478Schristos /* $NetBSD: kqueue.c,v 1.7 2024/08/18 20:47:21 christos Exp $ */ 28585484eSchristos 38585484eSchristos /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */ 48585484eSchristos 58585484eSchristos /* 68585484eSchristos * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu> 78585484eSchristos * Copyright 2007-2012 Niels Provos and Nick Mathewson 88585484eSchristos * 98585484eSchristos * Redistribution and use in source and binary forms, with or without 108585484eSchristos * modification, are permitted provided that the following conditions 118585484eSchristos * are met: 128585484eSchristos * 1. Redistributions of source code must retain the above copyright 138585484eSchristos * notice, this list of conditions and the following disclaimer. 148585484eSchristos * 2. Redistributions in binary form must reproduce the above copyright 158585484eSchristos * notice, this list of conditions and the following disclaimer in the 168585484eSchristos * documentation and/or other materials provided with the distribution. 178585484eSchristos * 3. The name of the author may not be used to endorse or promote products 188585484eSchristos * derived from this software without specific prior written permission. 198585484eSchristos * 208585484eSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 218585484eSchristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 228585484eSchristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 238585484eSchristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 248585484eSchristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 258585484eSchristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 268585484eSchristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 278585484eSchristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 288585484eSchristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 298585484eSchristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 308585484eSchristos */ 318585484eSchristos #include "event2/event-config.h" 328585484eSchristos #include "evconfig-private.h" 338585484eSchristos 348585484eSchristos #ifdef EVENT__HAVE_KQUEUE 358585484eSchristos 368585484eSchristos #include <sys/types.h> 378585484eSchristos #ifdef EVENT__HAVE_SYS_TIME_H 388585484eSchristos #include <sys/time.h> 398585484eSchristos #endif 408585484eSchristos #include <sys/queue.h> 418585484eSchristos #include <sys/event.h> 42*eabc0478Schristos #include <limits.h> 438585484eSchristos #include <signal.h> 448585484eSchristos #include <stdio.h> 458585484eSchristos #include <stdlib.h> 468585484eSchristos #include <string.h> 478585484eSchristos #include <unistd.h> 488585484eSchristos #include <errno.h> 498585484eSchristos #ifdef EVENT__HAVE_INTTYPES_H 508585484eSchristos #include <inttypes.h> 518585484eSchristos #endif 528585484eSchristos 538585484eSchristos /* Some platforms apparently define the udata field of struct kevent as 548585484eSchristos * intptr_t, whereas others define it as void*. There doesn't seem to be an 558585484eSchristos * easy way to tell them apart via autoconf, so we need to use OS macros. */ 56*eabc0478Schristos #if defined(__NetBSD__) 57*eabc0478Schristos #define PTR_TO_UDATA(x) ((typeof(((struct kevent *)0)->udata))(x)) 58*eabc0478Schristos #define INT_TO_UDATA(x) ((typeof(((struct kevent *)0)->udata))(intptr_t)(x)) 59*eabc0478Schristos #elif defined(EVENT__HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) && !defined(__CloudABI__) 608585484eSchristos #define PTR_TO_UDATA(x) ((intptr_t)(x)) 618585484eSchristos #define INT_TO_UDATA(x) ((intptr_t)(x)) 628585484eSchristos #else 638585484eSchristos #define PTR_TO_UDATA(x) (x) 648585484eSchristos #define INT_TO_UDATA(x) ((void*)(x)) 658585484eSchristos #endif 668585484eSchristos 678585484eSchristos #include "event-internal.h" 688585484eSchristos #include "log-internal.h" 698585484eSchristos #include "evmap-internal.h" 708585484eSchristos #include "event2/thread.h" 71*eabc0478Schristos #include "event2/util.h" 728585484eSchristos #include "evthread-internal.h" 738585484eSchristos #include "changelist-internal.h" 748585484eSchristos 758585484eSchristos #include "kqueue-internal.h" 768585484eSchristos 778585484eSchristos #define NEVENT 64 788585484eSchristos 798585484eSchristos struct kqop { 808585484eSchristos struct kevent *changes; 818585484eSchristos int changes_size; 828585484eSchristos 838585484eSchristos struct kevent *events; 848585484eSchristos int events_size; 858585484eSchristos int kq; 868585484eSchristos int notify_event_added; 878585484eSchristos pid_t pid; 888585484eSchristos }; 898585484eSchristos 908585484eSchristos static void kqop_free(struct kqop *kqop); 918585484eSchristos 928585484eSchristos static void *kq_init(struct event_base *); 938585484eSchristos static int kq_sig_add(struct event_base *, int, short, short, void *); 948585484eSchristos static int kq_sig_del(struct event_base *, int, short, short, void *); 958585484eSchristos static int kq_dispatch(struct event_base *, struct timeval *); 968585484eSchristos static void kq_dealloc(struct event_base *); 978585484eSchristos 988585484eSchristos const struct eventop kqops = { 998585484eSchristos "kqueue", 1008585484eSchristos kq_init, 1018585484eSchristos event_changelist_add_, 1028585484eSchristos event_changelist_del_, 1038585484eSchristos kq_dispatch, 1048585484eSchristos kq_dealloc, 1058585484eSchristos 1 /* need reinit */, 1068585484eSchristos EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS, 1078585484eSchristos EVENT_CHANGELIST_FDINFO_SIZE 1088585484eSchristos }; 1098585484eSchristos 1108585484eSchristos static const struct eventop kqsigops = { 1118585484eSchristos "kqueue_signal", 1128585484eSchristos NULL, 1138585484eSchristos kq_sig_add, 1148585484eSchristos kq_sig_del, 1158585484eSchristos NULL, 1168585484eSchristos NULL, 1178585484eSchristos 1 /* need reinit */, 1188585484eSchristos 0, 1198585484eSchristos 0 1208585484eSchristos }; 1218585484eSchristos 1228585484eSchristos static void * 1238585484eSchristos kq_init(struct event_base *base) 1248585484eSchristos { 1258585484eSchristos int kq = -1; 1268585484eSchristos struct kqop *kqueueop = NULL; 1278585484eSchristos 1288585484eSchristos if (!(kqueueop = mm_calloc(1, sizeof(struct kqop)))) 1298585484eSchristos return (NULL); 1308585484eSchristos 1318585484eSchristos /* Initialize the kernel queue */ 1328585484eSchristos 1338585484eSchristos if ((kq = kqueue()) == -1) { 1348585484eSchristos event_warn("kqueue"); 1358585484eSchristos goto err; 1368585484eSchristos } 1378585484eSchristos 1388585484eSchristos kqueueop->kq = kq; 1398585484eSchristos 1408585484eSchristos kqueueop->pid = getpid(); 1418585484eSchristos 1428585484eSchristos /* Initialize fields */ 1438585484eSchristos kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent)); 1448585484eSchristos if (kqueueop->changes == NULL) 1458585484eSchristos goto err; 1468585484eSchristos kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent)); 1478585484eSchristos if (kqueueop->events == NULL) 1488585484eSchristos goto err; 1498585484eSchristos kqueueop->events_size = kqueueop->changes_size = NEVENT; 1508585484eSchristos 1518585484eSchristos /* Check for Mac OS X kqueue bug. */ 1528585484eSchristos memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]); 1538585484eSchristos kqueueop->changes[0].ident = -1; 1548585484eSchristos kqueueop->changes[0].filter = EVFILT_READ; 1558585484eSchristos kqueueop->changes[0].flags = EV_ADD; 1568585484eSchristos /* 1578585484eSchristos * If kqueue works, then kevent will succeed, and it will 1588585484eSchristos * stick an error in events[0]. If kqueue is broken, then 1598585484eSchristos * kevent will fail. 1608585484eSchristos */ 1618585484eSchristos if (kevent(kq, 1628585484eSchristos kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 || 1638585484eSchristos (int)kqueueop->events[0].ident != -1 || 164*eabc0478Schristos !(kqueueop->events[0].flags & EV_ERROR)) { 1658585484eSchristos event_warn("%s: detected broken kqueue; not using.", __func__); 1668585484eSchristos goto err; 1678585484eSchristos } 1688585484eSchristos 1698585484eSchristos base->evsigsel = &kqsigops; 1708585484eSchristos 1718585484eSchristos return (kqueueop); 1728585484eSchristos err: 1738585484eSchristos if (kqueueop) 1748585484eSchristos kqop_free(kqueueop); 1758585484eSchristos 1768585484eSchristos return (NULL); 1778585484eSchristos } 1788585484eSchristos 1798585484eSchristos #define ADD_UDATA 0x30303 1808585484eSchristos 1818585484eSchristos static void 1828585484eSchristos kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change) 1838585484eSchristos { 1848585484eSchristos memset(out, 0, sizeof(struct kevent)); 1858585484eSchristos out->ident = fd; 1868585484eSchristos out->filter = filter; 1878585484eSchristos 1888585484eSchristos if (change & EV_CHANGE_ADD) { 1898585484eSchristos out->flags = EV_ADD; 1908585484eSchristos /* We set a magic number here so that we can tell 'add' 1918585484eSchristos * errors from 'del' errors. */ 1928585484eSchristos out->udata = INT_TO_UDATA(ADD_UDATA); 1938585484eSchristos if (change & EV_ET) 1948585484eSchristos out->flags |= EV_CLEAR; 1958585484eSchristos #ifdef NOTE_EOF 1968585484eSchristos /* Make it behave like select() and poll() */ 1978585484eSchristos if (filter == EVFILT_READ) 1988585484eSchristos out->fflags = NOTE_EOF; 1998585484eSchristos #endif 2008585484eSchristos } else { 2018585484eSchristos EVUTIL_ASSERT(change & EV_CHANGE_DEL); 2028585484eSchristos out->flags = EV_DELETE; 2038585484eSchristos } 2048585484eSchristos } 2058585484eSchristos 2068585484eSchristos static int 2078585484eSchristos kq_build_changes_list(const struct event_changelist *changelist, 2088585484eSchristos struct kqop *kqop) 2098585484eSchristos { 2108585484eSchristos int i; 2118585484eSchristos int n_changes = 0; 2128585484eSchristos 2138585484eSchristos for (i = 0; i < changelist->n_changes; ++i) { 2148585484eSchristos struct event_change *in_ch = &changelist->changes[i]; 2158585484eSchristos struct kevent *out_ch; 2168585484eSchristos if (n_changes >= kqop->changes_size - 1) { 217*eabc0478Schristos int newsize; 2188585484eSchristos struct kevent *newchanges; 2198585484eSchristos 220*eabc0478Schristos if (kqop->changes_size > INT_MAX / 2 || 221*eabc0478Schristos (size_t)kqop->changes_size * 2 > EV_SIZE_MAX / 222*eabc0478Schristos sizeof(struct kevent)) { 223*eabc0478Schristos event_warnx("%s: int overflow", __func__); 224*eabc0478Schristos return (-1); 225*eabc0478Schristos } 226*eabc0478Schristos 227*eabc0478Schristos newsize = kqop->changes_size * 2; 2288585484eSchristos newchanges = mm_realloc(kqop->changes, 2298585484eSchristos newsize * sizeof(struct kevent)); 2308585484eSchristos if (newchanges == NULL) { 2318585484eSchristos event_warn("%s: realloc", __func__); 2328585484eSchristos return (-1); 2338585484eSchristos } 2348585484eSchristos kqop->changes = newchanges; 2358585484eSchristos kqop->changes_size = newsize; 2368585484eSchristos } 2378585484eSchristos if (in_ch->read_change) { 2388585484eSchristos out_ch = &kqop->changes[n_changes++]; 2398585484eSchristos kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ, 2408585484eSchristos in_ch->read_change); 2418585484eSchristos } 2428585484eSchristos if (in_ch->write_change) { 2438585484eSchristos out_ch = &kqop->changes[n_changes++]; 2448585484eSchristos kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE, 2458585484eSchristos in_ch->write_change); 2468585484eSchristos } 2478585484eSchristos } 2488585484eSchristos return n_changes; 2498585484eSchristos } 2508585484eSchristos 2518585484eSchristos static int 2528585484eSchristos kq_grow_events(struct kqop *kqop, size_t new_size) 2538585484eSchristos { 2548585484eSchristos struct kevent *newresult; 2558585484eSchristos 2568585484eSchristos newresult = mm_realloc(kqop->events, 2578585484eSchristos new_size * sizeof(struct kevent)); 2588585484eSchristos 2598585484eSchristos if (newresult) { 2608585484eSchristos kqop->events = newresult; 2618585484eSchristos kqop->events_size = new_size; 2628585484eSchristos return 0; 2638585484eSchristos } else { 2648585484eSchristos return -1; 2658585484eSchristos } 2668585484eSchristos } 2678585484eSchristos 2688585484eSchristos static int 2698585484eSchristos kq_dispatch(struct event_base *base, struct timeval *tv) 2708585484eSchristos { 2718585484eSchristos struct kqop *kqop = base->evbase; 2728585484eSchristos struct kevent *events = kqop->events; 2738585484eSchristos struct kevent *changes; 2748585484eSchristos struct timespec ts, *ts_p = NULL; 2758585484eSchristos int i, n_changes, res; 2768585484eSchristos 2778585484eSchristos if (tv != NULL) { 278*eabc0478Schristos ts.tv_sec = tv->tv_sec; 279*eabc0478Schristos ts.tv_nsec = tv->tv_usec * 1000; 2808585484eSchristos ts_p = &ts; 2818585484eSchristos } 2828585484eSchristos 2838585484eSchristos /* Build "changes" from "base->changes" */ 2848585484eSchristos EVUTIL_ASSERT(kqop->changes); 2858585484eSchristos n_changes = kq_build_changes_list(&base->changelist, kqop); 2868585484eSchristos if (n_changes < 0) 2878585484eSchristos return -1; 2888585484eSchristos 2898585484eSchristos event_changelist_remove_all_(&base->changelist, base); 2908585484eSchristos 2918585484eSchristos /* steal the changes array in case some broken code tries to call 2928585484eSchristos * dispatch twice at once. */ 2938585484eSchristos changes = kqop->changes; 2948585484eSchristos kqop->changes = NULL; 2958585484eSchristos 2968585484eSchristos /* Make sure that 'events' is at least as long as the list of changes: 2978585484eSchristos * otherwise errors in the changes can get reported as a -1 return 2988585484eSchristos * value from kevent() rather than as EV_ERROR events in the events 2998585484eSchristos * array. 3008585484eSchristos * 3018585484eSchristos * (We could instead handle -1 return values from kevent() by 3028585484eSchristos * retrying with a smaller changes array or a larger events array, 3038585484eSchristos * but this approach seems less risky for now.) 3048585484eSchristos */ 3058585484eSchristos if (kqop->events_size < n_changes) { 3068585484eSchristos int new_size = kqop->events_size; 3078585484eSchristos do { 3088585484eSchristos new_size *= 2; 3098585484eSchristos } while (new_size < n_changes); 3108585484eSchristos 3118585484eSchristos kq_grow_events(kqop, new_size); 3128585484eSchristos events = kqop->events; 3138585484eSchristos } 3148585484eSchristos 3158585484eSchristos EVBASE_RELEASE_LOCK(base, th_base_lock); 3168585484eSchristos 3178585484eSchristos res = kevent(kqop->kq, changes, n_changes, 3188585484eSchristos events, kqop->events_size, ts_p); 3198585484eSchristos 3208585484eSchristos EVBASE_ACQUIRE_LOCK(base, th_base_lock); 3218585484eSchristos 3228585484eSchristos EVUTIL_ASSERT(kqop->changes == NULL); 3238585484eSchristos kqop->changes = changes; 3248585484eSchristos 3258585484eSchristos if (res == -1) { 3268585484eSchristos if (errno != EINTR) { 3278585484eSchristos event_warn("kevent"); 3288585484eSchristos return (-1); 3298585484eSchristos } 3308585484eSchristos 3318585484eSchristos return (0); 3328585484eSchristos } 3338585484eSchristos 3348585484eSchristos event_debug(("%s: kevent reports %d", __func__, res)); 3358585484eSchristos 3368585484eSchristos for (i = 0; i < res; i++) { 3378585484eSchristos int which = 0; 3388585484eSchristos 3398585484eSchristos if (events[i].flags & EV_ERROR) { 3408585484eSchristos switch (events[i].data) { 3418585484eSchristos 3428585484eSchristos /* Can occur on delete if we are not currently 3438585484eSchristos * watching any events on this fd. That can 3448585484eSchristos * happen when the fd was closed and another 3458585484eSchristos * file was opened with that fd. */ 3468585484eSchristos case ENOENT: 3478585484eSchristos /* Can occur for reasons not fully understood 3488585484eSchristos * on FreeBSD. */ 3498585484eSchristos case EINVAL: 3508585484eSchristos continue; 3517476e6e4Schristos #if defined(__FreeBSD__) && defined(ENOTCAPABLE) 3527476e6e4Schristos /* 3537476e6e4Schristos * This currently occurs if an FD is closed 3547476e6e4Schristos * before the EV_DELETE makes it out via kevent(). 3557476e6e4Schristos * The FreeBSD capabilities code sees the blank 3567476e6e4Schristos * capability set and rejects the request to 3577476e6e4Schristos * modify an event. 3587476e6e4Schristos * 3597476e6e4Schristos * To be strictly correct - when an FD is closed, 3607476e6e4Schristos * all the registered events are also removed. 3617476e6e4Schristos * Queuing EV_DELETE to a closed FD is wrong. 3627476e6e4Schristos * The event(s) should just be deleted from 3637476e6e4Schristos * the pending changelist. 3647476e6e4Schristos */ 3657476e6e4Schristos case ENOTCAPABLE: 3667476e6e4Schristos continue; 3677476e6e4Schristos #endif 3688585484eSchristos 3698585484eSchristos /* Can occur on a delete if the fd is closed. */ 3708585484eSchristos case EBADF: 3718585484eSchristos /* XXXX On NetBSD, we can also get EBADF if we 3728585484eSchristos * try to add the write side of a pipe, but 3738585484eSchristos * the read side has already been closed. 3748585484eSchristos * Other BSDs call this situation 'EPIPE'. It 3758585484eSchristos * would be good if we had a way to report 3768585484eSchristos * this situation. */ 3778585484eSchristos continue; 3788585484eSchristos /* These two can occur on an add if the fd was one side 3798585484eSchristos * of a pipe, and the other side was closed. */ 3808585484eSchristos case EPERM: 3818585484eSchristos case EPIPE: 3828585484eSchristos /* Report read events, if we're listening for 3838585484eSchristos * them, so that the user can learn about any 3848585484eSchristos * add errors. (If the operation was a 3858585484eSchristos * delete, then udata should be cleared.) */ 3868585484eSchristos if (events[i].udata) { 3878585484eSchristos /* The operation was an add: 3888585484eSchristos * report the error as a read. */ 3898585484eSchristos which |= EV_READ; 3908585484eSchristos break; 3918585484eSchristos } else { 3928585484eSchristos /* The operation was a del: 3938585484eSchristos * report nothing. */ 3948585484eSchristos continue; 3958585484eSchristos } 3968585484eSchristos 3978585484eSchristos /* Other errors shouldn't occur. */ 3988585484eSchristos default: 3998585484eSchristos errno = events[i].data; 4008585484eSchristos return (-1); 4018585484eSchristos } 4028585484eSchristos } else if (events[i].filter == EVFILT_READ) { 4038585484eSchristos which |= EV_READ; 4048585484eSchristos } else if (events[i].filter == EVFILT_WRITE) { 4058585484eSchristos which |= EV_WRITE; 4068585484eSchristos } else if (events[i].filter == EVFILT_SIGNAL) { 4078585484eSchristos which |= EV_SIGNAL; 4088585484eSchristos #ifdef EVFILT_USER 4098585484eSchristos } else if (events[i].filter == EVFILT_USER) { 4108585484eSchristos base->is_notify_pending = 0; 4118585484eSchristos #endif 4128585484eSchristos } 4138585484eSchristos 4148585484eSchristos if (!which) 4158585484eSchristos continue; 4168585484eSchristos 4178585484eSchristos if (events[i].filter == EVFILT_SIGNAL) { 4188585484eSchristos evmap_signal_active_(base, events[i].ident, 1); 4198585484eSchristos } else { 4208585484eSchristos evmap_io_active_(base, events[i].ident, which | EV_ET); 4218585484eSchristos } 4228585484eSchristos } 4238585484eSchristos 4248585484eSchristos if (res == kqop->events_size) { 4258585484eSchristos /* We used all the events space that we have. Maybe we should 4268585484eSchristos make it bigger. */ 4278585484eSchristos kq_grow_events(kqop, kqop->events_size * 2); 4288585484eSchristos } 4298585484eSchristos 4308585484eSchristos return (0); 4318585484eSchristos } 4328585484eSchristos 4338585484eSchristos static void 4348585484eSchristos kqop_free(struct kqop *kqop) 4358585484eSchristos { 4368585484eSchristos if (kqop->changes) 4378585484eSchristos mm_free(kqop->changes); 4388585484eSchristos if (kqop->events) 4398585484eSchristos mm_free(kqop->events); 4408585484eSchristos if (kqop->kq >= 0 && kqop->pid == getpid()) 4418585484eSchristos close(kqop->kq); 4428585484eSchristos memset(kqop, 0, sizeof(struct kqop)); 4438585484eSchristos mm_free(kqop); 4448585484eSchristos } 4458585484eSchristos 4468585484eSchristos static void 4478585484eSchristos kq_dealloc(struct event_base *base) 4488585484eSchristos { 4498585484eSchristos struct kqop *kqop = base->evbase; 4508585484eSchristos evsig_dealloc_(base); 4518585484eSchristos kqop_free(kqop); 4528585484eSchristos } 4538585484eSchristos 4548585484eSchristos /* signal handling */ 4558585484eSchristos static int 4568585484eSchristos kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p) 4578585484eSchristos { 4588585484eSchristos struct kqop *kqop = base->evbase; 4598585484eSchristos struct kevent kev; 4608585484eSchristos struct timespec timeout = { 0, 0 }; 4618585484eSchristos (void)p; 4628585484eSchristos 4638585484eSchristos EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG); 4648585484eSchristos 4658585484eSchristos memset(&kev, 0, sizeof(kev)); 4668585484eSchristos kev.ident = nsignal; 4678585484eSchristos kev.filter = EVFILT_SIGNAL; 4688585484eSchristos kev.flags = EV_ADD; 4698585484eSchristos 4708585484eSchristos /* Be ready for the signal if it is sent any 4718585484eSchristos * time between now and the next call to 4728585484eSchristos * kq_dispatch. */ 4738585484eSchristos if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) 4748585484eSchristos return (-1); 4758585484eSchristos 4768585484eSchristos /* We can set the handler for most signals to SIG_IGN and 4778585484eSchristos * still have them reported to us in the queue. However, 4788585484eSchristos * if the handler for SIGCHLD is SIG_IGN, the system reaps 4798585484eSchristos * zombie processes for us, and we don't get any notification. 4808585484eSchristos * This appears to be the only signal with this quirk. */ 4818585484eSchristos if (evsig_set_handler_(base, nsignal, 4828585484eSchristos nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1) 4838585484eSchristos return (-1); 4848585484eSchristos 4858585484eSchristos return (0); 4868585484eSchristos } 4878585484eSchristos 4888585484eSchristos static int 4898585484eSchristos kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p) 4908585484eSchristos { 4918585484eSchristos struct kqop *kqop = base->evbase; 4928585484eSchristos struct kevent kev; 4938585484eSchristos 4948585484eSchristos struct timespec timeout = { 0, 0 }; 4958585484eSchristos (void)p; 4968585484eSchristos 4978585484eSchristos EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG); 4988585484eSchristos 4998585484eSchristos memset(&kev, 0, sizeof(kev)); 5008585484eSchristos kev.ident = nsignal; 5018585484eSchristos kev.filter = EVFILT_SIGNAL; 5028585484eSchristos kev.flags = EV_DELETE; 5038585484eSchristos 5048585484eSchristos /* Because we insert signal events 5058585484eSchristos * immediately, we need to delete them 5068585484eSchristos * immediately, too */ 5078585484eSchristos if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) 5088585484eSchristos return (-1); 5098585484eSchristos 5108585484eSchristos if (evsig_restore_handler_(base, nsignal) == -1) 5118585484eSchristos return (-1); 5128585484eSchristos 5138585484eSchristos return (0); 5148585484eSchristos } 5158585484eSchristos 5168585484eSchristos 5178585484eSchristos /* OSX 10.6 and FreeBSD 8.1 add support for EVFILT_USER, which we can use 5188585484eSchristos * to wake up the event loop from another thread. */ 5198585484eSchristos 5208585484eSchristos /* Magic number we use for our filter ID. */ 5218585484eSchristos #define NOTIFY_IDENT 42 5228585484eSchristos 5238585484eSchristos int 5248585484eSchristos event_kq_add_notify_event_(struct event_base *base) 5258585484eSchristos { 5268585484eSchristos struct kqop *kqop = base->evbase; 5278585484eSchristos #if defined(EVFILT_USER) && defined(NOTE_TRIGGER) 5288585484eSchristos struct kevent kev; 5298585484eSchristos struct timespec timeout = { 0, 0 }; 5308585484eSchristos #endif 5318585484eSchristos 5328585484eSchristos if (kqop->notify_event_added) 5338585484eSchristos return 0; 5348585484eSchristos 5358585484eSchristos #if defined(EVFILT_USER) && defined(NOTE_TRIGGER) 5368585484eSchristos memset(&kev, 0, sizeof(kev)); 5378585484eSchristos kev.ident = NOTIFY_IDENT; 5388585484eSchristos kev.filter = EVFILT_USER; 5398585484eSchristos kev.flags = EV_ADD | EV_CLEAR; 5408585484eSchristos 5418585484eSchristos if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) { 5428585484eSchristos event_warn("kevent: adding EVFILT_USER event"); 5438585484eSchristos return -1; 5448585484eSchristos } 5458585484eSchristos 5468585484eSchristos kqop->notify_event_added = 1; 5478585484eSchristos 5488585484eSchristos return 0; 5498585484eSchristos #else 5508585484eSchristos return -1; 5518585484eSchristos #endif 5528585484eSchristos } 5538585484eSchristos 5548585484eSchristos int 5558585484eSchristos event_kq_notify_base_(struct event_base *base) 5568585484eSchristos { 5578585484eSchristos struct kqop *kqop = base->evbase; 5588585484eSchristos #if defined(EVFILT_USER) && defined(NOTE_TRIGGER) 5598585484eSchristos struct kevent kev; 5608585484eSchristos struct timespec timeout = { 0, 0 }; 5618585484eSchristos #endif 5628585484eSchristos if (! kqop->notify_event_added) 5638585484eSchristos return -1; 5648585484eSchristos 5658585484eSchristos #if defined(EVFILT_USER) && defined(NOTE_TRIGGER) 5668585484eSchristos memset(&kev, 0, sizeof(kev)); 5678585484eSchristos kev.ident = NOTIFY_IDENT; 5688585484eSchristos kev.filter = EVFILT_USER; 5698585484eSchristos kev.fflags = NOTE_TRIGGER; 5708585484eSchristos 5718585484eSchristos if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) { 5728585484eSchristos event_warn("kevent: triggering EVFILT_USER event"); 5738585484eSchristos return -1; 5748585484eSchristos } 5758585484eSchristos 5768585484eSchristos return 0; 5778585484eSchristos #else 5788585484eSchristos return -1; 5798585484eSchristos #endif 5808585484eSchristos } 5818585484eSchristos 5828585484eSchristos #endif /* EVENT__HAVE_KQUEUE */ 583