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
)
269 for (i
= 0; i
< cur_nfds
; i
++) {
270 if (cur_fds
[cur_fd
+ i
].fd
!= fd
)
273 cur_fds
[cur_fd
+ i
].fd
= NULL
;
280 uloop_fd_set_cb(fd
, 0);
282 fd
->registered
= false;
284 uloop_fd_stack_event(fd
, -1);
285 return __uloop_fd_delete(fd
);
288 static int64_t tv_diff(struct timeval
*t1
, struct timeval
*t2
)
291 (t1
->tv_sec
- t2
->tv_sec
) * 1000 +
292 (t1
->tv_usec
- t2
->tv_usec
) / 1000;
295 int uloop_timeout_add(struct uloop_timeout
*timeout
)
297 struct uloop_timeout
*tmp
;
298 struct list_head
*h
= &timeouts
;
300 if (timeout
->pending
)
303 list_for_each_entry(tmp
, &timeouts
, list
) {
304 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
310 list_add_tail(&timeout
->list
, h
);
311 timeout
->pending
= true;
316 static void uloop_gettime(struct timeval
*tv
)
320 clock_gettime(CLOCK_MONOTONIC
, &ts
);
321 tv
->tv_sec
= ts
.tv_sec
;
322 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
325 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
327 struct timeval
*time
= &timeout
->time
;
329 if (timeout
->pending
)
330 uloop_timeout_cancel(timeout
);
334 time
->tv_sec
+= msecs
/ 1000;
335 time
->tv_usec
+= (msecs
% 1000) * 1000;
337 if (time
->tv_usec
> 1000000) {
339 time
->tv_usec
-= 1000000;
342 return uloop_timeout_add(timeout
);
345 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
347 if (!timeout
->pending
)
350 list_del(&timeout
->list
);
351 timeout
->pending
= false;
356 int uloop_timeout_remaining(struct uloop_timeout
*timeout
)
361 if (!timeout
->pending
)
366 td
= tv_diff(&timeout
->time
, &now
);
370 else if (td
< INT_MIN
)
376 int64_t uloop_timeout_remaining64(struct uloop_timeout
*timeout
)
380 if (!timeout
->pending
)
385 return tv_diff(&timeout
->time
, &now
);
388 int uloop_process_add(struct uloop_process
*p
)
390 struct uloop_process
*tmp
;
391 struct list_head
*h
= &processes
;
396 list_for_each_entry(tmp
, &processes
, list
) {
397 if (tmp
->pid
> p
->pid
) {
403 list_add_tail(&p
->list
, h
);
409 int uloop_process_delete(struct uloop_process
*p
)
420 static void uloop_handle_processes(void)
422 struct uloop_process
*p
, *tmp
;
429 pid
= waitpid(-1, &ret
, WNOHANG
);
430 if (pid
< 0 && errno
== EINTR
)
436 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
443 uloop_process_delete(p
);
450 int uloop_interval_set(struct uloop_interval
*timer
, unsigned int msecs
)
452 return timer_register(timer
, msecs
);
455 int uloop_interval_cancel(struct uloop_interval
*timer
)
457 return timer_remove(timer
);
460 int64_t uloop_interval_remaining(struct uloop_interval
*timer
)
462 return timer_next(timer
);
465 static void uloop_signal_wake(int signo
)
467 uint8_t sigbyte
= signo
;
473 if (write(waker_pipe
, &sigbyte
, 1) < 0) {
481 static void uloop_handle_sigint(int signo
)
483 uloop_status
= signo
;
484 uloop_cancelled
= true;
485 uloop_signal_wake(signo
);
488 static void uloop_install_handler(int signum
, void (*handler
)(int), struct sigaction
* old
, bool add
)
491 struct sigaction
*act
;
494 sigaction(signum
, NULL
, &s
);
497 if (s
.sa_handler
== SIG_DFL
) { /* Do not override existing custom signal handlers */
498 memcpy(old
, &s
, sizeof(struct sigaction
));
499 s
.sa_handler
= handler
;
504 else if (s
.sa_handler
== handler
) { /* Do not restore if someone modified our handler */
509 sigaction(signum
, act
, NULL
);
512 static void uloop_ignore_signal(int signum
, bool ignore
)
515 void *new_handler
= NULL
;
517 sigaction(signum
, NULL
, &s
);
520 if (s
.sa_handler
== SIG_DFL
) /* Ignore only if there isn't any custom handler */
521 new_handler
= SIG_IGN
;
523 if (s
.sa_handler
== SIG_IGN
) /* Restore only if noone modified our SIG_IGN */
524 new_handler
= SIG_DFL
;
528 s
.sa_handler
= new_handler
;
530 sigaction(signum
, &s
, NULL
);
534 static void uloop_setup_signals(bool add
)
536 static struct sigaction old_sigint
, old_sigchld
, old_sigterm
;
538 uloop_install_handler(SIGINT
, uloop_handle_sigint
, &old_sigint
, add
);
539 uloop_install_handler(SIGTERM
, uloop_handle_sigint
, &old_sigterm
, add
);
541 if (uloop_handle_sigchld
)
542 uloop_install_handler(SIGCHLD
, uloop_signal_wake
, &old_sigchld
, add
);
544 uloop_ignore_signal(SIGPIPE
, add
);
547 int uloop_signal_add(struct uloop_signal
*s
)
549 struct list_head
*h
= &signals
;
550 struct uloop_signal
*tmp
;
556 list_for_each_entry(tmp
, &signals
, list
) {
557 if (tmp
->signo
> s
->signo
) {
563 list_add_tail(&s
->list
, h
);
566 sigaction(s
->signo
, NULL
, &s
->orig
);
568 if (s
->orig
.sa_handler
!= uloop_signal_wake
) {
569 sa
.sa_handler
= uloop_signal_wake
;
571 sigaction(s
->signo
, &sa
, NULL
);
577 int uloop_signal_delete(struct uloop_signal
*s
)
585 if (s
->orig
.sa_handler
!= uloop_signal_wake
)
586 sigaction(s
->signo
, &s
->orig
, NULL
);
591 int uloop_get_next_timeout(void)
593 struct uloop_timeout
*timeout
;
597 if (list_empty(&timeouts
))
602 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
603 diff
= tv_diff(&timeout
->time
, &tv
);
612 static void uloop_process_timeouts(void)
614 struct uloop_timeout
*t
;
617 if (list_empty(&timeouts
))
621 while (!list_empty(&timeouts
)) {
622 t
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
624 if (tv_diff(&t
->time
, &tv
) > 0)
627 uloop_timeout_cancel(t
);
633 static void uloop_clear_timeouts(void)
635 struct uloop_timeout
*t
, *tmp
;
637 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
638 uloop_timeout_cancel(t
);
641 static void uloop_clear_processes(void)
643 struct uloop_process
*p
, *tmp
;
645 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
646 uloop_process_delete(p
);
649 bool uloop_cancelling(void)
651 return uloop_run_depth
> 0 && uloop_cancelled
;
654 int uloop_run_timeout(int timeout
)
661 uloop_cancelled
= false;
663 uloop_process_timeouts();
666 uloop_handle_processes();
671 next_time
= uloop_get_next_timeout();
672 if (timeout
>= 0 && (next_time
< 0 || timeout
< next_time
))
674 uloop_run_events(next_time
);
675 } while (!uloop_cancelled
&& timeout
< 0);
682 void uloop_done(void)
684 uloop_setup_signals(false);
691 if (waker_pipe
>= 0) {
692 uloop_fd_delete(&waker_fd
);
698 uloop_clear_timeouts();
699 uloop_clear_processes();