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>
35 #include <sys/event.h>
38 #include <sys/epoll.h>
39 #include <sys/timerfd.h>
43 struct uloop_fd_event
{
48 struct uloop_fd_stack
{
49 struct uloop_fd_stack
*next
;
54 static struct uloop_fd_stack
*fd_stack
= NULL
;
56 #define ULOOP_MAX_EVENTS 10
58 static struct list_head timeouts
= LIST_HEAD_INIT(timeouts
);
59 static struct list_head processes
= LIST_HEAD_INIT(processes
);
60 static struct list_head signals
= LIST_HEAD_INIT(signals
);
62 static int poll_fd
= -1;
63 bool uloop_cancelled
= false;
64 bool uloop_handle_sigchld
= true;
65 static int uloop_status
= 0;
66 static bool do_sigchld
= false;
68 static struct uloop_fd_event cur_fds
[ULOOP_MAX_EVENTS
];
69 static int cur_fd
, cur_nfds
;
70 static int uloop_run_depth
= 0;
72 uloop_fd_handler uloop_fd_set_cb
= NULL
;
74 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
);
77 #include "uloop-kqueue.c"
81 #include "uloop-epoll.c"
84 static void set_signo(uint64_t *signums
, int signo
)
86 if (signo
>= 1 && signo
<= 64)
87 *signums
|= (1u << (signo
- 1));
90 static bool get_signo(uint64_t signums
, int signo
)
92 return (signo
>= 1) && (signo
<= 64) && (signums
& (1u << (signo
- 1)));
95 static void signal_consume(struct uloop_fd
*fd
, unsigned int events
)
97 struct uloop_signal
*usig
, *usig_next
;
103 nsigs
= read(fd
->fd
, buf
, sizeof(buf
));
105 for (ssize_t i
= 0; i
< nsigs
; i
++)
106 set_signo(&signums
, buf
[i
]);
110 list_for_each_entry_safe(usig
, usig_next
, &signals
, list
)
111 if (get_signo(signums
, usig
->signo
))
115 static int waker_pipe
= -1;
116 static struct uloop_fd waker_fd
= {
118 .cb
= signal_consume
,
121 static void waker_init_fd(int fd
)
123 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
124 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_NONBLOCK
);
127 static int waker_init(void)
137 waker_init_fd(fds
[0]);
138 waker_init_fd(fds
[1]);
141 waker_fd
.fd
= fds
[0];
142 waker_fd
.cb
= signal_consume
;
143 uloop_fd_add(&waker_fd
, ULOOP_READ
);
148 static void uloop_setup_signals(bool add
);
152 if (uloop_init_pollfd() < 0)
155 if (waker_init() < 0) {
160 uloop_setup_signals(true);
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(int64_t 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
);
254 uloop_fd_set_cb(sock
, flags
);
257 sock
->registered
= true;
265 int uloop_fd_delete(struct uloop_fd
*fd
)
270 for (i
= 0; i
< cur_nfds
; i
++) {
271 if (cur_fds
[cur_fd
+ i
].fd
!= fd
)
274 cur_fds
[cur_fd
+ i
].fd
= NULL
;
281 uloop_fd_set_cb(fd
, 0);
283 fd
->registered
= false;
284 uloop_fd_stack_event(fd
, -1);
285 ret
= __uloop_fd_delete(fd
);
291 static int64_t tv_diff(struct timeval
*t1
, struct timeval
*t2
)
294 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
295 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
298 int uloop_timeout_add(struct uloop_timeout
*timeout
)
300 struct uloop_timeout
*tmp
;
301 struct list_head
*h
= &timeouts
;
303 if (timeout
->pending
)
306 list_for_each_entry(tmp
, &timeouts
, list
) {
307 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
313 list_add_tail(&timeout
->list
, h
);
314 timeout
->pending
= true;
319 static void uloop_gettime(struct timeval
*tv
)
323 clock_gettime(CLOCK_MONOTONIC
, &ts
);
324 tv
->tv_sec
= ts
.tv_sec
;
325 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
328 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
330 struct timeval
*time
= &timeout
->time
;
332 if (timeout
->pending
)
333 uloop_timeout_cancel(timeout
);
337 time
->tv_sec
+= msecs
/ 1000;
338 time
->tv_usec
+= (msecs
% 1000) * 1000;
340 if (time
->tv_usec
> 1000000) {
342 time
->tv_usec
-= 1000000;
345 return uloop_timeout_add(timeout
);
348 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
350 if (!timeout
->pending
)
353 list_del(&timeout
->list
);
354 timeout
->pending
= false;
359 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
364 if (!timeout
->pending
)
369 td
= tv_diff(&timeout
->time
, &now
);
373 else if (td
< INT_MIN
)
379 int64_t uloop_timeout_remaining64(struct uloop_timeout
*timeout
)
383 if (!timeout
->pending
)
388 return tv_diff(&timeout
->time
, &now
);
391 int uloop_process_add(struct uloop_process
*p
)
393 struct uloop_process
*tmp
;
394 struct list_head
*h
= &processes
;
399 list_for_each_entry(tmp
, &processes
, list
) {
400 if (tmp
->pid
> p
->pid
) {
406 list_add_tail(&p
->list
, h
);
412 int uloop_process_delete(struct uloop_process
*p
)
423 static void uloop_handle_processes(void)
425 struct uloop_process
*p
, *tmp
;
432 pid
= waitpid(-1, &ret
, WNOHANG
);
433 if (pid
< 0 && errno
== EINTR
)
439 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
446 uloop_process_delete(p
);
453 int uloop_interval_set(struct uloop_interval
*timer
, unsigned int msecs
)
455 return timer_register(timer
, msecs
);
458 int uloop_interval_cancel(struct uloop_interval
*timer
)
460 return timer_remove(timer
);
463 int64_t uloop_interval_remaining(struct uloop_interval
*timer
)
465 return timer_next(timer
);
468 static void uloop_signal_wake(int signo
)
470 uint8_t sigbyte
= signo
;
472 if (signo
== SIGCHLD
)
476 if (write(waker_pipe
, &sigbyte
, 1) < 0) {
484 static void uloop_handle_sigint(int signo
)
486 uloop_status
= signo
;
487 uloop_cancelled
= true;
488 uloop_signal_wake(signo
);
491 static void uloop_install_handler(int signum
, void (*handler
)(int), struct sigaction
* old
, bool add
)
494 struct sigaction
*act
;
497 sigaction(signum
, NULL
, &s
);
500 if (s
.sa_handler
== SIG_DFL
) { /* Do not override existing custom signal handlers */
501 memcpy(old
, &s
, sizeof(struct sigaction
));
502 s
.sa_handler
= handler
;
507 else if (s
.sa_handler
== handler
) { /* Do not restore if someone modified our handler */
512 sigaction(signum
, act
, NULL
);
515 static void uloop_ignore_signal(int signum
, bool ignore
)
518 void *new_handler
= NULL
;
520 sigaction(signum
, NULL
, &s
);
523 if (s
.sa_handler
== SIG_DFL
) /* Ignore only if there isn't any custom handler */
524 new_handler
= SIG_IGN
;
526 if (s
.sa_handler
== SIG_IGN
) /* Restore only if noone modified our SIG_IGN */
527 new_handler
= SIG_DFL
;
531 s
.sa_handler
= new_handler
;
533 sigaction(signum
, &s
, NULL
);
537 static void uloop_setup_signals(bool add
)
539 static struct sigaction old_sigint
, old_sigchld
, old_sigterm
;
541 uloop_install_handler(SIGINT
, uloop_handle_sigint
, &old_sigint
, add
);
542 uloop_install_handler(SIGTERM
, uloop_handle_sigint
, &old_sigterm
, add
);
544 if (uloop_handle_sigchld
)
545 uloop_install_handler(SIGCHLD
, uloop_signal_wake
, &old_sigchld
, add
);
547 uloop_ignore_signal(SIGPIPE
, add
);
550 int uloop_signal_add(struct uloop_signal
*s
)
552 struct list_head
*h
= &signals
;
553 struct uloop_signal
*tmp
;
559 list_for_each_entry(tmp
, &signals
, list
) {
560 if (tmp
->signo
> s
->signo
) {
566 list_add_tail(&s
->list
, h
);
569 sigaction(s
->signo
, NULL
, &s
->orig
);
571 if (s
->orig
.sa_handler
!= uloop_signal_wake
) {
572 sa
.sa_handler
= uloop_signal_wake
;
574 sigemptyset(&sa
.sa_mask
);
575 sigaction(s
->signo
, &sa
, NULL
);
581 int uloop_signal_delete(struct uloop_signal
*s
)
589 if (s
->orig
.sa_handler
!= uloop_signal_wake
)
590 sigaction(s
->signo
, &s
->orig
, NULL
);
595 int uloop_get_next_timeout(void)
597 struct uloop_timeout
*timeout
;
601 if (list_empty(&timeouts
))
606 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
607 diff
= tv_diff(&timeout
->time
, &tv
);
616 static void uloop_process_timeouts(void)
618 struct uloop_timeout
*t
;
621 if (list_empty(&timeouts
))
625 while (!list_empty(&timeouts
)) {
626 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
628 if (tv_diff(&t
->time
, &tv
) > 0)
631 uloop_timeout_cancel(t
);
637 static void uloop_clear_timeouts(void)
639 struct uloop_timeout
*t
, *tmp
;
641 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
642 uloop_timeout_cancel(t
);
645 static void uloop_clear_processes(void)
647 struct uloop_process
*p
, *tmp
;
649 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
650 uloop_process_delete(p
);
653 bool uloop_cancelling(void)
655 return uloop_run_depth
> 0 && uloop_cancelled
;
658 int uloop_run_timeout(int timeout
)
665 uloop_cancelled
= false;
667 uloop_process_timeouts();
670 uloop_handle_processes();
675 next_time
= uloop_get_next_timeout();
676 if (timeout
>= 0 && (next_time
< 0 || timeout
< next_time
))
678 uloop_run_events(next_time
);
679 } while (!uloop_cancelled
&& timeout
< 0);
686 void uloop_done(void)
688 uloop_setup_signals(false);
695 if (waker_pipe
>= 0) {
696 uloop_fd_delete(&waker_fd
);
702 uloop_clear_timeouts();
703 uloop_clear_processes();