1 /* $NetBSD: select.c,v 1.2 2013/04/11 16:56:41 christos Exp $ */
2 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
5 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
6 * Copyright 2007-2012 Niels Provos and Nick Mathewson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
30 #include "event2/event-config.h"
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: select.c,v 1.2 2013/04/11 16:56:41 christos Exp $");
34 #include <sys/types.h>
35 #ifdef _EVENT_HAVE_SYS_TIME_H
38 #ifdef _EVENT_HAVE_SYS_SELECT_H
39 #include <sys/select.h>
41 #include <sys/queue.h>
49 #include "event-internal.h"
50 #include "evsignal-internal.h"
51 #include "event2/thread.h"
52 #include "evthread-internal.h"
53 #include "log-internal.h"
54 #include "evmap-internal.h"
56 #ifndef _EVENT_HAVE_FD_MASK
57 /* This type is mandatory, but Android doesn't define it. */
58 typedef unsigned long fd_mask
;
62 #define NFDBITS (sizeof(fd_mask)*8)
65 /* Divide positive x by y, rounding up. */
66 #define DIV_ROUNDUP(x, y) (((x)+((y)-1))/(y))
68 /* How many bytes to allocate for N fds? */
69 #define SELECT_ALLOC_SIZE(n) \
70 (DIV_ROUNDUP(n, NFDBITS) * sizeof(fd_mask))
73 int event_fds
; /* Highest fd in fd set */
76 fd_set
*event_readset_in
;
77 fd_set
*event_writeset_in
;
78 fd_set
*event_readset_out
;
79 fd_set
*event_writeset_out
;
82 static void *select_init(struct event_base
*);
83 static int select_add(struct event_base
*, int, short old
, short events
, void*);
84 static int select_del(struct event_base
*, int, short old
, short events
, void*);
85 static int select_dispatch(struct event_base
*, struct timeval
*);
86 static void select_dealloc(struct event_base
*);
88 const struct eventop selectops
= {
95 0, /* doesn't need reinit. */
100 static int select_resize(struct selectop
*sop
, int fdsz
);
101 static void select_free_selectop(struct selectop
*sop
);
104 select_init(struct event_base
*base
)
106 struct selectop
*sop
;
108 if (!(sop
= mm_calloc(1, sizeof(struct selectop
))))
111 if (select_resize(sop
, SELECT_ALLOC_SIZE(32 + 1))) {
112 select_free_selectop(sop
);
121 #ifdef CHECK_INVARIANTS
123 check_selectop(struct selectop
*sop
)
125 /* nothing to be done here */
128 #define check_selectop(sop) do { (void) sop; } while (/*CONSTCOND*/0)
132 select_dispatch(struct event_base
*base
, struct timeval
*tv
)
134 int res
=0, i
, j
, nfds
;
135 struct selectop
*sop
= base
->evbase
;
138 if (sop
->resize_out_sets
) {
139 fd_set
*readset_out
=NULL
, *writeset_out
=NULL
;
140 size_t sz
= sop
->event_fdsz
;
141 if (!(readset_out
= mm_realloc(sop
->event_readset_out
, sz
)))
143 sop
->event_readset_out
= readset_out
;
144 if (!(writeset_out
= mm_realloc(sop
->event_writeset_out
, sz
))) {
145 /* We don't free readset_out here, since it was
146 * already successfully reallocated. The next time
147 * we call select_dispatch, the realloc will be a
151 sop
->event_writeset_out
= writeset_out
;
152 sop
->resize_out_sets
= 0;
155 memcpy(sop
->event_readset_out
, sop
->event_readset_in
,
157 memcpy(sop
->event_writeset_out
, sop
->event_writeset_in
,
160 nfds
= sop
->event_fds
+1;
162 EVBASE_RELEASE_LOCK(base
, th_base_lock
);
164 res
= select(nfds
, sop
->event_readset_out
,
165 sop
->event_writeset_out
, NULL
, tv
);
167 EVBASE_ACQUIRE_LOCK(base
, th_base_lock
);
172 if (errno
!= EINTR
) {
173 event_warn("select");
180 event_debug(("%s: select reports %d", __func__
, res
));
184 for (j
= 0; j
< nfds
; ++j
) {
188 if (FD_ISSET(i
, sop
->event_readset_out
))
190 if (FD_ISSET(i
, sop
->event_writeset_out
))
196 evmap_io_active(base
, i
, res
);
204 select_resize(struct selectop
*sop
, int fdsz
)
206 fd_set
*readset_in
= NULL
;
207 fd_set
*writeset_in
= NULL
;
209 if (sop
->event_readset_in
)
212 if ((readset_in
= mm_realloc(sop
->event_readset_in
, fdsz
)) == NULL
)
214 sop
->event_readset_in
= readset_in
;
215 if ((writeset_in
= mm_realloc(sop
->event_writeset_in
, fdsz
)) == NULL
) {
216 /* Note that this will leave event_readset_in expanded.
217 * That's okay; we wouldn't want to free it, since that would
218 * change the semantics of select_resize from "expand the
219 * readset_in and writeset_in, or return -1" to "expand the
220 * *set_in members, or trash them and return -1."
224 sop
->event_writeset_in
= writeset_in
;
225 sop
->resize_out_sets
= 1;
227 memset((char *)sop
->event_readset_in
+ sop
->event_fdsz
, 0,
228 fdsz
- sop
->event_fdsz
);
229 memset((char *)sop
->event_writeset_in
+ sop
->event_fdsz
, 0,
230 fdsz
- sop
->event_fdsz
);
232 sop
->event_fdsz
= fdsz
;
238 event_warn("malloc");
244 select_add(struct event_base
*base
, int fd
, short old
, short events
, void *p
)
246 struct selectop
*sop
= base
->evbase
;
249 EVUTIL_ASSERT((events
& EV_SIGNAL
) == 0);
252 * Keep track of the highest fd, so that we can calculate the size
253 * of the fd_sets for select(2)
255 if (sop
->event_fds
< fd
) {
256 int fdsz
= sop
->event_fdsz
;
258 if (fdsz
< (int)sizeof(fd_mask
))
259 fdsz
= (int)sizeof(fd_mask
);
261 /* In theory we should worry about overflow here. In
262 * reality, though, the highest fd on a unixy system will
263 * not overflow here. XXXX */
264 while (fdsz
< (int) SELECT_ALLOC_SIZE(fd
+ 1))
267 if (fdsz
!= sop
->event_fdsz
) {
268 if (select_resize(sop
, fdsz
)) {
277 if (events
& EV_READ
)
278 FD_SET(fd
, sop
->event_readset_in
);
279 if (events
& EV_WRITE
)
280 FD_SET(fd
, sop
->event_writeset_in
);
287 * Nothing to be done here.
291 select_del(struct event_base
*base
, int fd
, short old
, short events
, void *p
)
293 struct selectop
*sop
= base
->evbase
;
296 EVUTIL_ASSERT((events
& EV_SIGNAL
) == 0);
299 if (sop
->event_fds
< fd
) {
304 if (events
& EV_READ
)
305 FD_CLR(fd
, sop
->event_readset_in
);
307 if (events
& EV_WRITE
)
308 FD_CLR(fd
, sop
->event_writeset_in
);
315 select_free_selectop(struct selectop
*sop
)
317 if (sop
->event_readset_in
)
318 mm_free(sop
->event_readset_in
);
319 if (sop
->event_writeset_in
)
320 mm_free(sop
->event_writeset_in
);
321 if (sop
->event_readset_out
)
322 mm_free(sop
->event_readset_out
);
323 if (sop
->event_writeset_out
)
324 mm_free(sop
->event_writeset_out
);
326 memset(sop
, 0, sizeof(struct selectop
));
331 select_dealloc(struct event_base
*base
)
335 select_free_selectop(base
->evbase
);