naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
c62b8e
From 2d8d8b2d713a3b0c0aad0552608cc2cd13583207 Mon Sep 17 00:00:00 2001
c62b8e
From: Michal Sekletar <msekleta@redhat.com>
c62b8e
Date: Fri, 26 Apr 2019 19:20:09 +0200
c62b8e
Subject: [PATCH] udev: check if the spawned PID didn't exit after reaping
c62b8e
 unexpected PID
c62b8e
c62b8e
We shouldn't just continue after getting SIGCHLD for the unexpected
c62b8e
process because signal coalescing might have happened. If it actually
c62b8e
did happen we won't get any more SIGCHLDs on signalfd.
c62b8e
c62b8e
Related: #1697909
c62b8e
---
c62b8e
 src/udev/udev-event.c | 27 +++++++++++++++++++++++----
c62b8e
 1 file changed, 23 insertions(+), 4 deletions(-)
c62b8e
c62b8e
diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
c62b8e
index 0ba079201c..7fe64f04a4 100644
c62b8e
--- a/src/udev/udev-event.c
c62b8e
+++ b/src/udev/udev-event.c
c62b8e
@@ -597,8 +597,9 @@ static int spawn_wait(struct udev_event *event,
c62b8e
                 }
c62b8e
 
c62b8e
                 if (pfd[0].revents & POLLIN) {
c62b8e
+                        int child_exited = -1;
c62b8e
                         struct signalfd_siginfo fdsi;
c62b8e
-                        int status;
c62b8e
+                        int status, r;
c62b8e
                         ssize_t size;
c62b8e
 
c62b8e
                         size = read(event->fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
c62b8e
@@ -612,10 +613,28 @@ static int spawn_wait(struct udev_event *event,
c62b8e
                         case SIGCHLD:
c62b8e
                                 if (pid != (pid_t) fdsi.ssi_pid) {
c62b8e
                                         log_debug("expected SIGCHLD from '%s' ["PID_FMT"] received from unknown process ["PID_FMT"]. Ignoring", cmd, pid, fdsi.ssi_pid);
c62b8e
-                                        continue;
c62b8e
+
c62b8e
+                                        /* We got SIGCHLD from unexpected process. Possibly some library that we use forked off something behind our back.
c62b8e
+                                           In case the PID we wait for also exited the kernel could coalesce SIGCHLDs and we won't get second SIGCHLD
c62b8e
+                                           on the signalfd. We can't know if coalescing happened or not, hence we need to call waitpid() in a loop until
c62b8e
+                                           the PID we care for exits, we if it haven't already. */
c62b8e
+                                        while (child_exited < 0) {
c62b8e
+                                                r = waitpid(-1, &status, 0);
c62b8e
+                                                if (r < 0 && errno == EINTR)
c62b8e
+                                                        continue;
c62b8e
+                                                else if (r < 0)
c62b8e
+                                                        break;
c62b8e
+                                                else if (r == pid)
c62b8e
+                                                        child_exited = 0;
c62b8e
+                                        }
c62b8e
                                 }
c62b8e
-                                if (waitpid(pid, &status, WNOHANG) <= 0)
c62b8e
-                                        break;
c62b8e
+
c62b8e
+                                /* We didn't wait for child yet, let's do that now */
c62b8e
+                                if (child_exited < 0) {
c62b8e
+                                        if (waitpid(pid, &status, WNOHANG) <= 0)
c62b8e
+                                                break;
c62b8e
+                                }
c62b8e
+
c62b8e
                                 if (WIFEXITED(status)) {
c62b8e
                                         log_debug("'%s' ["PID_FMT"] exit with return code %i", cmd, pid, WEXITSTATUS(status));
c62b8e
                                         if (WEXITSTATUS(status) != 0)