1e3a38431SPaul Bohm /*
2e3a38431SPaul Bohm * libev kqueue backend
3e3a38431SPaul Bohm *
4*93823e6cSPaul Bohm * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann <[email protected]>
5e3a38431SPaul Bohm * All rights reserved.
6e3a38431SPaul Bohm *
7e3a38431SPaul Bohm * Redistribution and use in source and binary forms, with or without modifica-
8e3a38431SPaul Bohm * tion, are permitted provided that the following conditions are met:
9e3a38431SPaul Bohm *
10e3a38431SPaul Bohm * 1. Redistributions of source code must retain the above copyright notice,
11e3a38431SPaul Bohm * this list of conditions and the following disclaimer.
12e3a38431SPaul Bohm *
13e3a38431SPaul Bohm * 2. Redistributions in binary form must reproduce the above copyright
14e3a38431SPaul Bohm * notice, this list of conditions and the following disclaimer in the
15e3a38431SPaul Bohm * documentation and/or other materials provided with the distribution.
16e3a38431SPaul Bohm *
17e3a38431SPaul Bohm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18e3a38431SPaul Bohm * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19e3a38431SPaul Bohm * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20e3a38431SPaul Bohm * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21e3a38431SPaul Bohm * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22e3a38431SPaul Bohm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23e3a38431SPaul Bohm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24e3a38431SPaul Bohm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25e3a38431SPaul Bohm * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26e3a38431SPaul Bohm * OF THE POSSIBILITY OF SUCH DAMAGE.
27e3a38431SPaul Bohm *
28e3a38431SPaul Bohm * Alternatively, the contents of this file may be used under the terms of
29e3a38431SPaul Bohm * the GNU General Public License ("GPL") version 2 or any later version,
30e3a38431SPaul Bohm * in which case the provisions of the GPL are applicable instead of
31e3a38431SPaul Bohm * the above. If you wish to allow the use of your version of this file
32e3a38431SPaul Bohm * only under the terms of the GPL and not to allow others to use your
33e3a38431SPaul Bohm * version of this file under the BSD license, indicate your decision
34e3a38431SPaul Bohm * by deleting the provisions above and replace them with the notice
35e3a38431SPaul Bohm * and other provisions required by the GPL. If you do not delete the
36e3a38431SPaul Bohm * provisions above, a recipient may use your version of this file under
37e3a38431SPaul Bohm * either the BSD or the GPL.
38e3a38431SPaul Bohm */
39e3a38431SPaul Bohm
40e3a38431SPaul Bohm #include <sys/types.h>
41e3a38431SPaul Bohm #include <sys/time.h>
42e3a38431SPaul Bohm #include <sys/event.h>
43e3a38431SPaul Bohm #include <string.h>
44e3a38431SPaul Bohm #include <errno.h>
45e3a38431SPaul Bohm
46e3a38431SPaul Bohm void inline_speed
kqueue_change(EV_P_ int fd,int filter,int flags,int fflags)47e3a38431SPaul Bohm kqueue_change (EV_P_ int fd, int filter, int flags, int fflags)
48e3a38431SPaul Bohm {
49e3a38431SPaul Bohm ++kqueue_changecnt;
50e3a38431SPaul Bohm array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2);
51e3a38431SPaul Bohm
52e3a38431SPaul Bohm EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0);
53e3a38431SPaul Bohm }
54e3a38431SPaul Bohm
55e3a38431SPaul Bohm /* OS X at least needs this */
56e3a38431SPaul Bohm #ifndef EV_ENABLE
57e3a38431SPaul Bohm # define EV_ENABLE 0
58e3a38431SPaul Bohm #endif
59e3a38431SPaul Bohm #ifndef NOTE_EOF
60e3a38431SPaul Bohm # define NOTE_EOF 0
61e3a38431SPaul Bohm #endif
62e3a38431SPaul Bohm
63e3a38431SPaul Bohm static void
kqueue_modify(EV_P_ int fd,int oev,int nev)64e3a38431SPaul Bohm kqueue_modify (EV_P_ int fd, int oev, int nev)
65e3a38431SPaul Bohm {
66e3a38431SPaul Bohm if (oev != nev)
67e3a38431SPaul Bohm {
68e3a38431SPaul Bohm if (oev & EV_READ)
69e3a38431SPaul Bohm kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0);
70e3a38431SPaul Bohm
71e3a38431SPaul Bohm if (oev & EV_WRITE)
72e3a38431SPaul Bohm kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0);
73e3a38431SPaul Bohm }
74e3a38431SPaul Bohm
75e3a38431SPaul Bohm /* to detect close/reopen reliably, we have to re-add */
76e3a38431SPaul Bohm /* event requests even when oev == nev */
77e3a38431SPaul Bohm
78e3a38431SPaul Bohm if (nev & EV_READ)
79e3a38431SPaul Bohm kqueue_change (EV_A_ fd, EVFILT_READ , EV_ADD | EV_ENABLE, NOTE_EOF);
80e3a38431SPaul Bohm
81e3a38431SPaul Bohm if (nev & EV_WRITE)
82e3a38431SPaul Bohm kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, NOTE_EOF);
83e3a38431SPaul Bohm }
84e3a38431SPaul Bohm
85e3a38431SPaul Bohm static void
kqueue_poll(EV_P_ ev_tstamp timeout)86e3a38431SPaul Bohm kqueue_poll (EV_P_ ev_tstamp timeout)
87e3a38431SPaul Bohm {
88e3a38431SPaul Bohm int res, i;
89e3a38431SPaul Bohm struct timespec ts;
90e3a38431SPaul Bohm
91e3a38431SPaul Bohm /* need to resize so there is enough space for errors */
92e3a38431SPaul Bohm if (kqueue_changecnt > kqueue_eventmax)
93e3a38431SPaul Bohm {
94e3a38431SPaul Bohm ev_free (kqueue_events);
95e3a38431SPaul Bohm kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_changecnt);
96e3a38431SPaul Bohm kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
97e3a38431SPaul Bohm }
98e3a38431SPaul Bohm
99e3a38431SPaul Bohm EV_RELEASE_CB;
100e3a38431SPaul Bohm EV_TS_SET (ts, timeout);
101e3a38431SPaul Bohm res = kevent (backend_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts);
102e3a38431SPaul Bohm EV_ACQUIRE_CB;
103e3a38431SPaul Bohm kqueue_changecnt = 0;
104e3a38431SPaul Bohm
105e3a38431SPaul Bohm if (expect_false (res < 0))
106e3a38431SPaul Bohm {
107e3a38431SPaul Bohm if (errno != EINTR)
108e3a38431SPaul Bohm ev_syserr ("(libev) kevent");
109e3a38431SPaul Bohm
110e3a38431SPaul Bohm return;
111e3a38431SPaul Bohm }
112e3a38431SPaul Bohm
113e3a38431SPaul Bohm for (i = 0; i < res; ++i)
114e3a38431SPaul Bohm {
115e3a38431SPaul Bohm int fd = kqueue_events [i].ident;
116e3a38431SPaul Bohm
117e3a38431SPaul Bohm if (expect_false (kqueue_events [i].flags & EV_ERROR))
118e3a38431SPaul Bohm {
119e3a38431SPaul Bohm int err = kqueue_events [i].data;
120e3a38431SPaul Bohm
121e3a38431SPaul Bohm /* we are only interested in errors for fds that we are interested in :) */
122e3a38431SPaul Bohm if (anfds [fd].events)
123e3a38431SPaul Bohm {
124e3a38431SPaul Bohm if (err == ENOENT) /* resubmit changes on ENOENT */
125e3a38431SPaul Bohm kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
126e3a38431SPaul Bohm else if (err == EBADF) /* on EBADF, we re-check the fd */
127e3a38431SPaul Bohm {
128e3a38431SPaul Bohm if (fd_valid (fd))
129e3a38431SPaul Bohm kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
130e3a38431SPaul Bohm else
131e3a38431SPaul Bohm fd_kill (EV_A_ fd);
132e3a38431SPaul Bohm }
133e3a38431SPaul Bohm else /* on all other errors, we error out on the fd */
134e3a38431SPaul Bohm fd_kill (EV_A_ fd);
135e3a38431SPaul Bohm }
136e3a38431SPaul Bohm }
137e3a38431SPaul Bohm else
138e3a38431SPaul Bohm fd_event (
139e3a38431SPaul Bohm EV_A_
140e3a38431SPaul Bohm fd,
141e3a38431SPaul Bohm kqueue_events [i].filter == EVFILT_READ ? EV_READ
142e3a38431SPaul Bohm : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE
143e3a38431SPaul Bohm : 0
144e3a38431SPaul Bohm );
145e3a38431SPaul Bohm }
146e3a38431SPaul Bohm
147e3a38431SPaul Bohm if (expect_false (res == kqueue_eventmax))
148e3a38431SPaul Bohm {
149e3a38431SPaul Bohm ev_free (kqueue_events);
150e3a38431SPaul Bohm kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_eventmax + 1);
151e3a38431SPaul Bohm kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
152e3a38431SPaul Bohm }
153e3a38431SPaul Bohm }
154e3a38431SPaul Bohm
155e3a38431SPaul Bohm int inline_size
kqueue_init(EV_P_ int flags)156e3a38431SPaul Bohm kqueue_init (EV_P_ int flags)
157e3a38431SPaul Bohm {
158*93823e6cSPaul Bohm /* initialize the kernel queue */
159*93823e6cSPaul Bohm kqueue_fd_pid = getpid ();
160e3a38431SPaul Bohm if ((backend_fd = kqueue ()) < 0)
161e3a38431SPaul Bohm return 0;
162e3a38431SPaul Bohm
163e3a38431SPaul Bohm fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
164e3a38431SPaul Bohm
165*93823e6cSPaul Bohm backend_mintime = 1e-9; /* apparently, they did the right thing in freebsd */
166e3a38431SPaul Bohm backend_modify = kqueue_modify;
167e3a38431SPaul Bohm backend_poll = kqueue_poll;
168e3a38431SPaul Bohm
169e3a38431SPaul Bohm kqueue_eventmax = 64; /* initial number of events receivable per poll */
170e3a38431SPaul Bohm kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
171e3a38431SPaul Bohm
172e3a38431SPaul Bohm kqueue_changes = 0;
173e3a38431SPaul Bohm kqueue_changemax = 0;
174e3a38431SPaul Bohm kqueue_changecnt = 0;
175e3a38431SPaul Bohm
176e3a38431SPaul Bohm return EVBACKEND_KQUEUE;
177e3a38431SPaul Bohm }
178e3a38431SPaul Bohm
179e3a38431SPaul Bohm void inline_size
kqueue_destroy(EV_P)180e3a38431SPaul Bohm kqueue_destroy (EV_P)
181e3a38431SPaul Bohm {
182e3a38431SPaul Bohm ev_free (kqueue_events);
183e3a38431SPaul Bohm ev_free (kqueue_changes);
184e3a38431SPaul Bohm }
185e3a38431SPaul Bohm
186e3a38431SPaul Bohm void inline_size
kqueue_fork(EV_P)187e3a38431SPaul Bohm kqueue_fork (EV_P)
188e3a38431SPaul Bohm {
189*93823e6cSPaul Bohm /* some BSD kernels don't just destroy the kqueue itself,
190*93823e6cSPaul Bohm * but also close the fd, which isn't documented, and
191*93823e6cSPaul Bohm * impossible to support properly.
192*93823e6cSPaul Bohm * we remember the pid of the kqueue call and only close
193*93823e6cSPaul Bohm * the fd if the pid is still the same.
194*93823e6cSPaul Bohm * this leaks fds on sane kernels, but BSD interfaces are
195*93823e6cSPaul Bohm * notoriously buggy and rarely get fixed.
196*93823e6cSPaul Bohm */
197*93823e6cSPaul Bohm pid_t newpid = getpid ();
198*93823e6cSPaul Bohm
199*93823e6cSPaul Bohm if (newpid == kqueue_fd_pid)
200e3a38431SPaul Bohm close (backend_fd);
201e3a38431SPaul Bohm
202*93823e6cSPaul Bohm kqueue_fd_pid = newpid;
203e3a38431SPaul Bohm while ((backend_fd = kqueue ()) < 0)
204e3a38431SPaul Bohm ev_syserr ("(libev) kqueue");
205e3a38431SPaul Bohm
206e3a38431SPaul Bohm fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
207e3a38431SPaul Bohm
208e3a38431SPaul Bohm /* re-register interest in fds */
209e3a38431SPaul Bohm fd_rearm_all (EV_A);
210e3a38431SPaul Bohm }
211e3a38431SPaul Bohm
212*93823e6cSPaul Bohm /* sys/event.h defines EV_ERROR */
213*93823e6cSPaul Bohm #undef EV_ERROR
214*93823e6cSPaul Bohm
215