12b15cb3dSCy Schubert /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
22b15cb3dSCy Schubert
32b15cb3dSCy Schubert /*
42b15cb3dSCy Schubert * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
52b15cb3dSCy Schubert * Copyright 2007-2012 Niels Provos and Nick Mathewson
62b15cb3dSCy Schubert *
72b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
82b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
92b15cb3dSCy Schubert * are met:
102b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
112b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
122b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
132b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
142b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
152b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
162b15cb3dSCy Schubert * derived from this software without specific prior written permission.
172b15cb3dSCy Schubert *
182b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
192b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
202b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
212b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
222b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
232b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
272b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282b15cb3dSCy Schubert */
292b15cb3dSCy Schubert #include "event2/event-config.h"
302b15cb3dSCy Schubert #include "evconfig-private.h"
312b15cb3dSCy Schubert
322b15cb3dSCy Schubert #ifdef EVENT__HAVE_POLL
332b15cb3dSCy Schubert
342b15cb3dSCy Schubert #include <sys/types.h>
352b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
362b15cb3dSCy Schubert #include <sys/time.h>
372b15cb3dSCy Schubert #endif
382b15cb3dSCy Schubert #include <sys/queue.h>
392b15cb3dSCy Schubert #include <poll.h>
402b15cb3dSCy Schubert #include <signal.h>
412b15cb3dSCy Schubert #include <limits.h>
422b15cb3dSCy Schubert #include <stdio.h>
432b15cb3dSCy Schubert #include <stdlib.h>
442b15cb3dSCy Schubert #include <string.h>
452b15cb3dSCy Schubert #include <unistd.h>
462b15cb3dSCy Schubert #include <errno.h>
472b15cb3dSCy Schubert
482b15cb3dSCy Schubert #include "event-internal.h"
492b15cb3dSCy Schubert #include "evsignal-internal.h"
502b15cb3dSCy Schubert #include "log-internal.h"
512b15cb3dSCy Schubert #include "evmap-internal.h"
522b15cb3dSCy Schubert #include "event2/thread.h"
532b15cb3dSCy Schubert #include "evthread-internal.h"
542b15cb3dSCy Schubert #include "time-internal.h"
552b15cb3dSCy Schubert
56*a466cc55SCy Schubert /* Since Linux 2.6.17, poll is able to report about peer half-closed connection
57*a466cc55SCy Schubert using special POLLRDHUP flag on a read event.
58*a466cc55SCy Schubert */
59*a466cc55SCy Schubert #if !defined(POLLRDHUP)
60*a466cc55SCy Schubert #define POLLRDHUP 0
61*a466cc55SCy Schubert #define EARLY_CLOSE_IF_HAVE_RDHUP 0
62*a466cc55SCy Schubert #else
63*a466cc55SCy Schubert #define EARLY_CLOSE_IF_HAVE_RDHUP EV_FEATURE_EARLY_CLOSE
64*a466cc55SCy Schubert #endif
65*a466cc55SCy Schubert
66*a466cc55SCy Schubert
672b15cb3dSCy Schubert struct pollidx {
682b15cb3dSCy Schubert int idxplus1;
692b15cb3dSCy Schubert };
702b15cb3dSCy Schubert
712b15cb3dSCy Schubert struct pollop {
722b15cb3dSCy Schubert int event_count; /* Highest number alloc */
732b15cb3dSCy Schubert int nfds; /* Highest number used */
742b15cb3dSCy Schubert int realloc_copy; /* True iff we must realloc
752b15cb3dSCy Schubert * event_set_copy */
762b15cb3dSCy Schubert struct pollfd *event_set;
772b15cb3dSCy Schubert struct pollfd *event_set_copy;
782b15cb3dSCy Schubert };
792b15cb3dSCy Schubert
802b15cb3dSCy Schubert static void *poll_init(struct event_base *);
812b15cb3dSCy Schubert static int poll_add(struct event_base *, int, short old, short events, void *idx);
822b15cb3dSCy Schubert static int poll_del(struct event_base *, int, short old, short events, void *idx);
832b15cb3dSCy Schubert static int poll_dispatch(struct event_base *, struct timeval *);
842b15cb3dSCy Schubert static void poll_dealloc(struct event_base *);
852b15cb3dSCy Schubert
862b15cb3dSCy Schubert const struct eventop pollops = {
872b15cb3dSCy Schubert "poll",
882b15cb3dSCy Schubert poll_init,
892b15cb3dSCy Schubert poll_add,
902b15cb3dSCy Schubert poll_del,
912b15cb3dSCy Schubert poll_dispatch,
922b15cb3dSCy Schubert poll_dealloc,
93*a466cc55SCy Schubert 1, /* need_reinit */
94*a466cc55SCy Schubert EV_FEATURE_FDS|EARLY_CLOSE_IF_HAVE_RDHUP,
952b15cb3dSCy Schubert sizeof(struct pollidx),
962b15cb3dSCy Schubert };
972b15cb3dSCy Schubert
982b15cb3dSCy Schubert static void *
poll_init(struct event_base * base)992b15cb3dSCy Schubert poll_init(struct event_base *base)
1002b15cb3dSCy Schubert {
1012b15cb3dSCy Schubert struct pollop *pollop;
1022b15cb3dSCy Schubert
1032b15cb3dSCy Schubert if (!(pollop = mm_calloc(1, sizeof(struct pollop))))
1042b15cb3dSCy Schubert return (NULL);
1052b15cb3dSCy Schubert
1062b15cb3dSCy Schubert evsig_init_(base);
1072b15cb3dSCy Schubert
1082b15cb3dSCy Schubert evutil_weakrand_seed_(&base->weakrand_seed, 0);
1092b15cb3dSCy Schubert
1102b15cb3dSCy Schubert return (pollop);
1112b15cb3dSCy Schubert }
1122b15cb3dSCy Schubert
1132b15cb3dSCy Schubert #ifdef CHECK_INVARIANTS
1142b15cb3dSCy Schubert static void
poll_check_ok(struct pollop * pop)1152b15cb3dSCy Schubert poll_check_ok(struct pollop *pop)
1162b15cb3dSCy Schubert {
1172b15cb3dSCy Schubert int i, idx;
1182b15cb3dSCy Schubert struct event *ev;
1192b15cb3dSCy Schubert
1202b15cb3dSCy Schubert for (i = 0; i < pop->fd_count; ++i) {
1212b15cb3dSCy Schubert idx = pop->idxplus1_by_fd[i]-1;
1222b15cb3dSCy Schubert if (idx < 0)
1232b15cb3dSCy Schubert continue;
1242b15cb3dSCy Schubert EVUTIL_ASSERT(pop->event_set[idx].fd == i);
1252b15cb3dSCy Schubert }
1262b15cb3dSCy Schubert for (i = 0; i < pop->nfds; ++i) {
1272b15cb3dSCy Schubert struct pollfd *pfd = &pop->event_set[i];
1282b15cb3dSCy Schubert EVUTIL_ASSERT(pop->idxplus1_by_fd[pfd->fd] == i+1);
1292b15cb3dSCy Schubert }
1302b15cb3dSCy Schubert }
1312b15cb3dSCy Schubert #else
1322b15cb3dSCy Schubert #define poll_check_ok(pop)
1332b15cb3dSCy Schubert #endif
1342b15cb3dSCy Schubert
1352b15cb3dSCy Schubert static int
poll_dispatch(struct event_base * base,struct timeval * tv)1362b15cb3dSCy Schubert poll_dispatch(struct event_base *base, struct timeval *tv)
1372b15cb3dSCy Schubert {
1382b15cb3dSCy Schubert int res, i, j, nfds;
1392b15cb3dSCy Schubert long msec = -1;
1402b15cb3dSCy Schubert struct pollop *pop = base->evbase;
1412b15cb3dSCy Schubert struct pollfd *event_set;
1422b15cb3dSCy Schubert
1432b15cb3dSCy Schubert poll_check_ok(pop);
1442b15cb3dSCy Schubert
1452b15cb3dSCy Schubert nfds = pop->nfds;
1462b15cb3dSCy Schubert
1472b15cb3dSCy Schubert #ifndef EVENT__DISABLE_THREAD_SUPPORT
1482b15cb3dSCy Schubert if (base->th_base_lock) {
1492b15cb3dSCy Schubert /* If we're using this backend in a multithreaded setting,
1502b15cb3dSCy Schubert * then we need to work on a copy of event_set, so that we can
1512b15cb3dSCy Schubert * let other threads modify the main event_set while we're
1522b15cb3dSCy Schubert * polling. If we're not multithreaded, then we'll skip the
1532b15cb3dSCy Schubert * copy step here to save memory and time. */
1542b15cb3dSCy Schubert if (pop->realloc_copy) {
1552b15cb3dSCy Schubert struct pollfd *tmp = mm_realloc(pop->event_set_copy,
1562b15cb3dSCy Schubert pop->event_count * sizeof(struct pollfd));
1572b15cb3dSCy Schubert if (tmp == NULL) {
1582b15cb3dSCy Schubert event_warn("realloc");
1592b15cb3dSCy Schubert return -1;
1602b15cb3dSCy Schubert }
1612b15cb3dSCy Schubert pop->event_set_copy = tmp;
1622b15cb3dSCy Schubert pop->realloc_copy = 0;
1632b15cb3dSCy Schubert }
1642b15cb3dSCy Schubert memcpy(pop->event_set_copy, pop->event_set,
1652b15cb3dSCy Schubert sizeof(struct pollfd)*nfds);
1662b15cb3dSCy Schubert event_set = pop->event_set_copy;
1672b15cb3dSCy Schubert } else {
1682b15cb3dSCy Schubert event_set = pop->event_set;
1692b15cb3dSCy Schubert }
1702b15cb3dSCy Schubert #else
1712b15cb3dSCy Schubert event_set = pop->event_set;
1722b15cb3dSCy Schubert #endif
1732b15cb3dSCy Schubert
1742b15cb3dSCy Schubert if (tv != NULL) {
1752b15cb3dSCy Schubert msec = evutil_tv_to_msec_(tv);
1762b15cb3dSCy Schubert if (msec < 0 || msec > INT_MAX)
1772b15cb3dSCy Schubert msec = INT_MAX;
1782b15cb3dSCy Schubert }
1792b15cb3dSCy Schubert
1802b15cb3dSCy Schubert EVBASE_RELEASE_LOCK(base, th_base_lock);
1812b15cb3dSCy Schubert
1822b15cb3dSCy Schubert res = poll(event_set, nfds, msec);
1832b15cb3dSCy Schubert
1842b15cb3dSCy Schubert EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1852b15cb3dSCy Schubert
1862b15cb3dSCy Schubert if (res == -1) {
1872b15cb3dSCy Schubert if (errno != EINTR) {
1882b15cb3dSCy Schubert event_warn("poll");
1892b15cb3dSCy Schubert return (-1);
1902b15cb3dSCy Schubert }
1912b15cb3dSCy Schubert
1922b15cb3dSCy Schubert return (0);
1932b15cb3dSCy Schubert }
1942b15cb3dSCy Schubert
1952b15cb3dSCy Schubert event_debug(("%s: poll reports %d", __func__, res));
1962b15cb3dSCy Schubert
1972b15cb3dSCy Schubert if (res == 0 || nfds == 0)
1982b15cb3dSCy Schubert return (0);
1992b15cb3dSCy Schubert
2002b15cb3dSCy Schubert i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
2012b15cb3dSCy Schubert for (j = 0; j < nfds; j++) {
2022b15cb3dSCy Schubert int what;
2032b15cb3dSCy Schubert if (++i == nfds)
2042b15cb3dSCy Schubert i = 0;
2052b15cb3dSCy Schubert what = event_set[i].revents;
2062b15cb3dSCy Schubert if (!what)
2072b15cb3dSCy Schubert continue;
2082b15cb3dSCy Schubert
2092b15cb3dSCy Schubert res = 0;
2102b15cb3dSCy Schubert
2112b15cb3dSCy Schubert /* If the file gets closed notify */
212*a466cc55SCy Schubert if (what & (POLLHUP|POLLERR|POLLNVAL))
2132b15cb3dSCy Schubert what |= POLLIN|POLLOUT;
2142b15cb3dSCy Schubert if (what & POLLIN)
2152b15cb3dSCy Schubert res |= EV_READ;
2162b15cb3dSCy Schubert if (what & POLLOUT)
2172b15cb3dSCy Schubert res |= EV_WRITE;
218*a466cc55SCy Schubert if (what & POLLRDHUP)
219*a466cc55SCy Schubert res |= EV_CLOSED;
2202b15cb3dSCy Schubert if (res == 0)
2212b15cb3dSCy Schubert continue;
2222b15cb3dSCy Schubert
2232b15cb3dSCy Schubert evmap_io_active_(base, event_set[i].fd, res);
2242b15cb3dSCy Schubert }
2252b15cb3dSCy Schubert
2262b15cb3dSCy Schubert return (0);
2272b15cb3dSCy Schubert }
2282b15cb3dSCy Schubert
2292b15cb3dSCy Schubert static int
poll_add(struct event_base * base,int fd,short old,short events,void * idx_)2302b15cb3dSCy Schubert poll_add(struct event_base *base, int fd, short old, short events, void *idx_)
2312b15cb3dSCy Schubert {
2322b15cb3dSCy Schubert struct pollop *pop = base->evbase;
2332b15cb3dSCy Schubert struct pollfd *pfd = NULL;
2342b15cb3dSCy Schubert struct pollidx *idx = idx_;
2352b15cb3dSCy Schubert int i;
2362b15cb3dSCy Schubert
2372b15cb3dSCy Schubert EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
238*a466cc55SCy Schubert if (!(events & (EV_READ|EV_WRITE|EV_CLOSED)))
2392b15cb3dSCy Schubert return (0);
2402b15cb3dSCy Schubert
2412b15cb3dSCy Schubert poll_check_ok(pop);
2422b15cb3dSCy Schubert if (pop->nfds + 1 >= pop->event_count) {
2432b15cb3dSCy Schubert struct pollfd *tmp_event_set;
2442b15cb3dSCy Schubert int tmp_event_count;
2452b15cb3dSCy Schubert
2462b15cb3dSCy Schubert if (pop->event_count < 32)
2472b15cb3dSCy Schubert tmp_event_count = 32;
2482b15cb3dSCy Schubert else
2492b15cb3dSCy Schubert tmp_event_count = pop->event_count * 2;
2502b15cb3dSCy Schubert
2512b15cb3dSCy Schubert /* We need more file descriptors */
2522b15cb3dSCy Schubert tmp_event_set = mm_realloc(pop->event_set,
2532b15cb3dSCy Schubert tmp_event_count * sizeof(struct pollfd));
2542b15cb3dSCy Schubert if (tmp_event_set == NULL) {
2552b15cb3dSCy Schubert event_warn("realloc");
2562b15cb3dSCy Schubert return (-1);
2572b15cb3dSCy Schubert }
2582b15cb3dSCy Schubert pop->event_set = tmp_event_set;
2592b15cb3dSCy Schubert
2602b15cb3dSCy Schubert pop->event_count = tmp_event_count;
2612b15cb3dSCy Schubert pop->realloc_copy = 1;
2622b15cb3dSCy Schubert }
2632b15cb3dSCy Schubert
2642b15cb3dSCy Schubert i = idx->idxplus1 - 1;
2652b15cb3dSCy Schubert
2662b15cb3dSCy Schubert if (i >= 0) {
2672b15cb3dSCy Schubert pfd = &pop->event_set[i];
2682b15cb3dSCy Schubert } else {
2692b15cb3dSCy Schubert i = pop->nfds++;
2702b15cb3dSCy Schubert pfd = &pop->event_set[i];
2712b15cb3dSCy Schubert pfd->events = 0;
2722b15cb3dSCy Schubert pfd->fd = fd;
2732b15cb3dSCy Schubert idx->idxplus1 = i + 1;
2742b15cb3dSCy Schubert }
2752b15cb3dSCy Schubert
2762b15cb3dSCy Schubert pfd->revents = 0;
2772b15cb3dSCy Schubert if (events & EV_WRITE)
2782b15cb3dSCy Schubert pfd->events |= POLLOUT;
2792b15cb3dSCy Schubert if (events & EV_READ)
2802b15cb3dSCy Schubert pfd->events |= POLLIN;
281*a466cc55SCy Schubert if (events & EV_CLOSED)
282*a466cc55SCy Schubert pfd->events |= POLLRDHUP;
2832b15cb3dSCy Schubert poll_check_ok(pop);
2842b15cb3dSCy Schubert
2852b15cb3dSCy Schubert return (0);
2862b15cb3dSCy Schubert }
2872b15cb3dSCy Schubert
2882b15cb3dSCy Schubert /*
2892b15cb3dSCy Schubert * Nothing to be done here.
2902b15cb3dSCy Schubert */
2912b15cb3dSCy Schubert
2922b15cb3dSCy Schubert static int
poll_del(struct event_base * base,int fd,short old,short events,void * idx_)2932b15cb3dSCy Schubert poll_del(struct event_base *base, int fd, short old, short events, void *idx_)
2942b15cb3dSCy Schubert {
2952b15cb3dSCy Schubert struct pollop *pop = base->evbase;
2962b15cb3dSCy Schubert struct pollfd *pfd = NULL;
2972b15cb3dSCy Schubert struct pollidx *idx = idx_;
2982b15cb3dSCy Schubert int i;
2992b15cb3dSCy Schubert
3002b15cb3dSCy Schubert EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
301*a466cc55SCy Schubert if (!(events & (EV_READ|EV_WRITE|EV_CLOSED)))
3022b15cb3dSCy Schubert return (0);
3032b15cb3dSCy Schubert
3042b15cb3dSCy Schubert poll_check_ok(pop);
3052b15cb3dSCy Schubert i = idx->idxplus1 - 1;
3062b15cb3dSCy Schubert if (i < 0)
3072b15cb3dSCy Schubert return (-1);
3082b15cb3dSCy Schubert
3092b15cb3dSCy Schubert /* Do we still want to read or write? */
3102b15cb3dSCy Schubert pfd = &pop->event_set[i];
3112b15cb3dSCy Schubert if (events & EV_READ)
3122b15cb3dSCy Schubert pfd->events &= ~POLLIN;
3132b15cb3dSCy Schubert if (events & EV_WRITE)
3142b15cb3dSCy Schubert pfd->events &= ~POLLOUT;
315*a466cc55SCy Schubert if (events & EV_CLOSED)
316*a466cc55SCy Schubert pfd->events &= ~POLLRDHUP;
3172b15cb3dSCy Schubert poll_check_ok(pop);
3182b15cb3dSCy Schubert if (pfd->events)
3192b15cb3dSCy Schubert /* Another event cares about that fd. */
3202b15cb3dSCy Schubert return (0);
3212b15cb3dSCy Schubert
3222b15cb3dSCy Schubert /* Okay, so we aren't interested in that fd anymore. */
3232b15cb3dSCy Schubert idx->idxplus1 = 0;
3242b15cb3dSCy Schubert
3252b15cb3dSCy Schubert --pop->nfds;
3262b15cb3dSCy Schubert if (i != pop->nfds) {
3272b15cb3dSCy Schubert /*
3282b15cb3dSCy Schubert * Shift the last pollfd down into the now-unoccupied
3292b15cb3dSCy Schubert * position.
3302b15cb3dSCy Schubert */
3312b15cb3dSCy Schubert memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
3322b15cb3dSCy Schubert sizeof(struct pollfd));
3332b15cb3dSCy Schubert idx = evmap_io_get_fdinfo_(&base->io, pop->event_set[i].fd);
3342b15cb3dSCy Schubert EVUTIL_ASSERT(idx);
3352b15cb3dSCy Schubert EVUTIL_ASSERT(idx->idxplus1 == pop->nfds + 1);
3362b15cb3dSCy Schubert idx->idxplus1 = i + 1;
3372b15cb3dSCy Schubert }
3382b15cb3dSCy Schubert
3392b15cb3dSCy Schubert poll_check_ok(pop);
3402b15cb3dSCy Schubert return (0);
3412b15cb3dSCy Schubert }
3422b15cb3dSCy Schubert
3432b15cb3dSCy Schubert static void
poll_dealloc(struct event_base * base)3442b15cb3dSCy Schubert poll_dealloc(struct event_base *base)
3452b15cb3dSCy Schubert {
3462b15cb3dSCy Schubert struct pollop *pop = base->evbase;
3472b15cb3dSCy Schubert
3482b15cb3dSCy Schubert evsig_dealloc_(base);
3492b15cb3dSCy Schubert if (pop->event_set)
3502b15cb3dSCy Schubert mm_free(pop->event_set);
3512b15cb3dSCy Schubert if (pop->event_set_copy)
3522b15cb3dSCy Schubert mm_free(pop->event_set_copy);
3532b15cb3dSCy Schubert
3542b15cb3dSCy Schubert memset(pop, 0, sizeof(struct pollop));
3552b15cb3dSCy Schubert mm_free(pop);
3562b15cb3dSCy Schubert }
3572b15cb3dSCy Schubert
3582b15cb3dSCy Schubert #endif /* EVENT__HAVE_POLL */
359