uloop: fix edge trigger handling on mac os x
[project/libubox.git] / uloop.c
1 /*
2 * uloop - event loop implementation
3 *
4 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include <sys/time.h>
19 #include <sys/types.h>
20
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <poll.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <stdbool.h>
29
30 #include "uloop.h"
31 #include "utils.h"
32
33 #ifdef USE_KQUEUE
34 #include <sys/event.h>
35 #endif
36 #ifdef USE_EPOLL
37 #include <sys/epoll.h>
38 #endif
39 #include <sys/wait.h>
40
41 struct uloop_fd_event {
42 struct uloop_fd *fd;
43 unsigned int events;
44 };
45
46 #define ULOOP_MAX_EVENTS 10
47
48 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
49 static struct list_head processes = LIST_HEAD_INIT(processes);
50
51 static int poll_fd = -1;
52 bool uloop_cancelled = false;
53 bool uloop_handle_sigchld = true;
54 static bool do_sigchld = false;
55
56 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
57 static int cur_fd, cur_nfds;
58
59 #ifdef USE_KQUEUE
60
61 int uloop_init(void)
62 {
63 struct timespec timeout = { 0, 0 };
64 struct kevent ev = {};
65
66 if (poll_fd >= 0)
67 return 0;
68
69 poll_fd = kqueue();
70 if (poll_fd < 0)
71 return -1;
72
73 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
74 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
75
76 return 0;
77 }
78
79
80 static uint16_t get_flags(unsigned int flags, unsigned int mask)
81 {
82 uint16_t kflags = 0;
83
84 if (!(flags & mask))
85 return EV_DELETE;
86
87 kflags = EV_ADD;
88 if (flags & ULOOP_EDGE_TRIGGER)
89 kflags |= EV_CLEAR;
90
91 return kflags;
92 }
93
94 static struct kevent events[ULOOP_MAX_EVENTS];
95
96 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
97 {
98 struct timespec timeout = { 0, 0 };
99 struct kevent ev[2];
100 int nev = 0;
101 unsigned int fl = 0;
102 unsigned int changed;
103 uint16_t kflags;
104
105 if (flags & ULOOP_EDGE_DEFER)
106 flags &= ~ULOOP_EDGE_TRIGGER;
107
108 changed = flags ^ fd->flags;
109 if (changed & ULOOP_EDGE_TRIGGER)
110 changed |= flags;
111
112 if (changed & ULOOP_READ) {
113 kflags = get_flags(flags, ULOOP_READ);
114 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
115 }
116
117 if (changed & ULOOP_WRITE) {
118 kflags = get_flags(flags, ULOOP_WRITE);
119 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
120 }
121
122 if (!flags)
123 fl |= EV_DELETE;
124
125 fd->flags = flags;
126 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
127 return -1;
128
129 return 0;
130 }
131
132 static int register_poll(struct uloop_fd *fd, unsigned int flags)
133 {
134 if (flags & ULOOP_EDGE_TRIGGER)
135 flags |= ULOOP_EDGE_DEFER;
136 else
137 flags &= ~ULOOP_EDGE_DEFER;
138
139 return register_kevent(fd, flags);
140 }
141
142 static int __uloop_fd_delete(struct uloop_fd *fd)
143 {
144 return register_poll(fd, 0);
145 }
146
147 static int uloop_fetch_events(int timeout)
148 {
149 struct timespec ts;
150 int nfds, n;
151
152 if (timeout >= 0) {
153 ts.tv_sec = timeout / 1000;
154 ts.tv_nsec = (timeout % 1000) * 1000000;
155 }
156
157 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
158 for (n = 0; n < nfds; n++) {
159 struct uloop_fd_event *cur = &cur_fds[n];
160 struct uloop_fd *u = events[n].udata;
161 unsigned int ev = 0;
162
163 cur->fd = u;
164 if (!u)
165 continue;
166
167 if (events[n].flags & EV_ERROR) {
168 u->error = true;
169 uloop_fd_delete(u);
170 }
171
172 if(events[n].filter == EVFILT_READ)
173 ev |= ULOOP_READ;
174 else if (events[n].filter == EVFILT_WRITE)
175 ev |= ULOOP_WRITE;
176
177 if (events[n].flags & EV_EOF)
178 u->eof = true;
179 else if (!ev)
180 cur->fd = NULL;
181
182 if (u->flags & ULOOP_EDGE_DEFER) {
183 u->flags &= ~ULOOP_EDGE_DEFER;
184 u->flags |= ULOOP_EDGE_TRIGGER;
185 register_kevent(u, u->flags);
186 }
187 }
188 return nfds;
189 }
190
191 #endif
192
193 #ifdef USE_EPOLL
194
195 /**
196 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
197 */
198 #ifndef EPOLLRDHUP
199 #define EPOLLRDHUP 0x2000
200 #endif
201
202 int uloop_init(void)
203 {
204 if (poll_fd >= 0)
205 return 0;
206
207 poll_fd = epoll_create(32);
208 if (poll_fd < 0)
209 return -1;
210
211 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
212 return 0;
213 }
214
215 static int register_poll(struct uloop_fd *fd, unsigned int flags)
216 {
217 struct epoll_event ev;
218 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
219
220 memset(&ev, 0, sizeof(struct epoll_event));
221
222 if (flags & ULOOP_READ)
223 ev.events |= EPOLLIN | EPOLLRDHUP;
224
225 if (flags & ULOOP_WRITE)
226 ev.events |= EPOLLOUT;
227
228 if (flags & ULOOP_EDGE_TRIGGER)
229 ev.events |= EPOLLET;
230
231 ev.data.fd = fd->fd;
232 ev.data.ptr = fd;
233
234 return epoll_ctl(poll_fd, op, fd->fd, &ev);
235 }
236
237 static struct epoll_event events[ULOOP_MAX_EVENTS];
238
239 static int __uloop_fd_delete(struct uloop_fd *sock)
240 {
241 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
242 }
243
244 static int uloop_fetch_events(int timeout)
245 {
246 int n, nfds;
247
248 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
249 for (n = 0; n < nfds; ++n) {
250 struct uloop_fd_event *cur = &cur_fds[n];
251 struct uloop_fd *u = events[n].data.ptr;
252 unsigned int ev = 0;
253
254 cur->fd = u;
255 if (!u)
256 continue;
257
258 if (events[n].events & (EPOLLERR|EPOLLHUP)) {
259 u->error = true;
260 uloop_fd_delete(u);
261 }
262
263 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP))) {
264 cur->fd = NULL;
265 continue;
266 }
267
268 if(events[n].events & EPOLLRDHUP)
269 u->eof = true;
270
271 if(events[n].events & EPOLLIN)
272 ev |= ULOOP_READ;
273
274 if(events[n].events & EPOLLOUT)
275 ev |= ULOOP_WRITE;
276
277 cur->events = ev;
278 }
279
280 return nfds;
281 }
282
283 #endif
284
285 static void uloop_run_events(int timeout)
286 {
287 struct uloop_fd_event *cur;
288 struct uloop_fd *fd;
289
290 if (!cur_nfds) {
291 cur_fd = 0;
292 cur_nfds = uloop_fetch_events(timeout);
293 if (cur_nfds < 0)
294 cur_nfds = 0;
295 }
296
297 while (cur_nfds > 0) {
298 cur = &cur_fds[cur_fd++];
299 cur_nfds--;
300
301 fd = cur->fd;
302 if (!fd)
303 continue;
304
305 if (!fd->cb)
306 continue;
307
308 fd->cb(fd, cur->events);
309 return;
310 }
311 }
312
313 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
314 {
315 unsigned int fl;
316 int ret;
317
318 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
319 return uloop_fd_delete(sock);
320
321 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
322 fl = fcntl(sock->fd, F_GETFL, 0);
323 fl |= O_NONBLOCK;
324 fcntl(sock->fd, F_SETFL, fl);
325 }
326
327 ret = register_poll(sock, flags);
328 if (ret < 0)
329 goto out;
330
331 sock->registered = true;
332 sock->eof = false;
333
334 out:
335 return ret;
336 }
337
338 int uloop_fd_delete(struct uloop_fd *fd)
339 {
340 int i;
341
342 if (!fd->registered)
343 return 0;
344
345 for (i = 0; i < cur_nfds; i++) {
346 if (cur_fds[cur_fd + i].fd != fd)
347 continue;
348
349 cur_fds[cur_fd + i].fd = NULL;
350 }
351 fd->registered = false;
352 return __uloop_fd_delete(fd);
353 }
354
355 static int tv_diff(struct timeval *t1, struct timeval *t2)
356 {
357 return
358 (t1->tv_sec - t2->tv_sec) * 1000 +
359 (t1->tv_usec - t2->tv_usec) / 1000;
360 }
361
362 int uloop_timeout_add(struct uloop_timeout *timeout)
363 {
364 struct uloop_timeout *tmp;
365 struct list_head *h = &timeouts;
366
367 if (timeout->pending)
368 return -1;
369
370 list_for_each_entry(tmp, &timeouts, list) {
371 if (tv_diff(&tmp->time, &timeout->time) > 0) {
372 h = &tmp->list;
373 break;
374 }
375 }
376
377 list_add_tail(&timeout->list, h);
378 timeout->pending = true;
379
380 return 0;
381 }
382
383 static void uloop_gettime(struct timeval *tv)
384 {
385 struct timespec ts;
386
387 clock_gettime(CLOCK_MONOTONIC, &ts);
388 tv->tv_sec = ts.tv_sec;
389 tv->tv_usec = ts.tv_nsec / 1000;
390 }
391
392 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
393 {
394 struct timeval *time = &timeout->time;
395
396 if (timeout->pending)
397 uloop_timeout_cancel(timeout);
398
399 uloop_gettime(&timeout->time);
400
401 time->tv_sec += msecs / 1000;
402 time->tv_usec += (msecs % 1000) * 1000;
403
404 if (time->tv_usec > 1000000) {
405 time->tv_sec++;
406 time->tv_usec %= 1000000;
407 }
408
409 return uloop_timeout_add(timeout);
410 }
411
412 int uloop_timeout_cancel(struct uloop_timeout *timeout)
413 {
414 if (!timeout->pending)
415 return -1;
416
417 list_del(&timeout->list);
418 timeout->pending = false;
419
420 return 0;
421 }
422
423 int uloop_timeout_remaining(struct uloop_timeout *timeout)
424 {
425 struct timeval now;
426
427 if (!timeout->pending)
428 return -1;
429
430 uloop_gettime(&now);
431
432 return tv_diff(&timeout->time, &now);
433 }
434
435 int uloop_process_add(struct uloop_process *p)
436 {
437 struct uloop_process *tmp;
438 struct list_head *h = &processes;
439
440 if (p->pending)
441 return -1;
442
443 list_for_each_entry(tmp, &processes, list) {
444 if (tmp->pid > p->pid) {
445 h = &tmp->list;
446 break;
447 }
448 }
449
450 list_add_tail(&p->list, h);
451 p->pending = true;
452
453 return 0;
454 }
455
456 int uloop_process_delete(struct uloop_process *p)
457 {
458 if (!p->pending)
459 return -1;
460
461 list_del(&p->list);
462 p->pending = false;
463
464 return 0;
465 }
466
467 static void uloop_handle_processes(void)
468 {
469 struct uloop_process *p, *tmp;
470 pid_t pid;
471 int ret;
472
473 do_sigchld = false;
474
475 while (1) {
476 pid = waitpid(-1, &ret, WNOHANG);
477 if (pid <= 0)
478 return;
479
480 list_for_each_entry_safe(p, tmp, &processes, list) {
481 if (p->pid < pid)
482 continue;
483
484 if (p->pid > pid)
485 break;
486
487 uloop_process_delete(p);
488 p->cb(p, ret);
489 }
490 }
491
492 }
493
494 static void uloop_handle_sigint(int signo)
495 {
496 uloop_cancelled = true;
497 }
498
499 static void uloop_sigchld(int signo)
500 {
501 do_sigchld = true;
502 }
503
504 static void uloop_setup_signals(void)
505 {
506 struct sigaction s;
507
508 memset(&s, 0, sizeof(struct sigaction));
509 s.sa_handler = uloop_handle_sigint;
510 s.sa_flags = 0;
511 sigaction(SIGINT, &s, NULL);
512
513 if (uloop_handle_sigchld) {
514 s.sa_handler = uloop_sigchld;
515 sigaction(SIGCHLD, &s, NULL);
516 }
517 }
518
519 static int uloop_get_next_timeout(struct timeval *tv)
520 {
521 struct uloop_timeout *timeout;
522 int diff;
523
524 if (list_empty(&timeouts))
525 return -1;
526
527 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
528 diff = tv_diff(&timeout->time, tv);
529 if (diff < 0)
530 return 0;
531
532 return diff;
533 }
534
535 static void uloop_process_timeouts(struct timeval *tv)
536 {
537 struct uloop_timeout *t;
538
539 while (!list_empty(&timeouts)) {
540 t = list_first_entry(&timeouts, struct uloop_timeout, list);
541
542 if (tv_diff(&t->time, tv) > 0)
543 break;
544
545 uloop_timeout_cancel(t);
546 if (t->cb)
547 t->cb(t);
548 }
549 }
550
551 static void uloop_clear_timeouts(void)
552 {
553 struct uloop_timeout *t, *tmp;
554
555 list_for_each_entry_safe(t, tmp, &timeouts, list)
556 uloop_timeout_cancel(t);
557 }
558
559 static void uloop_clear_processes(void)
560 {
561 struct uloop_process *p, *tmp;
562
563 list_for_each_entry_safe(p, tmp, &processes, list)
564 uloop_process_delete(p);
565 }
566
567 void uloop_run(void)
568 {
569 struct timeval tv;
570
571 uloop_setup_signals();
572 while(!uloop_cancelled)
573 {
574 uloop_gettime(&tv);
575 uloop_process_timeouts(&tv);
576 if (uloop_cancelled)
577 break;
578
579 if (do_sigchld)
580 uloop_handle_processes();
581 uloop_run_events(uloop_get_next_timeout(&tv));
582 }
583 }
584
585 void uloop_done(void)
586 {
587 if (poll_fd < 0)
588 return;
589
590 close(poll_fd);
591 poll_fd = -1;
592
593 uloop_clear_timeouts();
594 uloop_clear_processes();
595 }