xref: /aosp_15_r20/external/libevent/epoll.c (revision 663afb9b963571284e0f0a60f257164ab54f64bf)
1*663afb9bSAndroid Build Coastguard Worker /*
2*663afb9bSAndroid Build Coastguard Worker  * Copyright 2000-2007 Niels Provos <[email protected]>
3*663afb9bSAndroid Build Coastguard Worker  * Copyright 2007-2012 Niels Provos, Nick Mathewson
4*663afb9bSAndroid Build Coastguard Worker  *
5*663afb9bSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*663afb9bSAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*663afb9bSAndroid Build Coastguard Worker  * are met:
8*663afb9bSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright
9*663afb9bSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*663afb9bSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright
11*663afb9bSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in the
12*663afb9bSAndroid Build Coastguard Worker  *    documentation and/or other materials provided with the distribution.
13*663afb9bSAndroid Build Coastguard Worker  * 3. The name of the author may not be used to endorse or promote products
14*663afb9bSAndroid Build Coastguard Worker  *    derived from this software without specific prior written permission.
15*663afb9bSAndroid Build Coastguard Worker  *
16*663afb9bSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*663afb9bSAndroid Build Coastguard Worker  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*663afb9bSAndroid Build Coastguard Worker  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*663afb9bSAndroid Build Coastguard Worker  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*663afb9bSAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*663afb9bSAndroid Build Coastguard Worker  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*663afb9bSAndroid Build Coastguard Worker  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*663afb9bSAndroid Build Coastguard Worker  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*663afb9bSAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*663afb9bSAndroid Build Coastguard Worker  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*663afb9bSAndroid Build Coastguard Worker  */
27*663afb9bSAndroid Build Coastguard Worker #include "event2/event-config.h"
28*663afb9bSAndroid Build Coastguard Worker #include "evconfig-private.h"
29*663afb9bSAndroid Build Coastguard Worker 
30*663afb9bSAndroid Build Coastguard Worker #ifdef EVENT__HAVE_EPOLL
31*663afb9bSAndroid Build Coastguard Worker 
32*663afb9bSAndroid Build Coastguard Worker #include <stdint.h>
33*663afb9bSAndroid Build Coastguard Worker #include <sys/types.h>
34*663afb9bSAndroid Build Coastguard Worker #include <sys/resource.h>
35*663afb9bSAndroid Build Coastguard Worker #ifdef EVENT__HAVE_SYS_TIME_H
36*663afb9bSAndroid Build Coastguard Worker #include <sys/time.h>
37*663afb9bSAndroid Build Coastguard Worker #endif
38*663afb9bSAndroid Build Coastguard Worker #include <sys/queue.h>
39*663afb9bSAndroid Build Coastguard Worker #include <sys/epoll.h>
40*663afb9bSAndroid Build Coastguard Worker #include <signal.h>
41*663afb9bSAndroid Build Coastguard Worker #include <limits.h>
42*663afb9bSAndroid Build Coastguard Worker #include <stdio.h>
43*663afb9bSAndroid Build Coastguard Worker #include <stdlib.h>
44*663afb9bSAndroid Build Coastguard Worker #include <string.h>
45*663afb9bSAndroid Build Coastguard Worker #include <unistd.h>
46*663afb9bSAndroid Build Coastguard Worker #include <errno.h>
47*663afb9bSAndroid Build Coastguard Worker #ifdef EVENT__HAVE_FCNTL_H
48*663afb9bSAndroid Build Coastguard Worker #include <fcntl.h>
49*663afb9bSAndroid Build Coastguard Worker #endif
50*663afb9bSAndroid Build Coastguard Worker #ifdef EVENT__HAVE_SYS_TIMERFD_H
51*663afb9bSAndroid Build Coastguard Worker #include <sys/timerfd.h>
52*663afb9bSAndroid Build Coastguard Worker #endif
53*663afb9bSAndroid Build Coastguard Worker 
54*663afb9bSAndroid Build Coastguard Worker #include "event-internal.h"
55*663afb9bSAndroid Build Coastguard Worker #include "evsignal-internal.h"
56*663afb9bSAndroid Build Coastguard Worker #include "event2/thread.h"
57*663afb9bSAndroid Build Coastguard Worker #include "evthread-internal.h"
58*663afb9bSAndroid Build Coastguard Worker #include "log-internal.h"
59*663afb9bSAndroid Build Coastguard Worker #include "evmap-internal.h"
60*663afb9bSAndroid Build Coastguard Worker #include "changelist-internal.h"
61*663afb9bSAndroid Build Coastguard Worker #include "time-internal.h"
62*663afb9bSAndroid Build Coastguard Worker 
63*663afb9bSAndroid Build Coastguard Worker /* Since Linux 2.6.17, epoll is able to report about peer half-closed connection
64*663afb9bSAndroid Build Coastguard Worker    using special EPOLLRDHUP flag on a read event.
65*663afb9bSAndroid Build Coastguard Worker */
66*663afb9bSAndroid Build Coastguard Worker #if !defined(EPOLLRDHUP)
67*663afb9bSAndroid Build Coastguard Worker #define EPOLLRDHUP 0
68*663afb9bSAndroid Build Coastguard Worker #define EARLY_CLOSE_IF_HAVE_RDHUP 0
69*663afb9bSAndroid Build Coastguard Worker #else
70*663afb9bSAndroid Build Coastguard Worker #define EARLY_CLOSE_IF_HAVE_RDHUP EV_FEATURE_EARLY_CLOSE
71*663afb9bSAndroid Build Coastguard Worker #endif
72*663afb9bSAndroid Build Coastguard Worker 
73*663afb9bSAndroid Build Coastguard Worker #include "epolltable-internal.h"
74*663afb9bSAndroid Build Coastguard Worker 
75*663afb9bSAndroid Build Coastguard Worker #if defined(EVENT__HAVE_SYS_TIMERFD_H) &&			  \
76*663afb9bSAndroid Build Coastguard Worker 	defined(EVENT__HAVE_TIMERFD_CREATE) &&			  \
77*663afb9bSAndroid Build Coastguard Worker 	defined(HAVE_POSIX_MONOTONIC) && defined(TFD_NONBLOCK) && \
78*663afb9bSAndroid Build Coastguard Worker 	defined(TFD_CLOEXEC)
79*663afb9bSAndroid Build Coastguard Worker /* Note that we only use timerfd if TFD_NONBLOCK and TFD_CLOEXEC are available
80*663afb9bSAndroid Build Coastguard Worker    and working.  This means that we can't support it on 2.6.25 (where timerfd
81*663afb9bSAndroid Build Coastguard Worker    was introduced) or 2.6.26, since 2.6.27 introduced those flags.
82*663afb9bSAndroid Build Coastguard Worker  */
83*663afb9bSAndroid Build Coastguard Worker #define USING_TIMERFD
84*663afb9bSAndroid Build Coastguard Worker #endif
85*663afb9bSAndroid Build Coastguard Worker 
86*663afb9bSAndroid Build Coastguard Worker struct epollop {
87*663afb9bSAndroid Build Coastguard Worker 	struct epoll_event *events;
88*663afb9bSAndroid Build Coastguard Worker 	int nevents;
89*663afb9bSAndroid Build Coastguard Worker 	int epfd;
90*663afb9bSAndroid Build Coastguard Worker #ifdef USING_TIMERFD
91*663afb9bSAndroid Build Coastguard Worker 	int timerfd;
92*663afb9bSAndroid Build Coastguard Worker #endif
93*663afb9bSAndroid Build Coastguard Worker };
94*663afb9bSAndroid Build Coastguard Worker 
95*663afb9bSAndroid Build Coastguard Worker static void *epoll_init(struct event_base *);
96*663afb9bSAndroid Build Coastguard Worker static int epoll_dispatch(struct event_base *, struct timeval *);
97*663afb9bSAndroid Build Coastguard Worker static void epoll_dealloc(struct event_base *);
98*663afb9bSAndroid Build Coastguard Worker 
99*663afb9bSAndroid Build Coastguard Worker static const struct eventop epollops_changelist = {
100*663afb9bSAndroid Build Coastguard Worker 	"epoll (with changelist)",
101*663afb9bSAndroid Build Coastguard Worker 	epoll_init,
102*663afb9bSAndroid Build Coastguard Worker 	event_changelist_add_,
103*663afb9bSAndroid Build Coastguard Worker 	event_changelist_del_,
104*663afb9bSAndroid Build Coastguard Worker 	epoll_dispatch,
105*663afb9bSAndroid Build Coastguard Worker 	epoll_dealloc,
106*663afb9bSAndroid Build Coastguard Worker 	1, /* need reinit */
107*663afb9bSAndroid Build Coastguard Worker 	EV_FEATURE_ET|EV_FEATURE_O1| EARLY_CLOSE_IF_HAVE_RDHUP,
108*663afb9bSAndroid Build Coastguard Worker 	EVENT_CHANGELIST_FDINFO_SIZE
109*663afb9bSAndroid Build Coastguard Worker };
110*663afb9bSAndroid Build Coastguard Worker 
111*663afb9bSAndroid Build Coastguard Worker 
112*663afb9bSAndroid Build Coastguard Worker static int epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
113*663afb9bSAndroid Build Coastguard Worker     short old, short events, void *p);
114*663afb9bSAndroid Build Coastguard Worker static int epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
115*663afb9bSAndroid Build Coastguard Worker     short old, short events, void *p);
116*663afb9bSAndroid Build Coastguard Worker 
117*663afb9bSAndroid Build Coastguard Worker const struct eventop epollops = {
118*663afb9bSAndroid Build Coastguard Worker 	"epoll",
119*663afb9bSAndroid Build Coastguard Worker 	epoll_init,
120*663afb9bSAndroid Build Coastguard Worker 	epoll_nochangelist_add,
121*663afb9bSAndroid Build Coastguard Worker 	epoll_nochangelist_del,
122*663afb9bSAndroid Build Coastguard Worker 	epoll_dispatch,
123*663afb9bSAndroid Build Coastguard Worker 	epoll_dealloc,
124*663afb9bSAndroid Build Coastguard Worker 	1, /* need reinit */
125*663afb9bSAndroid Build Coastguard Worker 	EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_EARLY_CLOSE,
126*663afb9bSAndroid Build Coastguard Worker 	0
127*663afb9bSAndroid Build Coastguard Worker };
128*663afb9bSAndroid Build Coastguard Worker 
129*663afb9bSAndroid Build Coastguard Worker #define INITIAL_NEVENT 32
130*663afb9bSAndroid Build Coastguard Worker #define MAX_NEVENT 4096
131*663afb9bSAndroid Build Coastguard Worker 
132*663afb9bSAndroid Build Coastguard Worker /* On Linux kernels at least up to 2.6.24.4, epoll can't handle timeout
133*663afb9bSAndroid Build Coastguard Worker  * values bigger than (LONG_MAX - 999ULL)/HZ.  HZ in the wild can be
134*663afb9bSAndroid Build Coastguard Worker  * as big as 1000, and LONG_MAX can be as small as (1<<31)-1, so the
135*663afb9bSAndroid Build Coastguard Worker  * largest number of msec we can support here is 2147482.  Let's
136*663afb9bSAndroid Build Coastguard Worker  * round that down by 47 seconds.
137*663afb9bSAndroid Build Coastguard Worker  */
138*663afb9bSAndroid Build Coastguard Worker #define MAX_EPOLL_TIMEOUT_MSEC (35*60*1000)
139*663afb9bSAndroid Build Coastguard Worker 
140*663afb9bSAndroid Build Coastguard Worker static void *
epoll_init(struct event_base * base)141*663afb9bSAndroid Build Coastguard Worker epoll_init(struct event_base *base)
142*663afb9bSAndroid Build Coastguard Worker {
143*663afb9bSAndroid Build Coastguard Worker 	int epfd = -1;
144*663afb9bSAndroid Build Coastguard Worker 	struct epollop *epollop;
145*663afb9bSAndroid Build Coastguard Worker 
146*663afb9bSAndroid Build Coastguard Worker #ifdef EVENT__HAVE_EPOLL_CREATE1
147*663afb9bSAndroid Build Coastguard Worker 	/* First, try the shiny new epoll_create1 interface, if we have it. */
148*663afb9bSAndroid Build Coastguard Worker 	epfd = epoll_create1(EPOLL_CLOEXEC);
149*663afb9bSAndroid Build Coastguard Worker #endif
150*663afb9bSAndroid Build Coastguard Worker 	if (epfd == -1) {
151*663afb9bSAndroid Build Coastguard Worker 		/* Initialize the kernel queue using the old interface.  (The
152*663afb9bSAndroid Build Coastguard Worker 		size field is ignored   since 2.6.8.) */
153*663afb9bSAndroid Build Coastguard Worker 		if ((epfd = epoll_create(32000)) == -1) {
154*663afb9bSAndroid Build Coastguard Worker 			if (errno != ENOSYS)
155*663afb9bSAndroid Build Coastguard Worker 				event_warn("epoll_create");
156*663afb9bSAndroid Build Coastguard Worker 			return (NULL);
157*663afb9bSAndroid Build Coastguard Worker 		}
158*663afb9bSAndroid Build Coastguard Worker 		evutil_make_socket_closeonexec(epfd);
159*663afb9bSAndroid Build Coastguard Worker 	}
160*663afb9bSAndroid Build Coastguard Worker 
161*663afb9bSAndroid Build Coastguard Worker 	if (!(epollop = mm_calloc(1, sizeof(struct epollop)))) {
162*663afb9bSAndroid Build Coastguard Worker 		close(epfd);
163*663afb9bSAndroid Build Coastguard Worker 		return (NULL);
164*663afb9bSAndroid Build Coastguard Worker 	}
165*663afb9bSAndroid Build Coastguard Worker 
166*663afb9bSAndroid Build Coastguard Worker 	epollop->epfd = epfd;
167*663afb9bSAndroid Build Coastguard Worker 
168*663afb9bSAndroid Build Coastguard Worker 	/* Initialize fields */
169*663afb9bSAndroid Build Coastguard Worker 	epollop->events = mm_calloc(INITIAL_NEVENT, sizeof(struct epoll_event));
170*663afb9bSAndroid Build Coastguard Worker 	if (epollop->events == NULL) {
171*663afb9bSAndroid Build Coastguard Worker 		mm_free(epollop);
172*663afb9bSAndroid Build Coastguard Worker 		close(epfd);
173*663afb9bSAndroid Build Coastguard Worker 		return (NULL);
174*663afb9bSAndroid Build Coastguard Worker 	}
175*663afb9bSAndroid Build Coastguard Worker 	epollop->nevents = INITIAL_NEVENT;
176*663afb9bSAndroid Build Coastguard Worker 
177*663afb9bSAndroid Build Coastguard Worker 	if ((base->flags & EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST) != 0 ||
178*663afb9bSAndroid Build Coastguard Worker 	    ((base->flags & EVENT_BASE_FLAG_IGNORE_ENV) == 0 &&
179*663afb9bSAndroid Build Coastguard Worker 		evutil_getenv_("EVENT_EPOLL_USE_CHANGELIST") != NULL)) {
180*663afb9bSAndroid Build Coastguard Worker 
181*663afb9bSAndroid Build Coastguard Worker 		base->evsel = &epollops_changelist;
182*663afb9bSAndroid Build Coastguard Worker 	}
183*663afb9bSAndroid Build Coastguard Worker 
184*663afb9bSAndroid Build Coastguard Worker #ifdef USING_TIMERFD
185*663afb9bSAndroid Build Coastguard Worker 	/*
186*663afb9bSAndroid Build Coastguard Worker 	  The epoll interface ordinarily gives us one-millisecond precision,
187*663afb9bSAndroid Build Coastguard Worker 	  so on Linux it makes perfect sense to use the CLOCK_MONOTONIC_COARSE
188*663afb9bSAndroid Build Coastguard Worker 	  timer.  But when the user has set the new PRECISE_TIMER flag for an
189*663afb9bSAndroid Build Coastguard Worker 	  event_base, we can try to use timerfd to give them finer granularity.
190*663afb9bSAndroid Build Coastguard Worker 	*/
191*663afb9bSAndroid Build Coastguard Worker 	if ((base->flags & EVENT_BASE_FLAG_PRECISE_TIMER) &&
192*663afb9bSAndroid Build Coastguard Worker 	    base->monotonic_timer.monotonic_clock == CLOCK_MONOTONIC) {
193*663afb9bSAndroid Build Coastguard Worker 		int fd;
194*663afb9bSAndroid Build Coastguard Worker 		fd = epollop->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
195*663afb9bSAndroid Build Coastguard Worker 		if (epollop->timerfd >= 0) {
196*663afb9bSAndroid Build Coastguard Worker 			struct epoll_event epev;
197*663afb9bSAndroid Build Coastguard Worker 			memset(&epev, 0, sizeof(epev));
198*663afb9bSAndroid Build Coastguard Worker 			epev.data.fd = epollop->timerfd;
199*663afb9bSAndroid Build Coastguard Worker 			epev.events = EPOLLIN;
200*663afb9bSAndroid Build Coastguard Worker 			if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, fd, &epev) < 0) {
201*663afb9bSAndroid Build Coastguard Worker 				event_warn("epoll_ctl(timerfd)");
202*663afb9bSAndroid Build Coastguard Worker 				close(fd);
203*663afb9bSAndroid Build Coastguard Worker 				epollop->timerfd = -1;
204*663afb9bSAndroid Build Coastguard Worker 			}
205*663afb9bSAndroid Build Coastguard Worker 		} else {
206*663afb9bSAndroid Build Coastguard Worker 			if (errno != EINVAL && errno != ENOSYS) {
207*663afb9bSAndroid Build Coastguard Worker 				/* These errors probably mean that we were
208*663afb9bSAndroid Build Coastguard Worker 				 * compiled with timerfd/TFD_* support, but
209*663afb9bSAndroid Build Coastguard Worker 				 * we're running on a kernel that lacks those.
210*663afb9bSAndroid Build Coastguard Worker 				 */
211*663afb9bSAndroid Build Coastguard Worker 				event_warn("timerfd_create");
212*663afb9bSAndroid Build Coastguard Worker 			}
213*663afb9bSAndroid Build Coastguard Worker 			epollop->timerfd = -1;
214*663afb9bSAndroid Build Coastguard Worker 		}
215*663afb9bSAndroid Build Coastguard Worker 	} else {
216*663afb9bSAndroid Build Coastguard Worker 		epollop->timerfd = -1;
217*663afb9bSAndroid Build Coastguard Worker 	}
218*663afb9bSAndroid Build Coastguard Worker #endif
219*663afb9bSAndroid Build Coastguard Worker 
220*663afb9bSAndroid Build Coastguard Worker 	evsig_init_(base);
221*663afb9bSAndroid Build Coastguard Worker 
222*663afb9bSAndroid Build Coastguard Worker 	return (epollop);
223*663afb9bSAndroid Build Coastguard Worker }
224*663afb9bSAndroid Build Coastguard Worker 
225*663afb9bSAndroid Build Coastguard Worker static const char *
change_to_string(int change)226*663afb9bSAndroid Build Coastguard Worker change_to_string(int change)
227*663afb9bSAndroid Build Coastguard Worker {
228*663afb9bSAndroid Build Coastguard Worker 	change &= (EV_CHANGE_ADD|EV_CHANGE_DEL);
229*663afb9bSAndroid Build Coastguard Worker 	if (change == EV_CHANGE_ADD) {
230*663afb9bSAndroid Build Coastguard Worker 		return "add";
231*663afb9bSAndroid Build Coastguard Worker 	} else if (change == EV_CHANGE_DEL) {
232*663afb9bSAndroid Build Coastguard Worker 		return "del";
233*663afb9bSAndroid Build Coastguard Worker 	} else if (change == 0) {
234*663afb9bSAndroid Build Coastguard Worker 		return "none";
235*663afb9bSAndroid Build Coastguard Worker 	} else {
236*663afb9bSAndroid Build Coastguard Worker 		return "???";
237*663afb9bSAndroid Build Coastguard Worker 	}
238*663afb9bSAndroid Build Coastguard Worker }
239*663afb9bSAndroid Build Coastguard Worker 
240*663afb9bSAndroid Build Coastguard Worker static const char *
epoll_op_to_string(int op)241*663afb9bSAndroid Build Coastguard Worker epoll_op_to_string(int op)
242*663afb9bSAndroid Build Coastguard Worker {
243*663afb9bSAndroid Build Coastguard Worker 	return op == EPOLL_CTL_ADD?"ADD":
244*663afb9bSAndroid Build Coastguard Worker 	    op == EPOLL_CTL_DEL?"DEL":
245*663afb9bSAndroid Build Coastguard Worker 	    op == EPOLL_CTL_MOD?"MOD":
246*663afb9bSAndroid Build Coastguard Worker 	    "???";
247*663afb9bSAndroid Build Coastguard Worker }
248*663afb9bSAndroid Build Coastguard Worker 
249*663afb9bSAndroid Build Coastguard Worker #define PRINT_CHANGES(op, events, ch, status)  \
250*663afb9bSAndroid Build Coastguard Worker 	"Epoll %s(%d) on fd %d " status ". "       \
251*663afb9bSAndroid Build Coastguard Worker 	"Old events were %d; "                     \
252*663afb9bSAndroid Build Coastguard Worker 	"read change was %d (%s); "                \
253*663afb9bSAndroid Build Coastguard Worker 	"write change was %d (%s); "               \
254*663afb9bSAndroid Build Coastguard Worker 	"close change was %d (%s)",                \
255*663afb9bSAndroid Build Coastguard Worker 	epoll_op_to_string(op),                    \
256*663afb9bSAndroid Build Coastguard Worker 	events,                                    \
257*663afb9bSAndroid Build Coastguard Worker 	ch->fd,                                    \
258*663afb9bSAndroid Build Coastguard Worker 	ch->old_events,                            \
259*663afb9bSAndroid Build Coastguard Worker 	ch->read_change,                           \
260*663afb9bSAndroid Build Coastguard Worker 	change_to_string(ch->read_change),         \
261*663afb9bSAndroid Build Coastguard Worker 	ch->write_change,                          \
262*663afb9bSAndroid Build Coastguard Worker 	change_to_string(ch->write_change),        \
263*663afb9bSAndroid Build Coastguard Worker 	ch->close_change,                          \
264*663afb9bSAndroid Build Coastguard Worker 	change_to_string(ch->close_change)
265*663afb9bSAndroid Build Coastguard Worker 
266*663afb9bSAndroid Build Coastguard Worker static int
epoll_apply_one_change(struct event_base * base,struct epollop * epollop,const struct event_change * ch)267*663afb9bSAndroid Build Coastguard Worker epoll_apply_one_change(struct event_base *base,
268*663afb9bSAndroid Build Coastguard Worker     struct epollop *epollop,
269*663afb9bSAndroid Build Coastguard Worker     const struct event_change *ch)
270*663afb9bSAndroid Build Coastguard Worker {
271*663afb9bSAndroid Build Coastguard Worker 	struct epoll_event epev;
272*663afb9bSAndroid Build Coastguard Worker 	int op, events = 0;
273*663afb9bSAndroid Build Coastguard Worker 	int idx;
274*663afb9bSAndroid Build Coastguard Worker 
275*663afb9bSAndroid Build Coastguard Worker 	idx = EPOLL_OP_TABLE_INDEX(ch);
276*663afb9bSAndroid Build Coastguard Worker 	op = epoll_op_table[idx].op;
277*663afb9bSAndroid Build Coastguard Worker 	events = epoll_op_table[idx].events;
278*663afb9bSAndroid Build Coastguard Worker 
279*663afb9bSAndroid Build Coastguard Worker 	if (!events) {
280*663afb9bSAndroid Build Coastguard Worker 		EVUTIL_ASSERT(op == 0);
281*663afb9bSAndroid Build Coastguard Worker 		return 0;
282*663afb9bSAndroid Build Coastguard Worker 	}
283*663afb9bSAndroid Build Coastguard Worker 
284*663afb9bSAndroid Build Coastguard Worker 	if ((ch->read_change|ch->write_change|ch->close_change) & EV_CHANGE_ET)
285*663afb9bSAndroid Build Coastguard Worker 		events |= EPOLLET;
286*663afb9bSAndroid Build Coastguard Worker 
287*663afb9bSAndroid Build Coastguard Worker 	memset(&epev, 0, sizeof(epev));
288*663afb9bSAndroid Build Coastguard Worker 	epev.data.fd = ch->fd;
289*663afb9bSAndroid Build Coastguard Worker 	epev.events = events;
290*663afb9bSAndroid Build Coastguard Worker 	if (epoll_ctl(epollop->epfd, op, ch->fd, &epev) == 0) {
291*663afb9bSAndroid Build Coastguard Worker 		event_debug((PRINT_CHANGES(op, epev.events, ch, "okay")));
292*663afb9bSAndroid Build Coastguard Worker 		return 0;
293*663afb9bSAndroid Build Coastguard Worker 	}
294*663afb9bSAndroid Build Coastguard Worker 
295*663afb9bSAndroid Build Coastguard Worker 	switch (op) {
296*663afb9bSAndroid Build Coastguard Worker 	case EPOLL_CTL_MOD:
297*663afb9bSAndroid Build Coastguard Worker 		if (errno == ENOENT) {
298*663afb9bSAndroid Build Coastguard Worker 			/* If a MOD operation fails with ENOENT, the
299*663afb9bSAndroid Build Coastguard Worker 			 * fd was probably closed and re-opened.  We
300*663afb9bSAndroid Build Coastguard Worker 			 * should retry the operation as an ADD.
301*663afb9bSAndroid Build Coastguard Worker 			 */
302*663afb9bSAndroid Build Coastguard Worker 			if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, ch->fd, &epev) == -1) {
303*663afb9bSAndroid Build Coastguard Worker 				event_warn("Epoll MOD(%d) on %d retried as ADD; that failed too",
304*663afb9bSAndroid Build Coastguard Worker 				    (int)epev.events, ch->fd);
305*663afb9bSAndroid Build Coastguard Worker 				return -1;
306*663afb9bSAndroid Build Coastguard Worker 			} else {
307*663afb9bSAndroid Build Coastguard Worker 				event_debug(("Epoll MOD(%d) on %d retried as ADD; succeeded.",
308*663afb9bSAndroid Build Coastguard Worker 					(int)epev.events,
309*663afb9bSAndroid Build Coastguard Worker 					ch->fd));
310*663afb9bSAndroid Build Coastguard Worker 				return 0;
311*663afb9bSAndroid Build Coastguard Worker 			}
312*663afb9bSAndroid Build Coastguard Worker 		}
313*663afb9bSAndroid Build Coastguard Worker 		break;
314*663afb9bSAndroid Build Coastguard Worker 	case EPOLL_CTL_ADD:
315*663afb9bSAndroid Build Coastguard Worker 		if (errno == EEXIST) {
316*663afb9bSAndroid Build Coastguard Worker 			/* If an ADD operation fails with EEXIST,
317*663afb9bSAndroid Build Coastguard Worker 			 * either the operation was redundant (as with a
318*663afb9bSAndroid Build Coastguard Worker 			 * precautionary add), or we ran into a fun
319*663afb9bSAndroid Build Coastguard Worker 			 * kernel bug where using dup*() to duplicate the
320*663afb9bSAndroid Build Coastguard Worker 			 * same file into the same fd gives you the same epitem
321*663afb9bSAndroid Build Coastguard Worker 			 * rather than a fresh one.  For the second case,
322*663afb9bSAndroid Build Coastguard Worker 			 * we must retry with MOD. */
323*663afb9bSAndroid Build Coastguard Worker 			if (epoll_ctl(epollop->epfd, EPOLL_CTL_MOD, ch->fd, &epev) == -1) {
324*663afb9bSAndroid Build Coastguard Worker 				event_warn("Epoll ADD(%d) on %d retried as MOD; that failed too",
325*663afb9bSAndroid Build Coastguard Worker 				    (int)epev.events, ch->fd);
326*663afb9bSAndroid Build Coastguard Worker 				return -1;
327*663afb9bSAndroid Build Coastguard Worker 			} else {
328*663afb9bSAndroid Build Coastguard Worker 				event_debug(("Epoll ADD(%d) on %d retried as MOD; succeeded.",
329*663afb9bSAndroid Build Coastguard Worker 					(int)epev.events,
330*663afb9bSAndroid Build Coastguard Worker 					ch->fd));
331*663afb9bSAndroid Build Coastguard Worker 				return 0;
332*663afb9bSAndroid Build Coastguard Worker 			}
333*663afb9bSAndroid Build Coastguard Worker 		}
334*663afb9bSAndroid Build Coastguard Worker 		break;
335*663afb9bSAndroid Build Coastguard Worker 	case EPOLL_CTL_DEL:
336*663afb9bSAndroid Build Coastguard Worker 		if (errno == ENOENT || errno == EBADF || errno == EPERM) {
337*663afb9bSAndroid Build Coastguard Worker 			/* If a delete fails with one of these errors,
338*663afb9bSAndroid Build Coastguard Worker 			 * that's fine too: we closed the fd before we
339*663afb9bSAndroid Build Coastguard Worker 			 * got around to calling epoll_dispatch. */
340*663afb9bSAndroid Build Coastguard Worker 			event_debug(("Epoll DEL(%d) on fd %d gave %s: DEL was unnecessary.",
341*663afb9bSAndroid Build Coastguard Worker 				(int)epev.events,
342*663afb9bSAndroid Build Coastguard Worker 				ch->fd,
343*663afb9bSAndroid Build Coastguard Worker 				strerror(errno)));
344*663afb9bSAndroid Build Coastguard Worker 			return 0;
345*663afb9bSAndroid Build Coastguard Worker 		}
346*663afb9bSAndroid Build Coastguard Worker 		break;
347*663afb9bSAndroid Build Coastguard Worker 	default:
348*663afb9bSAndroid Build Coastguard Worker 		break;
349*663afb9bSAndroid Build Coastguard Worker 	}
350*663afb9bSAndroid Build Coastguard Worker 
351*663afb9bSAndroid Build Coastguard Worker 	event_warn(PRINT_CHANGES(op, epev.events, ch, "failed"));
352*663afb9bSAndroid Build Coastguard Worker 	return -1;
353*663afb9bSAndroid Build Coastguard Worker }
354*663afb9bSAndroid Build Coastguard Worker 
355*663afb9bSAndroid Build Coastguard Worker static int
epoll_apply_changes(struct event_base * base)356*663afb9bSAndroid Build Coastguard Worker epoll_apply_changes(struct event_base *base)
357*663afb9bSAndroid Build Coastguard Worker {
358*663afb9bSAndroid Build Coastguard Worker 	struct event_changelist *changelist = &base->changelist;
359*663afb9bSAndroid Build Coastguard Worker 	struct epollop *epollop = base->evbase;
360*663afb9bSAndroid Build Coastguard Worker 	struct event_change *ch;
361*663afb9bSAndroid Build Coastguard Worker 
362*663afb9bSAndroid Build Coastguard Worker 	int r = 0;
363*663afb9bSAndroid Build Coastguard Worker 	int i;
364*663afb9bSAndroid Build Coastguard Worker 
365*663afb9bSAndroid Build Coastguard Worker 	for (i = 0; i < changelist->n_changes; ++i) {
366*663afb9bSAndroid Build Coastguard Worker 		ch = &changelist->changes[i];
367*663afb9bSAndroid Build Coastguard Worker 		if (epoll_apply_one_change(base, epollop, ch) < 0)
368*663afb9bSAndroid Build Coastguard Worker 			r = -1;
369*663afb9bSAndroid Build Coastguard Worker 	}
370*663afb9bSAndroid Build Coastguard Worker 
371*663afb9bSAndroid Build Coastguard Worker 	return (r);
372*663afb9bSAndroid Build Coastguard Worker }
373*663afb9bSAndroid Build Coastguard Worker 
374*663afb9bSAndroid Build Coastguard Worker static int
epoll_nochangelist_add(struct event_base * base,evutil_socket_t fd,short old,short events,void * p)375*663afb9bSAndroid Build Coastguard Worker epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
376*663afb9bSAndroid Build Coastguard Worker     short old, short events, void *p)
377*663afb9bSAndroid Build Coastguard Worker {
378*663afb9bSAndroid Build Coastguard Worker 	struct event_change ch;
379*663afb9bSAndroid Build Coastguard Worker 	ch.fd = fd;
380*663afb9bSAndroid Build Coastguard Worker 	ch.old_events = old;
381*663afb9bSAndroid Build Coastguard Worker 	ch.read_change = ch.write_change = ch.close_change = 0;
382*663afb9bSAndroid Build Coastguard Worker 	if (events & EV_WRITE)
383*663afb9bSAndroid Build Coastguard Worker 		ch.write_change = EV_CHANGE_ADD |
384*663afb9bSAndroid Build Coastguard Worker 		    (events & EV_ET);
385*663afb9bSAndroid Build Coastguard Worker 	if (events & EV_READ)
386*663afb9bSAndroid Build Coastguard Worker 		ch.read_change = EV_CHANGE_ADD |
387*663afb9bSAndroid Build Coastguard Worker 		    (events & EV_ET);
388*663afb9bSAndroid Build Coastguard Worker 	if (events & EV_CLOSED)
389*663afb9bSAndroid Build Coastguard Worker 		ch.close_change = EV_CHANGE_ADD |
390*663afb9bSAndroid Build Coastguard Worker 		    (events & EV_ET);
391*663afb9bSAndroid Build Coastguard Worker 
392*663afb9bSAndroid Build Coastguard Worker 	return epoll_apply_one_change(base, base->evbase, &ch);
393*663afb9bSAndroid Build Coastguard Worker }
394*663afb9bSAndroid Build Coastguard Worker 
395*663afb9bSAndroid Build Coastguard Worker static int
epoll_nochangelist_del(struct event_base * base,evutil_socket_t fd,short old,short events,void * p)396*663afb9bSAndroid Build Coastguard Worker epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
397*663afb9bSAndroid Build Coastguard Worker     short old, short events, void *p)
398*663afb9bSAndroid Build Coastguard Worker {
399*663afb9bSAndroid Build Coastguard Worker 	struct event_change ch;
400*663afb9bSAndroid Build Coastguard Worker 	ch.fd = fd;
401*663afb9bSAndroid Build Coastguard Worker 	ch.old_events = old;
402*663afb9bSAndroid Build Coastguard Worker 	ch.read_change = ch.write_change = ch.close_change = 0;
403*663afb9bSAndroid Build Coastguard Worker 	if (events & EV_WRITE)
404*663afb9bSAndroid Build Coastguard Worker 		ch.write_change = EV_CHANGE_DEL |
405*663afb9bSAndroid Build Coastguard Worker 		    (events & EV_ET);
406*663afb9bSAndroid Build Coastguard Worker 	if (events & EV_READ)
407*663afb9bSAndroid Build Coastguard Worker 		ch.read_change = EV_CHANGE_DEL |
408*663afb9bSAndroid Build Coastguard Worker 		    (events & EV_ET);
409*663afb9bSAndroid Build Coastguard Worker 	if (events & EV_CLOSED)
410*663afb9bSAndroid Build Coastguard Worker 		ch.close_change = EV_CHANGE_DEL |
411*663afb9bSAndroid Build Coastguard Worker 		    (events & EV_ET);
412*663afb9bSAndroid Build Coastguard Worker 
413*663afb9bSAndroid Build Coastguard Worker 	return epoll_apply_one_change(base, base->evbase, &ch);
414*663afb9bSAndroid Build Coastguard Worker }
415*663afb9bSAndroid Build Coastguard Worker 
416*663afb9bSAndroid Build Coastguard Worker static int
epoll_dispatch(struct event_base * base,struct timeval * tv)417*663afb9bSAndroid Build Coastguard Worker epoll_dispatch(struct event_base *base, struct timeval *tv)
418*663afb9bSAndroid Build Coastguard Worker {
419*663afb9bSAndroid Build Coastguard Worker 	struct epollop *epollop = base->evbase;
420*663afb9bSAndroid Build Coastguard Worker 	struct epoll_event *events = epollop->events;
421*663afb9bSAndroid Build Coastguard Worker 	int i, res;
422*663afb9bSAndroid Build Coastguard Worker 	long timeout = -1;
423*663afb9bSAndroid Build Coastguard Worker 
424*663afb9bSAndroid Build Coastguard Worker #ifdef USING_TIMERFD
425*663afb9bSAndroid Build Coastguard Worker 	if (epollop->timerfd >= 0) {
426*663afb9bSAndroid Build Coastguard Worker 		struct itimerspec is;
427*663afb9bSAndroid Build Coastguard Worker 		is.it_interval.tv_sec = 0;
428*663afb9bSAndroid Build Coastguard Worker 		is.it_interval.tv_nsec = 0;
429*663afb9bSAndroid Build Coastguard Worker 		if (tv == NULL) {
430*663afb9bSAndroid Build Coastguard Worker 			/* No timeout; disarm the timer. */
431*663afb9bSAndroid Build Coastguard Worker 			is.it_value.tv_sec = 0;
432*663afb9bSAndroid Build Coastguard Worker 			is.it_value.tv_nsec = 0;
433*663afb9bSAndroid Build Coastguard Worker 		} else {
434*663afb9bSAndroid Build Coastguard Worker 			if (tv->tv_sec == 0 && tv->tv_usec == 0) {
435*663afb9bSAndroid Build Coastguard Worker 				/* we need to exit immediately; timerfd can't
436*663afb9bSAndroid Build Coastguard Worker 				 * do that. */
437*663afb9bSAndroid Build Coastguard Worker 				timeout = 0;
438*663afb9bSAndroid Build Coastguard Worker 			}
439*663afb9bSAndroid Build Coastguard Worker 			is.it_value.tv_sec = tv->tv_sec;
440*663afb9bSAndroid Build Coastguard Worker 			is.it_value.tv_nsec = tv->tv_usec * 1000;
441*663afb9bSAndroid Build Coastguard Worker 		}
442*663afb9bSAndroid Build Coastguard Worker 		/* TODO: we could avoid unnecessary syscalls here by only
443*663afb9bSAndroid Build Coastguard Worker 		   calling timerfd_settime when the top timeout changes, or
444*663afb9bSAndroid Build Coastguard Worker 		   when we're called with a different timeval.
445*663afb9bSAndroid Build Coastguard Worker 		*/
446*663afb9bSAndroid Build Coastguard Worker 		if (timerfd_settime(epollop->timerfd, 0, &is, NULL) < 0) {
447*663afb9bSAndroid Build Coastguard Worker 			event_warn("timerfd_settime");
448*663afb9bSAndroid Build Coastguard Worker 		}
449*663afb9bSAndroid Build Coastguard Worker 	} else
450*663afb9bSAndroid Build Coastguard Worker #endif
451*663afb9bSAndroid Build Coastguard Worker 	if (tv != NULL) {
452*663afb9bSAndroid Build Coastguard Worker 		timeout = evutil_tv_to_msec_(tv);
453*663afb9bSAndroid Build Coastguard Worker 		if (timeout < 0 || timeout > MAX_EPOLL_TIMEOUT_MSEC) {
454*663afb9bSAndroid Build Coastguard Worker 			/* Linux kernels can wait forever if the timeout is
455*663afb9bSAndroid Build Coastguard Worker 			 * too big; see comment on MAX_EPOLL_TIMEOUT_MSEC. */
456*663afb9bSAndroid Build Coastguard Worker 			timeout = MAX_EPOLL_TIMEOUT_MSEC;
457*663afb9bSAndroid Build Coastguard Worker 		}
458*663afb9bSAndroid Build Coastguard Worker 	}
459*663afb9bSAndroid Build Coastguard Worker 
460*663afb9bSAndroid Build Coastguard Worker 	epoll_apply_changes(base);
461*663afb9bSAndroid Build Coastguard Worker 	event_changelist_remove_all_(&base->changelist, base);
462*663afb9bSAndroid Build Coastguard Worker 
463*663afb9bSAndroid Build Coastguard Worker 	EVBASE_RELEASE_LOCK(base, th_base_lock);
464*663afb9bSAndroid Build Coastguard Worker 
465*663afb9bSAndroid Build Coastguard Worker 	res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
466*663afb9bSAndroid Build Coastguard Worker 
467*663afb9bSAndroid Build Coastguard Worker 	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
468*663afb9bSAndroid Build Coastguard Worker 
469*663afb9bSAndroid Build Coastguard Worker 	if (res == -1) {
470*663afb9bSAndroid Build Coastguard Worker 		if (errno != EINTR) {
471*663afb9bSAndroid Build Coastguard Worker 			event_warn("epoll_wait");
472*663afb9bSAndroid Build Coastguard Worker 			return (-1);
473*663afb9bSAndroid Build Coastguard Worker 		}
474*663afb9bSAndroid Build Coastguard Worker 
475*663afb9bSAndroid Build Coastguard Worker 		return (0);
476*663afb9bSAndroid Build Coastguard Worker 	}
477*663afb9bSAndroid Build Coastguard Worker 
478*663afb9bSAndroid Build Coastguard Worker 	event_debug(("%s: epoll_wait reports %d", __func__, res));
479*663afb9bSAndroid Build Coastguard Worker 	EVUTIL_ASSERT(res <= epollop->nevents);
480*663afb9bSAndroid Build Coastguard Worker 
481*663afb9bSAndroid Build Coastguard Worker 	for (i = 0; i < res; i++) {
482*663afb9bSAndroid Build Coastguard Worker 		int what = events[i].events;
483*663afb9bSAndroid Build Coastguard Worker 		short ev = 0;
484*663afb9bSAndroid Build Coastguard Worker #ifdef USING_TIMERFD
485*663afb9bSAndroid Build Coastguard Worker 		if (events[i].data.fd == epollop->timerfd)
486*663afb9bSAndroid Build Coastguard Worker 			continue;
487*663afb9bSAndroid Build Coastguard Worker #endif
488*663afb9bSAndroid Build Coastguard Worker 
489*663afb9bSAndroid Build Coastguard Worker 		if (what & EPOLLERR) {
490*663afb9bSAndroid Build Coastguard Worker 			ev = EV_READ | EV_WRITE;
491*663afb9bSAndroid Build Coastguard Worker 		} else if ((what & EPOLLHUP) && !(what & EPOLLRDHUP)) {
492*663afb9bSAndroid Build Coastguard Worker 			ev = EV_READ | EV_WRITE;
493*663afb9bSAndroid Build Coastguard Worker 		} else {
494*663afb9bSAndroid Build Coastguard Worker 			if (what & EPOLLIN)
495*663afb9bSAndroid Build Coastguard Worker 				ev |= EV_READ;
496*663afb9bSAndroid Build Coastguard Worker 			if (what & EPOLLOUT)
497*663afb9bSAndroid Build Coastguard Worker 				ev |= EV_WRITE;
498*663afb9bSAndroid Build Coastguard Worker 			if (what & EPOLLRDHUP)
499*663afb9bSAndroid Build Coastguard Worker 				ev |= EV_CLOSED;
500*663afb9bSAndroid Build Coastguard Worker 		}
501*663afb9bSAndroid Build Coastguard Worker 
502*663afb9bSAndroid Build Coastguard Worker 		if (!ev)
503*663afb9bSAndroid Build Coastguard Worker 			continue;
504*663afb9bSAndroid Build Coastguard Worker 
505*663afb9bSAndroid Build Coastguard Worker 		evmap_io_active_(base, events[i].data.fd, ev | EV_ET);
506*663afb9bSAndroid Build Coastguard Worker 	}
507*663afb9bSAndroid Build Coastguard Worker 
508*663afb9bSAndroid Build Coastguard Worker 	if (res == epollop->nevents && epollop->nevents < MAX_NEVENT) {
509*663afb9bSAndroid Build Coastguard Worker 		/* We used all of the event space this time.  We should
510*663afb9bSAndroid Build Coastguard Worker 		   be ready for more events next time. */
511*663afb9bSAndroid Build Coastguard Worker 		int new_nevents = epollop->nevents * 2;
512*663afb9bSAndroid Build Coastguard Worker 		struct epoll_event *new_events;
513*663afb9bSAndroid Build Coastguard Worker 
514*663afb9bSAndroid Build Coastguard Worker 		new_events = mm_realloc(epollop->events,
515*663afb9bSAndroid Build Coastguard Worker 		    new_nevents * sizeof(struct epoll_event));
516*663afb9bSAndroid Build Coastguard Worker 		if (new_events) {
517*663afb9bSAndroid Build Coastguard Worker 			epollop->events = new_events;
518*663afb9bSAndroid Build Coastguard Worker 			epollop->nevents = new_nevents;
519*663afb9bSAndroid Build Coastguard Worker 		}
520*663afb9bSAndroid Build Coastguard Worker 	}
521*663afb9bSAndroid Build Coastguard Worker 
522*663afb9bSAndroid Build Coastguard Worker 	return (0);
523*663afb9bSAndroid Build Coastguard Worker }
524*663afb9bSAndroid Build Coastguard Worker 
525*663afb9bSAndroid Build Coastguard Worker 
526*663afb9bSAndroid Build Coastguard Worker static void
epoll_dealloc(struct event_base * base)527*663afb9bSAndroid Build Coastguard Worker epoll_dealloc(struct event_base *base)
528*663afb9bSAndroid Build Coastguard Worker {
529*663afb9bSAndroid Build Coastguard Worker 	struct epollop *epollop = base->evbase;
530*663afb9bSAndroid Build Coastguard Worker 
531*663afb9bSAndroid Build Coastguard Worker 	evsig_dealloc_(base);
532*663afb9bSAndroid Build Coastguard Worker 	if (epollop->events)
533*663afb9bSAndroid Build Coastguard Worker 		mm_free(epollop->events);
534*663afb9bSAndroid Build Coastguard Worker 	if (epollop->epfd >= 0)
535*663afb9bSAndroid Build Coastguard Worker 		close(epollop->epfd);
536*663afb9bSAndroid Build Coastguard Worker #ifdef USING_TIMERFD
537*663afb9bSAndroid Build Coastguard Worker 	if (epollop->timerfd >= 0)
538*663afb9bSAndroid Build Coastguard Worker 		close(epollop->timerfd);
539*663afb9bSAndroid Build Coastguard Worker #endif
540*663afb9bSAndroid Build Coastguard Worker 
541*663afb9bSAndroid Build Coastguard Worker 	memset(epollop, 0, sizeof(struct epollop));
542*663afb9bSAndroid Build Coastguard Worker 	mm_free(epollop);
543*663afb9bSAndroid Build Coastguard Worker }
544*663afb9bSAndroid Build Coastguard Worker 
545*663afb9bSAndroid Build Coastguard Worker #endif /* EVENT__HAVE_EPOLL */
546