lua: add gc/delete support for processes
[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 struct uloop_fd_stack {
47         struct uloop_fd_stack *next;
48         struct uloop_fd *fd;
49         unsigned int events;
50 };
51
52 static struct uloop_fd_stack *fd_stack = NULL;
53
54 #define ULOOP_MAX_EVENTS 10
55
56 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
57 static struct list_head processes = LIST_HEAD_INIT(processes);
58
59 static int poll_fd = -1;
60 bool uloop_cancelled = false;
61 static bool do_sigchld = false;
62
63 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
64 static int cur_fd, cur_nfds;
65
66 #ifdef USE_KQUEUE
67
68 int uloop_init(void)
69 {
70         struct timespec timeout = { 0, 0 };
71         struct kevent ev = {};
72
73         if (poll_fd >= 0)
74                 return 0;
75
76         poll_fd = kqueue();
77         if (poll_fd < 0)
78                 return -1;
79
80         EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
81         kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
82
83         return 0;
84 }
85
86
87 static uint16_t get_flags(unsigned int flags, unsigned int mask)
88 {
89         uint16_t kflags = 0;
90
91         if (!(flags & mask))
92                 return EV_DELETE;
93
94         kflags = EV_ADD;
95         if (flags & ULOOP_EDGE_TRIGGER)
96                 kflags |= EV_CLEAR;
97
98         return kflags;
99 }
100
101 static struct kevent events[ULOOP_MAX_EVENTS];
102
103 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
104 {
105         struct timespec timeout = { 0, 0 };
106         struct kevent ev[2];
107         int nev = 0;
108         unsigned int fl = 0;
109         unsigned int changed;
110         uint16_t kflags;
111
112         if (flags & ULOOP_EDGE_DEFER)
113                 flags &= ~ULOOP_EDGE_TRIGGER;
114
115         changed = flags ^ fd->flags;
116         if (changed & ULOOP_EDGE_TRIGGER)
117                 changed |= flags;
118
119         if (changed & ULOOP_READ) {
120                 kflags = get_flags(flags, ULOOP_READ);
121                 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
122         }
123
124         if (changed & ULOOP_WRITE) {
125                 kflags = get_flags(flags, ULOOP_WRITE);
126                 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
127         }
128
129         if (!flags)
130                 fl |= EV_DELETE;
131
132         fd->flags = flags;
133         if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
134                 return -1;
135
136         return 0;
137 }
138
139 static int register_poll(struct uloop_fd *fd, unsigned int flags)
140 {
141         if (flags & ULOOP_EDGE_TRIGGER)
142                 flags |= ULOOP_EDGE_DEFER;
143         else
144                 flags &= ~ULOOP_EDGE_DEFER;
145
146         return register_kevent(fd, flags);
147 }
148
149 static int __uloop_fd_delete(struct uloop_fd *fd)
150 {
151         return register_poll(fd, 0);
152 }
153
154 static int uloop_fetch_events(int timeout)
155 {
156         struct timespec ts;
157         int nfds, n;
158
159         if (timeout >= 0) {
160                 ts.tv_sec = timeout / 1000;
161                 ts.tv_nsec = (timeout % 1000) * 1000000;
162         }
163
164         nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
165         for (n = 0; n < nfds; n++) {
166                 struct uloop_fd_event *cur = &cur_fds[n];
167                 struct uloop_fd *u = events[n].udata;
168                 unsigned int ev = 0;
169
170                 cur->fd = u;
171                 if (!u)
172                         continue;
173
174                 if (events[n].flags & EV_ERROR) {
175                         u->error = true;
176                         if (!(u->flags & ULOOP_ERROR_CB))
177                                 uloop_fd_delete(u);
178                 }
179
180                 if(events[n].filter == EVFILT_READ)
181                         ev |= ULOOP_READ;
182                 else if (events[n].filter == EVFILT_WRITE)
183                         ev |= ULOOP_WRITE;
184
185                 if (events[n].flags & EV_EOF)
186                         u->eof = true;
187                 else if (!ev)
188                         cur->fd = NULL;
189
190                 cur->events = ev;
191                 if (u->flags & ULOOP_EDGE_DEFER) {
192                         u->flags &= ~ULOOP_EDGE_DEFER;
193                         u->flags |= ULOOP_EDGE_TRIGGER;
194                         register_kevent(u, u->flags);
195                 }
196         }
197         return nfds;
198 }
199
200 #endif
201
202 #ifdef USE_EPOLL
203
204 /**
205  * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
206  */
207 #ifndef EPOLLRDHUP
208 #define EPOLLRDHUP 0x2000
209 #endif
210
211 int uloop_init(void)
212 {
213         if (poll_fd >= 0)
214                 return 0;
215
216         poll_fd = epoll_create(32);
217         if (poll_fd < 0)
218                 return -1;
219
220         fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
221         return 0;
222 }
223
224 static int register_poll(struct uloop_fd *fd, unsigned int flags)
225 {
226         struct epoll_event ev;
227         int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
228
229         memset(&ev, 0, sizeof(struct epoll_event));
230
231         if (flags & ULOOP_READ)
232                 ev.events |= EPOLLIN | EPOLLRDHUP;
233
234         if (flags & ULOOP_WRITE)
235                 ev.events |= EPOLLOUT;
236
237         if (flags & ULOOP_EDGE_TRIGGER)
238                 ev.events |= EPOLLET;
239
240         ev.data.fd = fd->fd;
241         ev.data.ptr = fd;
242         fd->flags = flags;
243
244         return epoll_ctl(poll_fd, op, fd->fd, &ev);
245 }
246
247 static struct epoll_event events[ULOOP_MAX_EVENTS];
248
249 static int __uloop_fd_delete(struct uloop_fd *sock)
250 {
251         sock->flags = 0;
252         return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
253 }
254
255 static int uloop_fetch_events(int timeout)
256 {
257         int n, nfds;
258
259         nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
260         for (n = 0; n < nfds; ++n) {
261                 struct uloop_fd_event *cur = &cur_fds[n];
262                 struct uloop_fd *u = events[n].data.ptr;
263                 unsigned int ev = 0;
264
265                 cur->fd = u;
266                 if (!u)
267                         continue;
268
269                 if (events[n].events & (EPOLLERR|EPOLLHUP)) {
270                         u->error = true;
271                         if (!(u->flags & ULOOP_ERROR_CB))
272                                 uloop_fd_delete(u);
273                 }
274
275                 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP))) {
276                         cur->fd = NULL;
277                         continue;
278                 }
279
280                 if(events[n].events & EPOLLRDHUP)
281                         u->eof = true;
282
283                 if(events[n].events & EPOLLIN)
284                         ev |= ULOOP_READ;
285
286                 if(events[n].events & EPOLLOUT)
287                         ev |= ULOOP_WRITE;
288
289                 cur->events = ev;
290         }
291
292         return nfds;
293 }
294
295 #endif
296
297 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
298 {
299         struct uloop_fd_stack *cur;
300
301         /*
302          * Do not buffer events for level-triggered fds, they will keep firing.
303          * Caller needs to take care of recursion issues.
304          */
305         if (!(fd->flags & ULOOP_EDGE_TRIGGER))
306                 return false;
307
308         for (cur = fd_stack; cur; cur = cur->next) {
309                 if (cur->fd != fd)
310                         continue;
311
312                 if (events < 0)
313                         cur->fd = NULL;
314                 else
315                         cur->events |= events | ULOOP_EVENT_BUFFERED;
316
317                 return true;
318         }
319
320         return false;
321 }
322
323 static void uloop_run_events(int timeout)
324 {
325         struct uloop_fd_event *cur;
326         struct uloop_fd *fd;
327
328         if (!cur_nfds) {
329                 cur_fd = 0;
330                 cur_nfds = uloop_fetch_events(timeout);
331                 if (cur_nfds < 0)
332                         cur_nfds = 0;
333         }
334
335         while (cur_nfds > 0) {
336                 struct uloop_fd_stack stack_cur;
337                 unsigned int events;
338
339                 cur = &cur_fds[cur_fd++];
340                 cur_nfds--;
341
342                 fd = cur->fd;
343                 events = cur->events;
344                 if (!fd)
345                         continue;
346
347                 if (!fd->cb)
348                         continue;
349
350                 if (uloop_fd_stack_event(fd, cur->events))
351                         continue;
352
353                 stack_cur.next = fd_stack;
354                 stack_cur.fd = fd;
355                 fd_stack = &stack_cur;
356                 do {
357                         stack_cur.events = 0;
358                         fd->cb(fd, events);
359                         events = stack_cur.events & ULOOP_EVENT_MASK;
360                 } while (stack_cur.fd && events);
361                 fd_stack = stack_cur.next;
362
363                 return;
364         }
365 }
366
367 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
368 {
369         unsigned int fl;
370         int ret;
371
372         if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
373                 return uloop_fd_delete(sock);
374
375         if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
376                 fl = fcntl(sock->fd, F_GETFL, 0);
377                 fl |= O_NONBLOCK;
378                 fcntl(sock->fd, F_SETFL, fl);
379         }
380
381         ret = register_poll(sock, flags);
382         if (ret < 0)
383                 goto out;
384
385         sock->registered = true;
386         sock->eof = false;
387         sock->error = false;
388
389 out:
390         return ret;
391 }
392
393 int uloop_fd_delete(struct uloop_fd *fd)
394 {
395         int i;
396
397         for (i = 0; i < cur_nfds; i++) {
398                 if (cur_fds[cur_fd + i].fd != fd)
399                         continue;
400
401                 cur_fds[cur_fd + i].fd = NULL;
402         }
403
404         if (!fd->registered)
405                 return 0;
406
407         fd->registered = false;
408         uloop_fd_stack_event(fd, -1);
409         return __uloop_fd_delete(fd);
410 }
411
412 static int tv_diff(struct timeval *t1, struct timeval *t2)
413 {
414         return
415                 (t1->tv_sec - t2->tv_sec) * 1000 +
416                 (t1->tv_usec - t2->tv_usec) / 1000;
417 }
418
419 int uloop_timeout_add(struct uloop_timeout *timeout)
420 {
421         struct uloop_timeout *tmp;
422         struct list_head *h = &timeouts;
423
424         if (timeout->pending)
425                 return -1;
426
427         list_for_each_entry(tmp, &timeouts, list) {
428                 if (tv_diff(&tmp->time, &timeout->time) > 0) {
429                         h = &tmp->list;
430                         break;
431                 }
432         }
433
434         list_add_tail(&timeout->list, h);
435         timeout->pending = true;
436
437         return 0;
438 }
439
440 static void uloop_gettime(struct timeval *tv)
441 {
442         struct timespec ts;
443
444         clock_gettime(CLOCK_MONOTONIC, &ts);
445         tv->tv_sec = ts.tv_sec;
446         tv->tv_usec = ts.tv_nsec / 1000;
447 }
448
449 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
450 {
451         struct timeval *time = &timeout->time;
452
453         if (timeout->pending)
454                 uloop_timeout_cancel(timeout);
455
456         uloop_gettime(time);
457
458         time->tv_sec += msecs / 1000;
459         time->tv_usec += (msecs % 1000) * 1000;
460
461         if (time->tv_usec > 1000000) {
462                 time->tv_sec++;
463                 time->tv_usec -= 1000000;
464         }
465
466         return uloop_timeout_add(timeout);
467 }
468
469 int uloop_timeout_cancel(struct uloop_timeout *timeout)
470 {
471         if (!timeout->pending)
472                 return -1;
473
474         list_del(&timeout->list);
475         timeout->pending = false;
476
477         return 0;
478 }
479
480 int uloop_timeout_remaining(struct uloop_timeout *timeout)
481 {
482         struct timeval now;
483
484         if (!timeout->pending)
485                 return -1;
486
487         uloop_gettime(&now);
488
489         return tv_diff(&timeout->time, &now);
490 }
491
492 int uloop_process_add(struct uloop_process *p)
493 {
494         struct uloop_process *tmp;
495         struct list_head *h = &processes;
496
497         if (p->pending)
498                 return -1;
499
500         list_for_each_entry(tmp, &processes, list) {
501                 if (tmp->pid > p->pid) {
502                         h = &tmp->list;
503                         break;
504                 }
505         }
506
507         list_add_tail(&p->list, h);
508         p->pending = true;
509
510         return 0;
511 }
512
513 int uloop_process_delete(struct uloop_process *p)
514 {
515         if (!p->pending)
516                 return -1;
517
518         list_del(&p->list);
519         p->pending = false;
520
521         return 0;
522 }
523
524 static void uloop_handle_processes(void)
525 {
526         struct uloop_process *p, *tmp;
527         pid_t pid;
528         int ret;
529
530         do_sigchld = false;
531
532         while (1) {
533                 pid = waitpid(-1, &ret, WNOHANG);
534                 if (pid <= 0)
535                         return;
536
537                 list_for_each_entry_safe(p, tmp, &processes, list) {
538                         if (p->pid < pid)
539                                 continue;
540
541                         if (p->pid > pid)
542                                 break;
543
544                         uloop_process_delete(p);
545                         p->cb(p, ret);
546                 }
547         }
548
549 }
550
551 static void uloop_handle_sigint(int signo)
552 {
553         uloop_cancelled = true;
554 }
555
556 static void uloop_sigchld(int signo)
557 {
558         do_sigchld = true;
559 }
560
561 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
562 {
563         struct sigaction s;
564         struct sigaction *act;
565
566         act = NULL;
567         sigaction(signum, NULL, &s);
568
569         if (add) {
570                 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
571                         memcpy(old, &s, sizeof(struct sigaction));
572                         s.sa_handler = handler;
573                         s.sa_flags = 0;
574                         act = &s;
575                 }
576         }
577         else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
578                         act = old;
579         }
580
581         if (act != NULL)
582                 sigaction(signum, act, NULL);
583 }
584
585 static void uloop_ignore_signal(int signum, bool ignore)
586 {
587         struct sigaction s;
588         void *new_handler = NULL;
589
590         sigaction(signum, NULL, &s);
591
592         if (ignore) {
593                 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
594                         new_handler = SIG_IGN;
595         } else {
596                 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
597                         new_handler = SIG_DFL;
598         }
599
600         if (new_handler) {
601                 s.sa_handler = new_handler;
602                 s.sa_flags = 0;
603                 sigaction(signum, &s, NULL);
604         }
605 }
606
607 static void uloop_setup_signals(bool add)
608 {
609         static struct sigaction old_sigint, old_sigchld, old_sigterm;
610
611         uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
612         uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
613         uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
614
615         uloop_ignore_signal(SIGPIPE, add);
616 }
617
618 static int uloop_get_next_timeout(struct timeval *tv)
619 {
620         struct uloop_timeout *timeout;
621         int diff;
622
623         if (list_empty(&timeouts))
624                 return -1;
625
626         timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
627         diff = tv_diff(&timeout->time, tv);
628         if (diff < 0)
629                 return 0;
630
631         return diff;
632 }
633
634 static void uloop_process_timeouts(struct timeval *tv)
635 {
636         struct uloop_timeout *t;
637
638         while (!list_empty(&timeouts)) {
639                 t = list_first_entry(&timeouts, struct uloop_timeout, list);
640
641                 if (tv_diff(&t->time, tv) > 0)
642                         break;
643
644                 uloop_timeout_cancel(t);
645                 if (t->cb)
646                         t->cb(t);
647         }
648 }
649
650 static void uloop_clear_timeouts(void)
651 {
652         struct uloop_timeout *t, *tmp;
653
654         list_for_each_entry_safe(t, tmp, &timeouts, list)
655                 uloop_timeout_cancel(t);
656 }
657
658 static void uloop_clear_processes(void)
659 {
660         struct uloop_process *p, *tmp;
661
662         list_for_each_entry_safe(p, tmp, &processes, list)
663                 uloop_process_delete(p);
664 }
665
666 void uloop_run(void)
667 {
668         static int recursive_calls = 0;
669         struct timeval tv;
670
671         /*
672          * Handlers are only updated for the first call to uloop_run() (and restored
673          * when this call is done).
674          */
675         if (!recursive_calls++)
676                 uloop_setup_signals(true);
677
678         uloop_cancelled = false;
679         while(!uloop_cancelled)
680         {
681                 uloop_gettime(&tv);
682                 uloop_process_timeouts(&tv);
683
684                 if (do_sigchld)
685                         uloop_handle_processes();
686
687                 if (uloop_cancelled)
688                         break;
689
690                 uloop_gettime(&tv);
691                 uloop_run_events(uloop_get_next_timeout(&tv));
692         }
693
694         if (!--recursive_calls)
695                 uloop_setup_signals(false);
696 }
697
698 void uloop_done(void)
699 {
700         if (poll_fd < 0)
701                 return;
702
703         close(poll_fd);
704         poll_fd = -1;
705
706         uloop_clear_timeouts();
707         uloop_clear_processes();
708 }