1 /*	$OpenBSD: select.c,v 1.16 2010/04/21 21:02:47 nicm Exp $	*/
2 
3 /*
4  * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
5  * All rights reserved.
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 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #else
37 #include <sys/_libevent_time.h>
38 #endif
39 #ifdef HAVE_SYS_SELECT_H
40 #include <sys/select.h>
41 #endif
42 #include <sys/queue.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #ifdef CHECK_INVARIANTS
50 #include <assert.h>
51 #endif
52 
53 #include "event.h"
54 #include "evutil.h"
55 #include "event-internal.h"
56 #include "evsignal.h"
57 #include "log.h"
58 
59 __RCSID("$MirOS: src/lib/libevent/select.c,v 1.8 2014/03/12 23:45:11 tg Exp $");
60 
61 #ifndef howmany
62 #define        howmany(x, y)   (((x)+((y)-1))/(y))
63 #endif
64 
65 #ifndef HAVE_FD_MASK
66 /* This type is mandatory, but Android doesn't define it. */
67 #undef NFDBITS
68 #define NFDBITS (sizeof(long)*8)
69 typedef unsigned long fd_mask;
70 #endif
71 
72 struct selectop {
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 	struct event **event_r_by_fd;
78 	struct event **event_w_by_fd;
79 	size_t event_fdsz;
80 	int event_fds;		/* Highest fd in fd set */
81 };
82 
83 static void *select_init	(struct event_base *);
84 static int select_add		(void *, struct event *);
85 static int select_del		(void *, struct event *);
86 static int select_dispatch	(struct event_base *, void *, struct timeval *);
87 static void select_dealloc     (struct event_base *, void *);
88 
89 const struct eventop selectops = {
90 	"select",
91 	select_init,
92 	select_add,
93 	select_del,
94 	select_dispatch,
95 	select_dealloc,
96 	0
97 };
98 
99 static int select_resize(struct selectop *sop, size_t fdsz);
100 
101 static void *
select_init(struct event_base * base)102 select_init(struct event_base *base)
103 {
104 	struct selectop *sop;
105 
106 	/* Disable select when this environment variable is set */
107 	if (evutil_getenv("EVENT_NOSELECT"))
108 		return (NULL);
109 
110 	if (!(sop = calloc(1, sizeof(struct selectop))))
111 		return (NULL);
112 
113 	select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask));
114 
115 	evsignal_init(base);
116 
117 	return (sop);
118 }
119 
120 #ifdef CHECK_INVARIANTS
121 static void
check_selectop(struct selectop * sop)122 check_selectop(struct selectop *sop)
123 {
124 	int i;
125 	for (i = 0; i <= sop->event_fds; ++i) {
126 		if (FD_ISSET(i, sop->event_readset_in)) {
127 			assert(sop->event_r_by_fd[i]);
128 			assert(sop->event_r_by_fd[i]->ev_events & EV_READ);
129 			assert(sop->event_r_by_fd[i]->ev_fd == i);
130 		} else {
131 			assert(! sop->event_r_by_fd[i]);
132 		}
133 		if (FD_ISSET(i, sop->event_writeset_in)) {
134 			assert(sop->event_w_by_fd[i]);
135 			assert(sop->event_w_by_fd[i]->ev_events & EV_WRITE);
136 			assert(sop->event_w_by_fd[i]->ev_fd == i);
137 		} else {
138 			assert(! sop->event_w_by_fd[i]);
139 		}
140 	}
141 
142 }
143 #else
144 #define check_selectop(sop) do { (void) sop; } while (0)
145 #endif
146 
147 static int
select_dispatch(struct event_base * base,void * arg,struct timeval * tv)148 select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
149 {
150 	int res, i, j;
151 	struct selectop *sop = arg;
152 
153 	check_selectop(sop);
154 
155 	memcpy(sop->event_readset_out, sop->event_readset_in,
156 	       sop->event_fdsz);
157 	memcpy(sop->event_writeset_out, sop->event_writeset_in,
158 	       sop->event_fdsz);
159 
160 	res = select(sop->event_fds + 1, sop->event_readset_out,
161 	    sop->event_writeset_out, NULL, tv);
162 
163 	check_selectop(sop);
164 
165 	if (res == -1) {
166 		if (errno != EINTR) {
167 			event_warn("select");
168 			return (-1);
169 		}
170 
171 		evsignal_process(base);
172 		return (0);
173 	} else if (base->sig.evsignal_caught) {
174 		evsignal_process(base);
175 	}
176 
177 	event_debug(("%s: select reports %d", __func__, res));
178 
179 	check_selectop(sop);
180 	i = arc4random_uniform(sop->event_fds + 1);
181 	for (j = 0; j <= sop->event_fds; ++j) {
182 		struct event *r_ev = NULL, *w_ev = NULL;
183 		if (++i >= sop->event_fds+1)
184 			i = 0;
185 
186 		res = 0;
187 		if (FD_ISSET(i, sop->event_readset_out)) {
188 			r_ev = sop->event_r_by_fd[i];
189 			res |= EV_READ;
190 		}
191 		if (FD_ISSET(i, sop->event_writeset_out)) {
192 			w_ev = sop->event_w_by_fd[i];
193 			res |= EV_WRITE;
194 		}
195 		if (r_ev && (res & r_ev->ev_events)) {
196 			event_active(r_ev, res & r_ev->ev_events, 1);
197 		}
198 		if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
199 			event_active(w_ev, res & w_ev->ev_events, 1);
200 		}
201 	}
202 	check_selectop(sop);
203 
204 	return (0);
205 }
206 
207 
208 static int
select_resize(struct selectop * sop,size_t fdsz)209 select_resize(struct selectop *sop, size_t fdsz)
210 {
211 	int n_events, n_events_old;
212 
213 	fd_set *readset_in = NULL;
214 	fd_set *writeset_in = NULL;
215 	fd_set *readset_out = NULL;
216 	fd_set *writeset_out = NULL;
217 	struct event **r_by_fd = NULL;
218 	struct event **w_by_fd = NULL;
219 
220 	n_events = (fdsz/sizeof(fd_mask)) * NFDBITS;
221 	n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS;
222 
223 	if (sop->event_readset_in)
224 		check_selectop(sop);
225 
226 	if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL)
227 		goto error;
228 	sop->event_readset_in = readset_in;
229 	if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL)
230 		goto error;
231 	sop->event_readset_out = readset_out;
232 	if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL)
233 		goto error;
234 	sop->event_writeset_in = writeset_in;
235 	if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL)
236 		goto error;
237 	sop->event_writeset_out = writeset_out;
238 	if ((r_by_fd = realloc(sop->event_r_by_fd,
239 		 n_events*sizeof(struct event*))) == NULL)
240 		goto error;
241 	sop->event_r_by_fd = r_by_fd;
242 	if ((w_by_fd = realloc(sop->event_w_by_fd,
243 		 n_events * sizeof(struct event*))) == NULL)
244 		goto error;
245 	sop->event_w_by_fd = w_by_fd;
246 
247 	memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
248 	    fdsz - sop->event_fdsz);
249 	memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
250 	    fdsz - sop->event_fdsz);
251 	memset(sop->event_r_by_fd + n_events_old, 0,
252 	    (n_events-n_events_old) * sizeof(struct event*));
253 	memset(sop->event_w_by_fd + n_events_old, 0,
254 	    (n_events-n_events_old) * sizeof(struct event*));
255 
256 	sop->event_fdsz = fdsz;
257 	check_selectop(sop);
258 
259 	return (0);
260 
261  error:
262 	event_warn("malloc");
263 	return (-1);
264 }
265 
266 
267 static int
select_add(void * arg,struct event * ev)268 select_add(void *arg, struct event *ev)
269 {
270 	struct selectop *sop = arg;
271 
272 	if (ev->ev_events & EV_SIGNAL)
273 		return (evsignal_add(ev));
274 
275 	check_selectop(sop);
276 	/*
277 	 * Keep track of the highest fd, so that we can calculate the size
278 	 * of the fd_sets for select(2)
279 	 */
280 	if (sop->event_fds < ev->ev_fd) {
281 		size_t fdsz = sop->event_fdsz;
282 
283 		if (fdsz < sizeof(fd_mask))
284 			fdsz = sizeof(fd_mask);
285 
286 		while (fdsz <
287 		    (howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask)))
288 			fdsz *= 2;
289 
290 		if (fdsz != sop->event_fdsz) {
291 			if (select_resize(sop, fdsz)) {
292 				check_selectop(sop);
293 				return (-1);
294 			}
295 		}
296 
297 		sop->event_fds = ev->ev_fd;
298 	}
299 
300 	if (ev->ev_events & EV_READ) {
301 		FD_SET(ev->ev_fd, sop->event_readset_in);
302 		sop->event_r_by_fd[ev->ev_fd] = ev;
303 	}
304 	if (ev->ev_events & EV_WRITE) {
305 		FD_SET(ev->ev_fd, sop->event_writeset_in);
306 		sop->event_w_by_fd[ev->ev_fd] = ev;
307 	}
308 	check_selectop(sop);
309 
310 	return (0);
311 }
312 
313 /*
314  * Nothing to be done here.
315  */
316 
317 static int
select_del(void * arg,struct event * ev)318 select_del(void *arg, struct event *ev)
319 {
320 	struct selectop *sop = arg;
321 
322 	check_selectop(sop);
323 	if (ev->ev_events & EV_SIGNAL)
324 		return (evsignal_del(ev));
325 
326 	if (sop->event_fds < ev->ev_fd) {
327 		check_selectop(sop);
328 		return (0);
329 	}
330 
331 	if (ev->ev_events & EV_READ) {
332 		FD_CLR(ev->ev_fd, sop->event_readset_in);
333 		sop->event_r_by_fd[ev->ev_fd] = NULL;
334 	}
335 
336 	if (ev->ev_events & EV_WRITE) {
337 		FD_CLR(ev->ev_fd, sop->event_writeset_in);
338 		sop->event_w_by_fd[ev->ev_fd] = NULL;
339 	}
340 
341 	check_selectop(sop);
342 	return (0);
343 }
344 
345 static void
select_dealloc(struct event_base * base,void * arg)346 select_dealloc(struct event_base *base, void *arg)
347 {
348 	struct selectop *sop = arg;
349 
350 	evsignal_dealloc(base);
351 	if (sop->event_readset_in)
352 		free(sop->event_readset_in);
353 	if (sop->event_writeset_in)
354 		free(sop->event_writeset_in);
355 	if (sop->event_readset_out)
356 		free(sop->event_readset_out);
357 	if (sop->event_writeset_out)
358 		free(sop->event_writeset_out);
359 	if (sop->event_r_by_fd)
360 		free(sop->event_r_by_fd);
361 	if (sop->event_w_by_fd)
362 		free(sop->event_w_by_fd);
363 
364 	memset(sop, 0, sizeof(struct selectop));
365 	free(sop);
366 }
367