utils: ensure that byte-order conversion functions evaluate the argument only once
[project/libubox.git] / uloop.c
diff --git a/uloop.c b/uloop.c
index 6ef7210..8517366 100644 (file)
--- a/uloop.c
+++ b/uloop.c
@@ -56,15 +56,17 @@ static struct uloop_fd_stack *fd_stack = NULL;
 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
 static struct list_head processes = LIST_HEAD_INIT(processes);
 
-static int signal_fd = -1;
 static int poll_fd = -1;
 bool uloop_cancelled = false;
+bool uloop_handle_sigchld = true;
+static int uloop_status = 0;
 static bool do_sigchld = false;
 
 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
 static int cur_fd, cur_nfds;
+static int uloop_run_depth = 0;
 
-static void uloop_handle_signal(int signo);
+int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
 
 #ifdef USE_KQUEUE
 #include "uloop-kqueue.c"
@@ -74,6 +76,64 @@ static void uloop_handle_signal(int signo);
 #include "uloop-epoll.c"
 #endif
 
+static void waker_consume(struct uloop_fd *fd, unsigned int events)
+{
+       char buf[4];
+
+       while (read(fd->fd, buf, 4) > 0)
+               ;
+}
+
+static int waker_pipe = -1;
+static struct uloop_fd waker_fd = {
+       .fd = -1,
+       .cb = waker_consume,
+};
+
+static void waker_init_fd(int fd)
+{
+       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+       fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
+}
+
+static int waker_init(void)
+{
+       int fds[2];
+
+       if (waker_pipe >= 0)
+               return 0;
+
+       if (pipe(fds) < 0)
+               return -1;
+
+       waker_init_fd(fds[0]);
+       waker_init_fd(fds[1]);
+       waker_pipe = fds[1];
+
+       waker_fd.fd = fds[0];
+       waker_fd.cb = waker_consume;
+       uloop_fd_add(&waker_fd, ULOOP_READ);
+
+       return 0;
+}
+
+static void uloop_setup_signals(bool add);
+
+int uloop_init(void)
+{
+       if (uloop_init_pollfd() < 0)
+               return -1;
+
+       if (waker_init() < 0) {
+               uloop_done();
+               return -1;
+       }
+
+       uloop_setup_signals(true);
+
+       return 0;
+}
+
 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
 {
        struct uloop_fd_stack *cur;
@@ -311,6 +371,9 @@ static void uloop_handle_processes(void)
 
        while (1) {
                pid = waitpid(-1, &ret, WNOHANG);
+               if (pid < 0 && errno == EINTR)
+                       continue;
+
                if (pid <= 0)
                        return;
 
@@ -328,17 +391,28 @@ static void uloop_handle_processes(void)
 
 }
 
-static void uloop_handle_signal(int signo)
+static void uloop_signal_wake(void)
 {
-       switch (signo) {
-       case SIGINT:
-       case SIGQUIT:
-       case SIGTERM:
-               uloop_cancelled = true;
+       do {
+               if (write(waker_pipe, "w", 1) < 0) {
+                       if (errno == EINTR)
+                               continue;
+               }
                break;
-       case SIGCHLD:
-               do_sigchld = true;
-       }
+       } while (1);
+}
+
+static void uloop_handle_sigint(int signo)
+{
+       uloop_status = signo;
+       uloop_cancelled = true;
+       uloop_signal_wake();
+}
+
+static void uloop_sigchld(int signo)
+{
+       do_sigchld = true;
+       uloop_signal_wake();
 }
 
 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
@@ -391,13 +465,11 @@ static void uloop_setup_signals(bool add)
 {
        static struct sigaction old_sigint, old_sigchld, old_sigterm;
 
-       if (uloop_setup_signalfd(add))
-               return;
+       uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
+       uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
 
-       uloop_install_handler(SIGINT, uloop_handle_signal, &old_sigint, add);
-       uloop_install_handler(SIGTERM, uloop_handle_signal, &old_sigterm, add);
-       uloop_install_handler(SIGQUIT, uloop_handle_signal, &old_sigterm, add);
-       uloop_install_handler(SIGCHLD, uloop_handle_signal, &old_sigchld, add);
+       if (uloop_handle_sigchld)
+               uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
 
        uloop_ignore_signal(SIGPIPE, add);
 }
@@ -450,20 +522,21 @@ static void uloop_clear_processes(void)
                uloop_process_delete(p);
 }
 
-void uloop_run(void)
+bool uloop_cancelling(void)
 {
-       static int recursive_calls = 0;
+       return uloop_run_depth > 0 && uloop_cancelled;
+}
+
+int uloop_run_timeout(int timeout)
+{
+       int next_time = 0;
        struct timeval tv;
 
-       /*
-        * Handlers are only updated for the first call to uloop_run() (and restored
-        * when this call is done).
-        */
-       if (!recursive_calls++)
-               uloop_setup_signals(true);
+       uloop_run_depth++;
 
+       uloop_status = 0;
        uloop_cancelled = false;
-       while(!uloop_cancelled)
+       while (!uloop_cancelled)
        {
                uloop_gettime(&tv);
                uloop_process_timeouts(&tv);
@@ -475,25 +548,33 @@ void uloop_run(void)
                        break;
 
                uloop_gettime(&tv);
-               uloop_run_events(uloop_get_next_timeout(&tv));
+
+               next_time = uloop_get_next_timeout(&tv);
+               if (timeout >= 0 && timeout < next_time)
+                       next_time = timeout;
+               uloop_run_events(next_time);
        }
 
-       if (!--recursive_calls)
-               uloop_setup_signals(false);
+       --uloop_run_depth;
+
+       return uloop_status;
 }
 
 void uloop_done(void)
 {
-       if (signal_fd >= 0) {
-               close(signal_fd);
-               signal_fd = -1;
-       }
+       uloop_setup_signals(false);
 
-       if (poll_fd < 0)
-               return;
+       if (poll_fd >= 0) {
+               close(poll_fd);
+               poll_fd = -1;
+       }
 
-       close(poll_fd);
-       poll_fd = -1;
+       if (waker_pipe >= 0) {
+               uloop_fd_delete(&waker_fd);
+               close(waker_pipe);
+               close(waker_fd.fd);
+               waker_pipe = -1;
+       }
 
        uloop_clear_timeouts();
        uloop_clear_processes();