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
+7 -6
View File
@@ -16,6 +16,7 @@
#include "log.h"
#include "mkdir.h"
#include "pidfd-util.h"
#include "pidref.h"
#include "process-util.h"
#include "set.h"
#include "signal-util.h"
@@ -143,19 +144,19 @@ static size_t manager_current_workers(Manager *m) {
static int start_one_worker(Manager *m) {
_cleanup_(sd_event_source_disable_unrefp) sd_event_source *source = NULL;
bool fixed;
pid_t pid;
int r;
assert(m);
fixed = set_size(m->workers_fixed) < USERDB_WORKERS_MIN;
r = safe_fork_full(
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
r = pidref_safe_fork_full(
"(sd-worker)",
/* stdio_fds= */ NULL,
&m->listen_fd, 1,
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_CLOSE_ALL_FDS,
&pid);
&pidref);
if (r < 0)
return log_error_errno(r, "Failed to fork new worker child: %m");
if (r == 0) {
@@ -176,7 +177,7 @@ static int start_one_worker(Manager *m) {
safe_close(m->listen_fd);
}
r = setenvf("LISTEN_PID", /* overwrite= */ true, PID_FMT, pid);
r = setenvf("LISTEN_PID", /* overwrite= */ true, PID_FMT, pidref.pid);
if (r < 0) {
log_error_errno(r, "Failed to set $LISTEN_PID: %m");
_exit(EXIT_FAILURE);
@@ -212,9 +213,9 @@ static int start_one_worker(Manager *m) {
_exit(EXIT_FAILURE);
}
r = sd_event_add_child(m->event, &source, pid, WEXITED, on_worker_exit, m);
r = event_add_child_pidref(m->event, &source, &pidref, WEXITED, on_worker_exit, m);
if (r < 0)
return log_error_errno(r, "Failed to watch child " PID_FMT ": %m", pid);
return log_error_errno(r, "Failed to watch child " PID_FMT ": %m", pidref.pid);
r = set_ensure_put(
fixed ? &m->workers_fixed : &m->workers_dynamic,