1 /* $NetBSD: poll.c,v 1.1.1.1 2009/11/02 10:01:02 plunky Exp $ */ 2 /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ 3 4 /* 5 * Copyright 2000-2003 Niels Provos <provos@citi.umich.edu> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #ifdef HAVE_CONFIG_H 31 #include "config.h" 32 #endif 33 34 #include <sys/types.h> 35 #ifdef HAVE_SYS_TIME_H 36 #include <sys/time.h> 37 #else 38 #include <sys/_time.h> 39 #endif 40 #include <sys/queue.h> 41 #include <poll.h> 42 #include <signal.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <errno.h> 48 #ifdef CHECK_INVARIANTS 49 #include <assert.h> 50 #endif 51 52 #include "event.h" 53 #include "event-internal.h" 54 #include "evsignal.h" 55 #include "log.h" 56 57 struct pollop { 58 int event_count; /* Highest number alloc */ 59 int nfds; /* Size of event_* */ 60 int fd_count; /* Size of idxplus1_by_fd */ 61 struct pollfd *event_set; 62 struct event **event_r_back; 63 struct event **event_w_back; 64 int *idxplus1_by_fd; /* Index into event_set by fd; we add 1 so 65 * that 0 (which is easy to memset) can mean 66 * "no entry." */ 67 }; 68 69 static void *poll_init (struct event_base *); 70 static int poll_add (void *, struct event *); 71 static int poll_del (void *, struct event *); 72 static int poll_dispatch (struct event_base *, void *, struct timeval *); 73 static void poll_dealloc (struct event_base *, void *); 74 75 const struct eventop pollops = { 76 "poll", 77 poll_init, 78 poll_add, 79 poll_del, 80 poll_dispatch, 81 poll_dealloc, 82 0 83 }; 84 85 static void * 86 poll_init(struct event_base *base) 87 { 88 struct pollop *pollop; 89 90 /* Disable poll when this environment variable is set */ 91 if (getenv("EVENT_NOPOLL")) 92 return (NULL); 93 94 if (!(pollop = calloc(1, sizeof(struct pollop)))) 95 return (NULL); 96 97 evsignal_init(base); 98 99 return (pollop); 100 } 101 102 #ifdef CHECK_INVARIANTS 103 static void 104 poll_check_ok(struct pollop *pop) 105 { 106 int i, idx; 107 struct event *ev; 108 109 for (i = 0; i < pop->fd_count; ++i) { 110 idx = pop->idxplus1_by_fd[i]-1; 111 if (idx < 0) 112 continue; 113 assert(pop->event_set[idx].fd == i); 114 if (pop->event_set[idx].events & POLLIN) { 115 ev = pop->event_r_back[idx]; 116 assert(ev); 117 assert(ev->ev_events & EV_READ); 118 assert(ev->ev_fd == i); 119 } 120 if (pop->event_set[idx].events & POLLOUT) { 121 ev = pop->event_w_back[idx]; 122 assert(ev); 123 assert(ev->ev_events & EV_WRITE); 124 assert(ev->ev_fd == i); 125 } 126 } 127 for (i = 0; i < pop->nfds; ++i) { 128 struct pollfd *pfd = &pop->event_set[i]; 129 assert(pop->idxplus1_by_fd[pfd->fd] == i+1); 130 } 131 } 132 #else 133 #define poll_check_ok(pop) 134 #endif 135 136 static int 137 poll_dispatch(struct event_base *base, void *arg, struct timeval *tv) 138 { 139 int res, i, j, msec = -1, nfds; 140 struct pollop *pop = arg; 141 142 poll_check_ok(pop); 143 144 if (tv != NULL) 145 msec = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; 146 147 nfds = pop->nfds; 148 res = poll(pop->event_set, nfds, msec); 149 150 if (res == -1) { 151 if (errno != EINTR) { 152 event_warn("poll"); 153 return (-1); 154 } 155 156 evsignal_process(base); 157 return (0); 158 } else if (base->sig.evsignal_caught) { 159 evsignal_process(base); 160 } 161 162 event_debug(("%s: poll reports %d", __func__, res)); 163 164 if (res == 0 || nfds == 0) 165 return (0); 166 167 i = random() % nfds; 168 for (j = 0; j < nfds; j++) { 169 struct event *r_ev = NULL, *w_ev = NULL; 170 int what; 171 if (++i == nfds) 172 i = 0; 173 what = pop->event_set[i].revents; 174 175 if (!what) 176 continue; 177 178 res = 0; 179 180 /* If the file gets closed notify */ 181 if (what & (POLLHUP|POLLERR)) 182 what |= POLLIN|POLLOUT; 183 if (what & POLLIN) { 184 res |= EV_READ; 185 r_ev = pop->event_r_back[i]; 186 } 187 if (what & POLLOUT) { 188 res |= EV_WRITE; 189 w_ev = pop->event_w_back[i]; 190 } 191 if (res == 0) 192 continue; 193 194 if (r_ev && (res & r_ev->ev_events)) { 195 event_active(r_ev, res & r_ev->ev_events, 1); 196 } 197 if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) { 198 event_active(w_ev, res & w_ev->ev_events, 1); 199 } 200 } 201 202 return (0); 203 } 204 205 static int 206 poll_add(void *arg, struct event *ev) 207 { 208 struct pollop *pop = arg; 209 struct pollfd *pfd = NULL; 210 int i; 211 212 if (ev->ev_events & EV_SIGNAL) 213 return (evsignal_add(ev)); 214 if (!(ev->ev_events & (EV_READ|EV_WRITE))) 215 return (0); 216 217 poll_check_ok(pop); 218 if (pop->nfds + 1 >= pop->event_count) { 219 struct pollfd *tmp_event_set; 220 struct event **tmp_event_r_back; 221 struct event **tmp_event_w_back; 222 int tmp_event_count; 223 224 if (pop->event_count < 32) 225 tmp_event_count = 32; 226 else 227 tmp_event_count = pop->event_count * 2; 228 229 /* We need more file descriptors */ 230 tmp_event_set = realloc(pop->event_set, 231 tmp_event_count * sizeof(struct pollfd)); 232 if (tmp_event_set == NULL) { 233 event_warn("realloc"); 234 return (-1); 235 } 236 pop->event_set = tmp_event_set; 237 238 tmp_event_r_back = realloc(pop->event_r_back, 239 tmp_event_count * sizeof(struct event *)); 240 if (tmp_event_r_back == NULL) { 241 /* event_set overallocated; that's okay. */ 242 event_warn("realloc"); 243 return (-1); 244 } 245 pop->event_r_back = tmp_event_r_back; 246 247 tmp_event_w_back = realloc(pop->event_w_back, 248 tmp_event_count * sizeof(struct event *)); 249 if (tmp_event_w_back == NULL) { 250 /* event_set and event_r_back overallocated; that's 251 * okay. */ 252 event_warn("realloc"); 253 return (-1); 254 } 255 pop->event_w_back = tmp_event_w_back; 256 257 pop->event_count = tmp_event_count; 258 } 259 if (ev->ev_fd >= pop->fd_count) { 260 int *tmp_idxplus1_by_fd; 261 int new_count; 262 if (pop->fd_count < 32) 263 new_count = 32; 264 else 265 new_count = pop->fd_count * 2; 266 while (new_count <= ev->ev_fd) 267 new_count *= 2; 268 tmp_idxplus1_by_fd = 269 realloc(pop->idxplus1_by_fd, new_count * sizeof(int)); 270 if (tmp_idxplus1_by_fd == NULL) { 271 event_warn("realloc"); 272 return (-1); 273 } 274 pop->idxplus1_by_fd = tmp_idxplus1_by_fd; 275 memset(pop->idxplus1_by_fd + pop->fd_count, 276 0, sizeof(int)*(new_count - pop->fd_count)); 277 pop->fd_count = new_count; 278 } 279 280 i = pop->idxplus1_by_fd[ev->ev_fd] - 1; 281 if (i >= 0) { 282 pfd = &pop->event_set[i]; 283 } else { 284 i = pop->nfds++; 285 pfd = &pop->event_set[i]; 286 pfd->events = 0; 287 pfd->fd = ev->ev_fd; 288 pop->event_w_back[i] = pop->event_r_back[i] = NULL; 289 pop->idxplus1_by_fd[ev->ev_fd] = i + 1; 290 } 291 292 pfd->revents = 0; 293 if (ev->ev_events & EV_WRITE) { 294 pfd->events |= POLLOUT; 295 pop->event_w_back[i] = ev; 296 } 297 if (ev->ev_events & EV_READ) { 298 pfd->events |= POLLIN; 299 pop->event_r_back[i] = ev; 300 } 301 poll_check_ok(pop); 302 303 return (0); 304 } 305 306 /* 307 * Nothing to be done here. 308 */ 309 310 static int 311 poll_del(void *arg, struct event *ev) 312 { 313 struct pollop *pop = arg; 314 struct pollfd *pfd = NULL; 315 int i; 316 317 if (ev->ev_events & EV_SIGNAL) 318 return (evsignal_del(ev)); 319 320 if (!(ev->ev_events & (EV_READ|EV_WRITE))) 321 return (0); 322 323 poll_check_ok(pop); 324 i = pop->idxplus1_by_fd[ev->ev_fd] - 1; 325 if (i < 0) 326 return (-1); 327 328 /* Do we still want to read or write? */ 329 pfd = &pop->event_set[i]; 330 if (ev->ev_events & EV_READ) { 331 pfd->events &= ~POLLIN; 332 pop->event_r_back[i] = NULL; 333 } 334 if (ev->ev_events & EV_WRITE) { 335 pfd->events &= ~POLLOUT; 336 pop->event_w_back[i] = NULL; 337 } 338 poll_check_ok(pop); 339 if (pfd->events) 340 /* Another event cares about that fd. */ 341 return (0); 342 343 /* Okay, so we aren't interested in that fd anymore. */ 344 pop->idxplus1_by_fd[ev->ev_fd] = 0; 345 346 --pop->nfds; 347 if (i != pop->nfds) { 348 /* 349 * Shift the last pollfd down into the now-unoccupied 350 * position. 351 */ 352 memcpy(&pop->event_set[i], &pop->event_set[pop->nfds], 353 sizeof(struct pollfd)); 354 pop->event_r_back[i] = pop->event_r_back[pop->nfds]; 355 pop->event_w_back[i] = pop->event_w_back[pop->nfds]; 356 pop->idxplus1_by_fd[pop->event_set[i].fd] = i + 1; 357 } 358 359 poll_check_ok(pop); 360 return (0); 361 } 362 363 static void 364 poll_dealloc(struct event_base *base, void *arg) 365 { 366 struct pollop *pop = arg; 367 368 evsignal_dealloc(base); 369 if (pop->event_set) 370 free(pop->event_set); 371 if (pop->event_r_back) 372 free(pop->event_r_back); 373 if (pop->event_w_back) 374 free(pop->event_w_back); 375 if (pop->idxplus1_by_fd) 376 free(pop->idxplus1_by_fd); 377 378 memset(pop, 0, sizeof(struct pollop)); 379 free(pop); 380 } 381