1e3a38431SPaul Bohm /*
2e3a38431SPaul Bohm * libev epoll fd activity backend
3e3a38431SPaul Bohm *
4e3a38431SPaul Bohm * Copyright (c) 2007,2008,2009,2010,2011 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 /*
41e3a38431SPaul Bohm * general notes about epoll:
42e3a38431SPaul Bohm *
43e3a38431SPaul Bohm * a) epoll silently removes fds from the fd set. as nothing tells us
44e3a38431SPaul Bohm * that an fd has been removed otherwise, we have to continually
45e3a38431SPaul Bohm * "rearm" fds that we suspect *might* have changed (same
46e3a38431SPaul Bohm * problem with kqueue, but much less costly there).
47e3a38431SPaul Bohm * b) the fact that ADD != MOD creates a lot of extra syscalls due to a)
48e3a38431SPaul Bohm * and seems not to have any advantage.
49e3a38431SPaul Bohm * c) the inability to handle fork or file descriptors (think dup)
50e3a38431SPaul Bohm * limits the applicability over poll, so this is not a generic
51e3a38431SPaul Bohm * poll replacement.
52e3a38431SPaul Bohm * d) epoll doesn't work the same as select with many file descriptors
53e3a38431SPaul Bohm * (such as files). while not critical, no other advanced interface
54e3a38431SPaul Bohm * seems to share this (rather non-unixy) limitation.
55e3a38431SPaul Bohm * e) epoll claims to be embeddable, but in practise you never get
56e3a38431SPaul Bohm * a ready event for the epoll fd (broken: <=2.6.26, working: >=2.6.32).
57e3a38431SPaul Bohm * f) epoll_ctl returning EPERM means the fd is always ready.
58e3a38431SPaul Bohm *
59e3a38431SPaul Bohm * lots of "weird code" and complication handling in this file is due
60e3a38431SPaul Bohm * to these design problems with epoll, as we try very hard to avoid
61e3a38431SPaul Bohm * epoll_ctl syscalls for common usage patterns and handle the breakage
62e3a38431SPaul Bohm * ensuing from receiving events for closed and otherwise long gone
63e3a38431SPaul Bohm * file descriptors.
64e3a38431SPaul Bohm */
65e3a38431SPaul Bohm
66e3a38431SPaul Bohm #include <sys/epoll.h>
67e3a38431SPaul Bohm
68e3a38431SPaul Bohm #define EV_EMASK_EPERM 0x80
69e3a38431SPaul Bohm
70e3a38431SPaul Bohm static void
epoll_modify(EV_P_ int fd,int oev,int nev)71e3a38431SPaul Bohm epoll_modify (EV_P_ int fd, int oev, int nev)
72e3a38431SPaul Bohm {
73e3a38431SPaul Bohm struct epoll_event ev;
74e3a38431SPaul Bohm unsigned char oldmask;
75e3a38431SPaul Bohm
76e3a38431SPaul Bohm /*
77e3a38431SPaul Bohm * we handle EPOLL_CTL_DEL by ignoring it here
78e3a38431SPaul Bohm * on the assumption that the fd is gone anyways
79e3a38431SPaul Bohm * if that is wrong, we have to handle the spurious
80e3a38431SPaul Bohm * event in epoll_poll.
81e3a38431SPaul Bohm * if the fd is added again, we try to ADD it, and, if that
82e3a38431SPaul Bohm * fails, we assume it still has the same eventmask.
83e3a38431SPaul Bohm */
84e3a38431SPaul Bohm if (!nev)
85e3a38431SPaul Bohm return;
86e3a38431SPaul Bohm
87e3a38431SPaul Bohm oldmask = anfds [fd].emask;
88e3a38431SPaul Bohm anfds [fd].emask = nev;
89e3a38431SPaul Bohm
90e3a38431SPaul Bohm /* store the generation counter in the upper 32 bits, the fd in the lower 32 bits */
91e3a38431SPaul Bohm ev.data.u64 = (uint64_t)(uint32_t)fd
92e3a38431SPaul Bohm | ((uint64_t)(uint32_t)++anfds [fd].egen << 32);
93e3a38431SPaul Bohm ev.events = (nev & EV_READ ? EPOLLIN : 0)
94e3a38431SPaul Bohm | (nev & EV_WRITE ? EPOLLOUT : 0);
95e3a38431SPaul Bohm
96e3a38431SPaul Bohm if (expect_true (!epoll_ctl (backend_fd, oev && oldmask != nev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev)))
97e3a38431SPaul Bohm return;
98e3a38431SPaul Bohm
99e3a38431SPaul Bohm if (expect_true (errno == ENOENT))
100e3a38431SPaul Bohm {
101e3a38431SPaul Bohm /* if ENOENT then the fd went away, so try to do the right thing */
102e3a38431SPaul Bohm if (!nev)
103e3a38431SPaul Bohm goto dec_egen;
104e3a38431SPaul Bohm
105e3a38431SPaul Bohm if (!epoll_ctl (backend_fd, EPOLL_CTL_ADD, fd, &ev))
106e3a38431SPaul Bohm return;
107e3a38431SPaul Bohm }
108e3a38431SPaul Bohm else if (expect_true (errno == EEXIST))
109e3a38431SPaul Bohm {
110e3a38431SPaul Bohm /* EEXIST means we ignored a previous DEL, but the fd is still active */
111e3a38431SPaul Bohm /* if the kernel mask is the same as the new mask, we assume it hasn't changed */
112e3a38431SPaul Bohm if (oldmask == nev)
113e3a38431SPaul Bohm goto dec_egen;
114e3a38431SPaul Bohm
115e3a38431SPaul Bohm if (!epoll_ctl (backend_fd, EPOLL_CTL_MOD, fd, &ev))
116e3a38431SPaul Bohm return;
117e3a38431SPaul Bohm }
118e3a38431SPaul Bohm else if (expect_true (errno == EPERM))
119e3a38431SPaul Bohm {
120e3a38431SPaul Bohm /* EPERM means the fd is always ready, but epoll is too snobbish */
121e3a38431SPaul Bohm /* to handle it, unlike select or poll. */
122e3a38431SPaul Bohm anfds [fd].emask = EV_EMASK_EPERM;
123e3a38431SPaul Bohm
124e3a38431SPaul Bohm /* add fd to epoll_eperms, if not already inside */
125e3a38431SPaul Bohm if (!(oldmask & EV_EMASK_EPERM))
126e3a38431SPaul Bohm {
127e3a38431SPaul Bohm array_needsize (int, epoll_eperms, epoll_epermmax, epoll_epermcnt + 1, EMPTY2);
128e3a38431SPaul Bohm epoll_eperms [epoll_epermcnt++] = fd;
129e3a38431SPaul Bohm }
130e3a38431SPaul Bohm
131e3a38431SPaul Bohm return;
132e3a38431SPaul Bohm }
133e3a38431SPaul Bohm
134e3a38431SPaul Bohm fd_kill (EV_A_ fd);
135e3a38431SPaul Bohm
136e3a38431SPaul Bohm dec_egen:
137e3a38431SPaul Bohm /* we didn't successfully call epoll_ctl, so decrement the generation counter again */
138e3a38431SPaul Bohm --anfds [fd].egen;
139e3a38431SPaul Bohm }
140e3a38431SPaul Bohm
141e3a38431SPaul Bohm static void
epoll_poll(EV_P_ ev_tstamp timeout)142e3a38431SPaul Bohm epoll_poll (EV_P_ ev_tstamp timeout)
143e3a38431SPaul Bohm {
144e3a38431SPaul Bohm int i;
145e3a38431SPaul Bohm int eventcnt;
146e3a38431SPaul Bohm
147*93823e6cSPaul Bohm if (expect_false (epoll_epermcnt))
148*93823e6cSPaul Bohm timeout = 0.;
149*93823e6cSPaul Bohm
150e3a38431SPaul Bohm /* epoll wait times cannot be larger than (LONG_MAX - 999UL) / HZ msecs, which is below */
151e3a38431SPaul Bohm /* the default libev max wait time, however. */
152e3a38431SPaul Bohm EV_RELEASE_CB;
153*93823e6cSPaul Bohm eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, timeout * 1e3);
154e3a38431SPaul Bohm EV_ACQUIRE_CB;
155e3a38431SPaul Bohm
156e3a38431SPaul Bohm if (expect_false (eventcnt < 0))
157e3a38431SPaul Bohm {
158e3a38431SPaul Bohm if (errno != EINTR)
159e3a38431SPaul Bohm ev_syserr ("(libev) epoll_wait");
160e3a38431SPaul Bohm
161e3a38431SPaul Bohm return;
162e3a38431SPaul Bohm }
163e3a38431SPaul Bohm
164e3a38431SPaul Bohm for (i = 0; i < eventcnt; ++i)
165e3a38431SPaul Bohm {
166e3a38431SPaul Bohm struct epoll_event *ev = epoll_events + i;
167e3a38431SPaul Bohm
168e3a38431SPaul Bohm int fd = (uint32_t)ev->data.u64; /* mask out the lower 32 bits */
169e3a38431SPaul Bohm int want = anfds [fd].events;
170e3a38431SPaul Bohm int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
171e3a38431SPaul Bohm | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0);
172e3a38431SPaul Bohm
173*93823e6cSPaul Bohm /*
174*93823e6cSPaul Bohm * check for spurious notification.
175*93823e6cSPaul Bohm * this only finds spurious notifications on egen updates
176*93823e6cSPaul Bohm * other spurious notifications will be found by epoll_ctl, below
177*93823e6cSPaul Bohm * we assume that fd is always in range, as we never shrink the anfds array
178*93823e6cSPaul Bohm */
179e3a38431SPaul Bohm if (expect_false ((uint32_t)anfds [fd].egen != (uint32_t)(ev->data.u64 >> 32)))
180e3a38431SPaul Bohm {
181e3a38431SPaul Bohm /* recreate kernel state */
182*93823e6cSPaul Bohm postfork |= 2;
183e3a38431SPaul Bohm continue;
184e3a38431SPaul Bohm }
185e3a38431SPaul Bohm
186e3a38431SPaul Bohm if (expect_false (got & ~want))
187e3a38431SPaul Bohm {
188e3a38431SPaul Bohm anfds [fd].emask = want;
189e3a38431SPaul Bohm
190*93823e6cSPaul Bohm /*
191*93823e6cSPaul Bohm * we received an event but are not interested in it, try mod or del
192*93823e6cSPaul Bohm * this often happens because we optimistically do not unregister fds
193*93823e6cSPaul Bohm * when we are no longer interested in them, but also when we get spurious
194*93823e6cSPaul Bohm * notifications for fds from another process. this is partially handled
195*93823e6cSPaul Bohm * above with the gencounter check (== our fd is not the event fd), and
196*93823e6cSPaul Bohm * partially here, when epoll_ctl returns an error (== a child has the fd
197*93823e6cSPaul Bohm * but we closed it).
198*93823e6cSPaul Bohm */
199e3a38431SPaul Bohm ev->events = (want & EV_READ ? EPOLLIN : 0)
200e3a38431SPaul Bohm | (want & EV_WRITE ? EPOLLOUT : 0);
201e3a38431SPaul Bohm
202e3a38431SPaul Bohm /* pre-2.6.9 kernels require a non-null pointer with EPOLL_CTL_DEL, */
203e3a38431SPaul Bohm /* which is fortunately easy to do for us. */
204e3a38431SPaul Bohm if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev))
205e3a38431SPaul Bohm {
206*93823e6cSPaul Bohm postfork |= 2; /* an error occurred, recreate kernel state */
207e3a38431SPaul Bohm continue;
208e3a38431SPaul Bohm }
209e3a38431SPaul Bohm }
210e3a38431SPaul Bohm
211e3a38431SPaul Bohm fd_event (EV_A_ fd, got);
212e3a38431SPaul Bohm }
213e3a38431SPaul Bohm
214e3a38431SPaul Bohm /* if the receive array was full, increase its size */
215e3a38431SPaul Bohm if (expect_false (eventcnt == epoll_eventmax))
216e3a38431SPaul Bohm {
217e3a38431SPaul Bohm ev_free (epoll_events);
218e3a38431SPaul Bohm epoll_eventmax = array_nextsize (sizeof (struct epoll_event), epoll_eventmax, epoll_eventmax + 1);
219e3a38431SPaul Bohm epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
220e3a38431SPaul Bohm }
221e3a38431SPaul Bohm
222e3a38431SPaul Bohm /* now synthesize events for all fds where epoll fails, while select works... */
223e3a38431SPaul Bohm for (i = epoll_epermcnt; i--; )
224e3a38431SPaul Bohm {
225e3a38431SPaul Bohm int fd = epoll_eperms [i];
226e3a38431SPaul Bohm unsigned char events = anfds [fd].events & (EV_READ | EV_WRITE);
227e3a38431SPaul Bohm
228e3a38431SPaul Bohm if (anfds [fd].emask & EV_EMASK_EPERM && events)
229e3a38431SPaul Bohm fd_event (EV_A_ fd, events);
230e3a38431SPaul Bohm else
231*93823e6cSPaul Bohm {
232e3a38431SPaul Bohm epoll_eperms [i] = epoll_eperms [--epoll_epermcnt];
233*93823e6cSPaul Bohm anfds [fd].emask = 0;
234*93823e6cSPaul Bohm }
235e3a38431SPaul Bohm }
236e3a38431SPaul Bohm }
237e3a38431SPaul Bohm
238e3a38431SPaul Bohm int inline_size
epoll_init(EV_P_ int flags)239e3a38431SPaul Bohm epoll_init (EV_P_ int flags)
240e3a38431SPaul Bohm {
241e3a38431SPaul Bohm #ifdef EPOLL_CLOEXEC
242e3a38431SPaul Bohm backend_fd = epoll_create1 (EPOLL_CLOEXEC);
243e3a38431SPaul Bohm
244*93823e6cSPaul Bohm if (backend_fd < 0 && (errno == EINVAL || errno == ENOSYS))
245e3a38431SPaul Bohm #endif
246e3a38431SPaul Bohm backend_fd = epoll_create (256);
247e3a38431SPaul Bohm
248e3a38431SPaul Bohm if (backend_fd < 0)
249e3a38431SPaul Bohm return 0;
250e3a38431SPaul Bohm
251e3a38431SPaul Bohm fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
252e3a38431SPaul Bohm
253*93823e6cSPaul Bohm backend_mintime = 1e-3; /* epoll does sometimes return early, this is just to avoid the worst */
254e3a38431SPaul Bohm backend_modify = epoll_modify;
255e3a38431SPaul Bohm backend_poll = epoll_poll;
256e3a38431SPaul Bohm
257e3a38431SPaul Bohm epoll_eventmax = 64; /* initial number of events receivable per poll */
258e3a38431SPaul Bohm epoll_events = (struct epoll_event *)ev_malloc (sizeof (struct epoll_event) * epoll_eventmax);
259e3a38431SPaul Bohm
260e3a38431SPaul Bohm return EVBACKEND_EPOLL;
261e3a38431SPaul Bohm }
262e3a38431SPaul Bohm
263e3a38431SPaul Bohm void inline_size
epoll_destroy(EV_P)264e3a38431SPaul Bohm epoll_destroy (EV_P)
265e3a38431SPaul Bohm {
266e3a38431SPaul Bohm ev_free (epoll_events);
267e3a38431SPaul Bohm array_free (epoll_eperm, EMPTY);
268e3a38431SPaul Bohm }
269e3a38431SPaul Bohm
270e3a38431SPaul Bohm void inline_size
epoll_fork(EV_P)271e3a38431SPaul Bohm epoll_fork (EV_P)
272e3a38431SPaul Bohm {
273e3a38431SPaul Bohm close (backend_fd);
274e3a38431SPaul Bohm
275e3a38431SPaul Bohm while ((backend_fd = epoll_create (256)) < 0)
276e3a38431SPaul Bohm ev_syserr ("(libev) epoll_create");
277e3a38431SPaul Bohm
278e3a38431SPaul Bohm fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
279e3a38431SPaul Bohm
280e3a38431SPaul Bohm fd_rearm_all (EV_A);
281e3a38431SPaul Bohm }
282e3a38431SPaul Bohm
283