tree-wide: Migrate to pidref_safe_fork()

Let's migrate all remaining callers of safe_fork()
to pidref_safe_fork() and get rid of safe_fork().
This commit is contained in:
Daan De Meyer
2026-01-02 20:08:41 +01:00
committed by DaanDeMeyer
parent e6703a9aa4
commit 52ecf376fa
60 changed files with 321 additions and 303 deletions
+6 -38
View File
@@ -1576,13 +1576,9 @@ int pidref_safe_fork_full(
}
if (ret) {
if (FLAGS_SET(flags, _FORK_PID_ONLY))
r = pidref_set_pid(ret, pid);
if (r < 0) /* Let's not fail for this, no matter what, the process exists after all, and that's key */
*ret = PIDREF_MAKE_FROM_PID(pid);
else {
r = pidref_set_pid(ret, pid);
if (r < 0) /* Let's not fail for this, no matter what, the process exists after all, and that's key */
*ret = PIDREF_MAKE_FROM_PID(pid);
}
}
return 1;
@@ -1764,44 +1760,16 @@ int pidref_safe_fork_full(
freeze();
if (ret) {
if (FLAGS_SET(flags, _FORK_PID_ONLY))
*ret = PIDREF_MAKE_FROM_PID(getpid_cached());
else {
r = pidref_set_self(ret);
if (r < 0) {
log_full_errno(prio, r, "Failed to acquire PID reference on ourselves: %m");
_exit(EXIT_FAILURE);
}
r = pidref_set_self(ret);
if (r < 0) {
log_full_errno(prio, r, "Failed to acquire PID reference on ourselves: %m");
_exit(EXIT_FAILURE);
}
}
return 0;
}
int safe_fork_full(
const char *name,
const int stdio_fds[3],
int except_fds[],
size_t n_except_fds,
ForkFlags flags,
pid_t *ret) {
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
int r;
/* Getting the detached child process pid without pidfd is racy, so don't allow it if not returning
* a pidref to the caller. */
assert(!FLAGS_SET(flags, FORK_DETACH) || !ret);
r = pidref_safe_fork_full(name, stdio_fds, except_fds, n_except_fds, flags|_FORK_PID_ONLY, ret ? &pidref : NULL);
if (r < 0 || !ret)
return r;
*ret = pidref.pid;
return r;
}
int namespace_fork_full(
const char *outer_name,
const char *inner_name,
-14
View File
@@ -176,8 +176,6 @@ typedef enum ForkFlags {
FORK_NEW_PIDNS = 1 << 21, /* Run child in its own PID namespace 💣 DO NOT USE IN THREADED PROGRAMS! 💣 */
FORK_FREEZE = 1 << 22, /* Don't return in child, just call freeze() instead */
FORK_ALLOW_DLOPEN = 1 << 23, /* Do not block dlopen() in child */
_FORK_PID_ONLY = 1 << 24, /* Don't open a pidfd referencing the child process */
} ForkFlags;
int pidref_safe_fork_full(
@@ -192,18 +190,6 @@ static inline int pidref_safe_fork(const char *name, ForkFlags flags, PidRef *re
return pidref_safe_fork_full(name, NULL, NULL, 0, flags, ret);
}
int safe_fork_full(
const char *name,
const int stdio_fds[3],
int except_fds[],
size_t n_except_fds,
ForkFlags flags,
pid_t *ret);
static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret) {
return safe_fork_full(name, NULL, NULL, 0, flags, ret);
}
int namespace_fork_full(
const char *outer_name,
const char *inner_name,