core: use PidRef in exec_spawn

This commit is contained in:
Luca Boccassi 2024-01-12 21:18:27 +00:00 committed by Luca Boccassi
parent 7a87d01f28
commit 556d2bc4a1
8 changed files with 17 additions and 38 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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;
}

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;