]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-event: set pid to event source after all setup processes finished
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 7 Apr 2022 06:21:56 +0000 (15:21 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 12 Apr 2022 16:00:08 +0000 (01:00 +0900)
Otherwise, the assertion in source_disconnect() may be triggered,

src/libsystemd/sd-event/sd-event.c

index e15d277038e52ae2d12f9c64cba53fc742552c31..e7c3fcd37c66051e79c4a2201c8614ff3e8ebb81 100644 (file)
@@ -1426,7 +1426,6 @@ _public_ int sd_event_add_child(
                 return -ENOMEM;
 
         s->wakeup = WAKEUP_EVENT_SOURCE;
-        s->child.pid = pid;
         s->child.options = options;
         s->child.callback = callback;
         s->userdata = userdata;
@@ -1436,7 +1435,7 @@ _public_ int sd_event_add_child(
          * pin the PID, and make regular waitid() handling race-free. */
 
         if (shall_use_pidfd()) {
-                s->child.pidfd = pidfd_open(s->child.pid, 0);
+                s->child.pidfd = pidfd_open(pid, 0);
                 if (s->child.pidfd < 0) {
                         /* Propagate errors unless the syscall is not supported or blocked */
                         if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
@@ -1446,10 +1445,6 @@ _public_ int sd_event_add_child(
         } else
                 s->child.pidfd = -1;
 
-        r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s);
-        if (r < 0)
-                return r;
-
         if (EVENT_SOURCE_WATCH_PIDFD(s)) {
                 /* We have a pidfd and we only want to watch for exit */
                 r = source_child_pidfd_register(s, s->enabled);
@@ -1465,6 +1460,12 @@ _public_ int sd_event_add_child(
                 e->need_process_child = true;
         }
 
+        r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s);
+        if (r < 0)
+                return r;
+
+        /* These must be done after everything succeeds. */
+        s->child.pid = pid;
         e->n_online_child_sources++;
 
         if (ret)