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