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 | /* |
michael@0 | 2 | * Copyright 2007-2012 Niels Provos and Nick Mathewson |
michael@0 | 3 | * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu> |
michael@0 | 4 | * Copyright 2003 Michael A. Davis <mike@datanerds.net> |
michael@0 | 5 | * |
michael@0 | 6 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | * modification, are permitted provided that the following conditions |
michael@0 | 8 | * are met: |
michael@0 | 9 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 12 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 13 | * documentation and/or other materials provided with the distribution. |
michael@0 | 14 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 15 | * derived from this software without specific prior written permission. |
michael@0 | 16 | * |
michael@0 | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 27 | */ |
michael@0 | 28 | |
michael@0 | 29 | #include <winsock2.h> |
michael@0 | 30 | #include <windows.h> |
michael@0 | 31 | #include <sys/types.h> |
michael@0 | 32 | #include <sys/queue.h> |
michael@0 | 33 | #include <limits.h> |
michael@0 | 34 | #include <signal.h> |
michael@0 | 35 | #include <stdio.h> |
michael@0 | 36 | #include <stdlib.h> |
michael@0 | 37 | #include <string.h> |
michael@0 | 38 | #include <errno.h> |
michael@0 | 39 | |
michael@0 | 40 | #include "event2/util.h" |
michael@0 | 41 | #include "event2/event-config.h" |
michael@0 | 42 | #include "util-internal.h" |
michael@0 | 43 | #include "log-internal.h" |
michael@0 | 44 | #include "event2/event.h" |
michael@0 | 45 | #include "event-internal.h" |
michael@0 | 46 | #include "evmap-internal.h" |
michael@0 | 47 | #include "event2/thread.h" |
michael@0 | 48 | #include "evthread-internal.h" |
michael@0 | 49 | |
michael@0 | 50 | #define XFREE(ptr) do { if (ptr) mm_free(ptr); } while (0) |
michael@0 | 51 | |
michael@0 | 52 | extern struct event_list timequeue; |
michael@0 | 53 | extern struct event_list addqueue; |
michael@0 | 54 | |
michael@0 | 55 | struct win_fd_set { |
michael@0 | 56 | u_int fd_count; |
michael@0 | 57 | SOCKET fd_array[1]; |
michael@0 | 58 | }; |
michael@0 | 59 | |
michael@0 | 60 | /* MSDN says this is required to handle SIGFPE */ |
michael@0 | 61 | volatile double SIGFPE_REQ = 0.0f; |
michael@0 | 62 | |
michael@0 | 63 | struct idx_info { |
michael@0 | 64 | int read_pos_plus1; |
michael@0 | 65 | int write_pos_plus1; |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | struct win32op { |
michael@0 | 69 | unsigned num_fds_in_fd_sets; |
michael@0 | 70 | int resize_out_sets; |
michael@0 | 71 | struct win_fd_set *readset_in; |
michael@0 | 72 | struct win_fd_set *writeset_in; |
michael@0 | 73 | struct win_fd_set *readset_out; |
michael@0 | 74 | struct win_fd_set *writeset_out; |
michael@0 | 75 | struct win_fd_set *exset_out; |
michael@0 | 76 | unsigned signals_are_broken : 1; |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | static void *win32_init(struct event_base *); |
michael@0 | 80 | static int win32_add(struct event_base *, evutil_socket_t, short old, short events, void *_idx); |
michael@0 | 81 | static int win32_del(struct event_base *, evutil_socket_t, short old, short events, void *_idx); |
michael@0 | 82 | static int win32_dispatch(struct event_base *base, struct timeval *); |
michael@0 | 83 | static void win32_dealloc(struct event_base *); |
michael@0 | 84 | |
michael@0 | 85 | struct eventop win32ops = { |
michael@0 | 86 | "win32", |
michael@0 | 87 | win32_init, |
michael@0 | 88 | win32_add, |
michael@0 | 89 | win32_del, |
michael@0 | 90 | win32_dispatch, |
michael@0 | 91 | win32_dealloc, |
michael@0 | 92 | 0, /* doesn't need reinit */ |
michael@0 | 93 | 0, /* No features supported. */ |
michael@0 | 94 | sizeof(struct idx_info), |
michael@0 | 95 | }; |
michael@0 | 96 | |
michael@0 | 97 | #define FD_SET_ALLOC_SIZE(n) ((sizeof(struct win_fd_set) + ((n)-1)*sizeof(SOCKET))) |
michael@0 | 98 | |
michael@0 | 99 | static int |
michael@0 | 100 | grow_fd_sets(struct win32op *op, unsigned new_num_fds) |
michael@0 | 101 | { |
michael@0 | 102 | size_t size; |
michael@0 | 103 | |
michael@0 | 104 | EVUTIL_ASSERT(new_num_fds >= op->readset_in->fd_count && |
michael@0 | 105 | new_num_fds >= op->writeset_in->fd_count); |
michael@0 | 106 | EVUTIL_ASSERT(new_num_fds >= 1); |
michael@0 | 107 | |
michael@0 | 108 | size = FD_SET_ALLOC_SIZE(new_num_fds); |
michael@0 | 109 | if (!(op->readset_in = mm_realloc(op->readset_in, size))) |
michael@0 | 110 | return (-1); |
michael@0 | 111 | if (!(op->writeset_in = mm_realloc(op->writeset_in, size))) |
michael@0 | 112 | return (-1); |
michael@0 | 113 | op->resize_out_sets = 1; |
michael@0 | 114 | op->num_fds_in_fd_sets = new_num_fds; |
michael@0 | 115 | return (0); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | static int |
michael@0 | 119 | do_fd_set(struct win32op *op, struct idx_info *ent, evutil_socket_t s, int read) |
michael@0 | 120 | { |
michael@0 | 121 | struct win_fd_set *set = read ? op->readset_in : op->writeset_in; |
michael@0 | 122 | if (read) { |
michael@0 | 123 | if (ent->read_pos_plus1 > 0) |
michael@0 | 124 | return (0); |
michael@0 | 125 | } else { |
michael@0 | 126 | if (ent->write_pos_plus1 > 0) |
michael@0 | 127 | return (0); |
michael@0 | 128 | } |
michael@0 | 129 | if (set->fd_count == op->num_fds_in_fd_sets) { |
michael@0 | 130 | if (grow_fd_sets(op, op->num_fds_in_fd_sets*2)) |
michael@0 | 131 | return (-1); |
michael@0 | 132 | /* set pointer will have changed and needs reiniting! */ |
michael@0 | 133 | set = read ? op->readset_in : op->writeset_in; |
michael@0 | 134 | } |
michael@0 | 135 | set->fd_array[set->fd_count] = s; |
michael@0 | 136 | if (read) |
michael@0 | 137 | ent->read_pos_plus1 = set->fd_count+1; |
michael@0 | 138 | else |
michael@0 | 139 | ent->write_pos_plus1 = set->fd_count+1; |
michael@0 | 140 | return (set->fd_count++); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | static int |
michael@0 | 144 | do_fd_clear(struct event_base *base, |
michael@0 | 145 | struct win32op *op, struct idx_info *ent, int read) |
michael@0 | 146 | { |
michael@0 | 147 | int i; |
michael@0 | 148 | struct win_fd_set *set = read ? op->readset_in : op->writeset_in; |
michael@0 | 149 | if (read) { |
michael@0 | 150 | i = ent->read_pos_plus1 - 1; |
michael@0 | 151 | ent->read_pos_plus1 = 0; |
michael@0 | 152 | } else { |
michael@0 | 153 | i = ent->write_pos_plus1 - 1; |
michael@0 | 154 | ent->write_pos_plus1 = 0; |
michael@0 | 155 | } |
michael@0 | 156 | if (i < 0) |
michael@0 | 157 | return (0); |
michael@0 | 158 | if (--set->fd_count != (unsigned)i) { |
michael@0 | 159 | struct idx_info *ent2; |
michael@0 | 160 | SOCKET s2; |
michael@0 | 161 | s2 = set->fd_array[i] = set->fd_array[set->fd_count]; |
michael@0 | 162 | |
michael@0 | 163 | ent2 = evmap_io_get_fdinfo(&base->io, s2); |
michael@0 | 164 | |
michael@0 | 165 | if (!ent2) /* This indicates a bug. */ |
michael@0 | 166 | return (0); |
michael@0 | 167 | if (read) |
michael@0 | 168 | ent2->read_pos_plus1 = i+1; |
michael@0 | 169 | else |
michael@0 | 170 | ent2->write_pos_plus1 = i+1; |
michael@0 | 171 | } |
michael@0 | 172 | return (0); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | #define NEVENT 32 |
michael@0 | 176 | void * |
michael@0 | 177 | win32_init(struct event_base *_base) |
michael@0 | 178 | { |
michael@0 | 179 | struct win32op *winop; |
michael@0 | 180 | size_t size; |
michael@0 | 181 | if (!(winop = mm_calloc(1, sizeof(struct win32op)))) |
michael@0 | 182 | return NULL; |
michael@0 | 183 | winop->num_fds_in_fd_sets = NEVENT; |
michael@0 | 184 | size = FD_SET_ALLOC_SIZE(NEVENT); |
michael@0 | 185 | if (!(winop->readset_in = mm_malloc(size))) |
michael@0 | 186 | goto err; |
michael@0 | 187 | if (!(winop->writeset_in = mm_malloc(size))) |
michael@0 | 188 | goto err; |
michael@0 | 189 | if (!(winop->readset_out = mm_malloc(size))) |
michael@0 | 190 | goto err; |
michael@0 | 191 | if (!(winop->writeset_out = mm_malloc(size))) |
michael@0 | 192 | goto err; |
michael@0 | 193 | if (!(winop->exset_out = mm_malloc(size))) |
michael@0 | 194 | goto err; |
michael@0 | 195 | winop->readset_in->fd_count = winop->writeset_in->fd_count = 0; |
michael@0 | 196 | winop->readset_out->fd_count = winop->writeset_out->fd_count |
michael@0 | 197 | = winop->exset_out->fd_count = 0; |
michael@0 | 198 | |
michael@0 | 199 | if (evsig_init(_base) < 0) |
michael@0 | 200 | winop->signals_are_broken = 1; |
michael@0 | 201 | |
michael@0 | 202 | return (winop); |
michael@0 | 203 | err: |
michael@0 | 204 | XFREE(winop->readset_in); |
michael@0 | 205 | XFREE(winop->writeset_in); |
michael@0 | 206 | XFREE(winop->readset_out); |
michael@0 | 207 | XFREE(winop->writeset_out); |
michael@0 | 208 | XFREE(winop->exset_out); |
michael@0 | 209 | XFREE(winop); |
michael@0 | 210 | return (NULL); |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | int |
michael@0 | 214 | win32_add(struct event_base *base, evutil_socket_t fd, |
michael@0 | 215 | short old, short events, void *_idx) |
michael@0 | 216 | { |
michael@0 | 217 | struct win32op *win32op = base->evbase; |
michael@0 | 218 | struct idx_info *idx = _idx; |
michael@0 | 219 | |
michael@0 | 220 | if ((events & EV_SIGNAL) && win32op->signals_are_broken) |
michael@0 | 221 | return (-1); |
michael@0 | 222 | |
michael@0 | 223 | if (!(events & (EV_READ|EV_WRITE))) |
michael@0 | 224 | return (0); |
michael@0 | 225 | |
michael@0 | 226 | event_debug(("%s: adding event for %d", __func__, (int)fd)); |
michael@0 | 227 | if (events & EV_READ) { |
michael@0 | 228 | if (do_fd_set(win32op, idx, fd, 1)<0) |
michael@0 | 229 | return (-1); |
michael@0 | 230 | } |
michael@0 | 231 | if (events & EV_WRITE) { |
michael@0 | 232 | if (do_fd_set(win32op, idx, fd, 0)<0) |
michael@0 | 233 | return (-1); |
michael@0 | 234 | } |
michael@0 | 235 | return (0); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | int |
michael@0 | 239 | win32_del(struct event_base *base, evutil_socket_t fd, short old, short events, |
michael@0 | 240 | void *_idx) |
michael@0 | 241 | { |
michael@0 | 242 | struct win32op *win32op = base->evbase; |
michael@0 | 243 | struct idx_info *idx = _idx; |
michael@0 | 244 | |
michael@0 | 245 | event_debug(("%s: Removing event for "EV_SOCK_FMT, |
michael@0 | 246 | __func__, EV_SOCK_ARG(fd))); |
michael@0 | 247 | if (events & EV_READ) |
michael@0 | 248 | do_fd_clear(base, win32op, idx, 1); |
michael@0 | 249 | if (events & EV_WRITE) |
michael@0 | 250 | do_fd_clear(base, win32op, idx, 0); |
michael@0 | 251 | |
michael@0 | 252 | return 0; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | static void |
michael@0 | 256 | fd_set_copy(struct win_fd_set *out, const struct win_fd_set *in) |
michael@0 | 257 | { |
michael@0 | 258 | out->fd_count = in->fd_count; |
michael@0 | 259 | memcpy(out->fd_array, in->fd_array, in->fd_count * (sizeof(SOCKET))); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | /* |
michael@0 | 263 | static void dump_fd_set(struct win_fd_set *s) |
michael@0 | 264 | { |
michael@0 | 265 | unsigned int i; |
michael@0 | 266 | printf("[ "); |
michael@0 | 267 | for(i=0;i<s->fd_count;++i) |
michael@0 | 268 | printf("%d ",(int)s->fd_array[i]); |
michael@0 | 269 | printf("]\n"); |
michael@0 | 270 | } |
michael@0 | 271 | */ |
michael@0 | 272 | |
michael@0 | 273 | int |
michael@0 | 274 | win32_dispatch(struct event_base *base, struct timeval *tv) |
michael@0 | 275 | { |
michael@0 | 276 | struct win32op *win32op = base->evbase; |
michael@0 | 277 | int res = 0; |
michael@0 | 278 | unsigned j, i; |
michael@0 | 279 | int fd_count; |
michael@0 | 280 | SOCKET s; |
michael@0 | 281 | |
michael@0 | 282 | if (win32op->resize_out_sets) { |
michael@0 | 283 | size_t size = FD_SET_ALLOC_SIZE(win32op->num_fds_in_fd_sets); |
michael@0 | 284 | if (!(win32op->readset_out = mm_realloc(win32op->readset_out, size))) |
michael@0 | 285 | return (-1); |
michael@0 | 286 | if (!(win32op->exset_out = mm_realloc(win32op->exset_out, size))) |
michael@0 | 287 | return (-1); |
michael@0 | 288 | if (!(win32op->writeset_out = mm_realloc(win32op->writeset_out, size))) |
michael@0 | 289 | return (-1); |
michael@0 | 290 | win32op->resize_out_sets = 0; |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | fd_set_copy(win32op->readset_out, win32op->readset_in); |
michael@0 | 294 | fd_set_copy(win32op->exset_out, win32op->writeset_in); |
michael@0 | 295 | fd_set_copy(win32op->writeset_out, win32op->writeset_in); |
michael@0 | 296 | |
michael@0 | 297 | fd_count = |
michael@0 | 298 | (win32op->readset_out->fd_count > win32op->writeset_out->fd_count) ? |
michael@0 | 299 | win32op->readset_out->fd_count : win32op->writeset_out->fd_count; |
michael@0 | 300 | |
michael@0 | 301 | if (!fd_count) { |
michael@0 | 302 | long msec = tv ? evutil_tv_to_msec(tv) : LONG_MAX; |
michael@0 | 303 | /* Sleep's DWORD argument is unsigned long */ |
michael@0 | 304 | if (msec < 0) |
michael@0 | 305 | msec = LONG_MAX; |
michael@0 | 306 | /* Windows doesn't like you to call select() with no sockets */ |
michael@0 | 307 | Sleep(msec); |
michael@0 | 308 | return (0); |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
michael@0 | 312 | |
michael@0 | 313 | res = select(fd_count, |
michael@0 | 314 | (struct fd_set*)win32op->readset_out, |
michael@0 | 315 | (struct fd_set*)win32op->writeset_out, |
michael@0 | 316 | (struct fd_set*)win32op->exset_out, tv); |
michael@0 | 317 | |
michael@0 | 318 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
michael@0 | 319 | |
michael@0 | 320 | event_debug(("%s: select returned %d", __func__, res)); |
michael@0 | 321 | |
michael@0 | 322 | if (res <= 0) { |
michael@0 | 323 | return res; |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | if (win32op->readset_out->fd_count) { |
michael@0 | 327 | i = rand() % win32op->readset_out->fd_count; |
michael@0 | 328 | for (j=0; j<win32op->readset_out->fd_count; ++j) { |
michael@0 | 329 | if (++i >= win32op->readset_out->fd_count) |
michael@0 | 330 | i = 0; |
michael@0 | 331 | s = win32op->readset_out->fd_array[i]; |
michael@0 | 332 | evmap_io_active(base, s, EV_READ); |
michael@0 | 333 | } |
michael@0 | 334 | } |
michael@0 | 335 | if (win32op->exset_out->fd_count) { |
michael@0 | 336 | i = rand() % win32op->exset_out->fd_count; |
michael@0 | 337 | for (j=0; j<win32op->exset_out->fd_count; ++j) { |
michael@0 | 338 | if (++i >= win32op->exset_out->fd_count) |
michael@0 | 339 | i = 0; |
michael@0 | 340 | s = win32op->exset_out->fd_array[i]; |
michael@0 | 341 | evmap_io_active(base, s, EV_WRITE); |
michael@0 | 342 | } |
michael@0 | 343 | } |
michael@0 | 344 | if (win32op->writeset_out->fd_count) { |
michael@0 | 345 | SOCKET s; |
michael@0 | 346 | i = rand() % win32op->writeset_out->fd_count; |
michael@0 | 347 | for (j=0; j<win32op->writeset_out->fd_count; ++j) { |
michael@0 | 348 | if (++i >= win32op->writeset_out->fd_count) |
michael@0 | 349 | i = 0; |
michael@0 | 350 | s = win32op->writeset_out->fd_array[i]; |
michael@0 | 351 | evmap_io_active(base, s, EV_WRITE); |
michael@0 | 352 | } |
michael@0 | 353 | } |
michael@0 | 354 | return (0); |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | void |
michael@0 | 358 | win32_dealloc(struct event_base *_base) |
michael@0 | 359 | { |
michael@0 | 360 | struct win32op *win32op = _base->evbase; |
michael@0 | 361 | |
michael@0 | 362 | evsig_dealloc(_base); |
michael@0 | 363 | if (win32op->readset_in) |
michael@0 | 364 | mm_free(win32op->readset_in); |
michael@0 | 365 | if (win32op->writeset_in) |
michael@0 | 366 | mm_free(win32op->writeset_in); |
michael@0 | 367 | if (win32op->readset_out) |
michael@0 | 368 | mm_free(win32op->readset_out); |
michael@0 | 369 | if (win32op->writeset_out) |
michael@0 | 370 | mm_free(win32op->writeset_out); |
michael@0 | 371 | if (win32op->exset_out) |
michael@0 | 372 | mm_free(win32op->exset_out); |
michael@0 | 373 | /* XXXXX free the tree. */ |
michael@0 | 374 | |
michael@0 | 375 | memset(win32op, 0, sizeof(win32op)); |
michael@0 | 376 | mm_free(win32op); |
michael@0 | 377 | } |