1 /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */
4 * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
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.
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_TIME_H
37 #include <sys/_time.h>
39 #include <sys/queue.h>
40 #include <sys/event.h>
47 #ifdef HAVE_INTTYPES_H
51 #if defined(HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
52 #define INTPTR(x) (intptr_t)x
60 #define EVLIST_X_KQINKERNEL 0x1000
65 struct kevent *changes;
67 struct kevent *events;
73 int kq_add (void *, struct event *);
74 int kq_del (void *, struct event *);
75 int kq_recalc (struct event_base *, void *, int);
76 int kq_dispatch (struct event_base *, void *, struct timeval *);
77 int kq_insert (struct kqop *, struct kevent *);
78 void kq_dealloc (void *);
80 const struct eventop kqops = {
94 struct kqop *kqueueop;
96 /* Disable kqueue when this environment variable is set */
97 if (getenv("EVENT_NOKQUEUE"))
100 if (!(kqueueop = calloc(1, sizeof(struct kqop))))
103 /* Initalize the kernel queue */
105 if ((kq = kqueue()) == -1) {
106 event_warn("kqueue");
113 /* Initalize fields */
114 kqueueop->changes = malloc(NEVENT * sizeof(struct kevent));
115 if (kqueueop->changes == NULL) {
119 kqueueop->events = malloc(NEVENT * sizeof(struct kevent));
120 if (kqueueop->events == NULL) {
121 free (kqueueop->changes);
125 kqueueop->nevents = NEVENT;
127 /* Check for Mac OS X kqueue bug. */
128 kqueueop->changes[0].ident = -1;
129 kqueueop->changes[0].filter = EVFILT_READ;
130 kqueueop->changes[0].flags = EV_ADD;
132 * If kqueue works, then kevent will succeed, and it will
133 * stick an error in events[0]. If kqueue is broken, then
137 kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
138 kqueueop->events[0].ident != -1 ||
139 kqueueop->events[0].flags != EV_ERROR) {
140 event_warn("%s: detected broken kqueue; not using.", __func__);
141 free(kqueueop->changes);
142 free(kqueueop->events);
152 kq_recalc(struct event_base *base, void *arg, int max)
158 kq_insert(struct kqop *kqop, struct kevent *kev)
160 int nevents = kqop->nevents;
162 if (kqop->nchanges == nevents) {
163 struct kevent *newchange;
164 struct kevent *newresult;
168 newchange = realloc(kqop->changes,
169 nevents * sizeof(struct kevent));
170 if (newchange == NULL) {
171 event_warn("%s: malloc", __func__);
174 kqop->changes = newchange;
176 newresult = realloc(kqop->events,
177 nevents * sizeof(struct kevent));
180 * If we fail, we don't have to worry about freeing,
181 * the next realloc will pick it up.
183 if (newresult == NULL) {
184 event_warn("%s: malloc", __func__);
187 kqop->events = newresult;
189 kqop->nevents = nevents;
192 memcpy(&kqop->changes[kqop->nchanges++], kev, sizeof(struct kevent));
194 event_debug(("%s: fd %d %s%s",
195 __func__, kev->ident,
196 kev->filter == EVFILT_READ ? "EVFILT_READ" : "EVFILT_WRITE",
197 kev->flags == EV_DELETE ? " (del)" : ""));
203 kq_sighandler(int sig)
205 /* Do nothing here */
209 kq_dispatch(struct event_base *base, void *arg, struct timeval *tv)
211 struct kqop *kqop = arg;
212 struct kevent *changes = kqop->changes;
213 struct kevent *events = kqop->events;
218 TIMEVAL_TO_TIMESPEC(tv, &ts);
220 res = kevent(kqop->kq, changes, kqop->nchanges,
221 events, kqop->nevents, &ts);
224 if (errno != EINTR) {
225 event_warn("kevent");
232 event_debug(("%s: kevent reports %d", __func__, res));
234 for (i = 0; i < res; i++) {
237 if (events[i].flags & EV_ERROR) {
239 * Error messages that can happen, when a delete fails.
240 * EBADF happens when the file discriptor has been
242 * ENOENT when the file discriptor was closed and
244 * EINVAL for some reasons not understood; EINVAL
245 * should not be returned ever; but FreeBSD does :-\
246 * An error is also indicated when a callback deletes
247 * an event we are still processing. In that case
248 * the data field is set to ENOENT.
250 if (events[i].data == EBADF ||
251 events[i].data == EINVAL ||
252 events[i].data == ENOENT)
254 errno = events[i].data;
258 ev = (struct event *)events[i].udata;
260 if (events[i].filter == EVFILT_READ) {
262 } else if (events[i].filter == EVFILT_WRITE) {
264 } else if (events[i].filter == EVFILT_SIGNAL) {
271 if (!(ev->ev_events & EV_PERSIST))
274 event_active(ev, which,
275 ev->ev_events & EV_SIGNAL ? events[i].data : 1);
283 kq_add(void *arg, struct event *ev)
285 struct kqop *kqop = arg;
288 if (ev->ev_events & EV_SIGNAL) {
289 int nsignal = EVENT_SIGNAL(ev);
291 memset(&kev, 0, sizeof(kev));
293 kev.filter = EVFILT_SIGNAL;
295 if (!(ev->ev_events & EV_PERSIST))
296 kev.flags |= EV_ONESHOT;
297 kev.udata = INTPTR(ev);
299 if (kq_insert(kqop, &kev) == -1)
302 if (signal(nsignal, kq_sighandler) == SIG_ERR)
305 ev->ev_flags |= EVLIST_X_KQINKERNEL;
309 if (ev->ev_events & EV_READ) {
310 memset(&kev, 0, sizeof(kev));
311 kev.ident = ev->ev_fd;
312 kev.filter = EVFILT_READ;
314 /* Make it behave like select() and poll() */
315 kev.fflags = NOTE_EOF;
318 if (!(ev->ev_events & EV_PERSIST))
319 kev.flags |= EV_ONESHOT;
320 kev.udata = INTPTR(ev);
322 if (kq_insert(kqop, &kev) == -1)
325 ev->ev_flags |= EVLIST_X_KQINKERNEL;
328 if (ev->ev_events & EV_WRITE) {
329 memset(&kev, 0, sizeof(kev));
330 kev.ident = ev->ev_fd;
331 kev.filter = EVFILT_WRITE;
333 if (!(ev->ev_events & EV_PERSIST))
334 kev.flags |= EV_ONESHOT;
335 kev.udata = INTPTR(ev);
337 if (kq_insert(kqop, &kev) == -1)
340 ev->ev_flags |= EVLIST_X_KQINKERNEL;
347 kq_del(void *arg, struct event *ev)
349 struct kqop *kqop = arg;
352 if (!(ev->ev_flags & EVLIST_X_KQINKERNEL))
355 if (ev->ev_events & EV_SIGNAL) {
356 int nsignal = EVENT_SIGNAL(ev);
358 memset(&kev, 0, sizeof(kev));
360 kev.filter = EVFILT_SIGNAL;
361 kev.flags = EV_DELETE;
363 if (kq_insert(kqop, &kev) == -1)
366 if (signal(nsignal, SIG_DFL) == SIG_ERR)
369 ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
373 if (ev->ev_events & EV_READ) {
374 memset(&kev, 0, sizeof(kev));
375 kev.ident = ev->ev_fd;
376 kev.filter = EVFILT_READ;
377 kev.flags = EV_DELETE;
379 if (kq_insert(kqop, &kev) == -1)
382 ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
385 if (ev->ev_events & EV_WRITE) {
386 memset(&kev, 0, sizeof(kev));
387 kev.ident = ev->ev_fd;
388 kev.filter = EVFILT_WRITE;
389 kev.flags = EV_DELETE;
391 if (kq_insert(kqop, &kev) == -1)
394 ev->ev_flags &= ~EVLIST_X_KQINKERNEL;
401 kq_dealloc(void *arg)
403 struct kqop *kqop = arg;
411 memset(kqop, 0, sizeof(struct kqop));