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