uloop: try to use signalfd for signal handling if available
[project/libubox.git] / uloop.c
diff --git a/uloop.c b/uloop.c
index d2784b7..6ef7210 100644 (file)
--- a/uloop.c
+++ b/uloop.c
@@ -56,6 +56,7 @@ 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;
 static bool do_sigchld = false;
@@ -63,6 +64,8 @@ static bool do_sigchld = false;
 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
 static int cur_fd, cur_nfds;
 
+static void uloop_handle_signal(int signo);
+
 #ifdef USE_KQUEUE
 #include "uloop-kqueue.c"
 #endif
@@ -325,14 +328,17 @@ static void uloop_handle_processes(void)
 
 }
 
-static void uloop_handle_sigint(int signo)
+static void uloop_handle_signal(int signo)
 {
-       uloop_cancelled = true;
-}
-
-static void uloop_sigchld(int signo)
-{
-       do_sigchld = true;
+       switch (signo) {
+       case SIGINT:
+       case SIGQUIT:
+       case SIGTERM:
+               uloop_cancelled = true;
+               break;
+       case SIGCHLD:
+               do_sigchld = true;
+       }
 }
 
 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
@@ -385,9 +391,13 @@ static void uloop_setup_signals(bool add)
 {
        static struct sigaction old_sigint, old_sigchld, old_sigterm;
 
-       uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
-       uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
-       uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
+       if (uloop_setup_signalfd(add))
+               return;
+
+       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);
 
        uloop_ignore_signal(SIGPIPE, add);
 }
@@ -474,6 +484,11 @@ void uloop_run(void)
 
 void uloop_done(void)
 {
+       if (signal_fd >= 0) {
+               close(signal_fd);
+               signal_fd = -1;
+       }
+
        if (poll_fd < 0)
                return;