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
@@ -655,12 +655,13 @@ static int asynchronous_expire(int dev_autofs_fd, int ioctl_fd) {
* child's PID, we are PID1/autoreaper after all, hence when it dies we'll automatically clean it up
* anyway. */
r = safe_fork_full("(sd-expire)",
/* stdio_fds= */ NULL,
(int[]) { dev_autofs_fd, ioctl_fd },
/* n_except_fds= */ 2,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG,
/* ret= */ NULL);
r = pidref_safe_fork_full(
"(sd-expire)",
/* stdio_fds= */ NULL,
(int[]) { dev_autofs_fd, ioctl_fd },
/* n_except_fds= */ 2,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG,
/* ret= */ NULL);
if (r != 0)
return r;
+1 -1
View File
@@ -1416,7 +1416,7 @@ static int setup_pam(
parent_pid = getpid_cached();
r = safe_fork("(sd-pam)", 0, NULL);
r = pidref_safe_fork("(sd-pam)", /* flags= */ 0, /* ret= */ NULL);
if (r < 0)
goto fail;
if (r == 0) {
+1 -1
View File
@@ -4133,7 +4133,7 @@ static int manager_run_generators(Manager *m) {
if (is_dir("/tmp", /* follow= */ false) > 0 && !MANAGER_IS_TEST_RUN(m))
flags |= FORK_PRIVATE_TMP;
r = safe_fork("(sd-gens)", flags, NULL);
r = pidref_safe_fork("(sd-gens)", flags, /* ret= */ NULL);
if (r == 0) {
r = manager_execute_generators(m, paths, /* remount_ro= */ true);
_exit(r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+5 -5
View File
@@ -3502,7 +3502,6 @@ static int is_extension_overlay(const char *path, int fd) {
static int unpeel_get_fd(const char *mount_path, int *ret_fd) {
_cleanup_close_pair_ int pipe_fds[2] = EBADF_PAIR;
_cleanup_close_ int fs_fd = -EBADF;
pid_t pid;
int r;
assert(mount_path);
@@ -3513,7 +3512,7 @@ static int unpeel_get_fd(const char *mount_path, int *ret_fd) {
return log_debug_errno(errno, "Failed to create socket pair: %m");
/* Clone mount namespace here to unpeel without affecting live process */
r = safe_fork("(sd-ns-unpeel)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &pid);
r = pidref_safe_fork("(sd-ns-unpeel)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, /* ret= */ NULL);
if (r < 0)
return r;
if (r == 0) {
@@ -3903,9 +3902,10 @@ int refresh_extensions_in_namespace(
* overlays to obtain FDs the underlying directories, over which we will reapply the overlays
* 3. In the child again, receive the FDs and reapply the overlays
*/
r = safe_fork("(sd-ns-refresh-exts)",
FORK_DEATHSIG_SIGTERM|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE,
NULL);
r = pidref_safe_fork(
"(sd-ns-refresh-exts)",
FORK_DEATHSIG_SIGTERM|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE,
/* ret= */ NULL);
if (r < 0)
return r;
if (r == 0) {
+5 -13
View File
@@ -5540,7 +5540,6 @@ int unit_set_exec_params(Unit *u, ExecParameters *p) {
int unit_fork_helper_process_full(Unit *u, const char *name, bool into_cgroup, ForkFlags flags, PidRef *ret) {
CGroupRuntime *crt = NULL;
pid_t pid;
int r;
assert(u);
@@ -5548,8 +5547,8 @@ int unit_fork_helper_process_full(Unit *u, const char *name, bool into_cgroup, F
assert(ret);
/* Forks off a helper process and makes sure it is a member of the unit's cgroup, if configured to
* do so. Returns == 0 in the child, and > 0 in the parent. The pid parameter is always filled in
* with the child's PID. */
* do so. Returns == 0 in the child, and > 0 in the parent. The pidref parameter is always filled in
* with the child's PID reference. */
if (into_cgroup) {
r = unit_realize_cgroup(u);
@@ -5559,19 +5558,11 @@ int unit_fork_helper_process_full(Unit *u, const char *name, bool into_cgroup, F
crt = unit_get_cgroup_runtime(u);
}
r = safe_fork(name, FORK_REOPEN_LOG|FORK_DEATHSIG_SIGTERM|flags, &pid);
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
r = pidref_safe_fork(name, FORK_REOPEN_LOG|FORK_DEATHSIG_SIGTERM|flags, &pidref);
if (r < 0)
return r;
if (r > 0) {
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
int q;
/* Parent */
q = pidref_set_pid(&pidref, pid);
if (q < 0)
return q;
*ret = TAKE_PIDREF(pidref);
return r;
}
@@ -5589,6 +5580,7 @@ int unit_fork_helper_process_full(Unit *u, const char *name, bool into_cgroup, F
}
}
*ret = TAKE_PIDREF(pidref);
return 0;
}