xref: /freebsd/contrib/libevent/kqueue.c (revision b50261e21f39a6c7249a49e7b60aa878c98512a8)
1c43e99fdSEd Maste /*	$OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $	*/
2c43e99fdSEd Maste 
3c43e99fdSEd Maste /*
4c43e99fdSEd Maste  * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5c43e99fdSEd Maste  * Copyright 2007-2012 Niels Provos and Nick Mathewson
6c43e99fdSEd Maste  *
7c43e99fdSEd Maste  * Redistribution and use in source and binary forms, with or without
8c43e99fdSEd Maste  * modification, are permitted provided that the following conditions
9c43e99fdSEd Maste  * are met:
10c43e99fdSEd Maste  * 1. Redistributions of source code must retain the above copyright
11c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer.
12c43e99fdSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
13c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer in the
14c43e99fdSEd Maste  *    documentation and/or other materials provided with the distribution.
15c43e99fdSEd Maste  * 3. The name of the author may not be used to endorse or promote products
16c43e99fdSEd Maste  *    derived from this software without specific prior written permission.
17c43e99fdSEd Maste  *
18c43e99fdSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19c43e99fdSEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20c43e99fdSEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21c43e99fdSEd Maste  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22c43e99fdSEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23c43e99fdSEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24c43e99fdSEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25c43e99fdSEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26c43e99fdSEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27c43e99fdSEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28c43e99fdSEd Maste  */
29c43e99fdSEd Maste #include "event2/event-config.h"
30c43e99fdSEd Maste #include "evconfig-private.h"
31c43e99fdSEd Maste 
32c43e99fdSEd Maste #ifdef EVENT__HAVE_KQUEUE
33c43e99fdSEd Maste 
34c43e99fdSEd Maste #include <sys/types.h>
35c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TIME_H
36c43e99fdSEd Maste #include <sys/time.h>
37c43e99fdSEd Maste #endif
38c43e99fdSEd Maste #include <sys/queue.h>
39c43e99fdSEd Maste #include <sys/event.h>
40*b50261e2SCy Schubert #include <limits.h>
41c43e99fdSEd Maste #include <signal.h>
42c43e99fdSEd Maste #include <stdio.h>
43c43e99fdSEd Maste #include <stdlib.h>
44c43e99fdSEd Maste #include <string.h>
45c43e99fdSEd Maste #include <unistd.h>
46c43e99fdSEd Maste #include <errno.h>
47c43e99fdSEd Maste #ifdef EVENT__HAVE_INTTYPES_H
48c43e99fdSEd Maste #include <inttypes.h>
49c43e99fdSEd Maste #endif
50c43e99fdSEd Maste 
51c43e99fdSEd Maste /* Some platforms apparently define the udata field of struct kevent as
52c43e99fdSEd Maste  * intptr_t, whereas others define it as void*.  There doesn't seem to be an
53c43e99fdSEd Maste  * easy way to tell them apart via autoconf, so we need to use OS macros. */
54*b50261e2SCy Schubert #if defined(__NetBSD__)
55*b50261e2SCy Schubert #define PTR_TO_UDATA(x) ((typeof(((struct kevent *)0)->udata))(x))
56*b50261e2SCy Schubert #define INT_TO_UDATA(x) ((typeof(((struct kevent *)0)->udata))(intptr_t)(x))
57*b50261e2SCy Schubert #elif defined(EVENT__HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) && !defined(__CloudABI__)
58c43e99fdSEd Maste #define PTR_TO_UDATA(x)	((intptr_t)(x))
59c43e99fdSEd Maste #define INT_TO_UDATA(x) ((intptr_t)(x))
60c43e99fdSEd Maste #else
61c43e99fdSEd Maste #define PTR_TO_UDATA(x)	(x)
62c43e99fdSEd Maste #define INT_TO_UDATA(x) ((void*)(x))
63c43e99fdSEd Maste #endif
64c43e99fdSEd Maste 
65c43e99fdSEd Maste #include "event-internal.h"
66c43e99fdSEd Maste #include "log-internal.h"
67c43e99fdSEd Maste #include "evmap-internal.h"
68c43e99fdSEd Maste #include "event2/thread.h"
69*b50261e2SCy Schubert #include "event2/util.h"
70c43e99fdSEd Maste #include "evthread-internal.h"
71c43e99fdSEd Maste #include "changelist-internal.h"
72c43e99fdSEd Maste 
73c43e99fdSEd Maste #include "kqueue-internal.h"
74c43e99fdSEd Maste 
75c43e99fdSEd Maste #define NEVENT		64
76c43e99fdSEd Maste 
77c43e99fdSEd Maste struct kqop {
78c43e99fdSEd Maste 	struct kevent *changes;
79c43e99fdSEd Maste 	int changes_size;
80c43e99fdSEd Maste 
81c43e99fdSEd Maste 	struct kevent *events;
82c43e99fdSEd Maste 	int events_size;
83c43e99fdSEd Maste 	int kq;
84c43e99fdSEd Maste 	int notify_event_added;
85c43e99fdSEd Maste 	pid_t pid;
86c43e99fdSEd Maste };
87c43e99fdSEd Maste 
88c43e99fdSEd Maste static void kqop_free(struct kqop *kqop);
89c43e99fdSEd Maste 
90c43e99fdSEd Maste static void *kq_init(struct event_base *);
91c43e99fdSEd Maste static int kq_sig_add(struct event_base *, int, short, short, void *);
92c43e99fdSEd Maste static int kq_sig_del(struct event_base *, int, short, short, void *);
93c43e99fdSEd Maste static int kq_dispatch(struct event_base *, struct timeval *);
94c43e99fdSEd Maste static void kq_dealloc(struct event_base *);
95c43e99fdSEd Maste 
96c43e99fdSEd Maste const struct eventop kqops = {
97c43e99fdSEd Maste 	"kqueue",
98c43e99fdSEd Maste 	kq_init,
99c43e99fdSEd Maste 	event_changelist_add_,
100c43e99fdSEd Maste 	event_changelist_del_,
101c43e99fdSEd Maste 	kq_dispatch,
102c43e99fdSEd Maste 	kq_dealloc,
103c43e99fdSEd Maste 	1 /* need reinit */,
104c43e99fdSEd Maste     EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS,
105c43e99fdSEd Maste 	EVENT_CHANGELIST_FDINFO_SIZE
106c43e99fdSEd Maste };
107c43e99fdSEd Maste 
108c43e99fdSEd Maste static const struct eventop kqsigops = {
109c43e99fdSEd Maste 	"kqueue_signal",
110c43e99fdSEd Maste 	NULL,
111c43e99fdSEd Maste 	kq_sig_add,
112c43e99fdSEd Maste 	kq_sig_del,
113c43e99fdSEd Maste 	NULL,
114c43e99fdSEd Maste 	NULL,
115c43e99fdSEd Maste 	1 /* need reinit */,
116c43e99fdSEd Maste 	0,
117c43e99fdSEd Maste 	0
118c43e99fdSEd Maste };
119c43e99fdSEd Maste 
120c43e99fdSEd Maste static void *
kq_init(struct event_base * base)121c43e99fdSEd Maste kq_init(struct event_base *base)
122c43e99fdSEd Maste {
123c43e99fdSEd Maste 	int kq = -1;
124c43e99fdSEd Maste 	struct kqop *kqueueop = NULL;
125c43e99fdSEd Maste 
126c43e99fdSEd Maste 	if (!(kqueueop = mm_calloc(1, sizeof(struct kqop))))
127c43e99fdSEd Maste 		return (NULL);
128c43e99fdSEd Maste 
129c43e99fdSEd Maste /* Initialize the kernel queue */
130c43e99fdSEd Maste 
131c43e99fdSEd Maste 	if ((kq = kqueue()) == -1) {
132c43e99fdSEd Maste 		event_warn("kqueue");
133c43e99fdSEd Maste 		goto err;
134c43e99fdSEd Maste 	}
135c43e99fdSEd Maste 
136c43e99fdSEd Maste 	kqueueop->kq = kq;
137c43e99fdSEd Maste 
138c43e99fdSEd Maste 	kqueueop->pid = getpid();
139c43e99fdSEd Maste 
140c43e99fdSEd Maste 	/* Initialize fields */
141c43e99fdSEd Maste 	kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent));
142c43e99fdSEd Maste 	if (kqueueop->changes == NULL)
143c43e99fdSEd Maste 		goto err;
144c43e99fdSEd Maste 	kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent));
145c43e99fdSEd Maste 	if (kqueueop->events == NULL)
146c43e99fdSEd Maste 		goto err;
147c43e99fdSEd Maste 	kqueueop->events_size = kqueueop->changes_size = NEVENT;
148c43e99fdSEd Maste 
149c43e99fdSEd Maste 	/* Check for Mac OS X kqueue bug. */
150c43e99fdSEd Maste 	memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]);
151c43e99fdSEd Maste 	kqueueop->changes[0].ident = -1;
152c43e99fdSEd Maste 	kqueueop->changes[0].filter = EVFILT_READ;
153c43e99fdSEd Maste 	kqueueop->changes[0].flags = EV_ADD;
154c43e99fdSEd Maste 	/*
155c43e99fdSEd Maste 	 * If kqueue works, then kevent will succeed, and it will
156c43e99fdSEd Maste 	 * stick an error in events[0].  If kqueue is broken, then
157c43e99fdSEd Maste 	 * kevent will fail.
158c43e99fdSEd Maste 	 */
159c43e99fdSEd Maste 	if (kevent(kq,
160c43e99fdSEd Maste 		kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 ||
161c43e99fdSEd Maste 	    (int)kqueueop->events[0].ident != -1 ||
162c43e99fdSEd Maste 	    !(kqueueop->events[0].flags & EV_ERROR)) {
163c43e99fdSEd Maste 		event_warn("%s: detected broken kqueue; not using.", __func__);
164c43e99fdSEd Maste 		goto err;
165c43e99fdSEd Maste 	}
166c43e99fdSEd Maste 
167c43e99fdSEd Maste 	base->evsigsel = &kqsigops;
168c43e99fdSEd Maste 
169c43e99fdSEd Maste 	return (kqueueop);
170c43e99fdSEd Maste err:
171c43e99fdSEd Maste 	if (kqueueop)
172c43e99fdSEd Maste 		kqop_free(kqueueop);
173c43e99fdSEd Maste 
174c43e99fdSEd Maste 	return (NULL);
175c43e99fdSEd Maste }
176c43e99fdSEd Maste 
177c43e99fdSEd Maste #define ADD_UDATA 0x30303
178c43e99fdSEd Maste 
179c43e99fdSEd Maste static void
kq_setup_kevent(struct kevent * out,evutil_socket_t fd,int filter,short change)180c43e99fdSEd Maste kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change)
181c43e99fdSEd Maste {
182c43e99fdSEd Maste 	memset(out, 0, sizeof(struct kevent));
183c43e99fdSEd Maste 	out->ident = fd;
184c43e99fdSEd Maste 	out->filter = filter;
185c43e99fdSEd Maste 
186c43e99fdSEd Maste 	if (change & EV_CHANGE_ADD) {
187c43e99fdSEd Maste 		out->flags = EV_ADD;
188c43e99fdSEd Maste 		/* We set a magic number here so that we can tell 'add'
189c43e99fdSEd Maste 		 * errors from 'del' errors. */
190c43e99fdSEd Maste 		out->udata = INT_TO_UDATA(ADD_UDATA);
191c43e99fdSEd Maste 		if (change & EV_ET)
192c43e99fdSEd Maste 			out->flags |= EV_CLEAR;
193c43e99fdSEd Maste #ifdef NOTE_EOF
194c43e99fdSEd Maste 		/* Make it behave like select() and poll() */
195c43e99fdSEd Maste 		if (filter == EVFILT_READ)
196c43e99fdSEd Maste 			out->fflags = NOTE_EOF;
197c43e99fdSEd Maste #endif
198c43e99fdSEd Maste 	} else {
199c43e99fdSEd Maste 		EVUTIL_ASSERT(change & EV_CHANGE_DEL);
200c43e99fdSEd Maste 		out->flags = EV_DELETE;
201c43e99fdSEd Maste 	}
202c43e99fdSEd Maste }
203c43e99fdSEd Maste 
204c43e99fdSEd Maste static int
kq_build_changes_list(const struct event_changelist * changelist,struct kqop * kqop)205c43e99fdSEd Maste kq_build_changes_list(const struct event_changelist *changelist,
206c43e99fdSEd Maste     struct kqop *kqop)
207c43e99fdSEd Maste {
208c43e99fdSEd Maste 	int i;
209c43e99fdSEd Maste 	int n_changes = 0;
210c43e99fdSEd Maste 
211c43e99fdSEd Maste 	for (i = 0; i < changelist->n_changes; ++i) {
212c43e99fdSEd Maste 		struct event_change *in_ch = &changelist->changes[i];
213c43e99fdSEd Maste 		struct kevent *out_ch;
214c43e99fdSEd Maste 		if (n_changes >= kqop->changes_size - 1) {
215*b50261e2SCy Schubert 			int newsize;
216c43e99fdSEd Maste 			struct kevent *newchanges;
217c43e99fdSEd Maste 
218*b50261e2SCy Schubert 			if (kqop->changes_size > INT_MAX / 2 ||
219*b50261e2SCy Schubert 			    (size_t)kqop->changes_size * 2 > EV_SIZE_MAX /
220*b50261e2SCy Schubert 			    sizeof(struct kevent)) {
221*b50261e2SCy Schubert 				event_warnx("%s: int overflow", __func__);
222*b50261e2SCy Schubert 				return (-1);
223*b50261e2SCy Schubert 			}
224*b50261e2SCy Schubert 
225*b50261e2SCy Schubert 			newsize = kqop->changes_size * 2;
226c43e99fdSEd Maste 			newchanges = mm_realloc(kqop->changes,
227c43e99fdSEd Maste 			    newsize * sizeof(struct kevent));
228c43e99fdSEd Maste 			if (newchanges == NULL) {
229c43e99fdSEd Maste 				event_warn("%s: realloc", __func__);
230c43e99fdSEd Maste 				return (-1);
231c43e99fdSEd Maste 			}
232c43e99fdSEd Maste 			kqop->changes = newchanges;
233c43e99fdSEd Maste 			kqop->changes_size = newsize;
234c43e99fdSEd Maste 		}
235c43e99fdSEd Maste 		if (in_ch->read_change) {
236c43e99fdSEd Maste 			out_ch = &kqop->changes[n_changes++];
237c43e99fdSEd Maste 			kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ,
238c43e99fdSEd Maste 			    in_ch->read_change);
239c43e99fdSEd Maste 		}
240c43e99fdSEd Maste 		if (in_ch->write_change) {
241c43e99fdSEd Maste 			out_ch = &kqop->changes[n_changes++];
242c43e99fdSEd Maste 			kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE,
243c43e99fdSEd Maste 			    in_ch->write_change);
244c43e99fdSEd Maste 		}
245c43e99fdSEd Maste 	}
246c43e99fdSEd Maste 	return n_changes;
247c43e99fdSEd Maste }
248c43e99fdSEd Maste 
249c43e99fdSEd Maste static int
kq_grow_events(struct kqop * kqop,size_t new_size)250c43e99fdSEd Maste kq_grow_events(struct kqop *kqop, size_t new_size)
251c43e99fdSEd Maste {
252c43e99fdSEd Maste 	struct kevent *newresult;
253c43e99fdSEd Maste 
254c43e99fdSEd Maste 	newresult = mm_realloc(kqop->events,
255c43e99fdSEd Maste 	    new_size * sizeof(struct kevent));
256c43e99fdSEd Maste 
257c43e99fdSEd Maste 	if (newresult) {
258c43e99fdSEd Maste 		kqop->events = newresult;
259c43e99fdSEd Maste 		kqop->events_size = new_size;
260c43e99fdSEd Maste 		return 0;
261c43e99fdSEd Maste 	} else {
262c43e99fdSEd Maste 		return -1;
263c43e99fdSEd Maste 	}
264c43e99fdSEd Maste }
265c43e99fdSEd Maste 
266c43e99fdSEd Maste static int
kq_dispatch(struct event_base * base,struct timeval * tv)267c43e99fdSEd Maste kq_dispatch(struct event_base *base, struct timeval *tv)
268c43e99fdSEd Maste {
269c43e99fdSEd Maste 	struct kqop *kqop = base->evbase;
270c43e99fdSEd Maste 	struct kevent *events = kqop->events;
271c43e99fdSEd Maste 	struct kevent *changes;
272c43e99fdSEd Maste 	struct timespec ts, *ts_p = NULL;
273c43e99fdSEd Maste 	int i, n_changes, res;
274c43e99fdSEd Maste 
275c43e99fdSEd Maste 	if (tv != NULL) {
276c43e99fdSEd Maste 		ts.tv_sec = tv->tv_sec;
277c43e99fdSEd Maste 		ts.tv_nsec = tv->tv_usec * 1000;
278c43e99fdSEd Maste 		ts_p = &ts;
279c43e99fdSEd Maste 	}
280c43e99fdSEd Maste 
281c43e99fdSEd Maste 	/* Build "changes" from "base->changes" */
282c43e99fdSEd Maste 	EVUTIL_ASSERT(kqop->changes);
283c43e99fdSEd Maste 	n_changes = kq_build_changes_list(&base->changelist, kqop);
284c43e99fdSEd Maste 	if (n_changes < 0)
285c43e99fdSEd Maste 		return -1;
286c43e99fdSEd Maste 
287c43e99fdSEd Maste 	event_changelist_remove_all_(&base->changelist, base);
288c43e99fdSEd Maste 
289c43e99fdSEd Maste 	/* steal the changes array in case some broken code tries to call
290c43e99fdSEd Maste 	 * dispatch twice at once. */
291c43e99fdSEd Maste 	changes = kqop->changes;
292c43e99fdSEd Maste 	kqop->changes = NULL;
293c43e99fdSEd Maste 
294c43e99fdSEd Maste 	/* Make sure that 'events' is at least as long as the list of changes:
295c43e99fdSEd Maste 	 * otherwise errors in the changes can get reported as a -1 return
296c43e99fdSEd Maste 	 * value from kevent() rather than as EV_ERROR events in the events
297c43e99fdSEd Maste 	 * array.
298c43e99fdSEd Maste 	 *
299c43e99fdSEd Maste 	 * (We could instead handle -1 return values from kevent() by
300c43e99fdSEd Maste 	 * retrying with a smaller changes array or a larger events array,
301c43e99fdSEd Maste 	 * but this approach seems less risky for now.)
302c43e99fdSEd Maste 	 */
303c43e99fdSEd Maste 	if (kqop->events_size < n_changes) {
304c43e99fdSEd Maste 		int new_size = kqop->events_size;
305c43e99fdSEd Maste 		do {
306c43e99fdSEd Maste 			new_size *= 2;
307c43e99fdSEd Maste 		} while (new_size < n_changes);
308c43e99fdSEd Maste 
309c43e99fdSEd Maste 		kq_grow_events(kqop, new_size);
310c43e99fdSEd Maste 		events = kqop->events;
311c43e99fdSEd Maste 	}
312c43e99fdSEd Maste 
313c43e99fdSEd Maste 	EVBASE_RELEASE_LOCK(base, th_base_lock);
314c43e99fdSEd Maste 
315c43e99fdSEd Maste 	res = kevent(kqop->kq, changes, n_changes,
316c43e99fdSEd Maste 	    events, kqop->events_size, ts_p);
317c43e99fdSEd Maste 
318c43e99fdSEd Maste 	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
319c43e99fdSEd Maste 
320c43e99fdSEd Maste 	EVUTIL_ASSERT(kqop->changes == NULL);
321c43e99fdSEd Maste 	kqop->changes = changes;
322c43e99fdSEd Maste 
323c43e99fdSEd Maste 	if (res == -1) {
324c43e99fdSEd Maste 		if (errno != EINTR) {
325c43e99fdSEd Maste 			event_warn("kevent");
326c43e99fdSEd Maste 			return (-1);
327c43e99fdSEd Maste 		}
328c43e99fdSEd Maste 
329c43e99fdSEd Maste 		return (0);
330c43e99fdSEd Maste 	}
331c43e99fdSEd Maste 
332c43e99fdSEd Maste 	event_debug(("%s: kevent reports %d", __func__, res));
333c43e99fdSEd Maste 
334c43e99fdSEd Maste 	for (i = 0; i < res; i++) {
335c43e99fdSEd Maste 		int which = 0;
336c43e99fdSEd Maste 
337c43e99fdSEd Maste 		if (events[i].flags & EV_ERROR) {
338c43e99fdSEd Maste 			switch (events[i].data) {
339c43e99fdSEd Maste 
340c43e99fdSEd Maste 			/* Can occur on delete if we are not currently
341c43e99fdSEd Maste 			 * watching any events on this fd.  That can
342c43e99fdSEd Maste 			 * happen when the fd was closed and another
343c43e99fdSEd Maste 			 * file was opened with that fd. */
344c43e99fdSEd Maste 			case ENOENT:
345c43e99fdSEd Maste 			/* Can occur for reasons not fully understood
346c43e99fdSEd Maste 			 * on FreeBSD. */
347c43e99fdSEd Maste 			case EINVAL:
348c43e99fdSEd Maste 				continue;
349c43e99fdSEd Maste #if defined(__FreeBSD__)
350c43e99fdSEd Maste 			/*
351c43e99fdSEd Maste 			 * This currently occurs if an FD is closed
352c43e99fdSEd Maste 			 * before the EV_DELETE makes it out via kevent().
353c43e99fdSEd Maste 			 * The FreeBSD capabilities code sees the blank
354c43e99fdSEd Maste 			 * capability set and rejects the request to
355c43e99fdSEd Maste 			 * modify an event.
356c43e99fdSEd Maste 			 *
357c43e99fdSEd Maste 			 * To be strictly correct - when an FD is closed,
358c43e99fdSEd Maste 			 * all the registered events are also removed.
359c43e99fdSEd Maste 			 * Queuing EV_DELETE to a closed FD is wrong.
360c43e99fdSEd Maste 			 * The event(s) should just be deleted from
361c43e99fdSEd Maste 			 * the pending changelist.
362c43e99fdSEd Maste 			 */
363c43e99fdSEd Maste 			case ENOTCAPABLE:
364c43e99fdSEd Maste 				continue;
365c43e99fdSEd Maste #endif
366c43e99fdSEd Maste 
367c43e99fdSEd Maste 			/* Can occur on a delete if the fd is closed. */
368c43e99fdSEd Maste 			case EBADF:
369c43e99fdSEd Maste 				/* XXXX On NetBSD, we can also get EBADF if we
370c43e99fdSEd Maste 				 * try to add the write side of a pipe, but
371c43e99fdSEd Maste 				 * the read side has already been closed.
372c43e99fdSEd Maste 				 * Other BSDs call this situation 'EPIPE'. It
373c43e99fdSEd Maste 				 * would be good if we had a way to report
374c43e99fdSEd Maste 				 * this situation. */
375c43e99fdSEd Maste 				continue;
376c43e99fdSEd Maste 			/* These two can occur on an add if the fd was one side
377c43e99fdSEd Maste 			 * of a pipe, and the other side was closed. */
378c43e99fdSEd Maste 			case EPERM:
379c43e99fdSEd Maste 			case EPIPE:
380c43e99fdSEd Maste 				/* Report read events, if we're listening for
381c43e99fdSEd Maste 				 * them, so that the user can learn about any
382c43e99fdSEd Maste 				 * add errors.  (If the operation was a
383c43e99fdSEd Maste 				 * delete, then udata should be cleared.) */
384c43e99fdSEd Maste 				if (events[i].udata) {
385c43e99fdSEd Maste 					/* The operation was an add:
386c43e99fdSEd Maste 					 * report the error as a read. */
387c43e99fdSEd Maste 					which |= EV_READ;
388c43e99fdSEd Maste 					break;
389c43e99fdSEd Maste 				} else {
390c43e99fdSEd Maste 					/* The operation was a del:
391c43e99fdSEd Maste 					 * report nothing. */
392c43e99fdSEd Maste 					continue;
393c43e99fdSEd Maste 				}
394c43e99fdSEd Maste 
395c43e99fdSEd Maste 			/* Other errors shouldn't occur. */
396c43e99fdSEd Maste 			default:
397c43e99fdSEd Maste 				errno = events[i].data;
398c43e99fdSEd Maste 				return (-1);
399c43e99fdSEd Maste 			}
400c43e99fdSEd Maste 		} else if (events[i].filter == EVFILT_READ) {
401c43e99fdSEd Maste 			which |= EV_READ;
402c43e99fdSEd Maste 		} else if (events[i].filter == EVFILT_WRITE) {
403c43e99fdSEd Maste 			which |= EV_WRITE;
404c43e99fdSEd Maste 		} else if (events[i].filter == EVFILT_SIGNAL) {
405c43e99fdSEd Maste 			which |= EV_SIGNAL;
406c43e99fdSEd Maste #ifdef EVFILT_USER
407c43e99fdSEd Maste 		} else if (events[i].filter == EVFILT_USER) {
408c43e99fdSEd Maste 			base->is_notify_pending = 0;
409c43e99fdSEd Maste #endif
410c43e99fdSEd Maste 		}
411c43e99fdSEd Maste 
412c43e99fdSEd Maste 		if (!which)
413c43e99fdSEd Maste 			continue;
414c43e99fdSEd Maste 
415c43e99fdSEd Maste 		if (events[i].filter == EVFILT_SIGNAL) {
416c43e99fdSEd Maste 			evmap_signal_active_(base, events[i].ident, 1);
417c43e99fdSEd Maste 		} else {
418c43e99fdSEd Maste 			evmap_io_active_(base, events[i].ident, which | EV_ET);
419c43e99fdSEd Maste 		}
420c43e99fdSEd Maste 	}
421c43e99fdSEd Maste 
422c43e99fdSEd Maste 	if (res == kqop->events_size) {
423c43e99fdSEd Maste 		/* We used all the events space that we have. Maybe we should
424c43e99fdSEd Maste 		   make it bigger. */
425c43e99fdSEd Maste 		kq_grow_events(kqop, kqop->events_size * 2);
426c43e99fdSEd Maste 	}
427c43e99fdSEd Maste 
428c43e99fdSEd Maste 	return (0);
429c43e99fdSEd Maste }
430c43e99fdSEd Maste 
431c43e99fdSEd Maste static void
kqop_free(struct kqop * kqop)432c43e99fdSEd Maste kqop_free(struct kqop *kqop)
433c43e99fdSEd Maste {
434c43e99fdSEd Maste 	if (kqop->changes)
435c43e99fdSEd Maste 		mm_free(kqop->changes);
436c43e99fdSEd Maste 	if (kqop->events)
437c43e99fdSEd Maste 		mm_free(kqop->events);
438c43e99fdSEd Maste 	if (kqop->kq >= 0 && kqop->pid == getpid())
439c43e99fdSEd Maste 		close(kqop->kq);
440c43e99fdSEd Maste 	memset(kqop, 0, sizeof(struct kqop));
441c43e99fdSEd Maste 	mm_free(kqop);
442c43e99fdSEd Maste }
443c43e99fdSEd Maste 
444c43e99fdSEd Maste static void
kq_dealloc(struct event_base * base)445c43e99fdSEd Maste kq_dealloc(struct event_base *base)
446c43e99fdSEd Maste {
447c43e99fdSEd Maste 	struct kqop *kqop = base->evbase;
448c43e99fdSEd Maste 	evsig_dealloc_(base);
449c43e99fdSEd Maste 	kqop_free(kqop);
450c43e99fdSEd Maste }
451c43e99fdSEd Maste 
452c43e99fdSEd Maste /* signal handling */
453c43e99fdSEd Maste static int
kq_sig_add(struct event_base * base,int nsignal,short old,short events,void * p)454c43e99fdSEd Maste kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p)
455c43e99fdSEd Maste {
456c43e99fdSEd Maste 	struct kqop *kqop = base->evbase;
457c43e99fdSEd Maste 	struct kevent kev;
458c43e99fdSEd Maste 	struct timespec timeout = { 0, 0 };
459c43e99fdSEd Maste 	(void)p;
460c43e99fdSEd Maste 
461c43e99fdSEd Maste 	EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
462c43e99fdSEd Maste 
463c43e99fdSEd Maste 	memset(&kev, 0, sizeof(kev));
464c43e99fdSEd Maste 	kev.ident = nsignal;
465c43e99fdSEd Maste 	kev.filter = EVFILT_SIGNAL;
466c43e99fdSEd Maste 	kev.flags = EV_ADD;
467c43e99fdSEd Maste 
468c43e99fdSEd Maste 	/* Be ready for the signal if it is sent any
469c43e99fdSEd Maste 	 * time between now and the next call to
470c43e99fdSEd Maste 	 * kq_dispatch. */
471c43e99fdSEd Maste 	if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
472c43e99fdSEd Maste 		return (-1);
473c43e99fdSEd Maste 
474c43e99fdSEd Maste         /* We can set the handler for most signals to SIG_IGN and
475c43e99fdSEd Maste          * still have them reported to us in the queue.  However,
476c43e99fdSEd Maste          * if the handler for SIGCHLD is SIG_IGN, the system reaps
477c43e99fdSEd Maste          * zombie processes for us, and we don't get any notification.
478c43e99fdSEd Maste          * This appears to be the only signal with this quirk. */
479c43e99fdSEd Maste 	if (evsig_set_handler_(base, nsignal,
480c43e99fdSEd Maste                                nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1)
481c43e99fdSEd Maste 		return (-1);
482c43e99fdSEd Maste 
483c43e99fdSEd Maste 	return (0);
484c43e99fdSEd Maste }
485c43e99fdSEd Maste 
486c43e99fdSEd Maste static int
kq_sig_del(struct event_base * base,int nsignal,short old,short events,void * p)487c43e99fdSEd Maste kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p)
488c43e99fdSEd Maste {
489c43e99fdSEd Maste 	struct kqop *kqop = base->evbase;
490c43e99fdSEd Maste 	struct kevent kev;
491c43e99fdSEd Maste 
492c43e99fdSEd Maste 	struct timespec timeout = { 0, 0 };
493c43e99fdSEd Maste 	(void)p;
494c43e99fdSEd Maste 
495c43e99fdSEd Maste 	EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG);
496c43e99fdSEd Maste 
497c43e99fdSEd Maste 	memset(&kev, 0, sizeof(kev));
498c43e99fdSEd Maste 	kev.ident = nsignal;
499c43e99fdSEd Maste 	kev.filter = EVFILT_SIGNAL;
500c43e99fdSEd Maste 	kev.flags = EV_DELETE;
501c43e99fdSEd Maste 
502c43e99fdSEd Maste 	/* Because we insert signal events
503c43e99fdSEd Maste 	 * immediately, we need to delete them
504c43e99fdSEd Maste 	 * immediately, too */
505c43e99fdSEd Maste 	if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
506c43e99fdSEd Maste 		return (-1);
507c43e99fdSEd Maste 
508c43e99fdSEd Maste 	if (evsig_restore_handler_(base, nsignal) == -1)
509c43e99fdSEd Maste 		return (-1);
510c43e99fdSEd Maste 
511c43e99fdSEd Maste 	return (0);
512c43e99fdSEd Maste }
513c43e99fdSEd Maste 
514c43e99fdSEd Maste 
515c43e99fdSEd Maste /* OSX 10.6 and FreeBSD 8.1 add support for EVFILT_USER, which we can use
516c43e99fdSEd Maste  * to wake up the event loop from another thread. */
517c43e99fdSEd Maste 
518c43e99fdSEd Maste /* Magic number we use for our filter ID. */
519c43e99fdSEd Maste #define NOTIFY_IDENT 42
520c43e99fdSEd Maste 
521c43e99fdSEd Maste int
event_kq_add_notify_event_(struct event_base * base)522c43e99fdSEd Maste event_kq_add_notify_event_(struct event_base *base)
523c43e99fdSEd Maste {
524c43e99fdSEd Maste 	struct kqop *kqop = base->evbase;
525c43e99fdSEd Maste #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
526c43e99fdSEd Maste 	struct kevent kev;
527c43e99fdSEd Maste 	struct timespec timeout = { 0, 0 };
528c43e99fdSEd Maste #endif
529c43e99fdSEd Maste 
530c43e99fdSEd Maste 	if (kqop->notify_event_added)
531c43e99fdSEd Maste 		return 0;
532c43e99fdSEd Maste 
533c43e99fdSEd Maste #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
534c43e99fdSEd Maste 	memset(&kev, 0, sizeof(kev));
535c43e99fdSEd Maste 	kev.ident = NOTIFY_IDENT;
536c43e99fdSEd Maste 	kev.filter = EVFILT_USER;
537c43e99fdSEd Maste 	kev.flags = EV_ADD | EV_CLEAR;
538c43e99fdSEd Maste 
539c43e99fdSEd Maste 	if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
540c43e99fdSEd Maste 		event_warn("kevent: adding EVFILT_USER event");
541c43e99fdSEd Maste 		return -1;
542c43e99fdSEd Maste 	}
543c43e99fdSEd Maste 
544c43e99fdSEd Maste 	kqop->notify_event_added = 1;
545c43e99fdSEd Maste 
546c43e99fdSEd Maste 	return 0;
547c43e99fdSEd Maste #else
548c43e99fdSEd Maste 	return -1;
549c43e99fdSEd Maste #endif
550c43e99fdSEd Maste }
551c43e99fdSEd Maste 
552c43e99fdSEd Maste int
event_kq_notify_base_(struct event_base * base)553c43e99fdSEd Maste event_kq_notify_base_(struct event_base *base)
554c43e99fdSEd Maste {
555c43e99fdSEd Maste 	struct kqop *kqop = base->evbase;
556c43e99fdSEd Maste #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
557c43e99fdSEd Maste 	struct kevent kev;
558c43e99fdSEd Maste 	struct timespec timeout = { 0, 0 };
559c43e99fdSEd Maste #endif
560c43e99fdSEd Maste 	if (! kqop->notify_event_added)
561c43e99fdSEd Maste 		return -1;
562c43e99fdSEd Maste 
563c43e99fdSEd Maste #if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
564c43e99fdSEd Maste 	memset(&kev, 0, sizeof(kev));
565c43e99fdSEd Maste 	kev.ident = NOTIFY_IDENT;
566c43e99fdSEd Maste 	kev.filter = EVFILT_USER;
567c43e99fdSEd Maste 	kev.fflags = NOTE_TRIGGER;
568c43e99fdSEd Maste 
569c43e99fdSEd Maste 	if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
570c43e99fdSEd Maste 		event_warn("kevent: triggering EVFILT_USER event");
571c43e99fdSEd Maste 		return -1;
572c43e99fdSEd Maste 	}
573c43e99fdSEd Maste 
574c43e99fdSEd Maste 	return 0;
575c43e99fdSEd Maste #else
576c43e99fdSEd Maste 	return -1;
577c43e99fdSEd Maste #endif
578c43e99fdSEd Maste }
579c43e99fdSEd Maste 
580c43e99fdSEd Maste #endif /* EVENT__HAVE_KQUEUE */
581