Skip to content
Newer
Older
100644 568 lines (482 sloc) 14.4 KB
5f86585 @provos sync with openbsd; API change: timeout_ is now evtimer_
provos authored
1 /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */
2
aa6567f @provos Initial revision
provos authored
3 /*
b85b710 Update copyright statements to reflect the facts that:
Nick Mathewson authored
4 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
e49e289 Update copyright notices to 2012
Nick Mathewson authored
5 * Copyright 2007-2012 Niels Provos and Nick Mathewson
aa6567f @provos Initial revision
provos authored
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
c3f496c @provos minor corrections; change license to 3-clause BSD license
provos authored
15 * 3. The name of the author may not be used to endorse or promote products
aa6567f @provos Initial revision
provos authored
16 * derived from this software without specific prior written permission.
17 *
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.
28 */
ec347b9 Move event-config.h to include/event2
Nick Mathewson authored
29 #include "event2/event-config.h"
c13e185 @kev009 Remove use and reference to event-private.h
kev009 authored
30 #include "evconfig-private.h"
4d48cf6 Fix kqueue.c build on GNU/kFreeBSD systems.
Nick Mathewson authored
31
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
32 #ifdef EVENT__HAVE_KQUEUE
76d4c92 @rosslagerwall Put #ifdef around some files to support alternate build systems.
rosslagerwall authored
33
aa6567f @provos Initial revision
provos authored
34 #include <sys/types.h>
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
35 #ifdef EVENT__HAVE_SYS_TIME_H
aa6567f @provos Initial revision
provos authored
36 #include <sys/time.h>
9d2401f @provos portability fixes from marius@umich.edu.
provos authored
37 #endif
aa6567f @provos Initial revision
provos authored
38 #include <sys/queue.h>
39 #include <sys/event.h>
d10f85d @provos signal support for kqueue; support of EV_PERSIST flag to event_set
provos authored
40 #include <signal.h>
aa6567f @provos Initial revision
provos authored
41 #include <stdio.h>
42 #include <stdlib.h>
5f86585 @provos sync with openbsd; API change: timeout_ is now evtimer_
provos authored
43 #include <string.h>
aa6567f @provos Initial revision
provos authored
44 #include <unistd.h>
45 #include <errno.h>
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
46 #ifdef EVENT__HAVE_INTTYPES_H
0036d79 @provos fix kqueue problem
provos authored
47 #include <inttypes.h>
48 #endif
aa6567f @provos Initial revision
provos authored
49
7d82158 Fix compile warning on osx: the udata field in struct kevent is suppo…
Nick Mathewson authored
50 /* Some platforms apparently define the udata field of struct kevent as
51 * intptr_t, whereas others define it as void*. There doesn't seem to be an
52 * easy way to tell them apart via autoconf, so we need to use OS macros. */
5602e45 @EdSchouten Assume that ke_udata is an integer type on CloudABI.
EdSchouten authored
53 #if defined(EVENT__HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) && !defined(__CloudABI__)
7d82158 Fix compile warning on osx: the udata field in struct kevent is suppo…
Nick Mathewson authored
54 #define PTR_TO_UDATA(x) ((intptr_t)(x))
1fd34ab Report kqueue ebadf, epipe, and eperm as EV_READ events
Nick Mathewson authored
55 #define INT_TO_UDATA(x) ((intptr_t)(x))
0036d79 @provos fix kqueue problem
provos authored
56 #else
7d82158 Fix compile warning on osx: the udata field in struct kevent is suppo…
Nick Mathewson authored
57 #define PTR_TO_UDATA(x) (x)
1fd34ab Report kqueue ebadf, epipe, and eperm as EV_READ events
Nick Mathewson authored
58 #define INT_TO_UDATA(x) ((void*)(x))
0036d79 @provos fix kqueue problem
provos authored
59 #endif
60
8889785 @provos provide event_reinit() to reinitialized an event_base after fork - ne…
provos authored
61 #include "event-internal.h"
169321c Rename four internal headers to follow the -internal.h convention.
Nick Mathewson authored
62 #include "log-internal.h"
63 #include "evmap-internal.h"
b89b58b OSX compilation issues
Nick Mathewson authored
64 #include "event2/thread.h"
65 #include "evthread-internal.h"
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
66 #include "changelist-internal.h"
aa6567f @provos Initial revision
provos authored
67
53a07fe Replace pipe-based notification with EVFILT_USER where possible
Nick Mathewson authored
68 #include "kqueue-internal.h"
69
aa6567f @provos Initial revision
provos authored
70 #define NEVENT 64
71
72 struct kqop {
73 struct kevent *changes;
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
74 int changes_size;
75
aa6567f @provos Initial revision
provos authored
76 struct kevent *events;
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
77 int events_size;
aa6567f @provos Initial revision
provos authored
78 int kq;
53a07fe Replace pipe-based notification with EVFILT_USER where possible
Nick Mathewson authored
79 int notify_event_added;
19dad16 @provos Do not free the kqop file descriptor in other processes, also allow i…
provos authored
80 pid_t pid;
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
81 };
aa6567f @provos Initial revision
provos authored
82
315fde1 Remove some duplicate code in kqueue.c and fix a small memory leak.
Nick Mathewson authored
83 static void kqop_free(struct kqop *kqop);
84
2c2618d more whitespace normalization
Nick Mathewson authored
85 static void *kq_init(struct event_base *);
86 static int kq_sig_add(struct event_base *, int, short, short, void *);
87 static int kq_sig_del(struct event_base *, int, short, short, void *);
88 static int kq_dispatch(struct event_base *, struct timeval *);
89 static void kq_dealloc(struct event_base *);
aa6567f @provos Initial revision
provos authored
90
e506eaf @provos constify; some windows stuff by mike davis; fix a poll bug
provos authored
91 const struct eventop kqops = {
aa6567f @provos Initial revision
provos authored
92 "kqueue",
93 kq_init,
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
94 event_changelist_add_,
95 event_changelist_del_,
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven…
provos authored
96 kq_dispatch,
8889785 @provos provide event_reinit() to reinitialized an event_base after fork - ne…
provos authored
97 kq_dealloc,
0596592 Add new functions to access backends by their features and to query t…
Nick Mathewson authored
98 1 /* need reinit */,
99 EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS,
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
100 EVENT_CHANGELIST_FDINFO_SIZE
aa6567f @provos Initial revision
provos authored
101 };
102
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
103 static const struct eventop kqsigops = {
104 "kqueue_signal",
105 NULL,
106 kq_sig_add,
107 kq_sig_del,
108 NULL,
109 NULL,
110 1 /* need reinit */,
554e149 Move per-fd info from eventops into evmap. Not done for win32.c yet.
Nick Mathewson authored
111 0,
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
112 0
113 };
114
ca42671 @provos make event methods static so that they are not exported; from Andrei …
provos authored
115 static void *
41b7cbc @provos more the signal base into the event base; this removes global state a…
provos authored
116 kq_init(struct event_base *base)
aa6567f @provos Initial revision
provos authored
117 {
369aafc Refactor kq_init error handling.
Nick Mathewson authored
118 int kq = -1;
119 struct kqop *kqueueop = NULL;
aa6567f @provos Initial revision
provos authored
120
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
121 if (!(kqueueop = mm_calloc(1, sizeof(struct kqop))))
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
122 return (NULL);
aa6567f @provos Initial revision
provos authored
123
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
124 /* Initialize the kernel queue */
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
125
aa6567f @provos Initial revision
provos authored
126 if ((kq = kqueue()) == -1) {
fbdaf3a @provos debugging callbacks from Nick Mathewson <nickm@freehaven.net>
provos authored
127 event_warn("kqueue");
369aafc Refactor kq_init error handling.
Nick Mathewson authored
128 goto err;
aa6567f @provos Initial revision
provos authored
129 }
130
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
131 kqueueop->kq = kq;
aa6567f @provos Initial revision
provos authored
132
19dad16 @provos Do not free the kqop file descriptor in other processes, also allow i…
provos authored
133 kqueueop->pid = getpid();
134
e3fd294 Spelling fixes in comments and strings.
Nick Mathewson authored
135 /* Initialize fields */
18a8cfa Prefer calloc(a,b) to malloc(a*b). via openbsd.
Nick Mathewson authored
136 kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent));
369aafc Refactor kq_init error handling.
Nick Mathewson authored
137 if (kqueueop->changes == NULL)
138 goto err;
18a8cfa Prefer calloc(a,b) to malloc(a*b). via openbsd.
Nick Mathewson authored
139 kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent));
369aafc Refactor kq_init error handling.
Nick Mathewson authored
140 if (kqueueop->events == NULL)
141 goto err;
3225dfb Remove kqueue->pend_changes.
Nick Mathewson authored
142 kqueueop->events_size = kqueueop->changes_size = NEVENT;
aa6567f @provos Initial revision
provos authored
143
57fafe6 @provos detect kqueue bug in Mac OS X 10.4; from Nick Mathewson
provos authored
144 /* Check for Mac OS X kqueue bug. */
56771a3 @wahern Valgrind fix: Clear struct kevent before checking for OSX bug.
wahern authored
145 memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]);
57fafe6 @provos detect kqueue bug in Mac OS X 10.4; from Nick Mathewson
provos authored
146 kqueueop->changes[0].ident = -1;
147 kqueueop->changes[0].filter = EVFILT_READ;
148 kqueueop->changes[0].flags = EV_ADD;
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
149 /*
57fafe6 @provos detect kqueue bug in Mac OS X 10.4; from Nick Mathewson
provos authored
150 * If kqueue works, then kevent will succeed, and it will
151 * stick an error in events[0]. If kqueue is broken, then
152 * kevent will fail.
153 */
154 if (kevent(kq,
155 kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
5e4bafb fix a signed/unsigned warning in kqueue.c
Nick Mathewson authored
156 (int)kqueueop->events[0].ident != -1 ||
57fafe6 @provos detect kqueue bug in Mac OS X 10.4; from Nick Mathewson
provos authored
157 kqueueop->events[0].flags != EV_ERROR) {
158 event_warn("%s: detected broken kqueue; not using.", __func__);
369aafc Refactor kq_init error handling.
Nick Mathewson authored
159 goto err;
57fafe6 @provos detect kqueue bug in Mac OS X 10.4; from Nick Mathewson
provos authored
160 }
161
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
162 base->evsigsel = &kqsigops;
163
3ba224d @provos fixes for threaded operations from Andrew Danforth
provos authored
164 return (kqueueop);
369aafc Refactor kq_init error handling.
Nick Mathewson authored
165 err:
315fde1 Remove some duplicate code in kqueue.c and fix a small memory leak.
Nick Mathewson authored
166 if (kqueueop)
167 kqop_free(kqueueop);
168
369aafc Refactor kq_init error handling.
Nick Mathewson authored
169 return (NULL);
aa6567f @provos Initial revision
provos authored
170 }
171
1fd34ab Report kqueue ebadf, epipe, and eperm as EV_READ events
Nick Mathewson authored
172 #define ADD_UDATA 0x30303
173
3821c7e @provos make kqueue signal callback work with sigchld. cast and better timeout.
provos authored
174 static void
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
175 kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change)
3821c7e @provos make kqueue signal callback work with sigchld. cast and better timeout.
provos authored
176 {
c2c7b39 @shahn Properly zero the kevent in kq_setup_kevent()
shahn authored
177 memset(out, 0, sizeof(struct kevent));
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
178 out->ident = fd;
179 out->filter = filter;
180
181 if (change & EV_CHANGE_ADD) {
182 out->flags = EV_ADD;
1fd34ab Report kqueue ebadf, epipe, and eperm as EV_READ events
Nick Mathewson authored
183 /* We set a magic number here so that we can tell 'add'
184 * errors from 'del' errors. */
185 out->udata = INT_TO_UDATA(ADD_UDATA);
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
186 if (change & EV_ET)
187 out->flags |= EV_CLEAR;
188 #ifdef NOTE_EOF
189 /* Make it behave like select() and poll() */
190 if (filter == EVFILT_READ)
191 out->fflags = NOTE_EOF;
192 #endif
193 } else {
194 EVUTIL_ASSERT(change & EV_CHANGE_DEL);
195 out->flags = EV_DELETE;
196 }
3821c7e @provos make kqueue signal callback work with sigchld. cast and better timeout.
provos authored
197 }
198
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
199 static int
200 kq_build_changes_list(const struct event_changelist *changelist,
201 struct kqop *kqop)
202 {
203 int i;
204 int n_changes = 0;
205
206 for (i = 0; i < changelist->n_changes; ++i) {
207 struct event_change *in_ch = &changelist->changes[i];
208 struct kevent *out_ch;
209 if (n_changes >= kqop->changes_size - 1) {
210 int newsize = kqop->changes_size * 2;
211 struct kevent *newchanges;
212
213 newchanges = mm_realloc(kqop->changes,
214 newsize * sizeof(struct kevent));
215 if (newchanges == NULL) {
216 event_warn("%s: realloc", __func__);
217 return (-1);
218 }
219 kqop->changes = newchanges;
220 kqop->changes_size = newsize;
221 }
222 if (in_ch->read_change) {
223 out_ch = &kqop->changes[n_changes++];
224 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ,
225 in_ch->read_change);
226 }
227 if (in_ch->write_change) {
228 out_ch = &kqop->changes[n_changes++];
229 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE,
230 in_ch->write_change);
231 }
232 }
233 return n_changes;
234 }
235
ca42671 @provos make event methods static so that they are not exported; from Andrei …
provos authored
236 static int
28317a0 Fix a warn-and-fail bug in kqueue by providing kevent() room to repor…
Nick Mathewson authored
237 kq_grow_events(struct kqop *kqop, size_t new_size)
238 {
239 struct kevent *newresult;
240
241 newresult = mm_realloc(kqop->events,
242 new_size * sizeof(struct kevent));
243
244 if (newresult) {
245 kqop->events = newresult;
246 kqop->events_size = new_size;
247 return 0;
248 } else {
249 return -1;
250 }
251 }
252
253 static int
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
254 kq_dispatch(struct event_base *base, struct timeval *tv)
aa6567f @provos Initial revision
provos authored
255 {
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
256 struct kqop *kqop = base->evbase;
aa6567f @provos Initial revision
provos authored
257 struct kevent *events = kqop->events;
3225dfb Remove kqueue->pend_changes.
Nick Mathewson authored
258 struct kevent *changes;
3ad6b47 @provos make clock_monotonic work; do not use default timeout;
provos authored
259 struct timespec ts, *ts_p = NULL;
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
260 int i, n_changes, res;
aa6567f @provos Initial revision
provos authored
261
3ad6b47 @provos make clock_monotonic work; do not use default timeout;
provos authored
262 if (tv != NULL) {
193c7de @EdSchouten Remove BSD-ism: TIMEVAL_TO_TIMESPEC().
EdSchouten authored
263 ts.tv_sec = tv->tv_sec;
264 ts.tv_nsec = tv->tv_usec * 1000;
3ad6b47 @provos make clock_monotonic work; do not use default timeout;
provos authored
265 ts_p = &ts;
266 }
aa6567f @provos Initial revision
provos authored
267
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
268 /* Build "changes" from "base->changes" */
3225dfb Remove kqueue->pend_changes.
Nick Mathewson authored
269 EVUTIL_ASSERT(kqop->changes);
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
270 n_changes = kq_build_changes_list(&base->changelist, kqop);
271 if (n_changes < 0)
272 return -1;
273
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
274 event_changelist_remove_all_(&base->changelist, base);
45e5ae3 Make kqueue use changelists.
Nick Mathewson authored
275
3225dfb Remove kqueue->pend_changes.
Nick Mathewson authored
276 /* steal the changes array in case some broken code tries to call
277 * dispatch twice at once. */
278 changes = kqop->changes;
279 kqop->changes = NULL;
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
280
28317a0 Fix a warn-and-fail bug in kqueue by providing kevent() room to repor…
Nick Mathewson authored
281 /* Make sure that 'events' is at least as long as the list of changes:
282 * otherwise errors in the changes can get reported as a -1 return
283 * value from kevent() rather than as EV_ERROR events in the events
284 * array.
285 *
286 * (We could instead handle -1 return values from kevent() by
287 * retrying with a smaller changes array or a larger events array,
288 * but this approach seems less risky for now.)
289 */
290 if (kqop->events_size < n_changes) {
291 int new_size = kqop->events_size;
292 do {
293 new_size *= 2;
294 } while (new_size < n_changes);
295
296 kq_grow_events(kqop, new_size);
297 events = kqop->events;
298 }
299
76cd2b7 Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Nick Mathewson authored
300 EVBASE_RELEASE_LOCK(base, th_base_lock);
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
301
3225dfb Remove kqueue->pend_changes.
Nick Mathewson authored
302 res = kevent(kqop->kq, changes, n_changes,
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
303 events, kqop->events_size, ts_p);
304
76cd2b7 Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Nick Mathewson authored
305 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
306
3225dfb Remove kqueue->pend_changes.
Nick Mathewson authored
307 EVUTIL_ASSERT(kqop->changes == NULL);
308 kqop->changes = changes;
309
aa6567f @provos Initial revision
provos authored
310 if (res == -1) {
311 if (errno != EINTR) {
e5bbd40 Clean up formatting: use tabs, not 8-spaces, to indent.
Nick Mathewson authored
312 event_warn("kevent");
aa6567f @provos Initial revision
provos authored
313 return (-1);
314 }
315
316 return (0);
317 }
318
fbdaf3a @provos debugging callbacks from Nick Mathewson <nickm@freehaven.net>
provos authored
319 event_debug(("%s: kevent reports %d", __func__, res));
aa6567f @provos Initial revision
provos authored
320
321 for (i = 0; i < res; i++) {
322 int which = 0;
323
324 if (events[i].flags & EV_ERROR) {
1fd34ab Report kqueue ebadf, epipe, and eperm as EV_READ events
Nick Mathewson authored
325 switch (events[i].data) {
326
327 /* Can occur on delete if we are not currently
328 * watching any events on this fd. That can
329 * happen when the fd was closed and another
330 * file was opened with that fd. */
331 case ENOENT:
332 /* Can occur for reasons not fully understood
333 * on FreeBSD. */
334 case EINVAL:
aa6567f @provos Initial revision
provos authored
335 continue;
6fd7394 Handle ENOTCAPABLE from FreeBSD - this is returned if an event in the
Adrian Chadd authored
336 #if defined(__FreeBSD__)
337 /*
338 * This currently occurs if an FD is closed
339 * before the EV_DELETE makes it out via kevent().
340 * The FreeBSD capabilities code sees the blank
341 * capability set and rejects the request to
342 * modify an event.
343 *
344 * To be strictly correct - when an FD is closed,
345 * all the registered events are also removed.
346 * Queuing EV_DELETE to a closed FD is wrong.
347 * The event(s) should just be deleted from
348 * the pending changelist.
349 */
350 case ENOTCAPABLE:
351 continue;
352 #endif
aa6567f @provos Initial revision
provos authored
353
5d7bfa1 In the kqueue backend, do not report EBADF as an EV_READ
Nick Mathewson authored
354 /* Can occur on a delete if the fd is closed. */
1fd34ab Report kqueue ebadf, epipe, and eperm as EV_READ events
Nick Mathewson authored
355 case EBADF:
5d7bfa1 In the kqueue backend, do not report EBADF as an EV_READ
Nick Mathewson authored
356 /* XXXX On NetBSD, we can also get EBADF if we
357 * try to add the write side of a pipe, but
358 * the read side has already been closed.
359 * Other BSDs call this situation 'EPIPE'. It
360 * would be good if we had a way to report
361 * this situation. */
362 continue;
1fd34ab Report kqueue ebadf, epipe, and eperm as EV_READ events
Nick Mathewson authored
363 /* These two can occur on an add if the fd was one side
364 * of a pipe, and the other side was closed. */
365 case EPERM:
366 case EPIPE:
367 /* Report read events, if we're listening for
368 * them, so that the user can learn about any
369 * add errors. (If the operation was a
370 * delete, then udata should be cleared.) */
371 if (events[i].udata) {
372 /* The operation was an add:
373 * report the error as a read. */
374 which |= EV_READ;
375 break;
376 } else {
377 /* The operation was a del:
378 * report nothing. */
379 continue;
380 }
381
382 /* Other errors shouldn't occur. */
383 default:
384 errno = events[i].data;
385 return (-1);
386 }
387 } else if (events[i].filter == EVFILT_READ) {
aa6567f @provos Initial revision
provos authored
388 which |= EV_READ;
389 } else if (events[i].filter == EVFILT_WRITE) {
390 which |= EV_WRITE;
d10f85d @provos signal support for kqueue; support of EV_PERSIST flag to event_set
provos authored
391 } else if (events[i].filter == EVFILT_SIGNAL) {
392 which |= EV_SIGNAL;
53a07fe Replace pipe-based notification with EVFILT_USER where possible
Nick Mathewson authored
393 #ifdef EVFILT_USER
394 } else if (events[i].filter == EVFILT_USER) {
395 base->is_notify_pending = 0;
396 #endif
0036d79 @provos fix kqueue problem
provos authored
397 }
484e594 @provos make kqueue work for callbacks that use both read and write events
provos authored
398
399 if (!which)
400 continue;
401
f7e6187 @provos support multiple events listening on the same signal; make signals re…
provos authored
402 if (events[i].filter == EVFILT_SIGNAL) {
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
403 evmap_signal_active_(base, events[i].ident, 1);
f7e6187 @provos support multiple events listening on the same signal; make signals re…
provos authored
404 } else {
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
405 evmap_io_active_(base, events[i].ident, which | EV_ET);
f7e6187 @provos support multiple events listening on the same signal; make signals re…
provos authored
406 }
484e594 @provos make kqueue work for callbacks that use both read and write events
provos authored
407 }
408
b89b58b OSX compilation issues
Nick Mathewson authored
409 if (res == kqop->events_size) {
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
410 /* We used all the events space that we have. Maybe we should
411 make it bigger. */
28317a0 Fix a warn-and-fail bug in kqueue by providing kevent() room to repor…
Nick Mathewson authored
412 kq_grow_events(kqop, kqop->events_size * 2);
6b22e74 Add locking to event_base_loop.
Nick Mathewson authored
413 }
414
aa6567f @provos Initial revision
provos authored
415 return (0);
416 }
417
ca42671 @provos make event methods static so that they are not exported; from Andrei …
provos authored
418 static void
315fde1 Remove some duplicate code in kqueue.c and fix a small memory leak.
Nick Mathewson authored
419 kqop_free(struct kqop *kqop)
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven…
provos authored
420 {
421 if (kqop->changes)
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
422 mm_free(kqop->changes);
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven…
provos authored
423 if (kqop->events)
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
424 mm_free(kqop->events);
19dad16 @provos Do not free the kqop file descriptor in other processes, also allow i…
provos authored
425 if (kqop->kq >= 0 && kqop->pid == getpid())
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven…
provos authored
426 close(kqop->kq);
427 memset(kqop, 0, sizeof(struct kqop));
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
428 mm_free(kqop);
2e8051f @provos introduce a way to free the base from Nick Mathewson <nickm@freehaven…
provos authored
429 }
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
430
315fde1 Remove some duplicate code in kqueue.c and fix a small memory leak.
Nick Mathewson authored
431 static void
432 kq_dealloc(struct event_base *base)
433 {
434 struct kqop *kqop = base->evbase;
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
435 evsig_dealloc_(base);
315fde1 Remove some duplicate code in kqueue.c and fix a small memory leak.
Nick Mathewson authored
436 kqop_free(kqop);
437 }
438
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
439 /* signal handling */
440 static int
2b7febc Fix osx compile
Nick Mathewson authored
441 kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p)
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
442 {
443 struct kqop *kqop = base->evbase;
444 struct kevent kev;
445 struct timespec timeout = { 0, 0 };
2b7febc Fix osx compile
Nick Mathewson authored
446 (void)p;
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
447
2e36dbe Use EVUTIL_ASSERT() consistently instead of assert.
Nick Mathewson authored
448 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
449
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
450 memset(&kev, 0, sizeof(kev));
451 kev.ident = nsignal;
452 kev.filter = EVFILT_SIGNAL;
453 kev.flags = EV_ADD;
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
454
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
455 /* Be ready for the signal if it is sent any
456 * time between now and the next call to
457 * kq_dispatch. */
458 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
459 return (-1);
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
460
148458e @zackw Use SIG_IGN instead of a do-nothing handler for signal events with kq…
zackw authored
461 /* We can set the handler for most signals to SIG_IGN and
462 * still have them reported to us in the queue. However,
463 * if the handler for SIGCHLD is SIG_IGN, the system reaps
464 * zombie processes for us, and we don't get any notification.
465 * This appears to be the only signal with this quirk. */
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
466 if (evsig_set_handler_(base, nsignal,
148458e @zackw Use SIG_IGN instead of a do-nothing handler for signal events with kq…
zackw authored
467 nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1)
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
468 return (-1);
469
470 return (0);
471 }
472
473 static int
2b7febc Fix osx compile
Nick Mathewson authored
474 kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p)
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
475 {
476 struct kqop *kqop = base->evbase;
477 struct kevent kev;
478
479 struct timespec timeout = { 0, 0 };
2b7febc Fix osx compile
Nick Mathewson authored
480 (void)p;
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
481
2e36dbe Use EVUTIL_ASSERT() consistently instead of assert.
Nick Mathewson authored
482 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
483
484 memset(&kev, 0, sizeof(kev));
485 kev.ident = nsignal;
486 kev.filter = EVFILT_SIGNAL;
487 kev.flags = EV_DELETE;
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
488
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
489 /* Because we insert signal events
490 * immediately, we need to delete them
491 * immediately, too */
492 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
493 return (-1);
494
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
495 if (evsig_restore_handler_(base, nsignal) == -1)
02b2b4d @provos Restructure the event backends so that they do not need to keep track…
provos authored
496 return (-1);
497
498 return (0);
499 }
76d4c92 @rosslagerwall Put #ifdef around some files to support alternate build systems.
rosslagerwall authored
500
53a07fe Replace pipe-based notification with EVFILT_USER where possible
Nick Mathewson authored
501
502 /* OSX 10.6 and FreeBSD 8.1 add support for EVFILT_USER, which we can use
503 * to wake up the event loop from another thread. */
504
505 /* Magic number we use for our filter ID. */
506 #define NOTIFY_IDENT 42
507
508 int
509 event_kq_add_notify_event_(struct event_base *base)
510 {
511 struct kqop *kqop = base->evbase;
512 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
513 struct kevent kev;
514 struct timespec timeout = { 0, 0 };
515 #endif
516
517 if (kqop->notify_event_added)
518 return 0;
519
520 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
521 memset(&kev, 0, sizeof(kev));
522 kev.ident = NOTIFY_IDENT;
523 kev.filter = EVFILT_USER;
524 kev.flags = EV_ADD | EV_CLEAR;
525
526 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
527 event_warn("kevent: adding EVFILT_USER event");
528 return -1;
529 }
530
531 kqop->notify_event_added = 1;
532
533 return 0;
534 #else
535 return -1;
536 #endif
537 }
538
539 int
540 event_kq_notify_base_(struct event_base *base)
541 {
542 struct kqop *kqop = base->evbase;
543 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
544 struct kevent kev;
545 struct timespec timeout = { 0, 0 };
546 #endif
547 if (! kqop->notify_event_added)
548 return -1;
549
550 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
551 memset(&kev, 0, sizeof(kev));
552 kev.ident = NOTIFY_IDENT;
553 kev.filter = EVFILT_USER;
554 kev.fflags = NOTE_TRIGGER;
555
556 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
557 event_warn("kevent: triggering EVFILT_USER event");
558 return -1;
559 }
560
561 return 0;
562 #else
563 return -1;
564 #endif
565 }
566
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
567 #endif /* EVENT__HAVE_KQUEUE */
Something went wrong with that request. Please try again.