1 1.5 rillig /* $NetBSD: select.c,v 1.5 2021/04/10 19:18:45 rillig Exp $ */ 2 1.1 plunky /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ 3 1.1 plunky 4 1.1 plunky /* 5 1.2 christos * Copyright 2000-2007 Niels Provos <provos (at) citi.umich.edu> 6 1.2 christos * Copyright 2007-2012 Niels Provos and Nick Mathewson 7 1.1 plunky * 8 1.1 plunky * Redistribution and use in source and binary forms, with or without 9 1.1 plunky * modification, are permitted provided that the following conditions 10 1.1 plunky * are met: 11 1.1 plunky * 1. Redistributions of source code must retain the above copyright 12 1.1 plunky * notice, this list of conditions and the following disclaimer. 13 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 plunky * notice, this list of conditions and the following disclaimer in the 15 1.1 plunky * documentation and/or other materials provided with the distribution. 16 1.1 plunky * 3. The name of the author may not be used to endorse or promote products 17 1.1 plunky * derived from this software without specific prior written permission. 18 1.1 plunky * 19 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 1.1 plunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 1.1 plunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 1.1 plunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 1.1 plunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 1.1 plunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 1.1 plunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 1.1 plunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 1.1 plunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 1.1 plunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 1.1 plunky */ 30 1.2 christos #include "event2/event-config.h" 31 1.2 christos #include <sys/cdefs.h> 32 1.5 rillig __RCSID("$NetBSD: select.c,v 1.5 2021/04/10 19:18:45 rillig Exp $"); 33 1.3 christos #include "evconfig-private.h" 34 1.3 christos 35 1.3 christos #ifdef EVENT__HAVE_SELECT 36 1.3 christos 37 1.3 christos #ifdef __APPLE__ 38 1.3 christos /* Apple wants us to define this if we might ever pass more than 39 1.3 christos * FD_SETSIZE bits to select(). */ 40 1.3 christos #define _DARWIN_UNLIMITED_SELECT 41 1.3 christos #endif 42 1.1 plunky 43 1.1 plunky #include <sys/types.h> 44 1.3 christos #ifdef EVENT__HAVE_SYS_TIME_H 45 1.1 plunky #include <sys/time.h> 46 1.1 plunky #endif 47 1.3 christos #ifdef EVENT__HAVE_SYS_SELECT_H 48 1.1 plunky #include <sys/select.h> 49 1.1 plunky #endif 50 1.1 plunky #include <sys/queue.h> 51 1.1 plunky #include <signal.h> 52 1.1 plunky #include <stdio.h> 53 1.1 plunky #include <stdlib.h> 54 1.1 plunky #include <string.h> 55 1.1 plunky #include <unistd.h> 56 1.1 plunky #include <errno.h> 57 1.1 plunky 58 1.1 plunky #include "event-internal.h" 59 1.2 christos #include "evsignal-internal.h" 60 1.2 christos #include "event2/thread.h" 61 1.2 christos #include "evthread-internal.h" 62 1.2 christos #include "log-internal.h" 63 1.2 christos #include "evmap-internal.h" 64 1.2 christos 65 1.3 christos #ifndef EVENT__HAVE_FD_MASK 66 1.2 christos /* This type is mandatory, but Android doesn't define it. */ 67 1.2 christos typedef unsigned long fd_mask; 68 1.2 christos #endif 69 1.1 plunky 70 1.2 christos #ifndef NFDBITS 71 1.2 christos #define NFDBITS (sizeof(fd_mask)*8) 72 1.1 plunky #endif 73 1.1 plunky 74 1.2 christos /* Divide positive x by y, rounding up. */ 75 1.2 christos #define DIV_ROUNDUP(x, y) (((x)+((y)-1))/(y)) 76 1.2 christos 77 1.2 christos /* How many bytes to allocate for N fds? */ 78 1.2 christos #define SELECT_ALLOC_SIZE(n) \ 79 1.2 christos (DIV_ROUNDUP(n, NFDBITS) * sizeof(fd_mask)) 80 1.2 christos 81 1.1 plunky struct selectop { 82 1.1 plunky int event_fds; /* Highest fd in fd set */ 83 1.1 plunky int event_fdsz; 84 1.2 christos int resize_out_sets; 85 1.1 plunky fd_set *event_readset_in; 86 1.1 plunky fd_set *event_writeset_in; 87 1.1 plunky fd_set *event_readset_out; 88 1.1 plunky fd_set *event_writeset_out; 89 1.1 plunky }; 90 1.1 plunky 91 1.2 christos static void *select_init(struct event_base *); 92 1.2 christos static int select_add(struct event_base *, int, short old, short events, void*); 93 1.2 christos static int select_del(struct event_base *, int, short old, short events, void*); 94 1.2 christos static int select_dispatch(struct event_base *, struct timeval *); 95 1.2 christos static void select_dealloc(struct event_base *); 96 1.1 plunky 97 1.1 plunky const struct eventop selectops = { 98 1.1 plunky "select", 99 1.1 plunky select_init, 100 1.1 plunky select_add, 101 1.1 plunky select_del, 102 1.1 plunky select_dispatch, 103 1.1 plunky select_dealloc, 104 1.4 christos 1, /* need_reinit. */ 105 1.2 christos EV_FEATURE_FDS, 106 1.2 christos 0, 107 1.1 plunky }; 108 1.1 plunky 109 1.1 plunky static int select_resize(struct selectop *sop, int fdsz); 110 1.2 christos static void select_free_selectop(struct selectop *sop); 111 1.1 plunky 112 1.1 plunky static void * 113 1.1 plunky select_init(struct event_base *base) 114 1.1 plunky { 115 1.1 plunky struct selectop *sop; 116 1.1 plunky 117 1.2 christos if (!(sop = mm_calloc(1, sizeof(struct selectop)))) 118 1.1 plunky return (NULL); 119 1.1 plunky 120 1.2 christos if (select_resize(sop, SELECT_ALLOC_SIZE(32 + 1))) { 121 1.2 christos select_free_selectop(sop); 122 1.1 plunky return (NULL); 123 1.2 christos } 124 1.1 plunky 125 1.3 christos evsig_init_(base); 126 1.3 christos 127 1.3 christos evutil_weakrand_seed_(&base->weakrand_seed, 0); 128 1.1 plunky 129 1.1 plunky return (sop); 130 1.1 plunky } 131 1.1 plunky 132 1.1 plunky #ifdef CHECK_INVARIANTS 133 1.1 plunky static void 134 1.1 plunky check_selectop(struct selectop *sop) 135 1.1 plunky { 136 1.2 christos /* nothing to be done here */ 137 1.1 plunky } 138 1.1 plunky #else 139 1.5 rillig #define check_selectop(sop) do { (void) sop; } while (0) 140 1.1 plunky #endif 141 1.1 plunky 142 1.1 plunky static int 143 1.2 christos select_dispatch(struct event_base *base, struct timeval *tv) 144 1.1 plunky { 145 1.2 christos int res=0, i, j, nfds; 146 1.2 christos struct selectop *sop = base->evbase; 147 1.1 plunky 148 1.1 plunky check_selectop(sop); 149 1.2 christos if (sop->resize_out_sets) { 150 1.2 christos fd_set *readset_out=NULL, *writeset_out=NULL; 151 1.2 christos size_t sz = sop->event_fdsz; 152 1.2 christos if (!(readset_out = mm_realloc(sop->event_readset_out, sz))) 153 1.2 christos return (-1); 154 1.2 christos sop->event_readset_out = readset_out; 155 1.2 christos if (!(writeset_out = mm_realloc(sop->event_writeset_out, sz))) { 156 1.2 christos /* We don't free readset_out here, since it was 157 1.2 christos * already successfully reallocated. The next time 158 1.2 christos * we call select_dispatch, the realloc will be a 159 1.2 christos * no-op. */ 160 1.2 christos return (-1); 161 1.2 christos } 162 1.2 christos sop->event_writeset_out = writeset_out; 163 1.2 christos sop->resize_out_sets = 0; 164 1.2 christos } 165 1.1 plunky 166 1.1 plunky memcpy(sop->event_readset_out, sop->event_readset_in, 167 1.1 plunky sop->event_fdsz); 168 1.1 plunky memcpy(sop->event_writeset_out, sop->event_writeset_in, 169 1.1 plunky sop->event_fdsz); 170 1.1 plunky 171 1.2 christos nfds = sop->event_fds+1; 172 1.2 christos 173 1.2 christos EVBASE_RELEASE_LOCK(base, th_base_lock); 174 1.2 christos 175 1.2 christos res = select(nfds, sop->event_readset_out, 176 1.1 plunky sop->event_writeset_out, NULL, tv); 177 1.1 plunky 178 1.2 christos EVBASE_ACQUIRE_LOCK(base, th_base_lock); 179 1.2 christos 180 1.1 plunky check_selectop(sop); 181 1.1 plunky 182 1.1 plunky if (res == -1) { 183 1.1 plunky if (errno != EINTR) { 184 1.1 plunky event_warn("select"); 185 1.1 plunky return (-1); 186 1.1 plunky } 187 1.1 plunky 188 1.1 plunky return (0); 189 1.1 plunky } 190 1.1 plunky 191 1.1 plunky event_debug(("%s: select reports %d", __func__, res)); 192 1.1 plunky 193 1.1 plunky check_selectop(sop); 194 1.3 christos i = evutil_weakrand_range_(&base->weakrand_seed, nfds); 195 1.2 christos for (j = 0; j < nfds; ++j) { 196 1.2 christos if (++i >= nfds) 197 1.1 plunky i = 0; 198 1.1 plunky res = 0; 199 1.2 christos if (FD_ISSET(i, sop->event_readset_out)) 200 1.1 plunky res |= EV_READ; 201 1.2 christos if (FD_ISSET(i, sop->event_writeset_out)) 202 1.1 plunky res |= EV_WRITE; 203 1.2 christos 204 1.2 christos if (res == 0) 205 1.2 christos continue; 206 1.2 christos 207 1.3 christos evmap_io_active_(base, i, res); 208 1.1 plunky } 209 1.1 plunky check_selectop(sop); 210 1.1 plunky 211 1.1 plunky return (0); 212 1.1 plunky } 213 1.1 plunky 214 1.1 plunky static int 215 1.1 plunky select_resize(struct selectop *sop, int fdsz) 216 1.1 plunky { 217 1.1 plunky fd_set *readset_in = NULL; 218 1.1 plunky fd_set *writeset_in = NULL; 219 1.1 plunky 220 1.1 plunky if (sop->event_readset_in) 221 1.1 plunky check_selectop(sop); 222 1.1 plunky 223 1.2 christos if ((readset_in = mm_realloc(sop->event_readset_in, fdsz)) == NULL) 224 1.1 plunky goto error; 225 1.1 plunky sop->event_readset_in = readset_in; 226 1.2 christos if ((writeset_in = mm_realloc(sop->event_writeset_in, fdsz)) == NULL) { 227 1.2 christos /* Note that this will leave event_readset_in expanded. 228 1.2 christos * That's okay; we wouldn't want to free it, since that would 229 1.2 christos * change the semantics of select_resize from "expand the 230 1.2 christos * readset_in and writeset_in, or return -1" to "expand the 231 1.2 christos * *set_in members, or trash them and return -1." 232 1.2 christos */ 233 1.1 plunky goto error; 234 1.2 christos } 235 1.1 plunky sop->event_writeset_in = writeset_in; 236 1.2 christos sop->resize_out_sets = 1; 237 1.1 plunky 238 1.1 plunky memset((char *)sop->event_readset_in + sop->event_fdsz, 0, 239 1.1 plunky fdsz - sop->event_fdsz); 240 1.1 plunky memset((char *)sop->event_writeset_in + sop->event_fdsz, 0, 241 1.1 plunky fdsz - sop->event_fdsz); 242 1.1 plunky 243 1.1 plunky sop->event_fdsz = fdsz; 244 1.1 plunky check_selectop(sop); 245 1.1 plunky 246 1.1 plunky return (0); 247 1.1 plunky 248 1.1 plunky error: 249 1.1 plunky event_warn("malloc"); 250 1.1 plunky return (-1); 251 1.1 plunky } 252 1.1 plunky 253 1.1 plunky 254 1.1 plunky static int 255 1.2 christos select_add(struct event_base *base, int fd, short old, short events, void *p) 256 1.1 plunky { 257 1.2 christos struct selectop *sop = base->evbase; 258 1.2 christos (void) p; 259 1.1 plunky 260 1.2 christos EVUTIL_ASSERT((events & EV_SIGNAL) == 0); 261 1.1 plunky check_selectop(sop); 262 1.1 plunky /* 263 1.1 plunky * Keep track of the highest fd, so that we can calculate the size 264 1.1 plunky * of the fd_sets for select(2) 265 1.1 plunky */ 266 1.2 christos if (sop->event_fds < fd) { 267 1.1 plunky int fdsz = sop->event_fdsz; 268 1.1 plunky 269 1.2 christos if (fdsz < (int)sizeof(fd_mask)) 270 1.2 christos fdsz = (int)sizeof(fd_mask); 271 1.1 plunky 272 1.2 christos /* In theory we should worry about overflow here. In 273 1.2 christos * reality, though, the highest fd on a unixy system will 274 1.2 christos * not overflow here. XXXX */ 275 1.2 christos while (fdsz < (int) SELECT_ALLOC_SIZE(fd + 1)) 276 1.1 plunky fdsz *= 2; 277 1.1 plunky 278 1.1 plunky if (fdsz != sop->event_fdsz) { 279 1.1 plunky if (select_resize(sop, fdsz)) { 280 1.1 plunky check_selectop(sop); 281 1.1 plunky return (-1); 282 1.1 plunky } 283 1.1 plunky } 284 1.1 plunky 285 1.2 christos sop->event_fds = fd; 286 1.1 plunky } 287 1.1 plunky 288 1.2 christos if (events & EV_READ) 289 1.2 christos FD_SET(fd, sop->event_readset_in); 290 1.2 christos if (events & EV_WRITE) 291 1.2 christos FD_SET(fd, sop->event_writeset_in); 292 1.1 plunky check_selectop(sop); 293 1.1 plunky 294 1.1 plunky return (0); 295 1.1 plunky } 296 1.1 plunky 297 1.1 plunky /* 298 1.1 plunky * Nothing to be done here. 299 1.1 plunky */ 300 1.1 plunky 301 1.1 plunky static int 302 1.2 christos select_del(struct event_base *base, int fd, short old, short events, void *p) 303 1.1 plunky { 304 1.2 christos struct selectop *sop = base->evbase; 305 1.2 christos (void)p; 306 1.1 plunky 307 1.2 christos EVUTIL_ASSERT((events & EV_SIGNAL) == 0); 308 1.1 plunky check_selectop(sop); 309 1.1 plunky 310 1.2 christos if (sop->event_fds < fd) { 311 1.1 plunky check_selectop(sop); 312 1.1 plunky return (0); 313 1.1 plunky } 314 1.1 plunky 315 1.2 christos if (events & EV_READ) 316 1.2 christos FD_CLR(fd, sop->event_readset_in); 317 1.1 plunky 318 1.2 christos if (events & EV_WRITE) 319 1.2 christos FD_CLR(fd, sop->event_writeset_in); 320 1.1 plunky 321 1.1 plunky check_selectop(sop); 322 1.1 plunky return (0); 323 1.1 plunky } 324 1.1 plunky 325 1.1 plunky static void 326 1.2 christos select_free_selectop(struct selectop *sop) 327 1.1 plunky { 328 1.1 plunky if (sop->event_readset_in) 329 1.2 christos mm_free(sop->event_readset_in); 330 1.1 plunky if (sop->event_writeset_in) 331 1.2 christos mm_free(sop->event_writeset_in); 332 1.1 plunky if (sop->event_readset_out) 333 1.2 christos mm_free(sop->event_readset_out); 334 1.1 plunky if (sop->event_writeset_out) 335 1.2 christos mm_free(sop->event_writeset_out); 336 1.1 plunky 337 1.1 plunky memset(sop, 0, sizeof(struct selectop)); 338 1.2 christos mm_free(sop); 339 1.2 christos } 340 1.2 christos 341 1.2 christos static void 342 1.2 christos select_dealloc(struct event_base *base) 343 1.2 christos { 344 1.3 christos evsig_dealloc_(base); 345 1.2 christos 346 1.2 christos select_free_selectop(base->evbase); 347 1.1 plunky } 348 1.3 christos 349 1.3 christos #endif /* EVENT__HAVE_SELECT */ 350