1 /*	$OpenBSD: poll.c,v 1.15 2010/04/21 20:02:40 nicm Exp $	*/
2 
3 /*
4  * Copyright 2000-2003 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 #include <sys/queue.h>
40 #include <poll.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #ifdef CHECK_INVARIANTS
48 #include <assert.h>
49 #endif
50 
51 #include "event.h"
52 #include "event-internal.h"
53 #include "evsignal.h"
54 #include "log.h"
55 
56 __RCSID("$MirOS: src/lib/libevent/poll.c,v 1.4 2014/03/12 23:45:11 tg Exp $");
57 
58 struct pollop {
59 	int event_count;		/* Highest number alloc */
60 	int nfds;                       /* Size of event_* */
61 	int fd_count;                   /* Size of idxplus1_by_fd */
62 	struct pollfd *event_set;
63 	struct event **event_r_back;
64 	struct event **event_w_back;
65 	int *idxplus1_by_fd; /* Index into event_set by fd; we add 1 so
66 			      * that 0 (which is easy to memset) can mean
67 			      * "no entry." */
68 };
69 
70 static void *poll_init	(struct event_base *);
71 static int poll_add		(void *, struct event *);
72 static int poll_del		(void *, struct event *);
73 static int poll_dispatch	(struct event_base *, void *, struct timeval *);
74 static void poll_dealloc	(struct event_base *, void *);
75 
76 const struct eventop pollops = {
77 	"poll",
78 	poll_init,
79 	poll_add,
80 	poll_del,
81 	poll_dispatch,
82 	poll_dealloc,
83     0
84 };
85 
86 static void *
poll_init(struct event_base * base)87 poll_init(struct event_base *base)
88 {
89 	struct pollop *pollop;
90 
91 	/* Disable poll when this environment variable is set */
92 	if (evutil_getenv("EVENT_NOPOLL"))
93 		return (NULL);
94 
95 	if (!(pollop = calloc(1, sizeof(struct pollop))))
96 		return (NULL);
97 
98 	evsignal_init(base);
99 
100 	return (pollop);
101 }
102 
103 #ifdef CHECK_INVARIANTS
104 static void
poll_check_ok(struct pollop * pop)105 poll_check_ok(struct pollop *pop)
106 {
107 	int i, idx;
108 	struct event *ev;
109 
110 	for (i = 0; i < pop->fd_count; ++i) {
111 		idx = pop->idxplus1_by_fd[i]-1;
112 		if (idx < 0)
113 			continue;
114 		assert(pop->event_set[idx].fd == i);
115 		if (pop->event_set[idx].events & POLLIN) {
116 			ev = pop->event_r_back[idx];
117 			assert(ev);
118 			assert(ev->ev_events & EV_READ);
119 			assert(ev->ev_fd == i);
120 		}
121 		if (pop->event_set[idx].events & POLLOUT) {
122 			ev = pop->event_w_back[idx];
123 			assert(ev);
124 			assert(ev->ev_events & EV_WRITE);
125 			assert(ev->ev_fd == i);
126 		}
127 	}
128 	for (i = 0; i < pop->nfds; ++i) {
129 		struct pollfd *pfd = &pop->event_set[i];
130 		assert(pop->idxplus1_by_fd[pfd->fd] == i+1);
131 	}
132 }
133 #else
134 #define poll_check_ok(pop)
135 #endif
136 
137 static int
poll_dispatch(struct event_base * base,void * arg,struct timeval * tv)138 poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
139 {
140 	int res, i, j, msec = -1, nfds;
141 	struct pollop *pop = arg;
142 
143 	poll_check_ok(pop);
144 
145 	if (tv != NULL)
146 		msec = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
147 
148 	nfds = pop->nfds;
149 	res = poll(pop->event_set, nfds, msec);
150 
151 	if (res == -1) {
152 		if (errno != EINTR) {
153                         event_warn("poll");
154 			return (-1);
155 		}
156 
157 		evsignal_process(base);
158 		return (0);
159 	} else if (base->sig.evsignal_caught) {
160 		evsignal_process(base);
161 	}
162 
163 	event_debug(("%s: poll reports %d", __func__, res));
164 
165 	if (res == 0 || nfds == 0)
166 		return (0);
167 
168 	i = arc4random_uniform(nfds);
169 	for (j = 0; j < nfds; j++) {
170 		struct event *r_ev = NULL, *w_ev = NULL;
171 		int what;
172 		if (++i == nfds)
173 			i = 0;
174 		what = pop->event_set[i].revents;
175 
176 		if (!what)
177 			continue;
178 
179 		res = 0;
180 
181 		/* If the file gets closed notify */
182 		if (what & (POLLHUP|POLLERR))
183 			what |= POLLIN|POLLOUT;
184 		if (what & POLLIN) {
185 			res |= EV_READ;
186 			r_ev = pop->event_r_back[i];
187 		}
188 		if (what & POLLOUT) {
189 			res |= EV_WRITE;
190 			w_ev = pop->event_w_back[i];
191 		}
192 		if (res == 0)
193 			continue;
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 
203 	return (0);
204 }
205 
206 static int
poll_add(void * arg,struct event * ev)207 poll_add(void *arg, struct event *ev)
208 {
209 	struct pollop *pop = arg;
210 	struct pollfd *pfd = NULL;
211 	int i;
212 
213 	if (ev->ev_events & EV_SIGNAL)
214 		return (evsignal_add(ev));
215 	if (!(ev->ev_events & (EV_READ|EV_WRITE)))
216 		return (0);
217 
218 	poll_check_ok(pop);
219 	if (pop->nfds + 1 >= pop->event_count) {
220 		struct pollfd *tmp_event_set;
221 		struct event **tmp_event_r_back;
222 		struct event **tmp_event_w_back;
223 		int tmp_event_count;
224 
225 		if (pop->event_count < 32)
226 			tmp_event_count = 32;
227 		else
228 			tmp_event_count = pop->event_count * 2;
229 
230 		/* We need more file descriptors */
231 		tmp_event_set = realloc(pop->event_set,
232 				 tmp_event_count * sizeof(struct pollfd));
233 		if (tmp_event_set == NULL) {
234 			event_warn("realloc");
235 			return (-1);
236 		}
237 		pop->event_set = tmp_event_set;
238 
239 		tmp_event_r_back = realloc(pop->event_r_back,
240 			    tmp_event_count * sizeof(struct event *));
241 		if (tmp_event_r_back == NULL) {
242 			/* event_set overallocated; that's okay. */
243 			event_warn("realloc");
244 			return (-1);
245 		}
246 		pop->event_r_back = tmp_event_r_back;
247 
248 		tmp_event_w_back = realloc(pop->event_w_back,
249 			    tmp_event_count * sizeof(struct event *));
250 		if (tmp_event_w_back == NULL) {
251 			/* event_set and event_r_back overallocated; that's
252 			 * okay. */
253 			event_warn("realloc");
254 			return (-1);
255 		}
256 		pop->event_w_back = tmp_event_w_back;
257 
258 		pop->event_count = tmp_event_count;
259 	}
260 	if (ev->ev_fd >= pop->fd_count) {
261 		int *tmp_idxplus1_by_fd;
262 		int new_count;
263 		if (pop->fd_count < 32)
264 			new_count = 32;
265 		else
266 			new_count = pop->fd_count * 2;
267 		while (new_count <= ev->ev_fd)
268 			new_count *= 2;
269 		tmp_idxplus1_by_fd =
270 			realloc(pop->idxplus1_by_fd, new_count * sizeof(int));
271 		if (tmp_idxplus1_by_fd == NULL) {
272 			event_warn("realloc");
273 			return (-1);
274 		}
275 		pop->idxplus1_by_fd = tmp_idxplus1_by_fd;
276 		memset(pop->idxplus1_by_fd + pop->fd_count,
277 		       0, sizeof(int)*(new_count - pop->fd_count));
278 		pop->fd_count = new_count;
279 	}
280 
281 	i = pop->idxplus1_by_fd[ev->ev_fd] - 1;
282 	if (i >= 0) {
283 		pfd = &pop->event_set[i];
284 	} else {
285 		i = pop->nfds++;
286 		pfd = &pop->event_set[i];
287 		pfd->events = 0;
288 		pfd->fd = ev->ev_fd;
289 		pop->event_w_back[i] = pop->event_r_back[i] = NULL;
290 		pop->idxplus1_by_fd[ev->ev_fd] = i + 1;
291 	}
292 
293 	pfd->revents = 0;
294 	if (ev->ev_events & EV_WRITE) {
295 		pfd->events |= POLLOUT;
296 		pop->event_w_back[i] = ev;
297 	}
298 	if (ev->ev_events & EV_READ) {
299 		pfd->events |= POLLIN;
300 		pop->event_r_back[i] = ev;
301 	}
302 	poll_check_ok(pop);
303 
304 	return (0);
305 }
306 
307 /*
308  * Nothing to be done here.
309  */
310 
311 static int
poll_del(void * arg,struct event * ev)312 poll_del(void *arg, struct event *ev)
313 {
314 	struct pollop *pop = arg;
315 	struct pollfd *pfd = NULL;
316 	int i;
317 
318 	if (ev->ev_events & EV_SIGNAL)
319 		return (evsignal_del(ev));
320 
321 	if (!(ev->ev_events & (EV_READ|EV_WRITE)))
322 		return (0);
323 
324 	poll_check_ok(pop);
325 	i = pop->idxplus1_by_fd[ev->ev_fd] - 1;
326 	if (i < 0)
327 		return (-1);
328 
329 	/* Do we still want to read or write? */
330 	pfd = &pop->event_set[i];
331 	if (ev->ev_events & EV_READ) {
332 		pfd->events &= ~POLLIN;
333 		pop->event_r_back[i] = NULL;
334 	}
335 	if (ev->ev_events & EV_WRITE) {
336 		pfd->events &= ~POLLOUT;
337 		pop->event_w_back[i] = NULL;
338 	}
339 	poll_check_ok(pop);
340 	if (pfd->events)
341 		/* Another event cares about that fd. */
342 		return (0);
343 
344 	/* Okay, so we aren't interested in that fd anymore. */
345 	pop->idxplus1_by_fd[ev->ev_fd] = 0;
346 
347 	--pop->nfds;
348 	if (i != pop->nfds) {
349 		/*
350 		 * Shift the last pollfd down into the now-unoccupied
351 		 * position.
352 		 */
353 		memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
354 		       sizeof(struct pollfd));
355 		pop->event_r_back[i] = pop->event_r_back[pop->nfds];
356 		pop->event_w_back[i] = pop->event_w_back[pop->nfds];
357 		pop->idxplus1_by_fd[pop->event_set[i].fd] = i + 1;
358 	}
359 
360 	poll_check_ok(pop);
361 	return (0);
362 }
363 
364 static void
poll_dealloc(struct event_base * base,void * arg)365 poll_dealloc(struct event_base *base, void *arg)
366 {
367 	struct pollop *pop = arg;
368 
369 	evsignal_dealloc(base);
370 	if (pop->event_set)
371 		free(pop->event_set);
372 	if (pop->event_r_back)
373 		free(pop->event_r_back);
374 	if (pop->event_w_back)
375 		free(pop->event_w_back);
376 	if (pop->idxplus1_by_fd)
377 		free(pop->idxplus1_by_fd);
378 
379 	memset(pop, 0, sizeof(struct pollop));
380 	free(pop);
381 }
382