]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: make pidref_safe_fork_full() work with FORK_WAIT
authorLennart Poettering <lennart@poettering.net>
Wed, 8 Jan 2025 09:50:06 +0000 (10:50 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 10 Jan 2025 13:14:17 +0000 (14:14 +0100)
(This is useful for the test case added in the next commit, where it's
kinda nice being able to use pidref_safe_fork_full() and acquiring a
pidref of the child in the child in one go. There's no other value in
this than a bit of synctactic sugar for that test. But otoh thre's no
good reason to prohibit FORK_WAIT use like this, hence either way, this
commit should be a good thing.)

src/basic/process-util.c

index f48285d3d52a36ab5a72ac3ce0579c61936a56fb..1264bae3e61e111c8ad24419e2e7f0c075403491 100644 (file)
@@ -1783,12 +1783,16 @@ int pidref_safe_fork_full(
         pid_t pid;
         int r, q;
 
-        assert(!FLAGS_SET(flags, FORK_WAIT));
-
         r = safe_fork_full(name, stdio_fds, except_fds, n_except_fds, flags, &pid);
-        if (r < 0)
+        if (r < 0 || !ret_pid)
                 return r;
 
+        if (r > 0 && FLAGS_SET(flags, FORK_WAIT)) {
+                /* If we are in the parent and successfully waited, then the process doesn't exist anymore */
+                *ret_pid = PIDREF_NULL;
+                return r;
+        }
+
         q = pidref_set_pid(ret_pid, pid);
         if (q < 0) /* Let's not fail for this, no matter what, the process exists after all, and that's key */
                 *ret_pid = PIDREF_MAKE_FROM_PID(pid);