1 /* $OpenBSD: select.c,v 1.25 2016/09/03 11:31:17 nayden Exp $ */ 2 3 /* 4 * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/types.h> 31 #include <sys/time.h> 32 #include <sys/select.h> 33 #include <sys/queue.h> 34 35 #include <signal.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <errno.h> 41 #ifdef CHECK_INVARIANTS 42 #include <assert.h> 43 #endif 44 45 #include "event.h" 46 #include "event-internal.h" 47 #include "evsignal.h" 48 #include "log.h" 49 50 struct selectop { 51 int event_fds; /* Highest fd in fd set */ 52 size_t event_fdsz; 53 fd_set *event_readset_in; 54 fd_set *event_writeset_in; 55 fd_set *event_readset_out; 56 fd_set *event_writeset_out; 57 struct event **event_r_by_fd; 58 struct event **event_w_by_fd; 59 }; 60 61 static void *select_init (struct event_base *); 62 static int select_add (void *, struct event *); 63 static int select_del (void *, struct event *); 64 static int select_dispatch (struct event_base *, void *, struct timeval *); 65 static void select_dealloc (struct event_base *, void *); 66 67 const struct eventop selectops = { 68 "select", 69 select_init, 70 select_add, 71 select_del, 72 select_dispatch, 73 select_dealloc, 74 0 75 }; 76 77 static int select_resize(struct selectop *sop, size_t fdsz); 78 79 static void * 80 select_init(struct event_base *base) 81 { 82 struct selectop *sop; 83 84 /* Disable select when this environment variable is set */ 85 if (!issetugid() && getenv("EVENT_NOSELECT")) 86 return (NULL); 87 88 if (!(sop = calloc(1, sizeof(struct selectop)))) 89 return (NULL); 90 91 select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask)); 92 93 evsignal_init(base); 94 95 return (sop); 96 } 97 98 #ifdef CHECK_INVARIANTS 99 static void 100 check_selectop(struct selectop *sop) 101 { 102 int i; 103 for (i = 0; i <= sop->event_fds; ++i) { 104 if (FD_ISSET(i, sop->event_readset_in)) { 105 assert(sop->event_r_by_fd[i]); 106 assert(sop->event_r_by_fd[i]->ev_events & EV_READ); 107 assert(sop->event_r_by_fd[i]->ev_fd == i); 108 } else { 109 assert(! sop->event_r_by_fd[i]); 110 } 111 if (FD_ISSET(i, sop->event_writeset_in)) { 112 assert(sop->event_w_by_fd[i]); 113 assert(sop->event_w_by_fd[i]->ev_events & EV_WRITE); 114 assert(sop->event_w_by_fd[i]->ev_fd == i); 115 } else { 116 assert(! sop->event_w_by_fd[i]); 117 } 118 } 119 120 } 121 #else 122 #define check_selectop(sop) do { (void) sop; } while (0) 123 #endif 124 125 static int 126 select_dispatch(struct event_base *base, void *arg, struct timeval *tv) 127 { 128 int res, i, j; 129 struct selectop *sop = arg; 130 131 check_selectop(sop); 132 133 memcpy(sop->event_readset_out, sop->event_readset_in, 134 sop->event_fdsz); 135 memcpy(sop->event_writeset_out, sop->event_writeset_in, 136 sop->event_fdsz); 137 138 res = select(sop->event_fds + 1, sop->event_readset_out, 139 sop->event_writeset_out, NULL, tv); 140 141 check_selectop(sop); 142 143 if (res == -1) { 144 if (errno != EINTR) { 145 event_warn("select"); 146 return (-1); 147 } 148 149 evsignal_process(base); 150 return (0); 151 } else if (base->sig.evsignal_caught) { 152 evsignal_process(base); 153 } 154 155 event_debug(("%s: select reports %d", __func__, res)); 156 157 check_selectop(sop); 158 i = arc4random_uniform(sop->event_fds + 1); 159 for (j = 0; j <= sop->event_fds; ++j) { 160 struct event *r_ev = NULL, *w_ev = NULL; 161 if (++i >= sop->event_fds+1) 162 i = 0; 163 164 res = 0; 165 if (FD_ISSET(i, sop->event_readset_out)) { 166 r_ev = sop->event_r_by_fd[i]; 167 res |= EV_READ; 168 } 169 if (FD_ISSET(i, sop->event_writeset_out)) { 170 w_ev = sop->event_w_by_fd[i]; 171 res |= EV_WRITE; 172 } 173 if (r_ev && (res & r_ev->ev_events)) { 174 event_active(r_ev, res & r_ev->ev_events, 1); 175 } 176 if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) { 177 event_active(w_ev, res & w_ev->ev_events, 1); 178 } 179 } 180 check_selectop(sop); 181 182 return (0); 183 } 184 185 186 static int 187 select_resize(struct selectop *sop, size_t fdsz) 188 { 189 size_t n_events, n_events_old; 190 191 fd_set *readset_in = NULL; 192 fd_set *writeset_in = NULL; 193 fd_set *readset_out = NULL; 194 fd_set *writeset_out = NULL; 195 struct event **r_by_fd = NULL; 196 struct event **w_by_fd = NULL; 197 198 n_events = (fdsz/sizeof(fd_mask)) * NFDBITS; 199 n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS; 200 201 if (sop->event_readset_in) 202 check_selectop(sop); 203 204 if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL) 205 goto error; 206 sop->event_readset_in = readset_in; 207 if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL) 208 goto error; 209 sop->event_readset_out = readset_out; 210 if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL) 211 goto error; 212 sop->event_writeset_in = writeset_in; 213 if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL) 214 goto error; 215 sop->event_writeset_out = writeset_out; 216 if ((r_by_fd = reallocarray(sop->event_r_by_fd, n_events, 217 sizeof(struct event *))) == NULL) 218 goto error; 219 sop->event_r_by_fd = r_by_fd; 220 if ((w_by_fd = reallocarray(sop->event_w_by_fd, n_events, 221 sizeof(struct event *))) == NULL) 222 goto error; 223 sop->event_w_by_fd = w_by_fd; 224 225 memset((char *)sop->event_readset_in + sop->event_fdsz, 0, 226 fdsz - sop->event_fdsz); 227 memset((char *)sop->event_writeset_in + sop->event_fdsz, 0, 228 fdsz - sop->event_fdsz); 229 memset(sop->event_r_by_fd + n_events_old, 0, 230 (n_events-n_events_old) * sizeof(struct event*)); 231 memset(sop->event_w_by_fd + n_events_old, 0, 232 (n_events-n_events_old) * sizeof(struct event*)); 233 234 sop->event_fdsz = fdsz; 235 check_selectop(sop); 236 237 return (0); 238 239 error: 240 event_warn("malloc"); 241 return (-1); 242 } 243 244 245 static int 246 select_add(void *arg, struct event *ev) 247 { 248 struct selectop *sop = arg; 249 250 if (ev->ev_events & EV_SIGNAL) 251 return (evsignal_add(ev)); 252 253 check_selectop(sop); 254 /* 255 * Keep track of the highest fd, so that we can calculate the size 256 * of the fd_sets for select(2) 257 */ 258 if (sop->event_fds < ev->ev_fd) { 259 size_t fdsz = sop->event_fdsz; 260 261 if (fdsz < sizeof(fd_mask)) 262 fdsz = sizeof(fd_mask); 263 264 while (fdsz < 265 (howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask))) 266 fdsz *= 2; 267 268 if (fdsz != sop->event_fdsz) { 269 if (select_resize(sop, fdsz)) { 270 check_selectop(sop); 271 return (-1); 272 } 273 } 274 275 sop->event_fds = ev->ev_fd; 276 } 277 278 if (ev->ev_events & EV_READ) { 279 FD_SET(ev->ev_fd, sop->event_readset_in); 280 sop->event_r_by_fd[ev->ev_fd] = ev; 281 } 282 if (ev->ev_events & EV_WRITE) { 283 FD_SET(ev->ev_fd, sop->event_writeset_in); 284 sop->event_w_by_fd[ev->ev_fd] = ev; 285 } 286 check_selectop(sop); 287 288 return (0); 289 } 290 291 /* 292 * Nothing to be done here. 293 */ 294 295 static int 296 select_del(void *arg, struct event *ev) 297 { 298 struct selectop *sop = arg; 299 300 check_selectop(sop); 301 if (ev->ev_events & EV_SIGNAL) 302 return (evsignal_del(ev)); 303 304 if (sop->event_fds < ev->ev_fd) { 305 check_selectop(sop); 306 return (0); 307 } 308 309 if (ev->ev_events & EV_READ) { 310 FD_CLR(ev->ev_fd, sop->event_readset_in); 311 sop->event_r_by_fd[ev->ev_fd] = NULL; 312 } 313 314 if (ev->ev_events & EV_WRITE) { 315 FD_CLR(ev->ev_fd, sop->event_writeset_in); 316 sop->event_w_by_fd[ev->ev_fd] = NULL; 317 } 318 319 check_selectop(sop); 320 return (0); 321 } 322 323 static void 324 select_dealloc(struct event_base *base, void *arg) 325 { 326 struct selectop *sop = arg; 327 328 evsignal_dealloc(base); 329 free(sop->event_readset_in); 330 free(sop->event_writeset_in); 331 free(sop->event_readset_out); 332 free(sop->event_writeset_out); 333 free(sop->event_r_by_fd); 334 free(sop->event_w_by_fd); 335 336 memset(sop, 0, sizeof(struct selectop)); 337 free(sop); 338 } 339