]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: use PidRef in exec_spawn
authorLuca Boccassi <bluca@debian.org>
Fri, 12 Jan 2024 21:18:27 +0000 (21:18 +0000)
committerLuca Boccassi <luca.boccassi@gmail.com>
Thu, 1 Feb 2024 21:06:14 +0000 (21:06 +0000)
src/basic/process-util.c
src/basic/process-util.h
src/core/execute.c
src/core/execute.h
src/core/mount.c
src/core/service.c
src/core/socket.c
src/core/swap.c

index 697c8d9c6baf91bd23d54a22131f618ca8737e4c..1238ef815058f9807866229170a3bacaba2e3a74 100644 (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);
index b270fc82ea1641f53c230a2a62b11d0b24276238..bfa43318956f5bec702a2651136198bef708be66 100644 (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);
index d4095ae01a0c39448baa876393174c1e35b0f6da..c9c07714a3b310c951415b35d87b9164d9f6343d 100644 (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;
 }
 
index 6f91309b165e69adedaa5fb74912e4c382a2238b..e226654c6abe64ef07f4d6be92db304130913171 100644 (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);
index 6c3f4be981a48c1652edb2f6809c6705a593672f..1fa2c2f9e2e5aac4f068c5fb63eb4bb396a487f6 100644 (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;
 
index 2d5c43bca59791c4cb3464d0c9b0c19a00cf09b8..8ea04c469d2606096dafb030dc05b042d9cab06d 100644 (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;
index bba7142c484dd086c3bdb20b0b8b1f2322249f48..2a5a8386d6912fd51859ceb35df1b3669355101b 100644 (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;
 
index 33f6cb2536a0ff465fb627014e01e3959711a8d0..4faee7abec2291ea0855e0decd5640221ef5dc75 100644 (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;