2 * uloop - event loop implementation
4 * Copyright (C) 2010-2016 Felix Fietkau <nbd@openwrt.org>
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.
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.
19 #include <sys/types.h>
34 #include <sys/event.h>
37 #include <sys/epoll.h>
41 struct uloop_fd_event {
46 struct uloop_fd_stack {
47 struct uloop_fd_stack *next;
52 static struct uloop_fd_stack *fd_stack = NULL;
54 #define ULOOP_MAX_EVENTS 10
56 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
57 static struct list_head processes = LIST_HEAD_INIT(processes);
59 static int poll_fd = -1;
60 bool uloop_cancelled = false;
61 bool uloop_handle_sigchld = true;
62 static int uloop_status = 0;
63 static bool do_sigchld = false;
65 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
66 static int cur_fd, cur_nfds;
67 static int uloop_run_depth = 0;
69 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
72 #include "uloop-kqueue.c"
76 #include "uloop-epoll.c"
79 static void waker_consume(struct uloop_fd *fd, unsigned int events)
83 while (read(fd->fd, buf, 4) > 0)
87 static int waker_pipe = -1;
88 static struct uloop_fd waker_fd = {
93 static void waker_init_fd(int fd)
95 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
96 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
99 static int waker_init(void)
109 waker_init_fd(fds[0]);
110 waker_init_fd(fds[1]);
113 waker_fd.fd = fds[0];
114 waker_fd.cb = waker_consume;
115 uloop_fd_add(&waker_fd, ULOOP_READ);
120 static void uloop_setup_signals(bool add);
124 if (uloop_init_pollfd() < 0)
127 if (waker_init() < 0) {
132 uloop_setup_signals(true);
137 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
139 struct uloop_fd_stack *cur;
142 * Do not buffer events for level-triggered fds, they will keep firing.
143 * Caller needs to take care of recursion issues.
145 if (!(fd->flags & ULOOP_EDGE_TRIGGER))
148 for (cur = fd_stack; cur; cur = cur->next) {
155 cur->events |= events | ULOOP_EVENT_BUFFERED;
163 static void uloop_run_events(int timeout)
165 struct uloop_fd_event *cur;
170 cur_nfds = uloop_fetch_events(timeout);
175 while (cur_nfds > 0) {
176 struct uloop_fd_stack stack_cur;
179 cur = &cur_fds[cur_fd++];
183 events = cur->events;
190 if (uloop_fd_stack_event(fd, cur->events))
193 stack_cur.next = fd_stack;
195 fd_stack = &stack_cur;
197 stack_cur.events = 0;
199 events = stack_cur.events & ULOOP_EVENT_MASK;
200 } while (stack_cur.fd && events);
201 fd_stack = stack_cur.next;
207 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
212 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
213 return uloop_fd_delete(sock);
215 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
216 fl = fcntl(sock->fd, F_GETFL, 0);
218 fcntl(sock->fd, F_SETFL, fl);
221 ret = register_poll(sock, flags);
225 sock->registered = true;
233 int uloop_fd_delete(struct uloop_fd *fd)
237 for (i = 0; i < cur_nfds; i++) {
238 if (cur_fds[cur_fd + i].fd != fd)
241 cur_fds[cur_fd + i].fd = NULL;
247 fd->registered = false;
248 uloop_fd_stack_event(fd, -1);
249 return __uloop_fd_delete(fd);
252 static int tv_diff(struct timeval *t1, struct timeval *t2)
255 (t1->tv_sec - t2->tv_sec) * 1000 +
256 (t1->tv_usec - t2->tv_usec) / 1000;
259 int uloop_timeout_add(struct uloop_timeout *timeout)
261 struct uloop_timeout *tmp;
262 struct list_head *h = &timeouts;
264 if (timeout->pending)
267 list_for_each_entry(tmp, &timeouts, list) {
268 if (tv_diff(&tmp->time, &timeout->time) > 0) {
274 list_add_tail(&timeout->list, h);
275 timeout->pending = true;
280 static void uloop_gettime(struct timeval *tv)
284 clock_gettime(CLOCK_MONOTONIC, &ts);
285 tv->tv_sec = ts.tv_sec;
286 tv->tv_usec = ts.tv_nsec / 1000;
289 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
291 struct timeval *time = &timeout->time;
293 if (timeout->pending)
294 uloop_timeout_cancel(timeout);
298 time->tv_sec += msecs / 1000;
299 time->tv_usec += (msecs % 1000) * 1000;
301 if (time->tv_usec > 1000000) {
303 time->tv_usec -= 1000000;
306 return uloop_timeout_add(timeout);
309 int uloop_timeout_cancel(struct uloop_timeout *timeout)
311 if (!timeout->pending)
314 list_del(&timeout->list);
315 timeout->pending = false;
320 int uloop_timeout_remaining(struct uloop_timeout *timeout)
324 if (!timeout->pending)
329 return tv_diff(&timeout->time, &now);
332 int uloop_process_add(struct uloop_process *p)
334 struct uloop_process *tmp;
335 struct list_head *h = &processes;
340 list_for_each_entry(tmp, &processes, list) {
341 if (tmp->pid > p->pid) {
347 list_add_tail(&p->list, h);
353 int uloop_process_delete(struct uloop_process *p)
364 static void uloop_handle_processes(void)
366 struct uloop_process *p, *tmp;
373 pid = waitpid(-1, &ret, WNOHANG);
374 if (pid < 0 && errno == EINTR)
380 list_for_each_entry_safe(p, tmp, &processes, list) {
387 uloop_process_delete(p);
394 static void uloop_signal_wake(void)
397 if (write(waker_pipe, "w", 1) < 0) {
405 static void uloop_handle_sigint(int signo)
407 uloop_status = signo;
408 uloop_cancelled = true;
412 static void uloop_sigchld(int signo)
418 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
421 struct sigaction *act;
424 sigaction(signum, NULL, &s);
427 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
428 memcpy(old, &s, sizeof(struct sigaction));
429 s.sa_handler = handler;
434 else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
439 sigaction(signum, act, NULL);
442 static void uloop_ignore_signal(int signum, bool ignore)
445 void *new_handler = NULL;
447 sigaction(signum, NULL, &s);
450 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
451 new_handler = SIG_IGN;
453 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
454 new_handler = SIG_DFL;
458 s.sa_handler = new_handler;
460 sigaction(signum, &s, NULL);
464 static void uloop_setup_signals(bool add)
466 static struct sigaction old_sigint, old_sigchld, old_sigterm;
468 uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
469 uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
471 if (uloop_handle_sigchld)
472 uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
474 uloop_ignore_signal(SIGPIPE, add);
477 static int uloop_get_next_timeout(struct timeval *tv)
479 struct uloop_timeout *timeout;
482 if (list_empty(&timeouts))
485 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
486 diff = tv_diff(&timeout->time, tv);
493 static void uloop_process_timeouts(struct timeval *tv)
495 struct uloop_timeout *t;
497 while (!list_empty(&timeouts)) {
498 t = list_first_entry(&timeouts, struct uloop_timeout, list);
500 if (tv_diff(&t->time, tv) > 0)
503 uloop_timeout_cancel(t);
509 static void uloop_clear_timeouts(void)
511 struct uloop_timeout *t, *tmp;
513 list_for_each_entry_safe(t, tmp, &timeouts, list)
514 uloop_timeout_cancel(t);
517 static void uloop_clear_processes(void)
519 struct uloop_process *p, *tmp;
521 list_for_each_entry_safe(p, tmp, &processes, list)
522 uloop_process_delete(p);
525 bool uloop_cancelling(void)
527 return uloop_run_depth > 0 && uloop_cancelled;
530 int uloop_run_timeout(int timeout)
538 uloop_cancelled = false;
539 while (!uloop_cancelled)
542 uloop_process_timeouts(&tv);
545 uloop_handle_processes();
552 next_time = uloop_get_next_timeout(&tv);
553 if (timeout >= 0 && timeout < next_time)
555 uloop_run_events(next_time);
563 void uloop_done(void)
565 uloop_setup_signals(false);
572 if (waker_pipe >= 0) {
573 uloop_fd_delete(&waker_fd);
579 uloop_clear_timeouts();
580 uloop_clear_processes();