1 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
2
3 /*
4 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
5 * Copyright 2007-2012 Niels Provos and Nick Mathewson
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
31 #include <sys/types.h>
32 #ifdef _EVENT_HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
35 #ifdef _EVENT_HAVE_SYS_SELECT_H
36 #include <sys/select.h>
37 #endif
38 #include <sys/queue.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <errno.h>
45
46 #include "event-internal.h"
47 #include "evsignal-internal.h"
48 #include "event2/thread.h"
49 #include "evthread-internal.h"
50 #include "log-internal.h"
51 #include "evmap-internal.h"
52
53 #ifndef _EVENT_HAVE_FD_MASK
54 /* This type is mandatory, but Android doesn't define it. */
55 typedef unsigned long fd_mask;
56 #endif
57
58 #ifndef NFDBITS
59 #define NFDBITS (sizeof(fd_mask)*8)
60 #endif
61
62 /* Divide positive x by y, rounding up. */
63 #define DIV_ROUNDUP(x, y) (((x)+((y)-1))/(y))
64
65 /* How many bytes to allocate for N fds? */
66 #define SELECT_ALLOC_SIZE(n) \
67 (DIV_ROUNDUP(n, NFDBITS) * sizeof(fd_mask))
68
69 struct selectop {
70 int event_fds; /* Highest fd in fd set */
71 int event_fdsz;
72 int resize_out_sets;
73 fd_set *event_readset_in;
74 fd_set *event_writeset_in;
75 fd_set *event_readset_out;
76 fd_set *event_writeset_out;
77 };
78
79 static void *select_init(struct event_base *);
80 static int select_add(struct event_base *, int, short old, short events, void*);
81 static int select_del(struct event_base *, int, short old, short events, void*);
82 static int select_dispatch(struct event_base *, struct timeval *);
83 static void select_dealloc(struct event_base *);
84
85 const struct eventop selectops = {
86 "select",
87 select_init,
88 select_add,
89 select_del,
90 select_dispatch,
91 select_dealloc,
92 0, /* doesn't need reinit. */
93 EV_FEATURE_FDS,
94 0,
95 };
96
97 static int select_resize(struct selectop *sop, int fdsz);
98 static void select_free_selectop(struct selectop *sop);
99
100 static void *
select_init(struct event_base * base)101 select_init(struct event_base *base)
102 {
103 struct selectop *sop;
104
105 if (!(sop = mm_calloc(1, sizeof(struct selectop))))
106 return (NULL);
107
108 if (select_resize(sop, SELECT_ALLOC_SIZE(32 + 1))) {
109 select_free_selectop(sop);
110 return (NULL);
111 }
112
113 evsig_init(base);
114
115 return (sop);
116 }
117
118 #ifdef CHECK_INVARIANTS
119 static void
check_selectop(struct selectop * sop)120 check_selectop(struct selectop *sop)
121 {
122 /* nothing to be done here */
123 }
124 #else
125 #define check_selectop(sop) do { (void) sop; } while (0)
126 #endif
127
128 static int
select_dispatch(struct event_base * base,struct timeval * tv)129 select_dispatch(struct event_base *base, struct timeval *tv)
130 {
131 int res=0, i, j, nfds;
132 struct selectop *sop = base->evbase;
133
134 check_selectop(sop);
135 if (sop->resize_out_sets) {
136 fd_set *readset_out=NULL, *writeset_out=NULL;
137 size_t sz = sop->event_fdsz;
138 if (!(readset_out = mm_realloc(sop->event_readset_out, sz)))
139 return (-1);
140 sop->event_readset_out = readset_out;
141 if (!(writeset_out = mm_realloc(sop->event_writeset_out, sz))) {
142 /* We don't free readset_out here, since it was
143 * already successfully reallocated. The next time
144 * we call select_dispatch, the realloc will be a
145 * no-op. */
146 return (-1);
147 }
148 sop->event_writeset_out = writeset_out;
149 sop->resize_out_sets = 0;
150 }
151
152 memcpy(sop->event_readset_out, sop->event_readset_in,
153 sop->event_fdsz);
154 memcpy(sop->event_writeset_out, sop->event_writeset_in,
155 sop->event_fdsz);
156
157 nfds = sop->event_fds+1;
158
159 EVBASE_RELEASE_LOCK(base, th_base_lock);
160
161 res = select(nfds, sop->event_readset_out,
162 sop->event_writeset_out, NULL, tv);
163
164 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
165
166 check_selectop(sop);
167
168 if (res == -1) {
169 if (errno != EINTR) {
170 event_warn("select");
171 return (-1);
172 }
173
174 return (0);
175 }
176
177 event_debug(("%s: select reports %d", __func__, res));
178
179 check_selectop(sop);
180 i = random() % nfds;
181 for (j = 0; j < nfds; ++j) {
182 if (++i >= nfds)
183 i = 0;
184 res = 0;
185 if (FD_ISSET(i, sop->event_readset_out))
186 res |= EV_READ;
187 if (FD_ISSET(i, sop->event_writeset_out))
188 res |= EV_WRITE;
189
190 if (res == 0)
191 continue;
192
193 evmap_io_active(base, i, res);
194 }
195 check_selectop(sop);
196
197 return (0);
198 }
199
200 static int
select_resize(struct selectop * sop,int fdsz)201 select_resize(struct selectop *sop, int fdsz)
202 {
203 fd_set *readset_in = NULL;
204 fd_set *writeset_in = NULL;
205
206 if (sop->event_readset_in)
207 check_selectop(sop);
208
209 if ((readset_in = mm_realloc(sop->event_readset_in, fdsz)) == NULL)
210 goto error;
211 sop->event_readset_in = readset_in;
212 if ((writeset_in = mm_realloc(sop->event_writeset_in, fdsz)) == NULL) {
213 /* Note that this will leave event_readset_in expanded.
214 * That's okay; we wouldn't want to free it, since that would
215 * change the semantics of select_resize from "expand the
216 * readset_in and writeset_in, or return -1" to "expand the
217 * *set_in members, or trash them and return -1."
218 */
219 goto error;
220 }
221 sop->event_writeset_in = writeset_in;
222 sop->resize_out_sets = 1;
223
224 memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
225 fdsz - sop->event_fdsz);
226 memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
227 fdsz - sop->event_fdsz);
228
229 sop->event_fdsz = fdsz;
230 check_selectop(sop);
231
232 return (0);
233
234 error:
235 event_warn("malloc");
236 return (-1);
237 }
238
239
240 static int
select_add(struct event_base * base,int fd,short old,short events,void * p)241 select_add(struct event_base *base, int fd, short old, short events, void *p)
242 {
243 struct selectop *sop = base->evbase;
244 (void) p;
245
246 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
247 check_selectop(sop);
248 /*
249 * Keep track of the highest fd, so that we can calculate the size
250 * of the fd_sets for select(2)
251 */
252 if (sop->event_fds < fd) {
253 int fdsz = sop->event_fdsz;
254
255 if (fdsz < (int)sizeof(fd_mask))
256 fdsz = (int)sizeof(fd_mask);
257
258 /* In theory we should worry about overflow here. In
259 * reality, though, the highest fd on a unixy system will
260 * not overflow here. XXXX */
261 while (fdsz < (int) SELECT_ALLOC_SIZE(fd + 1))
262 fdsz *= 2;
263
264 if (fdsz != sop->event_fdsz) {
265 if (select_resize(sop, fdsz)) {
266 check_selectop(sop);
267 return (-1);
268 }
269 }
270
271 sop->event_fds = fd;
272 }
273
274 if (events & EV_READ)
275 FD_SET(fd, sop->event_readset_in);
276 if (events & EV_WRITE)
277 FD_SET(fd, sop->event_writeset_in);
278 check_selectop(sop);
279
280 return (0);
281 }
282
283 /*
284 * Nothing to be done here.
285 */
286
287 static int
select_del(struct event_base * base,int fd,short old,short events,void * p)288 select_del(struct event_base *base, int fd, short old, short events, void *p)
289 {
290 struct selectop *sop = base->evbase;
291 (void)p;
292
293 EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
294 check_selectop(sop);
295
296 if (sop->event_fds < fd) {
297 check_selectop(sop);
298 return (0);
299 }
300
301 if (events & EV_READ)
302 FD_CLR(fd, sop->event_readset_in);
303
304 if (events & EV_WRITE)
305 FD_CLR(fd, sop->event_writeset_in);
306
307 check_selectop(sop);
308 return (0);
309 }
310
311 static void
select_free_selectop(struct selectop * sop)312 select_free_selectop(struct selectop *sop)
313 {
314 if (sop->event_readset_in)
315 mm_free(sop->event_readset_in);
316 if (sop->event_writeset_in)
317 mm_free(sop->event_writeset_in);
318 if (sop->event_readset_out)
319 mm_free(sop->event_readset_out);
320 if (sop->event_writeset_out)
321 mm_free(sop->event_writeset_out);
322
323 memset(sop, 0, sizeof(struct selectop));
324 mm_free(sop);
325 }
326
327 static void
select_dealloc(struct event_base * base)328 select_dealloc(struct event_base *base)
329 {
330 evsig_dealloc(base);
331
332 select_free_selectop(base->evbase);
333 }
334