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