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