michael@0: /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ michael@0: michael@0: /* michael@0: * Copyright 2000-2007 Niels Provos michael@0: * Copyright 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #include "event2/event-config.h" michael@0: michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_TIME_H michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_SYS_SELECT_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "event-internal.h" michael@0: #include "evsignal-internal.h" michael@0: #include "event2/thread.h" michael@0: #include "evthread-internal.h" michael@0: #include "log-internal.h" michael@0: #include "evmap-internal.h" michael@0: michael@0: #ifndef _EVENT_HAVE_FD_MASK michael@0: /* This type is mandatory, but Android doesn't define it. */ michael@0: typedef unsigned long fd_mask; michael@0: #endif michael@0: michael@0: #ifndef NFDBITS michael@0: #define NFDBITS (sizeof(fd_mask)*8) michael@0: #endif michael@0: michael@0: /* Divide positive x by y, rounding up. */ michael@0: #define DIV_ROUNDUP(x, y) (((x)+((y)-1))/(y)) michael@0: michael@0: /* How many bytes to allocate for N fds? */ michael@0: #define SELECT_ALLOC_SIZE(n) \ michael@0: (DIV_ROUNDUP(n, NFDBITS) * sizeof(fd_mask)) michael@0: michael@0: struct selectop { michael@0: int event_fds; /* Highest fd in fd set */ michael@0: int event_fdsz; michael@0: int resize_out_sets; michael@0: fd_set *event_readset_in; michael@0: fd_set *event_writeset_in; michael@0: fd_set *event_readset_out; michael@0: fd_set *event_writeset_out; michael@0: }; michael@0: michael@0: static void *select_init(struct event_base *); michael@0: static int select_add(struct event_base *, int, short old, short events, void*); michael@0: static int select_del(struct event_base *, int, short old, short events, void*); michael@0: static int select_dispatch(struct event_base *, struct timeval *); michael@0: static void select_dealloc(struct event_base *); michael@0: michael@0: const struct eventop selectops = { michael@0: "select", michael@0: select_init, michael@0: select_add, michael@0: select_del, michael@0: select_dispatch, michael@0: select_dealloc, michael@0: 0, /* doesn't need reinit. */ michael@0: EV_FEATURE_FDS, michael@0: 0, michael@0: }; michael@0: michael@0: static int select_resize(struct selectop *sop, int fdsz); michael@0: static void select_free_selectop(struct selectop *sop); michael@0: michael@0: static void * michael@0: select_init(struct event_base *base) michael@0: { michael@0: struct selectop *sop; michael@0: michael@0: if (!(sop = mm_calloc(1, sizeof(struct selectop)))) michael@0: return (NULL); michael@0: michael@0: if (select_resize(sop, SELECT_ALLOC_SIZE(32 + 1))) { michael@0: select_free_selectop(sop); michael@0: return (NULL); michael@0: } michael@0: michael@0: evsig_init(base); michael@0: michael@0: return (sop); michael@0: } michael@0: michael@0: #ifdef CHECK_INVARIANTS michael@0: static void michael@0: check_selectop(struct selectop *sop) michael@0: { michael@0: /* nothing to be done here */ michael@0: } michael@0: #else michael@0: #define check_selectop(sop) do { (void) sop; } while (0) michael@0: #endif michael@0: michael@0: static int michael@0: select_dispatch(struct event_base *base, struct timeval *tv) michael@0: { michael@0: int res=0, i, j, nfds; michael@0: struct selectop *sop = base->evbase; michael@0: michael@0: check_selectop(sop); michael@0: if (sop->resize_out_sets) { michael@0: fd_set *readset_out=NULL, *writeset_out=NULL; michael@0: size_t sz = sop->event_fdsz; michael@0: if (!(readset_out = mm_realloc(sop->event_readset_out, sz))) michael@0: return (-1); michael@0: sop->event_readset_out = readset_out; michael@0: if (!(writeset_out = mm_realloc(sop->event_writeset_out, sz))) { michael@0: /* We don't free readset_out here, since it was michael@0: * already successfully reallocated. The next time michael@0: * we call select_dispatch, the realloc will be a michael@0: * no-op. */ michael@0: return (-1); michael@0: } michael@0: sop->event_writeset_out = writeset_out; michael@0: sop->resize_out_sets = 0; michael@0: } michael@0: michael@0: memcpy(sop->event_readset_out, sop->event_readset_in, michael@0: sop->event_fdsz); michael@0: memcpy(sop->event_writeset_out, sop->event_writeset_in, michael@0: sop->event_fdsz); michael@0: michael@0: nfds = sop->event_fds+1; michael@0: michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: michael@0: res = select(nfds, sop->event_readset_out, michael@0: sop->event_writeset_out, NULL, tv); michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: michael@0: check_selectop(sop); michael@0: michael@0: if (res == -1) { michael@0: if (errno != EINTR) { michael@0: event_warn("select"); michael@0: return (-1); michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: event_debug(("%s: select reports %d", __func__, res)); michael@0: michael@0: check_selectop(sop); michael@0: i = random() % nfds; michael@0: for (j = 0; j < nfds; ++j) { michael@0: if (++i >= nfds) michael@0: i = 0; michael@0: res = 0; michael@0: if (FD_ISSET(i, sop->event_readset_out)) michael@0: res |= EV_READ; michael@0: if (FD_ISSET(i, sop->event_writeset_out)) michael@0: res |= EV_WRITE; michael@0: michael@0: if (res == 0) michael@0: continue; michael@0: michael@0: evmap_io_active(base, i, res); michael@0: } michael@0: check_selectop(sop); michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: static int michael@0: select_resize(struct selectop *sop, int fdsz) michael@0: { michael@0: fd_set *readset_in = NULL; michael@0: fd_set *writeset_in = NULL; michael@0: michael@0: if (sop->event_readset_in) michael@0: check_selectop(sop); michael@0: michael@0: if ((readset_in = mm_realloc(sop->event_readset_in, fdsz)) == NULL) michael@0: goto error; michael@0: sop->event_readset_in = readset_in; michael@0: if ((writeset_in = mm_realloc(sop->event_writeset_in, fdsz)) == NULL) { michael@0: /* Note that this will leave event_readset_in expanded. michael@0: * That's okay; we wouldn't want to free it, since that would michael@0: * change the semantics of select_resize from "expand the michael@0: * readset_in and writeset_in, or return -1" to "expand the michael@0: * *set_in members, or trash them and return -1." michael@0: */ michael@0: goto error; michael@0: } michael@0: sop->event_writeset_in = writeset_in; michael@0: sop->resize_out_sets = 1; michael@0: michael@0: memset((char *)sop->event_readset_in + sop->event_fdsz, 0, michael@0: fdsz - sop->event_fdsz); michael@0: memset((char *)sop->event_writeset_in + sop->event_fdsz, 0, michael@0: fdsz - sop->event_fdsz); michael@0: michael@0: sop->event_fdsz = fdsz; michael@0: check_selectop(sop); michael@0: michael@0: return (0); michael@0: michael@0: error: michael@0: event_warn("malloc"); michael@0: return (-1); michael@0: } michael@0: michael@0: michael@0: static int michael@0: select_add(struct event_base *base, int fd, short old, short events, void *p) michael@0: { michael@0: struct selectop *sop = base->evbase; michael@0: (void) p; michael@0: michael@0: EVUTIL_ASSERT((events & EV_SIGNAL) == 0); michael@0: check_selectop(sop); michael@0: /* michael@0: * Keep track of the highest fd, so that we can calculate the size michael@0: * of the fd_sets for select(2) michael@0: */ michael@0: if (sop->event_fds < fd) { michael@0: int fdsz = sop->event_fdsz; michael@0: michael@0: if (fdsz < (int)sizeof(fd_mask)) michael@0: fdsz = (int)sizeof(fd_mask); michael@0: michael@0: /* In theory we should worry about overflow here. In michael@0: * reality, though, the highest fd on a unixy system will michael@0: * not overflow here. XXXX */ michael@0: while (fdsz < (int) SELECT_ALLOC_SIZE(fd + 1)) michael@0: fdsz *= 2; michael@0: michael@0: if (fdsz != sop->event_fdsz) { michael@0: if (select_resize(sop, fdsz)) { michael@0: check_selectop(sop); michael@0: return (-1); michael@0: } michael@0: } michael@0: michael@0: sop->event_fds = fd; michael@0: } michael@0: michael@0: if (events & EV_READ) michael@0: FD_SET(fd, sop->event_readset_in); michael@0: if (events & EV_WRITE) michael@0: FD_SET(fd, sop->event_writeset_in); michael@0: check_selectop(sop); michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: /* michael@0: * Nothing to be done here. michael@0: */ michael@0: michael@0: static int michael@0: select_del(struct event_base *base, int fd, short old, short events, void *p) michael@0: { michael@0: struct selectop *sop = base->evbase; michael@0: (void)p; michael@0: michael@0: EVUTIL_ASSERT((events & EV_SIGNAL) == 0); michael@0: check_selectop(sop); michael@0: michael@0: if (sop->event_fds < fd) { michael@0: check_selectop(sop); michael@0: return (0); michael@0: } michael@0: michael@0: if (events & EV_READ) michael@0: FD_CLR(fd, sop->event_readset_in); michael@0: michael@0: if (events & EV_WRITE) michael@0: FD_CLR(fd, sop->event_writeset_in); michael@0: michael@0: check_selectop(sop); michael@0: return (0); michael@0: } michael@0: michael@0: static void michael@0: select_free_selectop(struct selectop *sop) michael@0: { michael@0: if (sop->event_readset_in) michael@0: mm_free(sop->event_readset_in); michael@0: if (sop->event_writeset_in) michael@0: mm_free(sop->event_writeset_in); michael@0: if (sop->event_readset_out) michael@0: mm_free(sop->event_readset_out); michael@0: if (sop->event_writeset_out) michael@0: mm_free(sop->event_writeset_out); michael@0: michael@0: memset(sop, 0, sizeof(struct selectop)); michael@0: mm_free(sop); michael@0: } michael@0: michael@0: static void michael@0: select_dealloc(struct event_base *base) michael@0: { michael@0: evsig_dealloc(base); michael@0: michael@0: select_free_selectop(base->evbase); michael@0: }