From: Lennart Poettering Date: Wed, 8 Jan 2025 09:50:06 +0000 (+0100) Subject: process-util: make pidref_safe_fork_full() work with FORK_WAIT X-Git-Tag: v258-rc1~1634^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=47e45ea7389ea2e4ad074110ce7ac7f768f79eb0;p=thirdparty%2Fsystemd.git process-util: make pidref_safe_fork_full() work with FORK_WAIT (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.) --- diff --git a/src/basic/process-util.c b/src/basic/process-util.c index f48285d3d52..1264bae3e61 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -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);