| 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 | #ifdef HAVE_SYS_SELECT_H |
| 39 | #include <sys/select.h> |
| 40 | #endif |
| 41 | #include <sys/queue.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 "evutil.h" |
| 54 | #include "event-internal.h" |
| 55 | #include "evsignal.h" |
| 56 | #include "log.h" |
| 57 |
| 58 | #ifndef howmany |
| 59 | #define howmany(x, y) (((x)+((y)-1))/(y)) |
| 60 | #endif |
| 61 |
| 62 | #ifndef _EVENT_HAVE_FD_MASK |
| 63 | /* This type is mandatory, but Android doesn't define it. */ |
| 64 | #undef NFDBITS |
| 65 | #define NFDBITS (sizeof(long)*8) |
| 66 | typedef unsigned long fd_mask; |
| 67 | #endif |
| 68 |
| 69 | struct selectop { |
| 70 | int event_fds; /* Highest fd in fd set */ |
| 71 | int event_fdsz; |
| 72 | fd_set *event_readset_in; |
| 73 | fd_set *event_writeset_in; |
| 74 | fd_set *event_readset_out; |
| 75 | fd_set *event_writeset_out; |
| 76 | struct event **event_r_by_fd; |
| 77 | struct event **event_w_by_fd; |
| 78 | }; |
| 79 |
| 80 | static void *select_init (struct event_base *); |
| 81 | static int select_add (void *, struct event *); |
| 82 | static int select_del (void *, struct event *); |
| 83 | static int select_dispatch (struct event_base *, void *, struct timeval *); |
| 84 | static void select_dealloc (struct event_base *, void *); |
| 85 |
| 86 | const struct eventop selectops = { |
| 87 | "select", |
| 88 | select_init, |
| 89 | select_add, |
| 90 | select_del, |
| 91 | select_dispatch, |
| 92 | select_dealloc, |
| 93 | 0 |
| 94 | }; |
| 95 |
| 96 | static int select_resize(struct selectop *sop, int fdsz); |
| 97 |
| 98 | static void * |
| 99 | select_init(struct event_base *base) |
| 100 | { |
| 101 | struct selectop *sop; |
| 102 |
| 103 | /* Disable select when this environment variable is set */ |
| 104 | if (evutil_getenv("EVENT_NOSELECT")) |
| 105 | return (NULL); |
| 106 |
| 107 | if (!(sop = calloc(1, sizeof(struct selectop)))) |
| 108 | return (NULL); |
| 109 |
| 110 | select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask)); |
| 111 |
| 112 | evsignal_init(base); |
| 113 |
| 114 | return (sop); |
| 115 | } |
| 116 |
| 117 | #ifdef CHECK_INVARIANTS |
| 118 | static void |
| 119 | check_selectop(struct selectop *sop) |
| 120 | { |
| 121 | int i; |
| 122 | for (i = 0; i <= sop->event_fds; ++i) { |
| 123 | if (FD_ISSET(i, sop->event_readset_in)) { |
| 124 | assert(sop->event_r_by_fd[i]); |
| 125 | assert(sop->event_r_by_fd[i]->ev_events & EV_READ); |
| 126 | assert(sop->event_r_by_fd[i]->ev_fd == i); |
| 127 | } else { |
| 128 | assert(! sop->event_r_by_fd[i]); |
| 129 | } |
| 130 | if (FD_ISSET(i, sop->event_writeset_in)) { |
| 131 | assert(sop->event_w_by_fd[i]); |
| 132 | assert(sop->event_w_by_fd[i]->ev_events & EV_WRITE); |
| 133 | assert(sop->event_w_by_fd[i]->ev_fd == i); |
| 134 | } else { |
| 135 | assert(! sop->event_w_by_fd[i]); |
| 136 | } |
| 137 | } |
| 138 |
| 139 | } |
| 140 | #else |
| 141 | #define check_selectop(sop) do { (void) sop; } while (0) |
| 142 | #endif |
| 143 |
| 144 | static int |
| 145 | select_dispatch(struct event_base *base, void *arg, struct timeval *tv) |
| 146 | { |
| 147 | int res, i, j; |
| 148 | struct selectop *sop = arg; |
| 149 |
| 150 | check_selectop(sop); |
| 151 |
| 152 | memcpy(sop->event_readset_out, sop->event_readset_in, |
| 153 | sop->event_fdsz); |
| 154 | memcpy(sop->event_writeset_out, sop->event_writeset_in, |
| 155 | sop->event_fdsz); |
| 156 |
| 157 | res = select(sop->event_fds + 1, sop->event_readset_out, |
| 158 | sop->event_writeset_out, NULL, tv); |
| 159 |
| 160 | check_selectop(sop); |
| 161 |
| 162 | if (res == -1) { |
| 163 | if (errno != EINTR) { |
| 164 | event_warn("select"); |
| 165 | return (-1); |
| 166 | } |
| 167 |
| 168 | evsignal_process(base); |
| 169 | return (0); |
| 170 | } else if (base->sig.evsignal_caught) { |
| 171 | evsignal_process(base); |
| 172 | } |
| 173 |
| 174 | event_debug(("%s: select reports %d", __func__, res)); |
| 175 |
| 176 | check_selectop(sop); |
| 177 | i = random() % (sop->event_fds+1); |
| 178 | for (j = 0; j <= sop->event_fds; ++j) { |
| 179 | struct event *r_ev = NULL, *w_ev = NULL; |
| 180 | if (++i >= sop->event_fds+1) |
| 181 | i = 0; |
| 182 |
| 183 | res = 0; |
| 184 | if (FD_ISSET(i, sop->event_readset_out)) { |
| 185 | r_ev = sop->event_r_by_fd[i]; |
| 186 | res |= EV_READ; |
| 187 | } |
| 188 | if (FD_ISSET(i, sop->event_writeset_out)) { |
| 189 | w_ev = sop->event_w_by_fd[i]; |
| 190 | res |= EV_WRITE; |
| 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 | check_selectop(sop); |
| 200 |
| 201 | return (0); |
| 202 | } |
| 203 |
| 204 |
| 205 | static int |
| 206 | select_resize(struct selectop *sop, int fdsz) |
| 207 | { |
| 208 | int n_events, n_events_old; |
| 209 |
| 210 | fd_set *readset_in = NULL; |
| 211 | fd_set *writeset_in = NULL; |
| 212 | fd_set *readset_out = NULL; |
| 213 | fd_set *writeset_out = NULL; |
| 214 | struct event **r_by_fd = NULL; |
| 215 | struct event **w_by_fd = NULL; |
| 216 |
| 217 | n_events = (fdsz/sizeof(fd_mask)) * NFDBITS; |
| 218 | n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS; |
| 219 |
| 220 | if (sop->event_readset_in) |
| 221 | check_selectop(sop); |
| 222 |
| 223 | if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL) |
| 224 | goto error; |
| 225 | sop->event_readset_in = readset_in; |
| 226 | if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL) |
| 227 | goto error; |
| 228 | sop->event_readset_out = readset_out; |
| 229 | if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL) |
| 230 | goto error; |
| 231 | sop->event_writeset_in = writeset_in; |
| 232 | if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL) |
| 233 | goto error; |
| 234 | sop->event_writeset_out = writeset_out; |
| 235 | if ((r_by_fd = realloc(sop->event_r_by_fd, |
| 236 | n_events*sizeof(struct event*))) == NULL) |
| 237 | goto error; |
| 238 | sop->event_r_by_fd = r_by_fd; |
| 239 | if ((w_by_fd = realloc(sop->event_w_by_fd, |
| 240 | n_events * sizeof(struct event*))) == NULL) |
| 241 | goto error; |
| 242 | sop->event_w_by_fd = w_by_fd; |
| 243 |
| 244 | memset((char *)sop->event_readset_in + sop->event_fdsz, 0, |
| 245 | fdsz - sop->event_fdsz); |
| 246 | memset((char *)sop->event_writeset_in + sop->event_fdsz, 0, |
| 247 | fdsz - sop->event_fdsz); |
| 248 | memset(sop->event_r_by_fd + n_events_old, 0, |
| 249 | (n_events-n_events_old) * sizeof(struct event*)); |
| 250 | memset(sop->event_w_by_fd + n_events_old, 0, |
| 251 | (n_events-n_events_old) * sizeof(struct event*)); |
| 252 |
| 253 | sop->event_fdsz = fdsz; |
| 254 | check_selectop(sop); |
| 255 |
| 256 | return (0); |
| 257 |
| 258 | error: |
| 259 | event_warn("malloc"); |
| 260 | return (-1); |
| 261 | } |
| 262 |
| 263 |
| 264 | static int |
| 265 | select_add(void *arg, struct event *ev) |
| 266 | { |
| 267 | struct selectop *sop = arg; |
| 268 |
| 269 | if (ev->ev_events & EV_SIGNAL) |
| 270 | return (evsignal_add(ev)); |
| 271 |
| 272 | check_selectop(sop); |
| 273 | /* |
| 274 | * Keep track of the highest fd, so that we can calculate the size |
| 275 | * of the fd_sets for select(2) |
| 276 | */ |
| 277 | if (sop->event_fds < ev->ev_fd) { |
| 278 | int fdsz = sop->event_fdsz; |
| 279 |
| 280 | if (fdsz < sizeof(fd_mask)) |
| 281 | fdsz = sizeof(fd_mask); |
| 282 |
| 283 | while (fdsz < |
| 284 | (howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask))) |
| 285 | fdsz *= 2; |
| 286 |
| 287 | if (fdsz != sop->event_fdsz) { |
| 288 | if (select_resize(sop, fdsz)) { |
| 289 | check_selectop(sop); |
| 290 | return (-1); |
| 291 | } |
| 292 | } |
| 293 |
| 294 | sop->event_fds = ev->ev_fd; |
| 295 | } |
| 296 |
| 297 | if (ev->ev_events & EV_READ) { |
| 298 | FD_SET(ev->ev_fd, sop->event_readset_in); |
| 299 | sop->event_r_by_fd[ev->ev_fd] = ev; |
| 300 | } |
| 301 | if (ev->ev_events & EV_WRITE) { |
| 302 | FD_SET(ev->ev_fd, sop->event_writeset_in); |
| 303 | sop->event_w_by_fd[ev->ev_fd] = ev; |
| 304 | } |
| 305 | check_selectop(sop); |
| 306 |
| 307 | return (0); |
| 308 | } |
| 309 |
| 310 | /* |
| 311 | * Nothing to be done here. |
| 312 | */ |
| 313 |
| 314 | static int |
| 315 | select_del(void *arg, struct event *ev) |
| 316 | { |
| 317 | struct selectop *sop = arg; |
| 318 |
| 319 | check_selectop(sop); |
| 320 | if (ev->ev_events & EV_SIGNAL) |
| 321 | return (evsignal_del(ev)); |
| 322 |
| 323 | if (sop->event_fds < ev->ev_fd) { |
| 324 | check_selectop(sop); |
| 325 | return (0); |
| 326 | } |
| 327 |
| 328 | if (ev->ev_events & EV_READ) { |
| 329 | FD_CLR(ev->ev_fd, sop->event_readset_in); |
| 330 | sop->event_r_by_fd[ev->ev_fd] = NULL; |
| 331 | } |
| 332 |
| 333 | if (ev->ev_events & EV_WRITE) { |
| 334 | FD_CLR(ev->ev_fd, sop->event_writeset_in); |
| 335 | sop->event_w_by_fd[ev->ev_fd] = NULL; |
| 336 | } |
| 337 |
| 338 | check_selectop(sop); |
| 339 | return (0); |
| 340 | } |
| 341 |
| 342 | static void |
| 343 | select_dealloc(struct event_base *base, void *arg) |
| 344 | { |
| 345 | struct selectop *sop = arg; |
| 346 |
| 347 | evsignal_dealloc(base); |
| 348 | if (sop->event_readset_in) |
| 349 | free(sop->event_readset_in); |
| 350 | if (sop->event_writeset_in) |
| 351 | free(sop->event_writeset_in); |
| 352 | if (sop->event_readset_out) |
| 353 | free(sop->event_readset_out); |
| 354 | if (sop->event_writeset_out) |
| 355 | free(sop->event_writeset_out); |
| 356 | if (sop->event_r_by_fd) |
| 357 | free(sop->event_r_by_fd); |
| 358 | if (sop->event_w_by_fd) |
| 359 | free(sop->event_w_by_fd); |
| 360 |
| 361 | memset(sop, 0, sizeof(struct selectop)); |
| 362 | free(sop); |
| 363 | } |
| 364 |