1 /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */
4 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5 * Copyright 2007-2010 Niels Provos and Nick Mathewson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "event2/event-config.h"
33 #include <sys/types.h>
34 #ifdef _EVENT_HAVE_SYS_TIME_H
37 #include <sys/queue.h>
38 #include <sys/event.h>
45 #ifdef _EVENT_HAVE_INTTYPES_H
49 /* Some platforms apparently define the udata field of struct kevent as
50 * intptr_t, whereas others define it as void*. There doesn't seem to be an
51 * easy way to tell them apart via autoconf, so we need to use OS macros. */
52 #if defined(_EVENT_HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__)
53 #define PTR_TO_UDATA(x) ((intptr_t)(x))
55 #define PTR_TO_UDATA(x) (x)
58 #include "event-internal.h"
59 #include "log-internal.h"
60 #include "evmap-internal.h"
61 #include "event2/thread.h"
62 #include "evthread-internal.h"
63 #include "changelist-internal.h"
68 struct kevent *changes;
71 struct kevent *events;
77 static void kqop_free(struct kqop *kqop);
79 static void *kq_init(struct event_base *);
80 static int kq_sig_add(struct event_base *, int, short, short, void *);
81 static int kq_sig_del(struct event_base *, int, short, short, void *);
82 static int kq_dispatch(struct event_base *, struct timeval *);
83 static void kq_dealloc(struct event_base *);
85 const struct eventop kqops = {
93 EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS,
94 EVENT_CHANGELIST_FDINFO_SIZE
97 static const struct eventop kqsigops = {
110 kq_init(struct event_base *base)
113 struct kqop *kqueueop = NULL;
115 if (!(kqueueop = mm_calloc(1, sizeof(struct kqop))))
118 /* Initialize the kernel queue */
120 if ((kq = kqueue()) == -1) {
121 event_warn("kqueue");
127 kqueueop->pid = getpid();
129 /* Initialize fields */
130 kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent));
131 if (kqueueop->changes == NULL)
133 kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent));
134 if (kqueueop->events == NULL)
136 kqueueop->events_size = kqueueop->changes_size = NEVENT;
138 /* Check for Mac OS X kqueue bug. */
139 memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]);
140 kqueueop->changes[0].ident = -1;
141 kqueueop->changes[0].filter = EVFILT_READ;
142 kqueueop->changes[0].flags = EV_ADD;
144 * If kqueue works, then kevent will succeed, and it will
145 * stick an error in events[0]. If kqueue is broken, then
149 kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
150 (int)kqueueop->events[0].ident != -1 ||
151 kqueueop->events[0].flags != EV_ERROR) {
152 event_warn("%s: detected broken kqueue; not using.", __func__);
156 base->evsigsel = &kqsigops;
167 kq_sighandler(int sig)
169 /* Do nothing here */
173 kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change)
175 memset(out, 0, sizeof(out));
177 out->filter = filter;
179 if (change & EV_CHANGE_ADD) {
182 out->flags |= EV_CLEAR;
184 /* Make it behave like select() and poll() */
185 if (filter == EVFILT_READ)
186 out->fflags = NOTE_EOF;
189 EVUTIL_ASSERT(change & EV_CHANGE_DEL);
190 out->flags = EV_DELETE;
195 kq_build_changes_list(const struct event_changelist *changelist,
201 for (i = 0; i < changelist->n_changes; ++i) {
202 struct event_change *in_ch = &changelist->changes[i];
203 struct kevent *out_ch;
204 if (n_changes >= kqop->changes_size - 1) {
205 int newsize = kqop->changes_size * 2;
206 struct kevent *newchanges;
208 newchanges = mm_realloc(kqop->changes,
209 newsize * sizeof(struct kevent));
210 if (newchanges == NULL) {
211 event_warn("%s: realloc", __func__);
214 kqop->changes = newchanges;
215 kqop->changes_size = newsize;
217 if (in_ch->read_change) {
218 out_ch = &kqop->changes[n_changes++];
219 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ,
222 if (in_ch->write_change) {
223 out_ch = &kqop->changes[n_changes++];
224 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE,
225 in_ch->write_change);
232 kq_grow_events(struct kqop *kqop, size_t new_size)
234 struct kevent *newresult;
236 newresult = mm_realloc(kqop->events,
237 new_size * sizeof(struct kevent));
240 kqop->events = newresult;
241 kqop->events_size = new_size;
249 kq_dispatch(struct event_base *base, struct timeval *tv)
251 struct kqop *kqop = base->evbase;
252 struct kevent *events = kqop->events;
253 struct kevent *changes;
254 struct timespec ts, *ts_p = NULL;
255 int i, n_changes, res;
258 TIMEVAL_TO_TIMESPEC(tv, &ts);
262 /* Build "changes" from "base->changes" */
263 EVUTIL_ASSERT(kqop->changes);
264 n_changes = kq_build_changes_list(&base->changelist, kqop);
268 event_changelist_remove_all(&base->changelist, base);
270 /* steal the changes array in case some broken code tries to call
271 * dispatch twice at once. */
272 changes = kqop->changes;
273 kqop->changes = NULL;
275 /* Make sure that 'events' is at least as long as the list of changes:
276 * otherwise errors in the changes can get reported as a -1 return
277 * value from kevent() rather than as EV_ERROR events in the events
280 * (We could instead handle -1 return values from kevent() by
281 * retrying with a smaller changes array or a larger events array,
282 * but this approach seems less risky for now.)
284 if (kqop->events_size < n_changes) {
285 int new_size = kqop->events_size;
288 } while (new_size < n_changes);
290 kq_grow_events(kqop, new_size);
291 events = kqop->events;
294 EVBASE_RELEASE_LOCK(base, th_base_lock);
296 res = kevent(kqop->kq, changes, n_changes,
297 events, kqop->events_size, ts_p);
299 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
301 EVUTIL_ASSERT(kqop->changes == NULL);
302 kqop->changes = changes;
305 if (errno != EINTR) {
306 event_warn("kevent");
313 event_debug(("%s: kevent reports %d", __func__, res));
315 for (i = 0; i < res; i++) {
318 if (events[i].flags & EV_ERROR) {
320 * Error messages that can happen, when a delete fails.
321 * EBADF happens when the file descriptor has been
323 * ENOENT when the file descriptor was closed and
325 * EINVAL for some reasons not understood; EINVAL
326 * should not be returned ever; but FreeBSD does :-\
327 * An error is also indicated when a callback deletes
328 * an event we are still processing. In that case
329 * the data field is set to ENOENT.
331 if (events[i].data == EBADF ||
332 events[i].data == EINVAL ||
333 events[i].data == ENOENT)
335 errno = events[i].data;
339 if (events[i].filter == EVFILT_READ) {
341 } else if (events[i].filter == EVFILT_WRITE) {
343 } else if (events[i].filter == EVFILT_SIGNAL) {
350 if (events[i].filter == EVFILT_SIGNAL) {
351 evmap_signal_active(base, events[i].ident, 1);
353 evmap_io_active(base, events[i].ident, which | EV_ET);
357 if (res == kqop->events_size) {
358 /* We used all the events space that we have. Maybe we should
360 kq_grow_events(kqop, kqop->events_size * 2);
367 kqop_free(struct kqop *kqop)
370 mm_free(kqop->changes);
372 mm_free(kqop->events);
373 if (kqop->kq >= 0 && kqop->pid == getpid())
375 memset(kqop, 0, sizeof(struct kqop));
380 kq_dealloc(struct event_base *base)
382 struct kqop *kqop = base->evbase;
387 /* signal handling */
389 kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p)
391 struct kqop *kqop = base->evbase;
393 struct timespec timeout = { 0, 0 };
396 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
398 memset(&kev, 0, sizeof(kev));
400 kev.filter = EVFILT_SIGNAL;
403 /* Be ready for the signal if it is sent any
404 * time between now and the next call to
406 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
409 /* XXXX The manpage suggest we could use SIG_IGN instead of a
410 * do-nothing handler */
411 if (_evsig_set_handler(base, nsignal, kq_sighandler) == -1)
418 kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p)
420 struct kqop *kqop = base->evbase;
423 struct timespec timeout = { 0, 0 };
426 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
428 memset(&kev, 0, sizeof(kev));
430 kev.filter = EVFILT_SIGNAL;
431 kev.flags = EV_DELETE;
433 /* Because we insert signal events
434 * immediately, we need to delete them
435 * immediately, too */
436 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
439 if (_evsig_restore_handler(base, nsignal) == -1)