(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.)
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);