Ensure that y,x are valid coordinates before using them.
[netbsd-mini2440.git] / lib / libevent / poll.c
blob6c756023a57332b8db471e8e97b6c960f1e57034
1 /* $NetBSD: poll.c,v 1.7 2008/05/16 20:24:58 peter Exp $ */
2 /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
4 /*
5 * Copyright 2000-2003 Niels Provos <provos@citi.umich.edu>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Niels Provos.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "config.h"
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #else
39 #include <sys/_time.h>
40 #endif
41 #include <sys/queue.h>
42 #include <poll.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
53 #include "event.h"
54 #include "event-internal.h"
55 #include "evsignal.h"
56 #include "log.h"
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." */
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 *);
76 const struct eventop pollops = {
77 "poll",
78 poll_init,
79 poll_add,
80 poll_del,
81 poll_dispatch,
82 poll_dealloc,
86 static void *
87 poll_init(struct event_base *base)
89 struct pollop *pollop;
91 /* Disable poll when this environment variable is set */
92 if (getenv("EVENT_NOPOLL"))
93 return (NULL);
95 if (!(pollop = calloc(1, sizeof(struct pollop))))
96 return (NULL);
98 evsignal_init(base);
100 return (pollop);
103 #ifdef CHECK_INVARIANTS
104 static void
105 poll_check_ok(struct pollop *pop)
107 int i, idx;
108 struct event *ev;
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);
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);
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);
133 #else
134 #define poll_check_ok(pop)
135 #endif
137 static int
138 poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
140 int res, i, msec = -1, nfds;
141 struct pollop *pop = arg;
143 poll_check_ok(pop);
145 if (tv != NULL)
146 msec = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
148 nfds = pop->nfds;
149 res = poll(pop->event_set, nfds, msec);
151 if (res == -1) {
152 if (errno != EINTR) {
153 event_warn("poll");
154 return (-1);
157 evsignal_process(base);
158 return (0);
159 } else if (base->sig.evsignal_caught) {
160 evsignal_process(base);
163 event_debug(("%s: poll reports %d", __func__, res));
165 if (res == 0)
166 return (0);
168 for (i = 0; i < nfds; i++) {
169 int what = pop->event_set[i].revents;
170 struct event *r_ev = NULL, *w_ev = NULL;
171 if (!what)
172 continue;
174 res = 0;
176 /* If the file gets closed notify */
177 if (what & (POLLHUP|POLLERR))
178 what |= POLLIN|POLLOUT;
179 if (what & POLLIN) {
180 res |= EV_READ;
181 r_ev = pop->event_r_back[i];
183 if (what & POLLOUT) {
184 res |= EV_WRITE;
185 w_ev = pop->event_w_back[i];
187 if (res == 0)
188 continue;
190 if (r_ev && (res & r_ev->ev_events)) {
191 event_active(r_ev, res & r_ev->ev_events, 1);
193 if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
194 event_active(w_ev, res & w_ev->ev_events, 1);
198 return (0);
201 static int
202 poll_add(void *arg, struct event *ev)
204 struct pollop *pop = arg;
205 struct pollfd *pfd = NULL;
206 int i;
208 if (ev->ev_events & EV_SIGNAL)
209 return (evsignal_add(ev));
210 if (!(ev->ev_events & (EV_READ|EV_WRITE)))
211 return (0);
213 poll_check_ok(pop);
214 if (pop->nfds + 1 >= pop->event_count) {
215 struct pollfd *tmp_event_set;
216 struct event **tmp_event_r_back;
217 struct event **tmp_event_w_back;
218 int tmp_event_count;
220 if (pop->event_count < 32)
221 tmp_event_count = 32;
222 else
223 tmp_event_count = pop->event_count * 2;
225 /* We need more file descriptors */
226 tmp_event_set = realloc(pop->event_set,
227 tmp_event_count * sizeof(struct pollfd));
228 if (tmp_event_set == NULL) {
229 event_warn("realloc");
230 return (-1);
232 pop->event_set = tmp_event_set;
234 tmp_event_r_back = realloc(pop->event_r_back,
235 tmp_event_count * sizeof(struct event *));
236 if (tmp_event_r_back == NULL) {
237 /* event_set overallocated; that's okay. */
238 event_warn("realloc");
239 return (-1);
241 pop->event_r_back = tmp_event_r_back;
243 tmp_event_w_back = realloc(pop->event_w_back,
244 tmp_event_count * sizeof(struct event *));
245 if (tmp_event_w_back == NULL) {
246 /* event_set and event_r_back overallocated; that's
247 * okay. */
248 event_warn("realloc");
249 return (-1);
251 pop->event_w_back = tmp_event_w_back;
253 pop->event_count = tmp_event_count;
255 if (ev->ev_fd >= pop->fd_count) {
256 int *tmp_idxplus1_by_fd;
257 int new_count;
258 if (pop->fd_count < 32)
259 new_count = 32;
260 else
261 new_count = pop->fd_count * 2;
262 while (new_count <= ev->ev_fd)
263 new_count *= 2;
264 tmp_idxplus1_by_fd =
265 realloc(pop->idxplus1_by_fd, new_count * sizeof(int));
266 if (tmp_idxplus1_by_fd == NULL) {
267 event_warn("realloc");
268 return (-1);
270 pop->idxplus1_by_fd = tmp_idxplus1_by_fd;
271 memset(pop->idxplus1_by_fd + pop->fd_count,
272 0, sizeof(int)*(new_count - pop->fd_count));
273 pop->fd_count = new_count;
276 i = pop->idxplus1_by_fd[ev->ev_fd] - 1;
277 if (i >= 0) {
278 pfd = &pop->event_set[i];
279 } else {
280 i = pop->nfds++;
281 pfd = &pop->event_set[i];
282 pfd->events = 0;
283 pfd->fd = ev->ev_fd;
284 pop->event_w_back[i] = pop->event_r_back[i] = NULL;
285 pop->idxplus1_by_fd[ev->ev_fd] = i + 1;
288 pfd->revents = 0;
289 if (ev->ev_events & EV_WRITE) {
290 pfd->events |= POLLOUT;
291 pop->event_w_back[i] = ev;
293 if (ev->ev_events & EV_READ) {
294 pfd->events |= POLLIN;
295 pop->event_r_back[i] = ev;
297 poll_check_ok(pop);
299 return (0);
303 * Nothing to be done here.
306 static int
307 poll_del(void *arg, struct event *ev)
309 struct pollop *pop = arg;
310 struct pollfd *pfd = NULL;
311 int i;
313 if (ev->ev_events & EV_SIGNAL)
314 return (evsignal_del(ev));
316 if (!(ev->ev_events & (EV_READ|EV_WRITE)))
317 return (0);
319 poll_check_ok(pop);
320 i = pop->idxplus1_by_fd[ev->ev_fd] - 1;
321 if (i < 0)
322 return (-1);
324 /* Do we still want to read or write? */
325 pfd = &pop->event_set[i];
326 if (ev->ev_events & EV_READ) {
327 pfd->events &= ~POLLIN;
328 pop->event_r_back[i] = NULL;
330 if (ev->ev_events & EV_WRITE) {
331 pfd->events &= ~POLLOUT;
332 pop->event_w_back[i] = NULL;
334 poll_check_ok(pop);
335 if (pfd->events)
336 /* Another event cares about that fd. */
337 return (0);
339 /* Okay, so we aren't interested in that fd anymore. */
340 pop->idxplus1_by_fd[ev->ev_fd] = 0;
342 --pop->nfds;
343 if (i != pop->nfds) {
345 * Shift the last pollfd down into the now-unoccupied
346 * position.
348 memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
349 sizeof(struct pollfd));
350 pop->event_r_back[i] = pop->event_r_back[pop->nfds];
351 pop->event_w_back[i] = pop->event_w_back[pop->nfds];
352 pop->idxplus1_by_fd[pop->event_set[i].fd] = i + 1;
355 poll_check_ok(pop);
356 return (0);
359 static void
360 poll_dealloc(struct event_base *base, void *arg)
362 struct pollop *pop = arg;
364 evsignal_dealloc(base);
365 if (pop->event_set)
366 free(pop->event_set);
367 if (pop->event_r_back)
368 free(pop->event_r_back);
369 if (pop->event_w_back)
370 free(pop->event_w_back);
371 if (pop->idxplus1_by_fd)
372 free(pop->idxplus1_by_fd);
374 memset(pop, 0, sizeof(struct pollop));
375 free(pop);