From 556d2bc4a10dbe1eae506ced22596ac441a4d04e Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Fri, 12 Jan 2024 21:18:27 +0000 Subject: [PATCH] core: use PidRef in exec_spawn --- src/basic/process-util.c | 9 ++++----- src/basic/process-util.h | 2 +- src/core/execute.c | 14 +++++++------- src/core/execute.h | 2 +- src/core/mount.c | 7 +------ src/core/service.c | 7 +------ src/core/socket.c | 7 +------ src/core/swap.c | 7 +------ 8 files changed, 17 insertions(+), 38 deletions(-) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 697c8d9c6ba..1238ef81505 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -2034,7 +2034,7 @@ int make_reaper_process(bool b) { return 0; } -int posix_spawn_wrapper(const char *path, char *const *argv, char *const *envp, pid_t *ret_pid) { +int posix_spawn_wrapper(const char *path, char *const *argv, char *const *envp, PidRef *ret_pidref) { posix_spawnattr_t attr; sigset_t mask; pid_t pid; @@ -2047,7 +2047,7 @@ int posix_spawn_wrapper(const char *path, char *const *argv, char *const *envp, assert(path); assert(argv); - assert(ret_pid); + assert(ret_pidref); assert_se(sigfillset(&mask) >= 0); @@ -2068,10 +2068,9 @@ int posix_spawn_wrapper(const char *path, char *const *argv, char *const *envp, if (r != 0) goto fail; - *ret_pid = pid; - posix_spawnattr_destroy(&attr); - return 0; + + return pidref_set_pid(ret_pidref, pid); fail: assert(r > 0); diff --git a/src/basic/process-util.h b/src/basic/process-util.h index b270fc82ea1..bfa43318956 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -238,7 +238,7 @@ int get_process_threads(pid_t pid); int is_reaper_process(void); int make_reaper_process(bool b); -int posix_spawn_wrapper(const char *path, char *const *argv, char *const *envp, pid_t *ret_pid); +int posix_spawn_wrapper(const char *path, char *const *argv, char *const *envp, PidRef *ret_pidref); int proc_dir_open(DIR **ret); int proc_dir_read(DIR *d, pid_t *ret); diff --git a/src/core/execute.c b/src/core/execute.c index d4095ae01a0..c9c07714a3b 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -357,13 +357,13 @@ int exec_spawn(Unit *unit, ExecParameters *params, ExecRuntime *runtime, const CGroupContext *cgroup_context, - pid_t *ret) { + PidRef *ret) { char serialization_fd_number[DECIMAL_STR_MAX(int) + 1]; _cleanup_free_ char *subcgroup_path = NULL, *log_level = NULL, *executor_path = NULL; + _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; _cleanup_fdset_free_ FDSet *fdset = NULL; _cleanup_fclose_ FILE *f = NULL; - pid_t pid; int r; assert(unit); @@ -451,21 +451,21 @@ int exec_spawn(Unit *unit, "--log-level", log_level, "--log-target", log_target_to_string(manager_get_executor_log_target(unit->manager))), environ, - &pid); + &pidref); if (r < 0) return log_unit_error_errno(unit, r, "Failed to spawn executor: %m"); - log_unit_debug(unit, "Forked %s as "PID_FMT, command->path, pid); + log_unit_debug(unit, "Forked %s as "PID_FMT, command->path, pidref.pid); /* We add the new process to the cgroup both in the child (so that we can be sure that no user code is ever * executed outside of the cgroup) and in the parent (so that we can be sure that when we kill the cgroup the * process will be killed too). */ if (subcgroup_path) - (void) cg_attach(SYSTEMD_CGROUP_CONTROLLER, subcgroup_path, pid); + (void) cg_attach(SYSTEMD_CGROUP_CONTROLLER, subcgroup_path, pidref.pid); - exec_status_start(&command->exec_status, pid); + exec_status_start(&command->exec_status, pidref.pid); - *ret = pid; + *ret = TAKE_PIDREF(pidref); return 0; } diff --git a/src/core/execute.h b/src/core/execute.h index 6f91309b165..e226654c6ab 100644 --- a/src/core/execute.h +++ b/src/core/execute.h @@ -482,7 +482,7 @@ int exec_spawn(Unit *unit, ExecParameters *exec_params, ExecRuntime *runtime, const CGroupContext *cgroup_context, - pid_t *ret); + PidRef *ret); void exec_command_done(ExecCommand *c); void exec_command_done_array(ExecCommand *c, size_t n); diff --git a/src/core/mount.c b/src/core/mount.c index 6c3f4be981a..1fa2c2f9e2e 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -917,7 +917,6 @@ static int mount_spawn(Mount *m, ExecCommand *c, PidRef *ret_pid) { _cleanup_(exec_params_shallow_clear) ExecParameters exec_params = EXEC_PARAMETERS_INIT( EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN); _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; - pid_t pid; int r; assert(m); @@ -942,11 +941,7 @@ static int mount_spawn(Mount *m, ExecCommand *c, PidRef *ret_pid) { &exec_params, m->exec_runtime, &m->cgroup_context, - &pid); - if (r < 0) - return r; - - r = pidref_set_pid(&pidref, pid); + &pidref); if (r < 0) return r; diff --git a/src/core/service.c b/src/core/service.c index 2d5c43bca59..8ea04c469d2 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1607,7 +1607,6 @@ static int service_spawn_internal( _cleanup_strv_free_ char **final_env = NULL, **our_env = NULL; _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; size_t n_env = 0; - pid_t pid; int r; assert(caller); @@ -1798,17 +1797,13 @@ static int service_spawn_internal( &exec_params, s->exec_runtime, &s->cgroup_context, - &pid); + &pidref); if (r < 0) return r; s->exec_fd_event_source = TAKE_PTR(exec_fd_source); s->exec_fd_hot = false; - r = pidref_set_pid(&pidref, pid); - if (r < 0) - return r; - r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true); if (r < 0) return r; diff --git a/src/core/socket.c b/src/core/socket.c index bba7142c484..2a5a8386d69 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -1879,7 +1879,6 @@ static int socket_spawn(Socket *s, ExecCommand *c, PidRef *ret_pid) { _cleanup_(exec_params_shallow_clear) ExecParameters exec_params = EXEC_PARAMETERS_INIT( EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN); _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; - pid_t pid; int r; assert(s); @@ -1904,11 +1903,7 @@ static int socket_spawn(Socket *s, ExecCommand *c, PidRef *ret_pid) { &exec_params, s->exec_runtime, &s->cgroup_context, - &pid); - if (r < 0) - return r; - - r = pidref_set_pid(&pidref, pid); + &pidref); if (r < 0) return r; diff --git a/src/core/swap.c b/src/core/swap.c index 33f6cb2536a..4faee7abec2 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -630,7 +630,6 @@ static int swap_spawn(Swap *s, ExecCommand *c, PidRef *ret_pid) { _cleanup_(exec_params_shallow_clear) ExecParameters exec_params = EXEC_PARAMETERS_INIT( EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN); _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; - pid_t pid; int r; assert(s); @@ -655,11 +654,7 @@ static int swap_spawn(Swap *s, ExecCommand *c, PidRef *ret_pid) { &exec_params, s->exec_runtime, &s->cgroup_context, - &pid); - if (r < 0) - return r; - - r = pidref_set_pid(&pidref, pid); + &pidref); if (r < 0) return r; -- 2.39.5