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 signal_fd = -1;
60 static int poll_fd = -1;
61 bool uloop_cancelled = false;
62 static bool do_sigchld = false;
64 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
65 static int cur_fd, cur_nfds;
67 static void uloop_handle_signal(int signo);
70 #include "uloop-kqueue.c"
74 #include "uloop-epoll.c"
77 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
79 struct uloop_fd_stack *cur;
82 * Do not buffer events for level-triggered fds, they will keep firing.
83 * Caller needs to take care of recursion issues.
85 if (!(fd->flags & ULOOP_EDGE_TRIGGER))
88 for (cur = fd_stack; cur; cur = cur->next) {
95 cur->events |= events | ULOOP_EVENT_BUFFERED;
103 static void uloop_run_events(int timeout)
105 struct uloop_fd_event *cur;
110 cur_nfds = uloop_fetch_events(timeout);
115 while (cur_nfds > 0) {
116 struct uloop_fd_stack stack_cur;
119 cur = &cur_fds[cur_fd++];
123 events = cur->events;
130 if (uloop_fd_stack_event(fd, cur->events))
133 stack_cur.next = fd_stack;
135 fd_stack = &stack_cur;
137 stack_cur.events = 0;
139 events = stack_cur.events & ULOOP_EVENT_MASK;
140 } while (stack_cur.fd && events);
141 fd_stack = stack_cur.next;
147 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
152 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
153 return uloop_fd_delete(sock);
155 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
156 fl = fcntl(sock->fd, F_GETFL, 0);
158 fcntl(sock->fd, F_SETFL, fl);
161 ret = register_poll(sock, flags);
165 sock->registered = true;
173 int uloop_fd_delete(struct uloop_fd *fd)
177 for (i = 0; i < cur_nfds; i++) {
178 if (cur_fds[cur_fd + i].fd != fd)
181 cur_fds[cur_fd + i].fd = NULL;
187 fd->registered = false;
188 uloop_fd_stack_event(fd, -1);
189 return __uloop_fd_delete(fd);
192 static int tv_diff(struct timeval *t1, struct timeval *t2)
195 (t1->tv_sec - t2->tv_sec) * 1000 +
196 (t1->tv_usec - t2->tv_usec) / 1000;
199 int uloop_timeout_add(struct uloop_timeout *timeout)
201 struct uloop_timeout *tmp;
202 struct list_head *h = &timeouts;
204 if (timeout->pending)
207 list_for_each_entry(tmp, &timeouts, list) {
208 if (tv_diff(&tmp->time, &timeout->time) > 0) {
214 list_add_tail(&timeout->list, h);
215 timeout->pending = true;
220 static void uloop_gettime(struct timeval *tv)
224 clock_gettime(CLOCK_MONOTONIC, &ts);
225 tv->tv_sec = ts.tv_sec;
226 tv->tv_usec = ts.tv_nsec / 1000;
229 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
231 struct timeval *time = &timeout->time;
233 if (timeout->pending)
234 uloop_timeout_cancel(timeout);
238 time->tv_sec += msecs / 1000;
239 time->tv_usec += (msecs % 1000) * 1000;
241 if (time->tv_usec > 1000000) {
243 time->tv_usec -= 1000000;
246 return uloop_timeout_add(timeout);
249 int uloop_timeout_cancel(struct uloop_timeout *timeout)
251 if (!timeout->pending)
254 list_del(&timeout->list);
255 timeout->pending = false;
260 int uloop_timeout_remaining(struct uloop_timeout *timeout)
264 if (!timeout->pending)
269 return tv_diff(&timeout->time, &now);
272 int uloop_process_add(struct uloop_process *p)
274 struct uloop_process *tmp;
275 struct list_head *h = &processes;
280 list_for_each_entry(tmp, &processes, list) {
281 if (tmp->pid > p->pid) {
287 list_add_tail(&p->list, h);
293 int uloop_process_delete(struct uloop_process *p)
304 static void uloop_handle_processes(void)
306 struct uloop_process *p, *tmp;
313 pid = waitpid(-1, &ret, WNOHANG);
314 if (pid < 0 && errno == EINTR)
320 list_for_each_entry_safe(p, tmp, &processes, list) {
327 uloop_process_delete(p);
334 static void uloop_handle_signal(int signo)
340 uloop_cancelled = true;
347 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
350 struct sigaction *act;
353 sigaction(signum, NULL, &s);
356 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
357 memcpy(old, &s, sizeof(struct sigaction));
358 s.sa_handler = handler;
363 else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
368 sigaction(signum, act, NULL);
371 static void uloop_ignore_signal(int signum, bool ignore)
374 void *new_handler = NULL;
376 sigaction(signum, NULL, &s);
379 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
380 new_handler = SIG_IGN;
382 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
383 new_handler = SIG_DFL;
387 s.sa_handler = new_handler;
389 sigaction(signum, &s, NULL);
393 static void uloop_setup_signals(bool add)
395 static struct sigaction old_sigint, old_sigchld, old_sigterm, old_sigquit;
397 uloop_setup_signalfd(add);
399 uloop_install_handler(SIGINT, uloop_handle_signal, &old_sigint, add);
400 uloop_install_handler(SIGTERM, uloop_handle_signal, &old_sigterm, add);
401 uloop_install_handler(SIGQUIT, uloop_handle_signal, &old_sigquit, add);
402 uloop_install_handler(SIGCHLD, uloop_handle_signal, &old_sigchld, add);
404 uloop_ignore_signal(SIGPIPE, add);
407 static int uloop_get_next_timeout(struct timeval *tv)
409 struct uloop_timeout *timeout;
412 if (list_empty(&timeouts))
415 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
416 diff = tv_diff(&timeout->time, tv);
423 static void uloop_process_timeouts(struct timeval *tv)
425 struct uloop_timeout *t;
427 while (!list_empty(&timeouts)) {
428 t = list_first_entry(&timeouts, struct uloop_timeout, list);
430 if (tv_diff(&t->time, tv) > 0)
433 uloop_timeout_cancel(t);
439 static void uloop_clear_timeouts(void)
441 struct uloop_timeout *t, *tmp;
443 list_for_each_entry_safe(t, tmp, &timeouts, list)
444 uloop_timeout_cancel(t);
447 static void uloop_clear_processes(void)
449 struct uloop_process *p, *tmp;
451 list_for_each_entry_safe(p, tmp, &processes, list)
452 uloop_process_delete(p);
457 static int recursive_calls = 0;
461 * Handlers are only updated for the first call to uloop_run() (and restored
462 * when this call is done).
464 if (!recursive_calls++)
465 uloop_setup_signals(true);
467 uloop_cancelled = false;
468 while(!uloop_cancelled)
471 uloop_process_timeouts(&tv);
474 uloop_handle_processes();
480 uloop_run_events(uloop_get_next_timeout(&tv));
483 if (!--recursive_calls)
484 uloop_setup_signals(false);
487 void uloop_done(void)
489 if (signal_fd >= 0) {
500 uloop_clear_timeouts();
501 uloop_clear_processes();