xref: /netbsd-src/external/bsd/libevent/dist/epoll.c (revision 657871a79c9a2060a6255a242fa1a1ef76b56ec6)
1*657871a7Schristos /*	$NetBSD: epoll.c,v 1.1.1.3 2021/04/07 02:43:14 christos Exp $	*/
26ecf6635Schristos /*
36ecf6635Schristos  * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
46ecf6635Schristos  * Copyright 2007-2012 Niels Provos, Nick Mathewson
56ecf6635Schristos  *
66ecf6635Schristos  * Redistribution and use in source and binary forms, with or without
76ecf6635Schristos  * modification, are permitted provided that the following conditions
86ecf6635Schristos  * are met:
96ecf6635Schristos  * 1. Redistributions of source code must retain the above copyright
106ecf6635Schristos  *    notice, this list of conditions and the following disclaimer.
116ecf6635Schristos  * 2. Redistributions in binary form must reproduce the above copyright
126ecf6635Schristos  *    notice, this list of conditions and the following disclaimer in the
136ecf6635Schristos  *    documentation and/or other materials provided with the distribution.
146ecf6635Schristos  * 3. The name of the author may not be used to endorse or promote products
156ecf6635Schristos  *    derived from this software without specific prior written permission.
166ecf6635Schristos  *
176ecf6635Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186ecf6635Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196ecf6635Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206ecf6635Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216ecf6635Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226ecf6635Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236ecf6635Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246ecf6635Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256ecf6635Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266ecf6635Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276ecf6635Schristos  */
286ecf6635Schristos #include "event2/event-config.h"
296ecf6635Schristos #include <sys/cdefs.h>
30*657871a7Schristos __RCSID("$NetBSD: epoll.c,v 1.1.1.3 2021/04/07 02:43:14 christos Exp $");
31805a1ce9Schristos #include "evconfig-private.h"
32805a1ce9Schristos 
33805a1ce9Schristos #ifdef EVENT__HAVE_EPOLL
346ecf6635Schristos 
356ecf6635Schristos #include <stdint.h>
366ecf6635Schristos #include <sys/types.h>
376ecf6635Schristos #include <sys/resource.h>
38805a1ce9Schristos #ifdef EVENT__HAVE_SYS_TIME_H
396ecf6635Schristos #include <sys/time.h>
406ecf6635Schristos #endif
416ecf6635Schristos #include <sys/queue.h>
426ecf6635Schristos #include <sys/epoll.h>
436ecf6635Schristos #include <signal.h>
446ecf6635Schristos #include <limits.h>
456ecf6635Schristos #include <stdio.h>
466ecf6635Schristos #include <stdlib.h>
476ecf6635Schristos #include <string.h>
486ecf6635Schristos #include <unistd.h>
496ecf6635Schristos #include <errno.h>
50805a1ce9Schristos #ifdef EVENT__HAVE_FCNTL_H
516ecf6635Schristos #include <fcntl.h>
526ecf6635Schristos #endif
53805a1ce9Schristos #ifdef EVENT__HAVE_SYS_TIMERFD_H
54805a1ce9Schristos #include <sys/timerfd.h>
55805a1ce9Schristos #endif
566ecf6635Schristos 
576ecf6635Schristos #include "event-internal.h"
586ecf6635Schristos #include "evsignal-internal.h"
596ecf6635Schristos #include "event2/thread.h"
606ecf6635Schristos #include "evthread-internal.h"
616ecf6635Schristos #include "log-internal.h"
626ecf6635Schristos #include "evmap-internal.h"
636ecf6635Schristos #include "changelist-internal.h"
64805a1ce9Schristos #include "time-internal.h"
65805a1ce9Schristos 
66805a1ce9Schristos /* Since Linux 2.6.17, epoll is able to report about peer half-closed connection
67805a1ce9Schristos    using special EPOLLRDHUP flag on a read event.
68805a1ce9Schristos */
69805a1ce9Schristos #if !defined(EPOLLRDHUP)
70805a1ce9Schristos #define EPOLLRDHUP 0
71805a1ce9Schristos #define EARLY_CLOSE_IF_HAVE_RDHUP 0
72805a1ce9Schristos #else
73805a1ce9Schristos #define EARLY_CLOSE_IF_HAVE_RDHUP EV_FEATURE_EARLY_CLOSE
74805a1ce9Schristos #endif
75805a1ce9Schristos 
76805a1ce9Schristos #include "epolltable-internal.h"
77805a1ce9Schristos 
78805a1ce9Schristos #if defined(EVENT__HAVE_SYS_TIMERFD_H) &&			  \
79805a1ce9Schristos 	defined(EVENT__HAVE_TIMERFD_CREATE) &&			  \
80805a1ce9Schristos 	defined(HAVE_POSIX_MONOTONIC) && defined(TFD_NONBLOCK) && \
81805a1ce9Schristos 	defined(TFD_CLOEXEC)
82805a1ce9Schristos /* Note that we only use timerfd if TFD_NONBLOCK and TFD_CLOEXEC are available
83805a1ce9Schristos    and working.  This means that we can't support it on 2.6.25 (where timerfd
84805a1ce9Schristos    was introduced) or 2.6.26, since 2.6.27 introduced those flags.
85805a1ce9Schristos  */
86805a1ce9Schristos #define USING_TIMERFD
87805a1ce9Schristos #endif
886ecf6635Schristos 
896ecf6635Schristos struct epollop {
906ecf6635Schristos 	struct epoll_event *events;
916ecf6635Schristos 	int nevents;
926ecf6635Schristos 	int epfd;
93805a1ce9Schristos #ifdef USING_TIMERFD
94805a1ce9Schristos 	int timerfd;
95805a1ce9Schristos #endif
966ecf6635Schristos };
976ecf6635Schristos 
986ecf6635Schristos static void *epoll_init(struct event_base *);
996ecf6635Schristos static int epoll_dispatch(struct event_base *, struct timeval *);
1006ecf6635Schristos static void epoll_dealloc(struct event_base *);
1016ecf6635Schristos 
1026ecf6635Schristos static const struct eventop epollops_changelist = {
1036ecf6635Schristos 	"epoll (with changelist)",
1046ecf6635Schristos 	epoll_init,
105805a1ce9Schristos 	event_changelist_add_,
106805a1ce9Schristos 	event_changelist_del_,
1076ecf6635Schristos 	epoll_dispatch,
1086ecf6635Schristos 	epoll_dealloc,
1096ecf6635Schristos 	1, /* need reinit */
110805a1ce9Schristos 	EV_FEATURE_ET|EV_FEATURE_O1| EARLY_CLOSE_IF_HAVE_RDHUP,
1116ecf6635Schristos 	EVENT_CHANGELIST_FDINFO_SIZE
1126ecf6635Schristos };
1136ecf6635Schristos 
1146ecf6635Schristos 
1156ecf6635Schristos static int epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
1166ecf6635Schristos     short old, short events, void *p);
1176ecf6635Schristos static int epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
1186ecf6635Schristos     short old, short events, void *p);
1196ecf6635Schristos 
1206ecf6635Schristos const struct eventop epollops = {
1216ecf6635Schristos 	"epoll",
1226ecf6635Schristos 	epoll_init,
1236ecf6635Schristos 	epoll_nochangelist_add,
1246ecf6635Schristos 	epoll_nochangelist_del,
1256ecf6635Schristos 	epoll_dispatch,
1266ecf6635Schristos 	epoll_dealloc,
1276ecf6635Schristos 	1, /* need reinit */
128805a1ce9Schristos 	EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_EARLY_CLOSE,
1296ecf6635Schristos 	0
1306ecf6635Schristos };
1316ecf6635Schristos 
1326ecf6635Schristos #define INITIAL_NEVENT 32
1336ecf6635Schristos #define MAX_NEVENT 4096
1346ecf6635Schristos 
1356ecf6635Schristos /* On Linux kernels at least up to 2.6.24.4, epoll can't handle timeout
1366ecf6635Schristos  * values bigger than (LONG_MAX - 999ULL)/HZ.  HZ in the wild can be
1376ecf6635Schristos  * as big as 1000, and LONG_MAX can be as small as (1<<31)-1, so the
1386ecf6635Schristos  * largest number of msec we can support here is 2147482.  Let's
1396ecf6635Schristos  * round that down by 47 seconds.
1406ecf6635Schristos  */
1416ecf6635Schristos #define MAX_EPOLL_TIMEOUT_MSEC (35*60*1000)
1426ecf6635Schristos 
1436ecf6635Schristos static void *
epoll_init(struct event_base * base)1446ecf6635Schristos epoll_init(struct event_base *base)
1456ecf6635Schristos {
146805a1ce9Schristos 	int epfd = -1;
1476ecf6635Schristos 	struct epollop *epollop;
1486ecf6635Schristos 
149805a1ce9Schristos #ifdef EVENT__HAVE_EPOLL_CREATE1
150805a1ce9Schristos 	/* First, try the shiny new epoll_create1 interface, if we have it. */
151805a1ce9Schristos 	epfd = epoll_create1(EPOLL_CLOEXEC);
152805a1ce9Schristos #endif
153805a1ce9Schristos 	if (epfd == -1) {
154805a1ce9Schristos 		/* Initialize the kernel queue using the old interface.  (The
155805a1ce9Schristos 		size field is ignored   since 2.6.8.) */
1566ecf6635Schristos 		if ((epfd = epoll_create(32000)) == -1) {
1576ecf6635Schristos 			if (errno != ENOSYS)
1586ecf6635Schristos 				event_warn("epoll_create");
1596ecf6635Schristos 			return (NULL);
1606ecf6635Schristos 		}
1616ecf6635Schristos 		evutil_make_socket_closeonexec(epfd);
162805a1ce9Schristos 	}
1636ecf6635Schristos 
1646ecf6635Schristos 	if (!(epollop = mm_calloc(1, sizeof(struct epollop)))) {
1656ecf6635Schristos 		close(epfd);
1666ecf6635Schristos 		return (NULL);
1676ecf6635Schristos 	}
1686ecf6635Schristos 
1696ecf6635Schristos 	epollop->epfd = epfd;
1706ecf6635Schristos 
1716ecf6635Schristos 	/* Initialize fields */
1726ecf6635Schristos 	epollop->events = mm_calloc(INITIAL_NEVENT, sizeof(struct epoll_event));
1736ecf6635Schristos 	if (epollop->events == NULL) {
1746ecf6635Schristos 		mm_free(epollop);
1756ecf6635Schristos 		close(epfd);
1766ecf6635Schristos 		return (NULL);
1776ecf6635Schristos 	}
1786ecf6635Schristos 	epollop->nevents = INITIAL_NEVENT;
1796ecf6635Schristos 
1806ecf6635Schristos 	if ((base->flags & EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST) != 0 ||
1816ecf6635Schristos 	    ((base->flags & EVENT_BASE_FLAG_IGNORE_ENV) == 0 &&
182805a1ce9Schristos 		evutil_getenv_("EVENT_EPOLL_USE_CHANGELIST") != NULL)) {
1836ecf6635Schristos 
184805a1ce9Schristos 		base->evsel = &epollops_changelist;
185805a1ce9Schristos 	}
186805a1ce9Schristos 
187805a1ce9Schristos #ifdef USING_TIMERFD
188805a1ce9Schristos 	/*
189805a1ce9Schristos 	  The epoll interface ordinarily gives us one-millisecond precision,
190805a1ce9Schristos 	  so on Linux it makes perfect sense to use the CLOCK_MONOTONIC_COARSE
191805a1ce9Schristos 	  timer.  But when the user has set the new PRECISE_TIMER flag for an
192805a1ce9Schristos 	  event_base, we can try to use timerfd to give them finer granularity.
193805a1ce9Schristos 	*/
194805a1ce9Schristos 	if ((base->flags & EVENT_BASE_FLAG_PRECISE_TIMER) &&
195805a1ce9Schristos 	    base->monotonic_timer.monotonic_clock == CLOCK_MONOTONIC) {
196805a1ce9Schristos 		int fd;
197805a1ce9Schristos 		fd = epollop->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
198805a1ce9Schristos 		if (epollop->timerfd >= 0) {
199805a1ce9Schristos 			struct epoll_event epev;
200805a1ce9Schristos 			memset(&epev, 0, sizeof(epev));
201805a1ce9Schristos 			epev.data.fd = epollop->timerfd;
202805a1ce9Schristos 			epev.events = EPOLLIN;
203805a1ce9Schristos 			if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, fd, &epev) < 0) {
204805a1ce9Schristos 				event_warn("epoll_ctl(timerfd)");
205805a1ce9Schristos 				close(fd);
206805a1ce9Schristos 				epollop->timerfd = -1;
207805a1ce9Schristos 			}
208805a1ce9Schristos 		} else {
209805a1ce9Schristos 			if (errno != EINVAL && errno != ENOSYS) {
210805a1ce9Schristos 				/* These errors probably mean that we were
211805a1ce9Schristos 				 * compiled with timerfd/TFD_* support, but
212805a1ce9Schristos 				 * we're running on a kernel that lacks those.
213805a1ce9Schristos 				 */
214805a1ce9Schristos 				event_warn("timerfd_create");
215805a1ce9Schristos 			}
216805a1ce9Schristos 			epollop->timerfd = -1;
217805a1ce9Schristos 		}
218805a1ce9Schristos 	} else {
219805a1ce9Schristos 		epollop->timerfd = -1;
220805a1ce9Schristos 	}
221805a1ce9Schristos #endif
222805a1ce9Schristos 
223805a1ce9Schristos 	evsig_init_(base);
2246ecf6635Schristos 
2256ecf6635Schristos 	return (epollop);
2266ecf6635Schristos }
2276ecf6635Schristos 
2286ecf6635Schristos static const char *
change_to_string(int change)2296ecf6635Schristos change_to_string(int change)
2306ecf6635Schristos {
2316ecf6635Schristos 	change &= (EV_CHANGE_ADD|EV_CHANGE_DEL);
2326ecf6635Schristos 	if (change == EV_CHANGE_ADD) {
2336ecf6635Schristos 		return "add";
2346ecf6635Schristos 	} else if (change == EV_CHANGE_DEL) {
2356ecf6635Schristos 		return "del";
2366ecf6635Schristos 	} else if (change == 0) {
2376ecf6635Schristos 		return "none";
2386ecf6635Schristos 	} else {
2396ecf6635Schristos 		return "???";
2406ecf6635Schristos 	}
2416ecf6635Schristos }
2426ecf6635Schristos 
2436ecf6635Schristos static const char *
epoll_op_to_string(int op)2446ecf6635Schristos epoll_op_to_string(int op)
2456ecf6635Schristos {
2466ecf6635Schristos 	return op == EPOLL_CTL_ADD?"ADD":
2476ecf6635Schristos 	    op == EPOLL_CTL_DEL?"DEL":
2486ecf6635Schristos 	    op == EPOLL_CTL_MOD?"MOD":
2496ecf6635Schristos 	    "???";
2506ecf6635Schristos }
2516ecf6635Schristos 
252805a1ce9Schristos #define PRINT_CHANGES(op, events, ch, status)  \
253805a1ce9Schristos 	"Epoll %s(%d) on fd %d " status ". "       \
254805a1ce9Schristos 	"Old events were %d; "                     \
255805a1ce9Schristos 	"read change was %d (%s); "                \
256805a1ce9Schristos 	"write change was %d (%s); "               \
257805a1ce9Schristos 	"close change was %d (%s)",                \
258805a1ce9Schristos 	epoll_op_to_string(op),                    \
259805a1ce9Schristos 	events,                                    \
260805a1ce9Schristos 	ch->fd,                                    \
261805a1ce9Schristos 	ch->old_events,                            \
262805a1ce9Schristos 	ch->read_change,                           \
263805a1ce9Schristos 	change_to_string(ch->read_change),         \
264805a1ce9Schristos 	ch->write_change,                          \
265805a1ce9Schristos 	change_to_string(ch->write_change),        \
266805a1ce9Schristos 	ch->close_change,                          \
267805a1ce9Schristos 	change_to_string(ch->close_change)
268805a1ce9Schristos 
2696ecf6635Schristos static int
epoll_apply_one_change(struct event_base * base,struct epollop * epollop,const struct event_change * ch)2706ecf6635Schristos epoll_apply_one_change(struct event_base *base,
2716ecf6635Schristos     struct epollop *epollop,
2726ecf6635Schristos     const struct event_change *ch)
2736ecf6635Schristos {
2746ecf6635Schristos 	struct epoll_event epev;
2756ecf6635Schristos 	int op, events = 0;
276805a1ce9Schristos 	int idx;
2776ecf6635Schristos 
278805a1ce9Schristos 	idx = EPOLL_OP_TABLE_INDEX(ch);
279805a1ce9Schristos 	op = epoll_op_table[idx].op;
280805a1ce9Schristos 	events = epoll_op_table[idx].events;
2816ecf6635Schristos 
282805a1ce9Schristos 	if (!events) {
283805a1ce9Schristos 		EVUTIL_ASSERT(op == 0);
2846ecf6635Schristos 		return 0;
285805a1ce9Schristos 	}
286805a1ce9Schristos 
287*657871a7Schristos 	if ((ch->read_change|ch->write_change|ch->close_change) & EV_CHANGE_ET)
288805a1ce9Schristos 		events |= EPOLLET;
2896ecf6635Schristos 
2906ecf6635Schristos 	memset(&epev, 0, sizeof(epev));
2916ecf6635Schristos 	epev.data.fd = ch->fd;
2926ecf6635Schristos 	epev.events = events;
293805a1ce9Schristos 	if (epoll_ctl(epollop->epfd, op, ch->fd, &epev) == 0) {
294805a1ce9Schristos 		event_debug((PRINT_CHANGES(op, epev.events, ch, "okay")));
295805a1ce9Schristos 		return 0;
296805a1ce9Schristos 	}
297805a1ce9Schristos 
298805a1ce9Schristos 	switch (op) {
299805a1ce9Schristos 	case EPOLL_CTL_MOD:
300805a1ce9Schristos 		if (errno == ENOENT) {
3016ecf6635Schristos 			/* If a MOD operation fails with ENOENT, the
3026ecf6635Schristos 			 * fd was probably closed and re-opened.  We
3036ecf6635Schristos 			 * should retry the operation as an ADD.
3046ecf6635Schristos 			 */
3056ecf6635Schristos 			if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, ch->fd, &epev) == -1) {
3066ecf6635Schristos 				event_warn("Epoll MOD(%d) on %d retried as ADD; that failed too",
3076ecf6635Schristos 				    (int)epev.events, ch->fd);
3086ecf6635Schristos 				return -1;
3096ecf6635Schristos 			} else {
3106ecf6635Schristos 				event_debug(("Epoll MOD(%d) on %d retried as ADD; succeeded.",
3116ecf6635Schristos 					(int)epev.events,
3126ecf6635Schristos 					ch->fd));
313805a1ce9Schristos 				return 0;
3146ecf6635Schristos 			}
315805a1ce9Schristos 		}
316805a1ce9Schristos 		break;
317805a1ce9Schristos 	case EPOLL_CTL_ADD:
318805a1ce9Schristos 		if (errno == EEXIST) {
3196ecf6635Schristos 			/* If an ADD operation fails with EEXIST,
3206ecf6635Schristos 			 * either the operation was redundant (as with a
3216ecf6635Schristos 			 * precautionary add), or we ran into a fun
3226ecf6635Schristos 			 * kernel bug where using dup*() to duplicate the
3236ecf6635Schristos 			 * same file into the same fd gives you the same epitem
3246ecf6635Schristos 			 * rather than a fresh one.  For the second case,
3256ecf6635Schristos 			 * we must retry with MOD. */
3266ecf6635Schristos 			if (epoll_ctl(epollop->epfd, EPOLL_CTL_MOD, ch->fd, &epev) == -1) {
3276ecf6635Schristos 				event_warn("Epoll ADD(%d) on %d retried as MOD; that failed too",
3286ecf6635Schristos 				    (int)epev.events, ch->fd);
3296ecf6635Schristos 				return -1;
3306ecf6635Schristos 			} else {
3316ecf6635Schristos 				event_debug(("Epoll ADD(%d) on %d retried as MOD; succeeded.",
3326ecf6635Schristos 					(int)epev.events,
3336ecf6635Schristos 					ch->fd));
334805a1ce9Schristos 				return 0;
3356ecf6635Schristos 			}
336805a1ce9Schristos 		}
337805a1ce9Schristos 		break;
338805a1ce9Schristos 	case EPOLL_CTL_DEL:
339805a1ce9Schristos 		if (errno == ENOENT || errno == EBADF || errno == EPERM) {
3406ecf6635Schristos 			/* If a delete fails with one of these errors,
3416ecf6635Schristos 			 * that's fine too: we closed the fd before we
3426ecf6635Schristos 			 * got around to calling epoll_dispatch. */
3436ecf6635Schristos 			event_debug(("Epoll DEL(%d) on fd %d gave %s: DEL was unnecessary.",
3446ecf6635Schristos 				(int)epev.events,
3456ecf6635Schristos 				ch->fd,
3466ecf6635Schristos 				strerror(errno)));
3476ecf6635Schristos 			return 0;
3486ecf6635Schristos 		}
349805a1ce9Schristos 		break;
350805a1ce9Schristos 	default:
351805a1ce9Schristos 		break;
352805a1ce9Schristos 	}
353805a1ce9Schristos 
354805a1ce9Schristos 	event_warn(PRINT_CHANGES(op, epev.events, ch, "failed"));
355805a1ce9Schristos 	return -1;
356805a1ce9Schristos }
3576ecf6635Schristos 
3586ecf6635Schristos static int
epoll_apply_changes(struct event_base * base)3596ecf6635Schristos epoll_apply_changes(struct event_base *base)
3606ecf6635Schristos {
3616ecf6635Schristos 	struct event_changelist *changelist = &base->changelist;
3626ecf6635Schristos 	struct epollop *epollop = base->evbase;
3636ecf6635Schristos 	struct event_change *ch;
3646ecf6635Schristos 
3656ecf6635Schristos 	int r = 0;
3666ecf6635Schristos 	int i;
3676ecf6635Schristos 
3686ecf6635Schristos 	for (i = 0; i < changelist->n_changes; ++i) {
3696ecf6635Schristos 		ch = &changelist->changes[i];
3706ecf6635Schristos 		if (epoll_apply_one_change(base, epollop, ch) < 0)
3716ecf6635Schristos 			r = -1;
3726ecf6635Schristos 	}
3736ecf6635Schristos 
3746ecf6635Schristos 	return (r);
3756ecf6635Schristos }
3766ecf6635Schristos 
3776ecf6635Schristos static int
epoll_nochangelist_add(struct event_base * base,evutil_socket_t fd,short old,short events,void * p)3786ecf6635Schristos epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
3796ecf6635Schristos     short old, short events, void *p)
3806ecf6635Schristos {
3816ecf6635Schristos 	struct event_change ch;
3826ecf6635Schristos 	ch.fd = fd;
3836ecf6635Schristos 	ch.old_events = old;
384805a1ce9Schristos 	ch.read_change = ch.write_change = ch.close_change = 0;
3856ecf6635Schristos 	if (events & EV_WRITE)
3866ecf6635Schristos 		ch.write_change = EV_CHANGE_ADD |
3876ecf6635Schristos 		    (events & EV_ET);
3886ecf6635Schristos 	if (events & EV_READ)
3896ecf6635Schristos 		ch.read_change = EV_CHANGE_ADD |
3906ecf6635Schristos 		    (events & EV_ET);
391805a1ce9Schristos 	if (events & EV_CLOSED)
392805a1ce9Schristos 		ch.close_change = EV_CHANGE_ADD |
393805a1ce9Schristos 		    (events & EV_ET);
3946ecf6635Schristos 
3956ecf6635Schristos 	return epoll_apply_one_change(base, base->evbase, &ch);
3966ecf6635Schristos }
3976ecf6635Schristos 
3986ecf6635Schristos static int
epoll_nochangelist_del(struct event_base * base,evutil_socket_t fd,short old,short events,void * p)3996ecf6635Schristos epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
4006ecf6635Schristos     short old, short events, void *p)
4016ecf6635Schristos {
4026ecf6635Schristos 	struct event_change ch;
4036ecf6635Schristos 	ch.fd = fd;
4046ecf6635Schristos 	ch.old_events = old;
405805a1ce9Schristos 	ch.read_change = ch.write_change = ch.close_change = 0;
4066ecf6635Schristos 	if (events & EV_WRITE)
407*657871a7Schristos 		ch.write_change = EV_CHANGE_DEL |
408*657871a7Schristos 		    (events & EV_ET);
4096ecf6635Schristos 	if (events & EV_READ)
410*657871a7Schristos 		ch.read_change = EV_CHANGE_DEL |
411*657871a7Schristos 		    (events & EV_ET);
412805a1ce9Schristos 	if (events & EV_CLOSED)
413*657871a7Schristos 		ch.close_change = EV_CHANGE_DEL |
414*657871a7Schristos 		    (events & EV_ET);
4156ecf6635Schristos 
4166ecf6635Schristos 	return epoll_apply_one_change(base, base->evbase, &ch);
4176ecf6635Schristos }
4186ecf6635Schristos 
4196ecf6635Schristos static int
epoll_dispatch(struct event_base * base,struct timeval * tv)4206ecf6635Schristos epoll_dispatch(struct event_base *base, struct timeval *tv)
4216ecf6635Schristos {
4226ecf6635Schristos 	struct epollop *epollop = base->evbase;
4236ecf6635Schristos 	struct epoll_event *events = epollop->events;
4246ecf6635Schristos 	int i, res;
4256ecf6635Schristos 	long timeout = -1;
4266ecf6635Schristos 
427805a1ce9Schristos #ifdef USING_TIMERFD
428805a1ce9Schristos 	if (epollop->timerfd >= 0) {
429805a1ce9Schristos 		struct itimerspec is;
430805a1ce9Schristos 		is.it_interval.tv_sec = 0;
431805a1ce9Schristos 		is.it_interval.tv_nsec = 0;
432805a1ce9Schristos 		if (tv == NULL) {
433805a1ce9Schristos 			/* No timeout; disarm the timer. */
434805a1ce9Schristos 			is.it_value.tv_sec = 0;
435805a1ce9Schristos 			is.it_value.tv_nsec = 0;
436805a1ce9Schristos 		} else {
437805a1ce9Schristos 			if (tv->tv_sec == 0 && tv->tv_usec == 0) {
438805a1ce9Schristos 				/* we need to exit immediately; timerfd can't
439805a1ce9Schristos 				 * do that. */
440805a1ce9Schristos 				timeout = 0;
441805a1ce9Schristos 			}
442805a1ce9Schristos 			is.it_value.tv_sec = tv->tv_sec;
443805a1ce9Schristos 			is.it_value.tv_nsec = tv->tv_usec * 1000;
444805a1ce9Schristos 		}
445805a1ce9Schristos 		/* TODO: we could avoid unnecessary syscalls here by only
446805a1ce9Schristos 		   calling timerfd_settime when the top timeout changes, or
447805a1ce9Schristos 		   when we're called with a different timeval.
448805a1ce9Schristos 		*/
449805a1ce9Schristos 		if (timerfd_settime(epollop->timerfd, 0, &is, NULL) < 0) {
450805a1ce9Schristos 			event_warn("timerfd_settime");
451805a1ce9Schristos 		}
452805a1ce9Schristos 	} else
453805a1ce9Schristos #endif
4546ecf6635Schristos 	if (tv != NULL) {
455805a1ce9Schristos 		timeout = evutil_tv_to_msec_(tv);
4566ecf6635Schristos 		if (timeout < 0 || timeout > MAX_EPOLL_TIMEOUT_MSEC) {
4576ecf6635Schristos 			/* Linux kernels can wait forever if the timeout is
4586ecf6635Schristos 			 * too big; see comment on MAX_EPOLL_TIMEOUT_MSEC. */
4596ecf6635Schristos 			timeout = MAX_EPOLL_TIMEOUT_MSEC;
4606ecf6635Schristos 		}
4616ecf6635Schristos 	}
4626ecf6635Schristos 
4636ecf6635Schristos 	epoll_apply_changes(base);
464805a1ce9Schristos 	event_changelist_remove_all_(&base->changelist, base);
4656ecf6635Schristos 
4666ecf6635Schristos 	EVBASE_RELEASE_LOCK(base, th_base_lock);
4676ecf6635Schristos 
4686ecf6635Schristos 	res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
4696ecf6635Schristos 
4706ecf6635Schristos 	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
4716ecf6635Schristos 
4726ecf6635Schristos 	if (res == -1) {
4736ecf6635Schristos 		if (errno != EINTR) {
4746ecf6635Schristos 			event_warn("epoll_wait");
4756ecf6635Schristos 			return (-1);
4766ecf6635Schristos 		}
4776ecf6635Schristos 
4786ecf6635Schristos 		return (0);
4796ecf6635Schristos 	}
4806ecf6635Schristos 
4816ecf6635Schristos 	event_debug(("%s: epoll_wait reports %d", __func__, res));
4826ecf6635Schristos 	EVUTIL_ASSERT(res <= epollop->nevents);
4836ecf6635Schristos 
4846ecf6635Schristos 	for (i = 0; i < res; i++) {
4856ecf6635Schristos 		int what = events[i].events;
4866ecf6635Schristos 		short ev = 0;
487805a1ce9Schristos #ifdef USING_TIMERFD
488805a1ce9Schristos 		if (events[i].data.fd == epollop->timerfd)
489805a1ce9Schristos 			continue;
490805a1ce9Schristos #endif
4916ecf6635Schristos 
492*657871a7Schristos 		if (what & EPOLLERR) {
493*657871a7Schristos 			ev = EV_READ | EV_WRITE;
494*657871a7Schristos 		} else if ((what & EPOLLHUP) && !(what & EPOLLRDHUP)) {
4956ecf6635Schristos 			ev = EV_READ | EV_WRITE;
4966ecf6635Schristos 		} else {
4976ecf6635Schristos 			if (what & EPOLLIN)
4986ecf6635Schristos 				ev |= EV_READ;
4996ecf6635Schristos 			if (what & EPOLLOUT)
5006ecf6635Schristos 				ev |= EV_WRITE;
501805a1ce9Schristos 			if (what & EPOLLRDHUP)
502805a1ce9Schristos 				ev |= EV_CLOSED;
5036ecf6635Schristos 		}
5046ecf6635Schristos 
5056ecf6635Schristos 		if (!ev)
5066ecf6635Schristos 			continue;
5076ecf6635Schristos 
508805a1ce9Schristos 		evmap_io_active_(base, events[i].data.fd, ev | EV_ET);
5096ecf6635Schristos 	}
5106ecf6635Schristos 
5116ecf6635Schristos 	if (res == epollop->nevents && epollop->nevents < MAX_NEVENT) {
5126ecf6635Schristos 		/* We used all of the event space this time.  We should
5136ecf6635Schristos 		   be ready for more events next time. */
5146ecf6635Schristos 		int new_nevents = epollop->nevents * 2;
5156ecf6635Schristos 		struct epoll_event *new_events;
5166ecf6635Schristos 
5176ecf6635Schristos 		new_events = mm_realloc(epollop->events,
5186ecf6635Schristos 		    new_nevents * sizeof(struct epoll_event));
5196ecf6635Schristos 		if (new_events) {
5206ecf6635Schristos 			epollop->events = new_events;
5216ecf6635Schristos 			epollop->nevents = new_nevents;
5226ecf6635Schristos 		}
5236ecf6635Schristos 	}
5246ecf6635Schristos 
5256ecf6635Schristos 	return (0);
5266ecf6635Schristos }
5276ecf6635Schristos 
5286ecf6635Schristos 
5296ecf6635Schristos static void
epoll_dealloc(struct event_base * base)5306ecf6635Schristos epoll_dealloc(struct event_base *base)
5316ecf6635Schristos {
5326ecf6635Schristos 	struct epollop *epollop = base->evbase;
5336ecf6635Schristos 
534805a1ce9Schristos 	evsig_dealloc_(base);
5356ecf6635Schristos 	if (epollop->events)
5366ecf6635Schristos 		mm_free(epollop->events);
5376ecf6635Schristos 	if (epollop->epfd >= 0)
5386ecf6635Schristos 		close(epollop->epfd);
539805a1ce9Schristos #ifdef USING_TIMERFD
540805a1ce9Schristos 	if (epollop->timerfd >= 0)
541805a1ce9Schristos 		close(epollop->timerfd);
542805a1ce9Schristos #endif
5436ecf6635Schristos 
5446ecf6635Schristos 	memset(epollop, 0, sizeof(struct epollop));
5456ecf6635Schristos 	mm_free(epollop);
5466ecf6635Schristos }
547805a1ce9Schristos 
548805a1ce9Schristos #endif /* EVENT__HAVE_EPOLL */
549