2002-07-26 14:45:50 +00:00
|
|
|
/* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
/*
|
2009-01-27 22:34:36 +00:00
|
|
|
* Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
|
2012-02-10 17:29:53 -05:00
|
|
|
* Copyright 2007-2012 Niels Provos and Nick Mathewson
|
2002-04-09 15:14:06 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-10-04 23:27:26 +00:00
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2002-04-09 15:14:06 +00:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2010-07-07 16:45:03 -04:00
|
|
|
#include "event2/event-config.h"
|
2011-01-07 00:34:22 -07:00
|
|
|
#include "evconfig-private.h"
|
2009-11-06 21:13:25 +00:00
|
|
|
|
2012-02-29 15:07:31 -05:00
|
|
|
#ifdef EVENT__HAVE_KQUEUE
|
2012-02-08 18:46:00 +02:00
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <sys/types.h>
|
2012-02-29 15:07:31 -05:00
|
|
|
#ifdef EVENT__HAVE_SYS_TIME_H
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <sys/time.h>
|
2002-10-07 00:47:34 +00:00
|
|
|
#endif
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/event.h>
|
2019-05-10 23:54:14 +02:00
|
|
|
#include <limits.h>
|
2002-04-10 02:10:47 +00:00
|
|
|
#include <signal.h>
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-07-26 14:45:50 +00:00
|
|
|
#include <string.h>
|
2002-04-09 15:14:06 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2012-02-29 15:07:31 -05:00
|
|
|
#ifdef EVENT__HAVE_INTTYPES_H
|
2003-03-08 16:33:18 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#endif
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-08-10 16:31:02 +00:00
|
|
|
/* Some platforms apparently define the udata field of struct kevent as
|
|
|
|
* intptr_t, whereas others define it as void*. There doesn't seem to be an
|
|
|
|
* easy way to tell them apart via autoconf, so we need to use OS macros. */
|
2019-10-04 01:26:47 +02:00
|
|
|
#if defined(__NetBSD__)
|
|
|
|
#define PTR_TO_UDATA(x) ((typeof(((struct kevent *)0)->udata))(x))
|
|
|
|
#define INT_TO_UDATA(x) ((typeof(((struct kevent *)0)->udata))(intptr_t)(x))
|
|
|
|
#elif defined(EVENT__HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) && !defined(__CloudABI__)
|
2007-08-10 16:31:02 +00:00
|
|
|
#define PTR_TO_UDATA(x) ((intptr_t)(x))
|
2011-06-08 14:56:19 -04:00
|
|
|
#define INT_TO_UDATA(x) ((intptr_t)(x))
|
2003-03-08 16:33:18 +00:00
|
|
|
#else
|
2007-08-10 16:31:02 +00:00
|
|
|
#define PTR_TO_UDATA(x) (x)
|
2011-06-08 14:56:19 -04:00
|
|
|
#define INT_TO_UDATA(x) ((void*)(x))
|
2003-03-08 16:33:18 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-25 06:57:59 +00:00
|
|
|
#include "event-internal.h"
|
2009-01-13 20:26:37 +00:00
|
|
|
#include "log-internal.h"
|
|
|
|
#include "evmap-internal.h"
|
2009-10-21 06:03:00 +00:00
|
|
|
#include "event2/thread.h"
|
2019-05-07 20:53:17 +02:00
|
|
|
#include "event2/util.h"
|
2009-10-21 06:03:00 +00:00
|
|
|
#include "evthread-internal.h"
|
2010-01-14 16:31:05 -05:00
|
|
|
#include "changelist-internal.h"
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2010-09-17 00:34:13 -04:00
|
|
|
#include "kqueue-internal.h"
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
#define NEVENT 64
|
|
|
|
|
|
|
|
struct kqop {
|
|
|
|
struct kevent *changes;
|
2009-10-21 03:54:00 +00:00
|
|
|
int changes_size;
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
struct kevent *events;
|
2009-10-21 03:54:00 +00:00
|
|
|
int events_size;
|
2002-04-09 15:14:06 +00:00
|
|
|
int kq;
|
2010-09-17 00:34:13 -04:00
|
|
|
int notify_event_added;
|
2008-03-02 01:46:00 +00:00
|
|
|
pid_t pid;
|
2005-01-03 18:58:40 +00:00
|
|
|
};
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2009-10-30 22:43:53 +00:00
|
|
|
static void kqop_free(struct kqop *kqop);
|
|
|
|
|
2010-03-05 13:00:15 -05:00
|
|
|
static void *kq_init(struct event_base *);
|
|
|
|
static int kq_sig_add(struct event_base *, int, short, short, void *);
|
|
|
|
static int kq_sig_del(struct event_base *, int, short, short, void *);
|
|
|
|
static int kq_dispatch(struct event_base *, struct timeval *);
|
|
|
|
static void kq_dealloc(struct event_base *);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2003-09-25 03:26:53 +00:00
|
|
|
const struct eventop kqops = {
|
2002-04-09 15:14:06 +00:00
|
|
|
"kqueue",
|
|
|
|
kq_init,
|
2012-02-29 15:07:33 -05:00
|
|
|
event_changelist_add_,
|
|
|
|
event_changelist_del_,
|
2006-03-28 04:40:54 +00:00
|
|
|
kq_dispatch,
|
2007-11-25 06:57:59 +00:00
|
|
|
kq_dealloc,
|
2008-05-31 14:37:31 +00:00
|
|
|
1 /* need reinit */,
|
|
|
|
EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS,
|
2010-01-14 16:31:05 -05:00
|
|
|
EVENT_CHANGELIST_FDINFO_SIZE
|
2002-04-09 15:14:06 +00:00
|
|
|
};
|
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
static const struct eventop kqsigops = {
|
|
|
|
"kqueue_signal",
|
|
|
|
NULL,
|
|
|
|
kq_sig_add,
|
|
|
|
kq_sig_del,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
1 /* need reinit */,
|
2009-01-14 20:52:32 +00:00
|
|
|
0,
|
2008-12-23 16:37:01 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2008-03-29 01:45:45 +00:00
|
|
|
static void *
|
2007-03-10 06:37:53 +00:00
|
|
|
kq_init(struct event_base *base)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2009-10-27 04:03:58 +00:00
|
|
|
int kq = -1;
|
|
|
|
struct kqop *kqueueop = NULL;
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2008-04-25 01:18:08 +00:00
|
|
|
if (!(kqueueop = mm_calloc(1, sizeof(struct kqop))))
|
2005-01-03 18:58:40 +00:00
|
|
|
return (NULL);
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2010-01-14 16:31:05 -05:00
|
|
|
/* Initialize the kernel queue */
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
if ((kq = kqueue()) == -1) {
|
2005-03-29 07:03:10 +00:00
|
|
|
event_warn("kqueue");
|
2009-10-27 04:03:58 +00:00
|
|
|
goto err;
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2005-01-03 18:58:40 +00:00
|
|
|
kqueueop->kq = kq;
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2008-03-02 01:46:00 +00:00
|
|
|
kqueueop->pid = getpid();
|
|
|
|
|
2009-10-16 13:19:57 +00:00
|
|
|
/* Initialize fields */
|
2009-11-15 19:00:12 +00:00
|
|
|
kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent));
|
2009-10-27 04:03:58 +00:00
|
|
|
if (kqueueop->changes == NULL)
|
|
|
|
goto err;
|
2009-11-15 19:00:12 +00:00
|
|
|
kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent));
|
2009-10-27 04:03:58 +00:00
|
|
|
if (kqueueop->events == NULL)
|
|
|
|
goto err;
|
2010-01-14 17:04:08 -05:00
|
|
|
kqueueop->events_size = kqueueop->changes_size = NEVENT;
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2005-05-11 03:34:42 +00:00
|
|
|
/* Check for Mac OS X kqueue bug. */
|
2009-11-29 10:20:46 -05:00
|
|
|
memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]);
|
2005-05-11 03:34:42 +00:00
|
|
|
kqueueop->changes[0].ident = -1;
|
|
|
|
kqueueop->changes[0].filter = EVFILT_READ;
|
|
|
|
kqueueop->changes[0].flags = EV_ADD;
|
2009-01-27 21:10:31 +00:00
|
|
|
/*
|
2005-05-11 03:34:42 +00:00
|
|
|
* If kqueue works, then kevent will succeed, and it will
|
|
|
|
* stick an error in events[0]. If kqueue is broken, then
|
|
|
|
* kevent will fail.
|
|
|
|
*/
|
|
|
|
if (kevent(kq,
|
|
|
|
kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
|
2010-10-14 13:15:32 -04:00
|
|
|
(int)kqueueop->events[0].ident != -1 ||
|
2016-07-08 09:43:39 -04:00
|
|
|
!(kqueueop->events[0].flags & EV_ERROR)) {
|
2005-05-11 03:34:42 +00:00
|
|
|
event_warn("%s: detected broken kqueue; not using.", __func__);
|
2009-10-27 04:03:58 +00:00
|
|
|
goto err;
|
2005-05-11 03:34:42 +00:00
|
|
|
}
|
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
base->evsigsel = &kqsigops;
|
|
|
|
|
2005-01-03 18:58:40 +00:00
|
|
|
return (kqueueop);
|
2009-10-27 04:03:58 +00:00
|
|
|
err:
|
2009-10-30 22:43:53 +00:00
|
|
|
if (kqueueop)
|
|
|
|
kqop_free(kqueueop);
|
|
|
|
|
2009-10-27 04:03:58 +00:00
|
|
|
return (NULL);
|
2002-04-09 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 14:56:19 -04:00
|
|
|
#define ADD_UDATA 0x30303
|
|
|
|
|
2002-06-13 01:54:07 +00:00
|
|
|
static void
|
2010-01-14 16:31:05 -05:00
|
|
|
kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change)
|
2002-06-13 01:54:07 +00:00
|
|
|
{
|
2012-03-13 08:33:06 +01:00
|
|
|
memset(out, 0, sizeof(struct kevent));
|
2010-01-14 16:31:05 -05:00
|
|
|
out->ident = fd;
|
|
|
|
out->filter = filter;
|
|
|
|
|
|
|
|
if (change & EV_CHANGE_ADD) {
|
|
|
|
out->flags = EV_ADD;
|
2011-06-08 14:56:19 -04:00
|
|
|
/* We set a magic number here so that we can tell 'add'
|
|
|
|
* errors from 'del' errors. */
|
|
|
|
out->udata = INT_TO_UDATA(ADD_UDATA);
|
2010-01-14 16:31:05 -05:00
|
|
|
if (change & EV_ET)
|
|
|
|
out->flags |= EV_CLEAR;
|
|
|
|
#ifdef NOTE_EOF
|
|
|
|
/* Make it behave like select() and poll() */
|
|
|
|
if (filter == EVFILT_READ)
|
|
|
|
out->fflags = NOTE_EOF;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
EVUTIL_ASSERT(change & EV_CHANGE_DEL);
|
|
|
|
out->flags = EV_DELETE;
|
|
|
|
}
|
2002-06-13 01:54:07 +00:00
|
|
|
}
|
|
|
|
|
2010-01-14 16:31:05 -05:00
|
|
|
static int
|
|
|
|
kq_build_changes_list(const struct event_changelist *changelist,
|
|
|
|
struct kqop *kqop)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n_changes = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < changelist->n_changes; ++i) {
|
|
|
|
struct event_change *in_ch = &changelist->changes[i];
|
|
|
|
struct kevent *out_ch;
|
|
|
|
if (n_changes >= kqop->changes_size - 1) {
|
2019-05-10 23:54:14 +02:00
|
|
|
int newsize;
|
2010-01-14 16:31:05 -05:00
|
|
|
struct kevent *newchanges;
|
|
|
|
|
2019-05-10 23:54:14 +02:00
|
|
|
if (kqop->changes_size > INT_MAX / 2 ||
|
|
|
|
(size_t)kqop->changes_size * 2 > EV_SIZE_MAX /
|
|
|
|
sizeof(struct kevent)) {
|
2019-05-07 20:53:17 +02:00
|
|
|
event_warnx("%s: int overflow", __func__);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2019-05-10 23:54:14 +02:00
|
|
|
newsize = kqop->changes_size * 2;
|
2010-01-14 16:31:05 -05:00
|
|
|
newchanges = mm_realloc(kqop->changes,
|
|
|
|
newsize * sizeof(struct kevent));
|
|
|
|
if (newchanges == NULL) {
|
|
|
|
event_warn("%s: realloc", __func__);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
kqop->changes = newchanges;
|
|
|
|
kqop->changes_size = newsize;
|
|
|
|
}
|
|
|
|
if (in_ch->read_change) {
|
|
|
|
out_ch = &kqop->changes[n_changes++];
|
|
|
|
kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ,
|
|
|
|
in_ch->read_change);
|
|
|
|
}
|
|
|
|
if (in_ch->write_change) {
|
|
|
|
out_ch = &kqop->changes[n_changes++];
|
|
|
|
kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE,
|
|
|
|
in_ch->write_change);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n_changes;
|
|
|
|
}
|
|
|
|
|
2011-05-03 13:54:57 -04:00
|
|
|
static int
|
|
|
|
kq_grow_events(struct kqop *kqop, size_t new_size)
|
|
|
|
{
|
|
|
|
struct kevent *newresult;
|
|
|
|
|
|
|
|
newresult = mm_realloc(kqop->events,
|
|
|
|
new_size * sizeof(struct kevent));
|
|
|
|
|
|
|
|
if (newresult) {
|
|
|
|
kqop->events = newresult;
|
|
|
|
kqop->events_size = new_size;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-29 01:45:45 +00:00
|
|
|
static int
|
2008-12-23 16:37:01 +00:00
|
|
|
kq_dispatch(struct event_base *base, struct timeval *tv)
|
2002-04-09 15:14:06 +00:00
|
|
|
{
|
2008-12-23 16:37:01 +00:00
|
|
|
struct kqop *kqop = base->evbase;
|
2002-04-09 15:14:06 +00:00
|
|
|
struct kevent *events = kqop->events;
|
2010-01-14 17:04:08 -05:00
|
|
|
struct kevent *changes;
|
2007-07-30 22:41:00 +00:00
|
|
|
struct timespec ts, *ts_p = NULL;
|
2010-01-14 16:31:05 -05:00
|
|
|
int i, n_changes, res;
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2007-07-30 22:41:00 +00:00
|
|
|
if (tv != NULL) {
|
2015-08-25 15:32:25 +02:00
|
|
|
ts.tv_sec = tv->tv_sec;
|
|
|
|
ts.tv_nsec = tv->tv_usec * 1000;
|
2007-07-30 22:41:00 +00:00
|
|
|
ts_p = &ts;
|
|
|
|
}
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2010-01-14 16:31:05 -05:00
|
|
|
/* Build "changes" from "base->changes" */
|
2010-01-14 17:04:08 -05:00
|
|
|
EVUTIL_ASSERT(kqop->changes);
|
2010-01-14 16:31:05 -05:00
|
|
|
n_changes = kq_build_changes_list(&base->changelist, kqop);
|
|
|
|
if (n_changes < 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-02-29 15:07:33 -05:00
|
|
|
event_changelist_remove_all_(&base->changelist, base);
|
2010-01-14 16:31:05 -05:00
|
|
|
|
2010-01-14 17:04:08 -05:00
|
|
|
/* steal the changes array in case some broken code tries to call
|
|
|
|
* dispatch twice at once. */
|
|
|
|
changes = kqop->changes;
|
|
|
|
kqop->changes = NULL;
|
2009-10-21 03:54:00 +00:00
|
|
|
|
2011-05-03 13:54:57 -04:00
|
|
|
/* Make sure that 'events' is at least as long as the list of changes:
|
|
|
|
* otherwise errors in the changes can get reported as a -1 return
|
|
|
|
* value from kevent() rather than as EV_ERROR events in the events
|
|
|
|
* array.
|
|
|
|
*
|
|
|
|
* (We could instead handle -1 return values from kevent() by
|
|
|
|
* retrying with a smaller changes array or a larger events array,
|
|
|
|
* but this approach seems less risky for now.)
|
|
|
|
*/
|
|
|
|
if (kqop->events_size < n_changes) {
|
|
|
|
int new_size = kqop->events_size;
|
|
|
|
do {
|
|
|
|
new_size *= 2;
|
|
|
|
} while (new_size < n_changes);
|
|
|
|
|
|
|
|
kq_grow_events(kqop, new_size);
|
|
|
|
events = kqop->events;
|
|
|
|
}
|
|
|
|
|
2009-11-27 16:44:47 -05:00
|
|
|
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
2009-10-21 03:54:00 +00:00
|
|
|
|
2010-01-14 17:04:08 -05:00
|
|
|
res = kevent(kqop->kq, changes, n_changes,
|
2009-10-21 03:54:00 +00:00
|
|
|
events, kqop->events_size, ts_p);
|
|
|
|
|
2009-11-27 16:44:47 -05:00
|
|
|
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
2009-10-21 03:54:00 +00:00
|
|
|
|
2010-01-14 17:04:08 -05:00
|
|
|
EVUTIL_ASSERT(kqop->changes == NULL);
|
|
|
|
kqop->changes = changes;
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
if (res == -1) {
|
|
|
|
if (errno != EINTR) {
|
2010-02-18 17:41:15 -05:00
|
|
|
event_warn("kevent");
|
2002-04-09 15:14:06 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-03-29 07:03:10 +00:00
|
|
|
event_debug(("%s: kevent reports %d", __func__, res));
|
2002-04-09 15:14:06 +00:00
|
|
|
|
|
|
|
for (i = 0; i < res; i++) {
|
|
|
|
int which = 0;
|
|
|
|
|
|
|
|
if (events[i].flags & EV_ERROR) {
|
2011-06-08 14:56:19 -04:00
|
|
|
switch (events[i].data) {
|
|
|
|
|
|
|
|
/* Can occur on delete if we are not currently
|
|
|
|
* watching any events on this fd. That can
|
|
|
|
* happen when the fd was closed and another
|
|
|
|
* file was opened with that fd. */
|
|
|
|
case ENOENT:
|
|
|
|
/* Can occur for reasons not fully understood
|
|
|
|
* on FreeBSD. */
|
|
|
|
case EINVAL:
|
2002-04-09 15:14:06 +00:00
|
|
|
continue;
|
2014-06-07 12:57:02 -07:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
/*
|
|
|
|
* This currently occurs if an FD is closed
|
|
|
|
* before the EV_DELETE makes it out via kevent().
|
|
|
|
* The FreeBSD capabilities code sees the blank
|
|
|
|
* capability set and rejects the request to
|
|
|
|
* modify an event.
|
|
|
|
*
|
|
|
|
* To be strictly correct - when an FD is closed,
|
|
|
|
* all the registered events are also removed.
|
|
|
|
* Queuing EV_DELETE to a closed FD is wrong.
|
|
|
|
* The event(s) should just be deleted from
|
|
|
|
* the pending changelist.
|
|
|
|
*/
|
|
|
|
case ENOTCAPABLE:
|
|
|
|
continue;
|
|
|
|
#endif
|
2002-04-09 15:14:06 +00:00
|
|
|
|
2012-02-10 11:24:51 -05:00
|
|
|
/* Can occur on a delete if the fd is closed. */
|
2011-06-08 14:56:19 -04:00
|
|
|
case EBADF:
|
2012-02-10 11:24:51 -05:00
|
|
|
/* XXXX On NetBSD, we can also get EBADF if we
|
|
|
|
* try to add the write side of a pipe, but
|
|
|
|
* the read side has already been closed.
|
|
|
|
* Other BSDs call this situation 'EPIPE'. It
|
|
|
|
* would be good if we had a way to report
|
|
|
|
* this situation. */
|
|
|
|
continue;
|
2011-06-08 14:56:19 -04:00
|
|
|
/* These two can occur on an add if the fd was one side
|
|
|
|
* of a pipe, and the other side was closed. */
|
|
|
|
case EPERM:
|
|
|
|
case EPIPE:
|
|
|
|
/* Report read events, if we're listening for
|
|
|
|
* them, so that the user can learn about any
|
|
|
|
* add errors. (If the operation was a
|
|
|
|
* delete, then udata should be cleared.) */
|
|
|
|
if (events[i].udata) {
|
|
|
|
/* The operation was an add:
|
|
|
|
* report the error as a read. */
|
|
|
|
which |= EV_READ;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* The operation was a del:
|
|
|
|
* report nothing. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Other errors shouldn't occur. */
|
|
|
|
default:
|
|
|
|
errno = events[i].data;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} else if (events[i].filter == EVFILT_READ) {
|
2002-04-09 15:14:06 +00:00
|
|
|
which |= EV_READ;
|
|
|
|
} else if (events[i].filter == EVFILT_WRITE) {
|
|
|
|
which |= EV_WRITE;
|
2002-04-10 02:10:47 +00:00
|
|
|
} else if (events[i].filter == EVFILT_SIGNAL) {
|
|
|
|
which |= EV_SIGNAL;
|
2010-09-17 00:34:13 -04:00
|
|
|
#ifdef EVFILT_USER
|
|
|
|
} else if (events[i].filter == EVFILT_USER) {
|
|
|
|
base->is_notify_pending = 0;
|
|
|
|
#endif
|
2003-03-08 16:33:18 +00:00
|
|
|
}
|
2002-06-11 18:38:37 +00:00
|
|
|
|
|
|
|
if (!which)
|
|
|
|
continue;
|
|
|
|
|
2008-07-11 15:49:04 +00:00
|
|
|
if (events[i].filter == EVFILT_SIGNAL) {
|
2012-02-29 15:07:33 -05:00
|
|
|
evmap_signal_active_(base, events[i].ident, 1);
|
2008-07-11 15:49:04 +00:00
|
|
|
} else {
|
2012-02-29 15:07:33 -05:00
|
|
|
evmap_io_active_(base, events[i].ident, which | EV_ET);
|
2008-07-11 15:49:04 +00:00
|
|
|
}
|
2002-06-11 18:38:37 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 06:03:00 +00:00
|
|
|
if (res == kqop->events_size) {
|
2009-10-21 03:54:00 +00:00
|
|
|
/* We used all the events space that we have. Maybe we should
|
|
|
|
make it bigger. */
|
2011-05-03 13:54:57 -04:00
|
|
|
kq_grow_events(kqop, kqop->events_size * 2);
|
2009-10-21 03:54:00 +00:00
|
|
|
}
|
|
|
|
|
2002-04-09 15:14:06 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-03-29 01:45:45 +00:00
|
|
|
static void
|
2009-10-30 22:43:53 +00:00
|
|
|
kqop_free(struct kqop *kqop)
|
2006-03-28 04:40:54 +00:00
|
|
|
{
|
|
|
|
if (kqop->changes)
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(kqop->changes);
|
2006-03-28 04:40:54 +00:00
|
|
|
if (kqop->events)
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(kqop->events);
|
2008-03-02 01:46:00 +00:00
|
|
|
if (kqop->kq >= 0 && kqop->pid == getpid())
|
2006-03-28 04:40:54 +00:00
|
|
|
close(kqop->kq);
|
|
|
|
memset(kqop, 0, sizeof(struct kqop));
|
2008-04-25 01:18:08 +00:00
|
|
|
mm_free(kqop);
|
2006-03-28 04:40:54 +00:00
|
|
|
}
|
2008-12-23 16:37:01 +00:00
|
|
|
|
2009-10-30 22:43:53 +00:00
|
|
|
static void
|
|
|
|
kq_dealloc(struct event_base *base)
|
|
|
|
{
|
|
|
|
struct kqop *kqop = base->evbase;
|
2012-02-29 15:07:33 -05:00
|
|
|
evsig_dealloc_(base);
|
2009-10-30 22:43:53 +00:00
|
|
|
kqop_free(kqop);
|
|
|
|
}
|
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
/* signal handling */
|
|
|
|
static int
|
2009-01-18 01:33:18 +00:00
|
|
|
kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p)
|
2008-12-23 16:37:01 +00:00
|
|
|
{
|
|
|
|
struct kqop *kqop = base->evbase;
|
|
|
|
struct kevent kev;
|
|
|
|
struct timespec timeout = { 0, 0 };
|
2009-01-18 01:33:18 +00:00
|
|
|
(void)p;
|
2008-12-23 16:37:01 +00:00
|
|
|
|
2009-10-26 20:00:43 +00:00
|
|
|
EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
memset(&kev, 0, sizeof(kev));
|
|
|
|
kev.ident = nsignal;
|
|
|
|
kev.filter = EVFILT_SIGNAL;
|
|
|
|
kev.flags = EV_ADD;
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
/* Be ready for the signal if it is sent any
|
|
|
|
* time between now and the next call to
|
|
|
|
* kq_dispatch. */
|
|
|
|
if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
|
|
|
|
return (-1);
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2011-08-11 09:34:51 -07:00
|
|
|
/* We can set the handler for most signals to SIG_IGN and
|
|
|
|
* still have them reported to us in the queue. However,
|
|
|
|
* if the handler for SIGCHLD is SIG_IGN, the system reaps
|
|
|
|
* zombie processes for us, and we don't get any notification.
|
|
|
|
* This appears to be the only signal with this quirk. */
|
2012-02-29 15:07:32 -05:00
|
|
|
if (evsig_set_handler_(base, nsignal,
|
2011-08-11 09:34:51 -07:00
|
|
|
nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1)
|
2008-12-23 16:37:01 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-01-18 01:33:18 +00:00
|
|
|
kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p)
|
2008-12-23 16:37:01 +00:00
|
|
|
{
|
|
|
|
struct kqop *kqop = base->evbase;
|
|
|
|
struct kevent kev;
|
|
|
|
|
|
|
|
struct timespec timeout = { 0, 0 };
|
2009-01-18 01:33:18 +00:00
|
|
|
(void)p;
|
2008-12-23 16:37:01 +00:00
|
|
|
|
2009-10-26 20:00:43 +00:00
|
|
|
EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
|
2008-12-23 16:37:01 +00:00
|
|
|
|
|
|
|
memset(&kev, 0, sizeof(kev));
|
|
|
|
kev.ident = nsignal;
|
|
|
|
kev.filter = EVFILT_SIGNAL;
|
|
|
|
kev.flags = EV_DELETE;
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2008-12-23 16:37:01 +00:00
|
|
|
/* Because we insert signal events
|
|
|
|
* immediately, we need to delete them
|
|
|
|
* immediately, too */
|
|
|
|
if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
|
|
|
|
return (-1);
|
|
|
|
|
2012-02-29 15:07:32 -05:00
|
|
|
if (evsig_restore_handler_(base, nsignal) == -1)
|
2008-12-23 16:37:01 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2012-02-08 18:46:00 +02:00
|
|
|
|
2010-09-17 00:34:13 -04:00
|
|
|
|
|
|
|
/* OSX 10.6 and FreeBSD 8.1 add support for EVFILT_USER, which we can use
|
|
|
|
* to wake up the event loop from another thread. */
|
|
|
|
|
|
|
|
/* Magic number we use for our filter ID. */
|
|
|
|
#define NOTIFY_IDENT 42
|
|
|
|
|
|
|
|
int
|
|
|
|
event_kq_add_notify_event_(struct event_base *base)
|
|
|
|
{
|
|
|
|
struct kqop *kqop = base->evbase;
|
|
|
|
#if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
|
|
|
|
struct kevent kev;
|
|
|
|
struct timespec timeout = { 0, 0 };
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (kqop->notify_event_added)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
|
|
|
|
memset(&kev, 0, sizeof(kev));
|
|
|
|
kev.ident = NOTIFY_IDENT;
|
|
|
|
kev.filter = EVFILT_USER;
|
|
|
|
kev.flags = EV_ADD | EV_CLEAR;
|
|
|
|
|
|
|
|
if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
|
|
|
|
event_warn("kevent: adding EVFILT_USER event");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
kqop->notify_event_added = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
event_kq_notify_base_(struct event_base *base)
|
|
|
|
{
|
|
|
|
struct kqop *kqop = base->evbase;
|
|
|
|
#if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
|
|
|
|
struct kevent kev;
|
|
|
|
struct timespec timeout = { 0, 0 };
|
|
|
|
#endif
|
|
|
|
if (! kqop->notify_event_added)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
|
|
|
|
memset(&kev, 0, sizeof(kev));
|
|
|
|
kev.ident = NOTIFY_IDENT;
|
|
|
|
kev.filter = EVFILT_USER;
|
|
|
|
kev.fflags = NOTE_TRIGGER;
|
|
|
|
|
|
|
|
if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
|
|
|
|
event_warn("kevent: triggering EVFILT_USER event");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-29 15:07:31 -05:00
|
|
|
#endif /* EVENT__HAVE_KQUEUE */
|