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