uloop: improve edge trigger reliability on mac os x
[project/libubox.git] / uloop.c
1 /*
2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
4 * Copyright (C) 2010 Steven Barth <steven@midlink.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
22 #include <sys/time.h>
23 #include <sys/types.h>
24
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <poll.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <stdbool.h>
33
34 #include "uloop.h"
35 #include "utils.h"
36
37 #ifdef USE_KQUEUE
38 #include <sys/event.h>
39 #endif
40 #ifdef USE_EPOLL
41 #include <sys/epoll.h>
42 #endif
43 #include <sys/wait.h>
44
45 #define ULOOP_MAX_EVENTS 10
46
47 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
48 static struct list_head processes = LIST_HEAD_INIT(processes);
49
50 static int poll_fd = -1;
51 bool uloop_cancelled = false;
52 bool uloop_handle_sigchld = true;
53 static bool do_sigchld = false;
54 static int cur_fd, cur_nfds;
55
56 #ifdef USE_KQUEUE
57
58 int uloop_init(void)
59 {
60 struct timespec timeout = { 0, 0 };
61 struct kevent ev = {};
62
63 if (poll_fd >= 0)
64 return 0;
65
66 poll_fd = kqueue();
67 if (poll_fd < 0)
68 return -1;
69
70 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
71 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
72
73 return 0;
74 }
75
76
77 static uint16_t get_flags(unsigned int flags, unsigned int mask)
78 {
79 uint16_t kflags = 0;
80
81 if (!(flags & mask))
82 return EV_DELETE;
83
84 kflags = EV_ADD;
85 if (flags & ULOOP_EDGE_TRIGGER)
86 kflags |= EV_CLEAR;
87
88 return kflags;
89 }
90
91 static struct kevent events[ULOOP_MAX_EVENTS];
92
93 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
94 {
95 struct timespec timeout = { 0, 0 };
96 struct kevent ev[2];
97 int nev = 0;
98 unsigned int fl = 0;
99 uint16_t kflags;
100
101 if (flags & ULOOP_EDGE_DEFER)
102 flags &= ~ULOOP_EDGE_TRIGGER;
103
104 kflags = get_flags(flags, ULOOP_READ);
105 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
106
107 kflags = get_flags(flags, ULOOP_WRITE);
108 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
109
110 if (!flags)
111 fl |= EV_DELETE;
112
113 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
114 return -1;
115
116 return 0;
117 }
118
119 static int register_poll(struct uloop_fd *fd, unsigned int flags)
120 {
121 if (flags & ULOOP_EDGE_TRIGGER)
122 flags |= ULOOP_EDGE_DEFER;
123 else
124 flags &= ~ULOOP_EDGE_DEFER;
125
126 fd->flags = flags;
127 return register_kevent(fd, flags);
128 }
129
130 int uloop_fd_delete(struct uloop_fd *sock)
131 {
132 int i;
133
134 for (i = cur_fd + 1; i < cur_nfds; i++) {
135 if (events[i].udata != sock)
136 continue;
137
138 events[i].udata = NULL;
139 }
140
141 sock->registered = false;
142 return register_poll(sock, 0);
143 }
144
145 static void uloop_run_events(int timeout)
146 {
147 struct timespec ts;
148 int nfds, n;
149
150 if (timeout >= 0) {
151 ts.tv_sec = timeout / 1000;
152 ts.tv_nsec = (timeout % 1000) * 1000000;
153 }
154
155 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
156 for(n = 0; n < nfds; ++n)
157 {
158 struct uloop_fd *u = events[n].udata;
159 unsigned int ev = 0;
160
161 if (!u)
162 continue;
163
164 if (events[n].flags & EV_ERROR) {
165 u->error = true;
166 uloop_fd_delete(u);
167 }
168
169 if(events[n].filter == EVFILT_READ)
170 ev |= ULOOP_READ;
171 else if (events[n].filter == EVFILT_WRITE)
172 ev |= ULOOP_WRITE;
173
174 if (events[n].flags & EV_EOF)
175 u->eof = true;
176 else if (!ev)
177 continue;
178
179 if (u->cb) {
180 cur_fd = n;
181 cur_nfds = nfds;
182 u->cb(u, ev);
183 if (u->flags & ULOOP_EDGE_DEFER) {
184 u->flags &= ~ULOOP_EDGE_DEFER;
185 register_kevent(u, u->flags);
186 }
187 }
188 }
189 cur_nfds = 0;
190 }
191
192 #endif
193
194 #ifdef USE_EPOLL
195
196 /**
197 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
198 */
199 #ifndef EPOLLRDHUP
200 #define EPOLLRDHUP 0x2000
201 #endif
202
203 int uloop_init(void)
204 {
205 if (poll_fd >= 0)
206 return 0;
207
208 poll_fd = epoll_create(32);
209 if (poll_fd < 0)
210 return -1;
211
212 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
213 return 0;
214 }
215
216 static int register_poll(struct uloop_fd *fd, unsigned int flags)
217 {
218 struct epoll_event ev;
219 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
220
221 memset(&ev, 0, sizeof(struct epoll_event));
222
223 if (flags & ULOOP_READ)
224 ev.events |= EPOLLIN | EPOLLRDHUP;
225
226 if (flags & ULOOP_WRITE)
227 ev.events |= EPOLLOUT;
228
229 if (flags & ULOOP_EDGE_TRIGGER)
230 ev.events |= EPOLLET;
231
232 ev.data.fd = fd->fd;
233 ev.data.ptr = fd;
234
235 return epoll_ctl(poll_fd, op, fd->fd, &ev);
236 }
237
238 static struct epoll_event events[ULOOP_MAX_EVENTS];
239
240 int uloop_fd_delete(struct uloop_fd *sock)
241 {
242 int i;
243
244 for (i = cur_fd + 1; i < cur_nfds; i++) {
245 if (events[i].data.ptr != sock)
246 continue;
247
248 events[i].data.ptr = NULL;
249 }
250 sock->registered = false;
251 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
252 }
253
254 static void uloop_run_events(int timeout)
255 {
256 int n, nfds;
257
258 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
259 for(n = 0; n < nfds; ++n)
260 {
261 struct uloop_fd *u = events[n].data.ptr;
262 unsigned int ev = 0;
263
264 if (!u)
265 continue;
266
267 if(events[n].events & (EPOLLERR|EPOLLHUP)) {
268 u->error = true;
269 uloop_fd_delete(u);
270 }
271
272 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)))
273 continue;
274
275 if(events[n].events & EPOLLRDHUP)
276 u->eof = true;
277
278 if(events[n].events & EPOLLIN)
279 ev |= ULOOP_READ;
280
281 if(events[n].events & EPOLLOUT)
282 ev |= ULOOP_WRITE;
283
284 if(u->cb) {
285 cur_fd = n;
286 cur_nfds = nfds;
287 u->cb(u, ev);
288 }
289 }
290 cur_nfds = 0;
291 }
292
293 #endif
294
295 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
296 {
297 unsigned int fl;
298 int ret;
299
300 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
301 fl = fcntl(sock->fd, F_GETFL, 0);
302 fl |= O_NONBLOCK;
303 fcntl(sock->fd, F_SETFL, fl);
304 }
305
306 ret = register_poll(sock, flags);
307 if (ret < 0)
308 goto out;
309
310 sock->registered = true;
311 sock->eof = false;
312
313 out:
314 return ret;
315 }
316
317 static int tv_diff(struct timeval *t1, struct timeval *t2)
318 {
319 return
320 (t1->tv_sec - t2->tv_sec) * 1000 +
321 (t1->tv_usec - t2->tv_usec) / 1000;
322 }
323
324 int uloop_timeout_add(struct uloop_timeout *timeout)
325 {
326 struct uloop_timeout *tmp;
327 struct list_head *h = &timeouts;
328
329 if (timeout->pending)
330 return -1;
331
332 list_for_each_entry(tmp, &timeouts, list) {
333 if (tv_diff(&tmp->time, &timeout->time) > 0) {
334 h = &tmp->list;
335 break;
336 }
337 }
338
339 list_add_tail(&timeout->list, h);
340 timeout->pending = true;
341
342 return 0;
343 }
344
345 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
346 {
347 struct timeval *time = &timeout->time;
348
349 if (timeout->pending)
350 uloop_timeout_cancel(timeout);
351
352 gettimeofday(&timeout->time, NULL);
353
354 time->tv_sec += msecs / 1000;
355 time->tv_usec += (msecs % 1000) * 1000;
356
357 if (time->tv_usec > 1000000) {
358 time->tv_sec++;
359 time->tv_usec %= 1000000;
360 }
361
362 return uloop_timeout_add(timeout);
363 }
364
365 int uloop_timeout_cancel(struct uloop_timeout *timeout)
366 {
367 if (!timeout->pending)
368 return -1;
369
370 list_del(&timeout->list);
371 timeout->pending = false;
372
373 return 0;
374 }
375
376 int uloop_process_add(struct uloop_process *p)
377 {
378 struct uloop_process *tmp;
379 struct list_head *h = &processes;
380
381 if (p->pending)
382 return -1;
383
384 list_for_each_entry(tmp, &processes, list) {
385 if (tmp->pid > p->pid) {
386 h = &tmp->list;
387 break;
388 }
389 }
390
391 list_add_tail(&p->list, h);
392 p->pending = true;
393
394 return 0;
395 }
396
397 int uloop_process_delete(struct uloop_process *p)
398 {
399 if (!p->pending)
400 return -1;
401
402 list_del(&p->list);
403 p->pending = false;
404
405 return 0;
406 }
407
408 static void uloop_handle_processes(void)
409 {
410 struct uloop_process *p, *tmp;
411 pid_t pid;
412 int ret;
413
414 do_sigchld = false;
415
416 while (1) {
417 pid = waitpid(-1, &ret, WNOHANG);
418 if (pid <= 0)
419 return;
420
421 list_for_each_entry_safe(p, tmp, &processes, list) {
422 if (p->pid < pid)
423 continue;
424
425 if (p->pid > pid)
426 break;
427
428 uloop_process_delete(p);
429 p->cb(p, ret);
430 }
431 }
432
433 }
434
435 static void uloop_handle_sigint(int signo)
436 {
437 uloop_cancelled = true;
438 }
439
440 static void uloop_sigchld(int signo)
441 {
442 do_sigchld = true;
443 }
444
445 static void uloop_setup_signals(void)
446 {
447 struct sigaction s;
448
449 memset(&s, 0, sizeof(struct sigaction));
450 s.sa_handler = uloop_handle_sigint;
451 s.sa_flags = 0;
452 sigaction(SIGINT, &s, NULL);
453
454 if (uloop_handle_sigchld) {
455 s.sa_handler = uloop_sigchld;
456 sigaction(SIGCHLD, &s, NULL);
457 }
458 }
459
460 static int uloop_get_next_timeout(struct timeval *tv)
461 {
462 struct uloop_timeout *timeout;
463 int diff;
464
465 if (list_empty(&timeouts))
466 return -1;
467
468 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
469 diff = tv_diff(&timeout->time, tv);
470 if (diff < 0)
471 return 0;
472
473 return diff;
474 }
475
476 static void uloop_process_timeouts(struct timeval *tv)
477 {
478 struct uloop_timeout *t;
479
480 while (!list_empty(&timeouts)) {
481 t = list_first_entry(&timeouts, struct uloop_timeout, list);
482
483 if (tv_diff(&t->time, tv) > 0)
484 break;
485
486 uloop_timeout_cancel(t);
487 if (t->cb)
488 t->cb(t);
489 }
490 }
491
492 static void uloop_clear_timeouts(void)
493 {
494 struct uloop_timeout *t, *tmp;
495
496 list_for_each_entry_safe(t, tmp, &timeouts, list)
497 uloop_timeout_cancel(t);
498 }
499
500 static void uloop_clear_processes(void)
501 {
502 struct uloop_process *p, *tmp;
503
504 list_for_each_entry_safe(p, tmp, &processes, list)
505 uloop_process_delete(p);
506 }
507
508 void uloop_run(void)
509 {
510 struct timeval tv;
511
512 uloop_setup_signals();
513 while(!uloop_cancelled)
514 {
515 gettimeofday(&tv, NULL);
516 uloop_process_timeouts(&tv);
517 if (uloop_cancelled)
518 break;
519
520 if (do_sigchld)
521 uloop_handle_processes();
522 uloop_run_events(uloop_get_next_timeout(&tv));
523 }
524 }
525
526 void uloop_done(void)
527 {
528 if (poll_fd < 0)
529 return;
530
531 close(poll_fd);
532 poll_fd = -1;
533
534 uloop_clear_timeouts();
535 uloop_clear_processes();
536 }