| 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 "config.h" |
| 31 |
| 32 | #include <sys/types.h> |
| 33 | #ifdef HAVE_SYS_TIME_H |
| 34 | #include <sys/time.h> |
| 35 | #else |
| 36 | #include <sys/_libevent_time.h> |
| 37 | #endif |
| 38 | #include <sys/queue.h> |
| 39 | #include <poll.h> |
| 40 | #include <signal.h> |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
| 44 | #include <unistd.h> |
| 45 | #include <errno.h> |
| 46 | #ifdef CHECK_INVARIANTS |
| 47 | #include <assert.h> |
| 48 | #endif |
| 49 |
| 50 | #include "event.h" |
| 51 | #include "event-internal.h" |
| 52 | #include "evsignal.h" |
| 53 | #include "log.h" |
| 54 |
| 55 | struct pollop { |
| 56 | int event_count; /* Highest number alloc */ |
| 57 | int nfds; /* Size of event_* */ |
| 58 | int fd_count; /* Size of idxplus1_by_fd */ |
| 59 | struct pollfd *event_set; |
| 60 | struct event **event_r_back; |
| 61 | struct event **event_w_back; |
| 62 | int *idxplus1_by_fd; /* Index into event_set by fd; we add 1 so |
| 63 | * that 0 (which is easy to memset) can mean |
| 64 | * "no entry." */ |
| 65 | }; |
| 66 |
| 67 | static void *poll_init (struct event_base *); |
| 68 | static int poll_add (void *, struct event *); |
| 69 | static int poll_del (void *, struct event *); |
| 70 | static int poll_dispatch (struct event_base *, void *, struct timeval *); |
| 71 | static void poll_dealloc (struct event_base *, void *); |
| 72 |
| 73 | const struct eventop pollops = { |
| 74 | "poll", |
| 75 | poll_init, |
| 76 | poll_add, |
| 77 | poll_del, |
| 78 | poll_dispatch, |
| 79 | poll_dealloc, |
| 80 | 0 |
| 81 | }; |
| 82 |
| 83 | static void * |
| 84 | poll_init(struct event_base *base) |
| 85 | { |
| 86 | struct pollop *pollop; |
| 87 |
| 88 | /* Disable poll when this environment variable is set */ |
| 89 | if (evutil_getenv("EVENT_NOPOLL")) |
| 90 | return (NULL); |
| 91 |
| 92 | if (!(pollop = calloc(1, sizeof(struct pollop)))) |
| 93 | return (NULL); |
| 94 |
| 95 | evsignal_init(base); |
| 96 |
| 97 | return (pollop); |
| 98 | } |
| 99 |
| 100 | #ifdef CHECK_INVARIANTS |
| 101 | static void |
| 102 | poll_check_ok(struct pollop *pop) |
| 103 | { |
| 104 | int i, idx; |
| 105 | struct event *ev; |
| 106 |
| 107 | for (i = 0; i < pop->fd_count; ++i) { |
| 108 | idx = pop->idxplus1_by_fd[i]-1; |
| 109 | if (idx < 0) |
| 110 | continue; |
| 111 | assert(pop->event_set[idx].fd == i); |
| 112 | if (pop->event_set[idx].events & POLLIN) { |
| 113 | ev = pop->event_r_back[idx]; |
| 114 | assert(ev); |
| 115 | assert(ev->ev_events & EV_READ); |
| 116 | assert(ev->ev_fd == i); |
| 117 | } |
| 118 | if (pop->event_set[idx].events & POLLOUT) { |
| 119 | ev = pop->event_w_back[idx]; |
| 120 | assert(ev); |
| 121 | assert(ev->ev_events & EV_WRITE); |
| 122 | assert(ev->ev_fd == i); |
| 123 | } |
| 124 | } |
| 125 | for (i = 0; i < pop->nfds; ++i) { |
| 126 | struct pollfd *pfd = &pop->event_set[i]; |
| 127 | assert(pop->idxplus1_by_fd[pfd->fd] == i+1); |
| 128 | } |
| 129 | } |
| 130 | #else |
| 131 | #define poll_check_ok(pop) |
| 132 | #endif |
| 133 |
| 134 | static int |
| 135 | poll_dispatch(struct event_base *base, void *arg, struct timeval *tv) |
| 136 | { |
| 137 | int res, i, j, msec = -1, nfds; |
| 138 | struct pollop *pop = arg; |
| 139 |
| 140 | poll_check_ok(pop); |
| 141 |
| 142 | if (tv != NULL) |
| 143 | msec = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; |
| 144 |
| 145 | nfds = pop->nfds; |
| 146 | res = poll(pop->event_set, nfds, msec); |
| 147 |
| 148 | if (res == -1) { |
| 149 | if (errno != EINTR) { |
| 150 | event_warn("poll"); |
| 151 | return (-1); |
| 152 | } |
| 153 |
| 154 | evsignal_process(base); |
| 155 | return (0); |
| 156 | } else if (base->sig.evsignal_caught) { |
| 157 | evsignal_process(base); |
| 158 | } |
| 159 |
| 160 | event_debug(("%s: poll reports %d", __func__, res)); |
| 161 |
| 162 | if (res == 0 || nfds == 0) |
| 163 | return (0); |
| 164 |
| 165 | i = random() % nfds; |
| 166 | for (j = 0; j < nfds; j++) { |
| 167 | struct event *r_ev = NULL, *w_ev = NULL; |
| 168 | int what; |
| 169 | if (++i == nfds) |
| 170 | i = 0; |
| 171 | what = pop->event_set[i].revents; |
| 172 |
| 173 | if (!what) |
| 174 | continue; |
| 175 |
| 176 | res = 0; |
| 177 |
| 178 | /* If the file gets closed notify */ |
| 179 | if (what & (POLLHUP|POLLERR)) |
| 180 | what |= POLLIN|POLLOUT; |
| 181 | if (what & POLLIN) { |
| 182 | res |= EV_READ; |
| 183 | r_ev = pop->event_r_back[i]; |
| 184 | } |
| 185 | if (what & POLLOUT) { |
| 186 | res |= EV_WRITE; |
| 187 | w_ev = pop->event_w_back[i]; |
| 188 | } |
| 189 | if (res == 0) |
| 190 | continue; |
| 191 |
| 192 | if (r_ev && (res & r_ev->ev_events)) { |
| 193 | event_active(r_ev, res & r_ev->ev_events, 1); |
| 194 | } |
| 195 | if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) { |
| 196 | event_active(w_ev, res & w_ev->ev_events, 1); |
| 197 | } |
| 198 | } |
| 199 |
| 200 | return (0); |
| 201 | } |
| 202 |
| 203 | static int |
| 204 | poll_add(void *arg, struct event *ev) |
| 205 | { |
| 206 | struct pollop *pop = arg; |
| 207 | struct pollfd *pfd = NULL; |
| 208 | int i; |
| 209 |
| 210 | if (ev->ev_events & EV_SIGNAL) |
| 211 | return (evsignal_add(ev)); |
| 212 | if (!(ev->ev_events & (EV_READ|EV_WRITE))) |
| 213 | return (0); |
| 214 |
| 215 | poll_check_ok(pop); |
| 216 | if (pop->nfds + 1 >= pop->event_count) { |
| 217 | struct pollfd *tmp_event_set; |
| 218 | struct event **tmp_event_r_back; |
| 219 | struct event **tmp_event_w_back; |
| 220 | int tmp_event_count; |
| 221 |
| 222 | if (pop->event_count < 32) |
| 223 | tmp_event_count = 32; |
| 224 | else |
| 225 | tmp_event_count = pop->event_count * 2; |
| 226 |
| 227 | /* We need more file descriptors */ |
| 228 | tmp_event_set = realloc(pop->event_set, |
| 229 | tmp_event_count * sizeof(struct pollfd)); |
| 230 | if (tmp_event_set == NULL) { |
| 231 | event_warn("realloc"); |
| 232 | return (-1); |
| 233 | } |
| 234 | pop->event_set = tmp_event_set; |
| 235 |
| 236 | tmp_event_r_back = realloc(pop->event_r_back, |
| 237 | tmp_event_count * sizeof(struct event *)); |
| 238 | if (tmp_event_r_back == NULL) { |
| 239 | /* event_set overallocated; that's okay. */ |
| 240 | event_warn("realloc"); |
| 241 | return (-1); |
| 242 | } |
| 243 | pop->event_r_back = tmp_event_r_back; |
| 244 |
| 245 | tmp_event_w_back = realloc(pop->event_w_back, |
| 246 | tmp_event_count * sizeof(struct event *)); |
| 247 | if (tmp_event_w_back == NULL) { |
| 248 | /* event_set and event_r_back overallocated; that's |
| 249 | * okay. */ |
| 250 | event_warn("realloc"); |
| 251 | return (-1); |
| 252 | } |
| 253 | pop->event_w_back = tmp_event_w_back; |
| 254 |
| 255 | pop->event_count = tmp_event_count; |
| 256 | } |
| 257 | if (ev->ev_fd >= pop->fd_count) { |
| 258 | int *tmp_idxplus1_by_fd; |
| 259 | int new_count; |
| 260 | if (pop->fd_count < 32) |
| 261 | new_count = 32; |
| 262 | else |
| 263 | new_count = pop->fd_count * 2; |
| 264 | while (new_count <= ev->ev_fd) |
| 265 | new_count *= 2; |
| 266 | tmp_idxplus1_by_fd = |
| 267 | realloc(pop->idxplus1_by_fd, new_count * sizeof(int)); |
| 268 | if (tmp_idxplus1_by_fd == NULL) { |
| 269 | event_warn("realloc"); |
| 270 | return (-1); |
| 271 | } |
| 272 | pop->idxplus1_by_fd = tmp_idxplus1_by_fd; |
| 273 | memset(pop->idxplus1_by_fd + pop->fd_count, |
| 274 | 0, sizeof(int)*(new_count - pop->fd_count)); |
| 275 | pop->fd_count = new_count; |
| 276 | } |
| 277 |
| 278 | i = pop->idxplus1_by_fd[ev->ev_fd] - 1; |
| 279 | if (i >= 0) { |
| 280 | pfd = &pop->event_set[i]; |
| 281 | } else { |
| 282 | i = pop->nfds++; |
| 283 | pfd = &pop->event_set[i]; |
| 284 | pfd->events = 0; |
| 285 | pfd->fd = ev->ev_fd; |
| 286 | pop->event_w_back[i] = pop->event_r_back[i] = NULL; |
| 287 | pop->idxplus1_by_fd[ev->ev_fd] = i + 1; |
| 288 | } |
| 289 |
| 290 | pfd->revents = 0; |
| 291 | if (ev->ev_events & EV_WRITE) { |
| 292 | pfd->events |= POLLOUT; |
| 293 | pop->event_w_back[i] = ev; |
| 294 | } |
| 295 | if (ev->ev_events & EV_READ) { |
| 296 | pfd->events |= POLLIN; |
| 297 | pop->event_r_back[i] = ev; |
| 298 | } |
| 299 | poll_check_ok(pop); |
| 300 |
| 301 | return (0); |
| 302 | } |
| 303 |
| 304 | /* |
| 305 | * Nothing to be done here. |
| 306 | */ |
| 307 |
| 308 | static int |
| 309 | poll_del(void *arg, struct event *ev) |
| 310 | { |
| 311 | struct pollop *pop = arg; |
| 312 | struct pollfd *pfd = NULL; |
| 313 | int i; |
| 314 |
| 315 | if (ev->ev_events & EV_SIGNAL) |
| 316 | return (evsignal_del(ev)); |
| 317 |
| 318 | if (!(ev->ev_events & (EV_READ|EV_WRITE))) |
| 319 | return (0); |
| 320 |
| 321 | poll_check_ok(pop); |
| 322 | i = pop->idxplus1_by_fd[ev->ev_fd] - 1; |
| 323 | if (i < 0) |
| 324 | return (-1); |
| 325 |
| 326 | /* Do we still want to read or write? */ |
| 327 | pfd = &pop->event_set[i]; |
| 328 | if (ev->ev_events & EV_READ) { |
| 329 | pfd->events &= ~POLLIN; |
| 330 | pop->event_r_back[i] = NULL; |
| 331 | } |
| 332 | if (ev->ev_events & EV_WRITE) { |
| 333 | pfd->events &= ~POLLOUT; |
| 334 | pop->event_w_back[i] = NULL; |
| 335 | } |
| 336 | poll_check_ok(pop); |
| 337 | if (pfd->events) |
| 338 | /* Another event cares about that fd. */ |
| 339 | return (0); |
| 340 |
| 341 | /* Okay, so we aren't interested in that fd anymore. */ |
| 342 | pop->idxplus1_by_fd[ev->ev_fd] = 0; |
| 343 |
| 344 | --pop->nfds; |
| 345 | if (i != pop->nfds) { |
| 346 | /* |
| 347 | * Shift the last pollfd down into the now-unoccupied |
| 348 | * position. |
| 349 | */ |
| 350 | memcpy(&pop->event_set[i], &pop->event_set[pop->nfds], |
| 351 | sizeof(struct pollfd)); |
| 352 | pop->event_r_back[i] = pop->event_r_back[pop->nfds]; |
| 353 | pop->event_w_back[i] = pop->event_w_back[pop->nfds]; |
| 354 | pop->idxplus1_by_fd[pop->event_set[i].fd] = i + 1; |
| 355 | } |
| 356 |
| 357 | poll_check_ok(pop); |
| 358 | return (0); |
| 359 | } |
| 360 |
| 361 | static void |
| 362 | poll_dealloc(struct event_base *base, void *arg) |
| 363 | { |
| 364 | struct pollop *pop = arg; |
| 365 |
| 366 | evsignal_dealloc(base); |
| 367 | if (pop->event_set) |
| 368 | free(pop->event_set); |
| 369 | if (pop->event_r_back) |
| 370 | free(pop->event_r_back); |
| 371 | if (pop->event_w_back) |
| 372 | free(pop->event_w_back); |
| 373 | if (pop->idxplus1_by_fd) |
| 374 | free(pop->idxplus1_by_fd); |
| 375 |
| 376 | memset(pop, 0, sizeof(struct pollop)); |
| 377 | free(pop); |
| 378 | } |
| 379 |