uloop: Fix race condition in SIGCHLD handling
[project/libubox.git] / uloop.c
diff --git a/uloop.c b/uloop.c
index 0c57753444e16f361afd8f2d51a180436323aec9..3813e188522d87c03877072ea820a48f5526eafe 100644 (file)
--- a/uloop.c
+++ b/uloop.c
@@ -116,6 +116,8 @@ static int waker_init(void)
        return 0;
 }
 
+static void uloop_setup_signals(bool add);
+
 int uloop_init(void)
 {
        if (uloop_init_pollfd() < 0)
@@ -126,6 +128,8 @@ int uloop_init(void)
                return -1;
        }
 
+       uloop_setup_signals(true);
+
        return 0;
 }
 
@@ -525,12 +529,7 @@ 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 (!uloop_run_depth++)
-               uloop_setup_signals(true);
+       uloop_run_depth++;
 
        uloop_status = 0;
        uloop_cancelled = false;
@@ -548,19 +547,20 @@ int uloop_run_timeout(int timeout)
                uloop_gettime(&tv);
 
                next_time = uloop_get_next_timeout(&tv);
-               if (timeout > 0 && timeout < next_time)
+               if (timeout >= 0 && timeout < next_time)
                        next_time = timeout;
                uloop_run_events(next_time);
        }
 
-       if (!--uloop_run_depth)
-               uloop_setup_signals(false);
+       --uloop_run_depth;
 
        return uloop_status;
 }
 
 void uloop_done(void)
 {
+       uloop_setup_signals(false);
+
        if (poll_fd >= 0) {
                close(poll_fd);
                poll_fd = -1;