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