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-2012 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))
54 #define INT_TO_UDATA(x) ((intptr_t)(x))
56 #define PTR_TO_UDATA(x) (x)
57 #define INT_TO_UDATA(x) ((void*)(x))
60 #include "event-internal.h"
61 #include "log-internal.h"
62 #include "evmap-internal.h"
63 #include "event2/thread.h"
64 #include "evthread-internal.h"
65 #include "changelist-internal.h"
70 struct kevent
*changes
;
73 struct kevent
*events
;
79 static void kqop_free(struct kqop
*kqop
);
81 static void *kq_init(struct event_base
*);
82 static int kq_sig_add(struct event_base
*, int, short, short, void *);
83 static int kq_sig_del(struct event_base
*, int, short, short, void *);
84 static int kq_dispatch(struct event_base
*, struct timeval
*);
85 static void kq_dealloc(struct event_base
*);
87 const struct eventop kqops
= {
95 EV_FEATURE_ET
|EV_FEATURE_O1
|EV_FEATURE_FDS
,
96 EVENT_CHANGELIST_FDINFO_SIZE
99 static const struct eventop kqsigops
= {
112 kq_init(struct event_base
*base
)
115 struct kqop
*kqueueop
= NULL
;
117 if (!(kqueueop
= mm_calloc(1, sizeof(struct kqop
))))
120 /* Initialize the kernel queue */
122 if ((kq
= kqueue()) == -1) {
123 event_warn("kqueue");
129 kqueueop
->pid
= getpid();
131 /* Initialize fields */
132 kqueueop
->changes
= mm_calloc(NEVENT
, sizeof(struct kevent
));
133 if (kqueueop
->changes
== NULL
)
135 kqueueop
->events
= mm_calloc(NEVENT
, sizeof(struct kevent
));
136 if (kqueueop
->events
== NULL
)
138 kqueueop
->events_size
= kqueueop
->changes_size
= NEVENT
;
140 /* Check for Mac OS X kqueue bug. */
141 memset(&kqueueop
->changes
[0], 0, sizeof kqueueop
->changes
[0]);
142 kqueueop
->changes
[0].ident
= -1;
143 kqueueop
->changes
[0].filter
= EVFILT_READ
;
144 kqueueop
->changes
[0].flags
= EV_ADD
;
146 * If kqueue works, then kevent will succeed, and it will
147 * stick an error in events[0]. If kqueue is broken, then
151 kqueueop
->changes
, 1, kqueueop
->events
, NEVENT
, NULL
) != 1 ||
152 (int)kqueueop
->events
[0].ident
!= -1 ||
153 kqueueop
->events
[0].flags
!= EV_ERROR
) {
154 event_warn("%s: detected broken kqueue; not using.", __func__
);
158 base
->evsigsel
= &kqsigops
;
169 kq_sighandler(int sig
)
171 /* Do nothing here */
174 #define ADD_UDATA 0x30303
177 kq_setup_kevent(struct kevent
*out
, evutil_socket_t fd
, int filter
, short change
)
179 memset(out
, 0, sizeof(struct kevent
));
181 out
->filter
= filter
;
183 if (change
& EV_CHANGE_ADD
) {
185 /* We set a magic number here so that we can tell 'add'
186 * errors from 'del' errors. */
187 out
->udata
= INT_TO_UDATA(ADD_UDATA
);
189 out
->flags
|= EV_CLEAR
;
191 /* Make it behave like select() and poll() */
192 if (filter
== EVFILT_READ
)
193 out
->fflags
= NOTE_EOF
;
196 EVUTIL_ASSERT(change
& EV_CHANGE_DEL
);
197 out
->flags
= EV_DELETE
;
202 kq_build_changes_list(const struct event_changelist
*changelist
,
208 for (i
= 0; i
< changelist
->n_changes
; ++i
) {
209 struct event_change
*in_ch
= &changelist
->changes
[i
];
210 struct kevent
*out_ch
;
211 if (n_changes
>= kqop
->changes_size
- 1) {
212 int newsize
= kqop
->changes_size
* 2;
213 struct kevent
*newchanges
;
215 newchanges
= mm_realloc(kqop
->changes
,
216 newsize
* sizeof(struct kevent
));
217 if (newchanges
== NULL
) {
218 event_warn("%s: realloc", __func__
);
221 kqop
->changes
= newchanges
;
222 kqop
->changes_size
= newsize
;
224 if (in_ch
->read_change
) {
225 out_ch
= &kqop
->changes
[n_changes
++];
226 kq_setup_kevent(out_ch
, in_ch
->fd
, EVFILT_READ
,
229 if (in_ch
->write_change
) {
230 out_ch
= &kqop
->changes
[n_changes
++];
231 kq_setup_kevent(out_ch
, in_ch
->fd
, EVFILT_WRITE
,
232 in_ch
->write_change
);
239 kq_grow_events(struct kqop
*kqop
, size_t new_size
)
241 struct kevent
*newresult
;
243 newresult
= mm_realloc(kqop
->events
,
244 new_size
* sizeof(struct kevent
));
247 kqop
->events
= newresult
;
248 kqop
->events_size
= new_size
;
256 kq_dispatch(struct event_base
*base
, struct timeval
*tv
)
258 struct kqop
*kqop
= base
->evbase
;
259 struct kevent
*events
= kqop
->events
;
260 struct kevent
*changes
;
261 struct timespec ts
, *ts_p
= NULL
;
262 int i
, n_changes
, res
;
265 TIMEVAL_TO_TIMESPEC(tv
, &ts
);
269 /* Build "changes" from "base->changes" */
270 EVUTIL_ASSERT(kqop
->changes
);
271 n_changes
= kq_build_changes_list(&base
->changelist
, kqop
);
275 event_changelist_remove_all(&base
->changelist
, base
);
277 /* steal the changes array in case some broken code tries to call
278 * dispatch twice at once. */
279 changes
= kqop
->changes
;
280 kqop
->changes
= NULL
;
282 /* Make sure that 'events' is at least as long as the list of changes:
283 * otherwise errors in the changes can get reported as a -1 return
284 * value from kevent() rather than as EV_ERROR events in the events
287 * (We could instead handle -1 return values from kevent() by
288 * retrying with a smaller changes array or a larger events array,
289 * but this approach seems less risky for now.)
291 if (kqop
->events_size
< n_changes
) {
292 int new_size
= kqop
->events_size
;
295 } while (new_size
< n_changes
);
297 kq_grow_events(kqop
, new_size
);
298 events
= kqop
->events
;
301 EVBASE_RELEASE_LOCK(base
, th_base_lock
);
303 res
= kevent(kqop
->kq
, changes
, n_changes
,
304 events
, kqop
->events_size
, ts_p
);
306 EVBASE_ACQUIRE_LOCK(base
, th_base_lock
);
308 EVUTIL_ASSERT(kqop
->changes
== NULL
);
309 kqop
->changes
= changes
;
312 if (errno
!= EINTR
) {
313 event_warn("kevent");
320 event_debug(("%s: kevent reports %d", __func__
, res
));
322 for (i
= 0; i
< res
; i
++) {
325 if (events
[i
].flags
& EV_ERROR
) {
326 switch (events
[i
].data
) {
328 /* Can occur on delete if we are not currently
329 * watching any events on this fd. That can
330 * happen when the fd was closed and another
331 * file was opened with that fd. */
333 /* Can occur for reasons not fully understood
338 /* Can occur on a delete if the fd is closed. */
340 /* XXXX On NetBSD, we can also get EBADF if we
341 * try to add the write side of a pipe, but
342 * the read side has already been closed.
343 * Other BSDs call this situation 'EPIPE'. It
344 * would be good if we had a way to report
347 /* These two can occur on an add if the fd was one side
348 * of a pipe, and the other side was closed. */
351 /* Report read events, if we're listening for
352 * them, so that the user can learn about any
353 * add errors. (If the operation was a
354 * delete, then udata should be cleared.) */
355 if (events
[i
].udata
) {
356 /* The operation was an add:
357 * report the error as a read. */
361 /* The operation was a del:
366 /* Other errors shouldn't occur. */
368 errno
= events
[i
].data
;
371 } else if (events
[i
].filter
== EVFILT_READ
) {
373 } else if (events
[i
].filter
== EVFILT_WRITE
) {
375 } else if (events
[i
].filter
== EVFILT_SIGNAL
) {
382 if (events
[i
].filter
== EVFILT_SIGNAL
) {
383 evmap_signal_active(base
, events
[i
].ident
, 1);
385 evmap_io_active(base
, events
[i
].ident
, which
| EV_ET
);
389 if (res
== kqop
->events_size
) {
390 /* We used all the events space that we have. Maybe we should
392 kq_grow_events(kqop
, kqop
->events_size
* 2);
399 kqop_free(struct kqop
*kqop
)
402 mm_free(kqop
->changes
);
404 mm_free(kqop
->events
);
405 if (kqop
->kq
>= 0 && kqop
->pid
== getpid())
407 memset(kqop
, 0, sizeof(struct kqop
));
412 kq_dealloc(struct event_base
*base
)
414 struct kqop
*kqop
= base
->evbase
;
419 /* signal handling */
421 kq_sig_add(struct event_base
*base
, int nsignal
, short old
, short events
, void *p
)
423 struct kqop
*kqop
= base
->evbase
;
425 struct timespec timeout
= { 0, 0 };
428 EVUTIL_ASSERT(nsignal
>= 0 && nsignal
< NSIG
);
430 memset(&kev
, 0, sizeof(kev
));
432 kev
.filter
= EVFILT_SIGNAL
;
435 /* Be ready for the signal if it is sent any
436 * time between now and the next call to
438 if (kevent(kqop
->kq
, &kev
, 1, NULL
, 0, &timeout
) == -1)
441 /* XXXX The manpage suggest we could use SIG_IGN instead of a
442 * do-nothing handler */
443 if (_evsig_set_handler(base
, nsignal
, kq_sighandler
) == -1)
450 kq_sig_del(struct event_base
*base
, int nsignal
, short old
, short events
, void *p
)
452 struct kqop
*kqop
= base
->evbase
;
455 struct timespec timeout
= { 0, 0 };
458 EVUTIL_ASSERT(nsignal
>= 0 && nsignal
< NSIG
);
460 memset(&kev
, 0, sizeof(kev
));
462 kev
.filter
= EVFILT_SIGNAL
;
463 kev
.flags
= EV_DELETE
;
465 /* Because we insert signal events
466 * immediately, we need to delete them
467 * immediately, too */
468 if (kevent(kqop
->kq
, &kev
, 1, NULL
, 0, &timeout
) == -1)
471 if (_evsig_restore_handler(base
, nsignal
) == -1)