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 static bool do_sigchld = false;
63 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
64 static int cur_fd, cur_nfds;
67 #include "uloop-kqueue.c"
73 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
76 #define EPOLLRDHUP 0x2000
84 poll_fd = epoll_create(32);
88 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
92 static int register_poll(struct uloop_fd *fd, unsigned int flags)
94 struct epoll_event ev;
95 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
97 memset(&ev, 0, sizeof(struct epoll_event));
99 if (flags & ULOOP_READ)
100 ev.events |= EPOLLIN | EPOLLRDHUP;
102 if (flags & ULOOP_WRITE)
103 ev.events |= EPOLLOUT;
105 if (flags & ULOOP_EDGE_TRIGGER)
106 ev.events |= EPOLLET;
112 return epoll_ctl(poll_fd, op, fd->fd, &ev);
115 static struct epoll_event events[ULOOP_MAX_EVENTS];
117 static int __uloop_fd_delete(struct uloop_fd *sock)
120 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
123 static int uloop_fetch_events(int timeout)
127 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
128 for (n = 0; n < nfds; ++n) {
129 struct uloop_fd_event *cur = &cur_fds[n];
130 struct uloop_fd *u = events[n].data.ptr;
137 if (events[n].events & (EPOLLERR|EPOLLHUP)) {
139 if (!(u->flags & ULOOP_ERROR_CB))
143 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP))) {
148 if(events[n].events & EPOLLRDHUP)
151 if(events[n].events & EPOLLIN)
154 if(events[n].events & EPOLLOUT)
165 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
167 struct uloop_fd_stack *cur;
170 * Do not buffer events for level-triggered fds, they will keep firing.
171 * Caller needs to take care of recursion issues.
173 if (!(fd->flags & ULOOP_EDGE_TRIGGER))
176 for (cur = fd_stack; cur; cur = cur->next) {
183 cur->events |= events | ULOOP_EVENT_BUFFERED;
191 static void uloop_run_events(int timeout)
193 struct uloop_fd_event *cur;
198 cur_nfds = uloop_fetch_events(timeout);
203 while (cur_nfds > 0) {
204 struct uloop_fd_stack stack_cur;
207 cur = &cur_fds[cur_fd++];
211 events = cur->events;
218 if (uloop_fd_stack_event(fd, cur->events))
221 stack_cur.next = fd_stack;
223 fd_stack = &stack_cur;
225 stack_cur.events = 0;
227 events = stack_cur.events & ULOOP_EVENT_MASK;
228 } while (stack_cur.fd && events);
229 fd_stack = stack_cur.next;
235 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
240 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
241 return uloop_fd_delete(sock);
243 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
244 fl = fcntl(sock->fd, F_GETFL, 0);
246 fcntl(sock->fd, F_SETFL, fl);
249 ret = register_poll(sock, flags);
253 sock->registered = true;
261 int uloop_fd_delete(struct uloop_fd *fd)
265 for (i = 0; i < cur_nfds; i++) {
266 if (cur_fds[cur_fd + i].fd != fd)
269 cur_fds[cur_fd + i].fd = NULL;
275 fd->registered = false;
276 uloop_fd_stack_event(fd, -1);
277 return __uloop_fd_delete(fd);
280 static int tv_diff(struct timeval *t1, struct timeval *t2)
283 (t1->tv_sec - t2->tv_sec) * 1000 +
284 (t1->tv_usec - t2->tv_usec) / 1000;
287 int uloop_timeout_add(struct uloop_timeout *timeout)
289 struct uloop_timeout *tmp;
290 struct list_head *h = &timeouts;
292 if (timeout->pending)
295 list_for_each_entry(tmp, &timeouts, list) {
296 if (tv_diff(&tmp->time, &timeout->time) > 0) {
302 list_add_tail(&timeout->list, h);
303 timeout->pending = true;
308 static void uloop_gettime(struct timeval *tv)
312 clock_gettime(CLOCK_MONOTONIC, &ts);
313 tv->tv_sec = ts.tv_sec;
314 tv->tv_usec = ts.tv_nsec / 1000;
317 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
319 struct timeval *time = &timeout->time;
321 if (timeout->pending)
322 uloop_timeout_cancel(timeout);
326 time->tv_sec += msecs / 1000;
327 time->tv_usec += (msecs % 1000) * 1000;
329 if (time->tv_usec > 1000000) {
331 time->tv_usec -= 1000000;
334 return uloop_timeout_add(timeout);
337 int uloop_timeout_cancel(struct uloop_timeout *timeout)
339 if (!timeout->pending)
342 list_del(&timeout->list);
343 timeout->pending = false;
348 int uloop_timeout_remaining(struct uloop_timeout *timeout)
352 if (!timeout->pending)
357 return tv_diff(&timeout->time, &now);
360 int uloop_process_add(struct uloop_process *p)
362 struct uloop_process *tmp;
363 struct list_head *h = &processes;
368 list_for_each_entry(tmp, &processes, list) {
369 if (tmp->pid > p->pid) {
375 list_add_tail(&p->list, h);
381 int uloop_process_delete(struct uloop_process *p)
392 static void uloop_handle_processes(void)
394 struct uloop_process *p, *tmp;
401 pid = waitpid(-1, &ret, WNOHANG);
405 list_for_each_entry_safe(p, tmp, &processes, list) {
412 uloop_process_delete(p);
419 static void uloop_handle_sigint(int signo)
421 uloop_cancelled = true;
424 static void uloop_sigchld(int signo)
429 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
432 struct sigaction *act;
435 sigaction(signum, NULL, &s);
438 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
439 memcpy(old, &s, sizeof(struct sigaction));
440 s.sa_handler = handler;
445 else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
450 sigaction(signum, act, NULL);
453 static void uloop_ignore_signal(int signum, bool ignore)
456 void *new_handler = NULL;
458 sigaction(signum, NULL, &s);
461 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
462 new_handler = SIG_IGN;
464 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
465 new_handler = SIG_DFL;
469 s.sa_handler = new_handler;
471 sigaction(signum, &s, NULL);
475 static void uloop_setup_signals(bool add)
477 static struct sigaction old_sigint, old_sigchld, old_sigterm;
479 uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
480 uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
481 uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
483 uloop_ignore_signal(SIGPIPE, add);
486 static int uloop_get_next_timeout(struct timeval *tv)
488 struct uloop_timeout *timeout;
491 if (list_empty(&timeouts))
494 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
495 diff = tv_diff(&timeout->time, tv);
502 static void uloop_process_timeouts(struct timeval *tv)
504 struct uloop_timeout *t;
506 while (!list_empty(&timeouts)) {
507 t = list_first_entry(&timeouts, struct uloop_timeout, list);
509 if (tv_diff(&t->time, tv) > 0)
512 uloop_timeout_cancel(t);
518 static void uloop_clear_timeouts(void)
520 struct uloop_timeout *t, *tmp;
522 list_for_each_entry_safe(t, tmp, &timeouts, list)
523 uloop_timeout_cancel(t);
526 static void uloop_clear_processes(void)
528 struct uloop_process *p, *tmp;
530 list_for_each_entry_safe(p, tmp, &processes, list)
531 uloop_process_delete(p);
536 static int recursive_calls = 0;
540 * Handlers are only updated for the first call to uloop_run() (and restored
541 * when this call is done).
543 if (!recursive_calls++)
544 uloop_setup_signals(true);
546 uloop_cancelled = false;
547 while(!uloop_cancelled)
550 uloop_process_timeouts(&tv);
553 uloop_handle_processes();
559 uloop_run_events(uloop_get_next_timeout(&tv));
562 if (!--recursive_calls)
563 uloop_setup_signals(false);
566 void uloop_done(void)
574 uloop_clear_timeouts();
575 uloop_clear_processes();