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