Home | History | Annotate | Line # | Download | only in libevent
poll.c revision 1.5
      1  1.5  christos /*	$NetBSD: poll.c,v 1.5 2020/05/25 20:47:33 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*	$OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $	*/
      4  1.1  christos 
      5  1.1  christos /*
      6  1.1  christos  * Copyright 2000-2007 Niels Provos <provos (at) citi.umich.edu>
      7  1.1  christos  * Copyright 2007-2012 Niels Provos and Nick Mathewson
      8  1.1  christos  *
      9  1.1  christos  * Redistribution and use in source and binary forms, with or without
     10  1.1  christos  * modification, are permitted provided that the following conditions
     11  1.1  christos  * are met:
     12  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     14  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  christos  *    documentation and/or other materials provided with the distribution.
     17  1.1  christos  * 3. The name of the author may not be used to endorse or promote products
     18  1.1  christos  *    derived from this software without specific prior written permission.
     19  1.1  christos  *
     20  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos #include "event2/event-config.h"
     32  1.1  christos #include "evconfig-private.h"
     33  1.1  christos 
     34  1.1  christos #ifdef EVENT__HAVE_POLL
     35  1.1  christos 
     36  1.1  christos #include <sys/types.h>
     37  1.1  christos #ifdef EVENT__HAVE_SYS_TIME_H
     38  1.1  christos #include <sys/time.h>
     39  1.1  christos #endif
     40  1.1  christos #include <sys/queue.h>
     41  1.1  christos #include <poll.h>
     42  1.1  christos #include <signal.h>
     43  1.1  christos #include <limits.h>
     44  1.1  christos #include <stdio.h>
     45  1.1  christos #include <stdlib.h>
     46  1.1  christos #include <string.h>
     47  1.1  christos #include <unistd.h>
     48  1.1  christos #include <errno.h>
     49  1.1  christos 
     50  1.1  christos #include "event-internal.h"
     51  1.1  christos #include "evsignal-internal.h"
     52  1.1  christos #include "log-internal.h"
     53  1.1  christos #include "evmap-internal.h"
     54  1.1  christos #include "event2/thread.h"
     55  1.1  christos #include "evthread-internal.h"
     56  1.1  christos #include "time-internal.h"
     57  1.1  christos 
     58  1.1  christos struct pollidx {
     59  1.1  christos 	int idxplus1;
     60  1.1  christos };
     61  1.1  christos 
     62  1.1  christos struct pollop {
     63  1.1  christos 	int event_count;		/* Highest number alloc */
     64  1.1  christos 	int nfds;			/* Highest number used */
     65  1.1  christos 	int realloc_copy;		/* True iff we must realloc
     66  1.1  christos 					 * event_set_copy */
     67  1.1  christos 	struct pollfd *event_set;
     68  1.1  christos 	struct pollfd *event_set_copy;
     69  1.1  christos };
     70  1.1  christos 
     71  1.1  christos static void *poll_init(struct event_base *);
     72  1.1  christos static int poll_add(struct event_base *, int, short old, short events, void *idx);
     73  1.1  christos static int poll_del(struct event_base *, int, short old, short events, void *idx);
     74  1.1  christos static int poll_dispatch(struct event_base *, struct timeval *);
     75  1.1  christos static void poll_dealloc(struct event_base *);
     76  1.1  christos 
     77  1.1  christos const struct eventop pollops = {
     78  1.1  christos 	"poll",
     79  1.1  christos 	poll_init,
     80  1.1  christos 	poll_add,
     81  1.1  christos 	poll_del,
     82  1.1  christos 	poll_dispatch,
     83  1.1  christos 	poll_dealloc,
     84  1.1  christos 	0, /* doesn't need_reinit */
     85  1.1  christos 	EV_FEATURE_FDS,
     86  1.1  christos 	sizeof(struct pollidx),
     87  1.1  christos };
     88  1.1  christos 
     89  1.1  christos static void *
     90  1.1  christos poll_init(struct event_base *base)
     91  1.1  christos {
     92  1.1  christos 	struct pollop *pollop;
     93  1.1  christos 
     94  1.1  christos 	if (!(pollop = mm_calloc(1, sizeof(struct pollop))))
     95  1.1  christos 		return (NULL);
     96  1.1  christos 
     97  1.1  christos 	evsig_init_(base);
     98  1.1  christos 
     99  1.1  christos 	evutil_weakrand_seed_(&base->weakrand_seed, 0);
    100  1.1  christos 
    101  1.1  christos 	return (pollop);
    102  1.1  christos }
    103  1.1  christos 
    104  1.1  christos #ifdef CHECK_INVARIANTS
    105  1.1  christos static void
    106  1.1  christos poll_check_ok(struct pollop *pop)
    107  1.1  christos {
    108  1.1  christos 	int i, idx;
    109  1.1  christos 	struct event *ev;
    110  1.1  christos 
    111  1.1  christos 	for (i = 0; i < pop->fd_count; ++i) {
    112  1.1  christos 		idx = pop->idxplus1_by_fd[i]-1;
    113  1.1  christos 		if (idx < 0)
    114  1.1  christos 			continue;
    115  1.1  christos 		EVUTIL_ASSERT(pop->event_set[idx].fd == i);
    116  1.1  christos 	}
    117  1.1  christos 	for (i = 0; i < pop->nfds; ++i) {
    118  1.1  christos 		struct pollfd *pfd = &pop->event_set[i];
    119  1.1  christos 		EVUTIL_ASSERT(pop->idxplus1_by_fd[pfd->fd] == i+1);
    120  1.1  christos 	}
    121  1.1  christos }
    122  1.1  christos #else
    123  1.1  christos #define poll_check_ok(pop)
    124  1.1  christos #endif
    125  1.1  christos 
    126  1.1  christos static int
    127  1.1  christos poll_dispatch(struct event_base *base, struct timeval *tv)
    128  1.1  christos {
    129  1.1  christos 	int res, i, j, nfds;
    130  1.1  christos 	long msec = -1;
    131  1.1  christos 	struct pollop *pop = base->evbase;
    132  1.1  christos 	struct pollfd *event_set;
    133  1.1  christos 
    134  1.1  christos 	poll_check_ok(pop);
    135  1.1  christos 
    136  1.1  christos 	nfds = pop->nfds;
    137  1.1  christos 
    138  1.1  christos #ifndef EVENT__DISABLE_THREAD_SUPPORT
    139  1.1  christos 	if (base->th_base_lock) {
    140  1.1  christos 		/* If we're using this backend in a multithreaded setting,
    141  1.1  christos 		 * then we need to work on a copy of event_set, so that we can
    142  1.1  christos 		 * let other threads modify the main event_set while we're
    143  1.1  christos 		 * polling. If we're not multithreaded, then we'll skip the
    144  1.1  christos 		 * copy step here to save memory and time. */
    145  1.1  christos 		if (pop->realloc_copy) {
    146  1.1  christos 			struct pollfd *tmp = mm_realloc(pop->event_set_copy,
    147  1.1  christos 			    pop->event_count * sizeof(struct pollfd));
    148  1.1  christos 			if (tmp == NULL) {
    149  1.1  christos 				event_warn("realloc");
    150  1.1  christos 				return -1;
    151  1.1  christos 			}
    152  1.1  christos 			pop->event_set_copy = tmp;
    153  1.1  christos 			pop->realloc_copy = 0;
    154  1.1  christos 		}
    155  1.1  christos 		memcpy(pop->event_set_copy, pop->event_set,
    156  1.1  christos 		    sizeof(struct pollfd)*nfds);
    157  1.1  christos 		event_set = pop->event_set_copy;
    158  1.1  christos 	} else {
    159  1.1  christos 		event_set = pop->event_set;
    160  1.1  christos 	}
    161  1.1  christos #else
    162  1.1  christos 	event_set = pop->event_set;
    163  1.1  christos #endif
    164  1.1  christos 
    165  1.1  christos 	if (tv != NULL) {
    166  1.1  christos 		msec = evutil_tv_to_msec_(tv);
    167  1.1  christos 		if (msec < 0 || msec > INT_MAX)
    168  1.1  christos 			msec = INT_MAX;
    169  1.1  christos 	}
    170  1.1  christos 
    171  1.1  christos 	EVBASE_RELEASE_LOCK(base, th_base_lock);
    172  1.1  christos 
    173  1.1  christos 	res = poll(event_set, nfds, msec);
    174  1.1  christos 
    175  1.1  christos 	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
    176  1.1  christos 
    177  1.1  christos 	if (res == -1) {
    178  1.1  christos 		if (errno != EINTR) {
    179  1.1  christos 			event_warn("poll");
    180  1.1  christos 			return (-1);
    181  1.1  christos 		}
    182  1.1  christos 
    183  1.1  christos 		return (0);
    184  1.1  christos 	}
    185  1.1  christos 
    186  1.1  christos 	event_debug(("%s: poll reports %d", __func__, res));
    187  1.1  christos 
    188  1.1  christos 	if (res == 0 || nfds == 0)
    189  1.1  christos 		return (0);
    190  1.1  christos 
    191  1.1  christos 	i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
    192  1.1  christos 	for (j = 0; j < nfds; j++) {
    193  1.1  christos 		int what;
    194  1.1  christos 		if (++i == nfds)
    195  1.1  christos 			i = 0;
    196  1.1  christos 		what = event_set[i].revents;
    197  1.1  christos 		if (!what)
    198  1.1  christos 			continue;
    199  1.1  christos 
    200  1.1  christos 		res = 0;
    201  1.1  christos 
    202  1.1  christos 		/* If the file gets closed notify */
    203  1.1  christos 		if (what & (POLLHUP|POLLERR))
    204  1.1  christos 			what |= POLLIN|POLLOUT;
    205  1.1  christos 		if (what & POLLIN)
    206  1.1  christos 			res |= EV_READ;
    207  1.1  christos 		if (what & POLLOUT)
    208  1.1  christos 			res |= EV_WRITE;
    209  1.1  christos 		if (res == 0)
    210  1.1  christos 			continue;
    211  1.1  christos 
    212  1.1  christos 		evmap_io_active_(base, event_set[i].fd, res);
    213  1.1  christos 	}
    214  1.1  christos 
    215  1.1  christos 	return (0);
    216  1.1  christos }
    217  1.1  christos 
    218  1.1  christos static int
    219  1.1  christos poll_add(struct event_base *base, int fd, short old, short events, void *idx_)
    220  1.1  christos {
    221  1.1  christos 	struct pollop *pop = base->evbase;
    222  1.1  christos 	struct pollfd *pfd = NULL;
    223  1.1  christos 	struct pollidx *idx = idx_;
    224  1.1  christos 	int i;
    225  1.1  christos 
    226  1.1  christos 	EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
    227  1.1  christos 	if (!(events & (EV_READ|EV_WRITE)))
    228  1.1  christos 		return (0);
    229  1.1  christos 
    230  1.1  christos 	poll_check_ok(pop);
    231  1.1  christos 	if (pop->nfds + 1 >= pop->event_count) {
    232  1.1  christos 		struct pollfd *tmp_event_set;
    233  1.1  christos 		int tmp_event_count;
    234  1.1  christos 
    235  1.1  christos 		if (pop->event_count < 32)
    236  1.1  christos 			tmp_event_count = 32;
    237  1.1  christos 		else
    238  1.1  christos 			tmp_event_count = pop->event_count * 2;
    239  1.1  christos 
    240  1.1  christos 		/* We need more file descriptors */
    241  1.1  christos 		tmp_event_set = mm_realloc(pop->event_set,
    242  1.1  christos 				 tmp_event_count * sizeof(struct pollfd));
    243  1.1  christos 		if (tmp_event_set == NULL) {
    244  1.1  christos 			event_warn("realloc");
    245  1.1  christos 			return (-1);
    246  1.1  christos 		}
    247  1.1  christos 		pop->event_set = tmp_event_set;
    248  1.1  christos 
    249  1.1  christos 		pop->event_count = tmp_event_count;
    250  1.1  christos 		pop->realloc_copy = 1;
    251  1.1  christos 	}
    252  1.1  christos 
    253  1.1  christos 	i = idx->idxplus1 - 1;
    254  1.1  christos 
    255  1.1  christos 	if (i >= 0) {
    256  1.1  christos 		pfd = &pop->event_set[i];
    257  1.1  christos 	} else {
    258  1.1  christos 		i = pop->nfds++;
    259  1.1  christos 		pfd = &pop->event_set[i];
    260  1.1  christos 		pfd->events = 0;
    261  1.1  christos 		pfd->fd = fd;
    262  1.1  christos 		idx->idxplus1 = i + 1;
    263  1.1  christos 	}
    264  1.1  christos 
    265  1.1  christos 	pfd->revents = 0;
    266  1.1  christos 	if (events & EV_WRITE)
    267  1.1  christos 		pfd->events |= POLLOUT;
    268  1.1  christos 	if (events & EV_READ)
    269  1.1  christos 		pfd->events |= POLLIN;
    270  1.1  christos 	poll_check_ok(pop);
    271  1.1  christos 
    272  1.1  christos 	return (0);
    273  1.1  christos }
    274  1.1  christos 
    275  1.1  christos /*
    276  1.1  christos  * Nothing to be done here.
    277  1.1  christos  */
    278  1.1  christos 
    279  1.1  christos static int
    280  1.1  christos poll_del(struct event_base *base, int fd, short old, short events, void *idx_)
    281  1.1  christos {
    282  1.1  christos 	struct pollop *pop = base->evbase;
    283  1.1  christos 	struct pollfd *pfd = NULL;
    284  1.1  christos 	struct pollidx *idx = idx_;
    285  1.1  christos 	int i;
    286  1.1  christos 
    287  1.1  christos 	EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
    288  1.1  christos 	if (!(events & (EV_READ|EV_WRITE)))
    289  1.1  christos 		return (0);
    290  1.1  christos 
    291  1.1  christos 	poll_check_ok(pop);
    292  1.1  christos 	i = idx->idxplus1 - 1;
    293  1.1  christos 	if (i < 0)
    294  1.1  christos 		return (-1);
    295  1.1  christos 
    296  1.1  christos 	/* Do we still want to read or write? */
    297  1.1  christos 	pfd = &pop->event_set[i];
    298  1.1  christos 	if (events & EV_READ)
    299  1.1  christos 		pfd->events &= ~POLLIN;
    300  1.1  christos 	if (events & EV_WRITE)
    301  1.1  christos 		pfd->events &= ~POLLOUT;
    302  1.1  christos 	poll_check_ok(pop);
    303  1.1  christos 	if (pfd->events)
    304  1.1  christos 		/* Another event cares about that fd. */
    305  1.1  christos 		return (0);
    306  1.1  christos 
    307  1.1  christos 	/* Okay, so we aren't interested in that fd anymore. */
    308  1.1  christos 	idx->idxplus1 = 0;
    309  1.1  christos 
    310  1.1  christos 	--pop->nfds;
    311  1.1  christos 	if (i != pop->nfds) {
    312  1.1  christos 		/*
    313  1.1  christos 		 * Shift the last pollfd down into the now-unoccupied
    314  1.1  christos 		 * position.
    315  1.1  christos 		 */
    316  1.1  christos 		memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
    317  1.1  christos 		       sizeof(struct pollfd));
    318  1.1  christos 		idx = evmap_io_get_fdinfo_(&base->io, pop->event_set[i].fd);
    319  1.1  christos 		EVUTIL_ASSERT(idx);
    320  1.1  christos 		EVUTIL_ASSERT(idx->idxplus1 == pop->nfds + 1);
    321  1.1  christos 		idx->idxplus1 = i + 1;
    322  1.1  christos 	}
    323  1.1  christos 
    324  1.1  christos 	poll_check_ok(pop);
    325  1.1  christos 	return (0);
    326  1.1  christos }
    327  1.1  christos 
    328  1.1  christos static void
    329  1.1  christos poll_dealloc(struct event_base *base)
    330  1.1  christos {
    331  1.1  christos 	struct pollop *pop = base->evbase;
    332  1.1  christos 
    333  1.1  christos 	evsig_dealloc_(base);
    334  1.1  christos 	if (pop->event_set)
    335  1.1  christos 		mm_free(pop->event_set);
    336  1.1  christos 	if (pop->event_set_copy)
    337  1.1  christos 		mm_free(pop->event_set_copy);
    338  1.1  christos 
    339  1.1  christos 	memset(pop, 0, sizeof(struct pollop));
    340  1.1  christos 	mm_free(pop);
    341  1.1  christos }
    342  1.1  christos 
    343  1.1  christos #endif /* EVENT__HAVE_POLL */
    344