]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: unify common code for preparing for forking off unit processes
authorLennart Poettering <lennart@poettering.net>
Fri, 17 Nov 2017 15:43:08 +0000 (16:43 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 21 Nov 2017 10:54:08 +0000 (11:54 +0100)
This introduces a new function unit_prepare_exec() that encapsulates a
number of calls we do in preparation for spawning off some processes in
all our unit types that do so.

This allows us to neatly unify a bit of code between unit types and
shorten our code.

src/core/mount.c
src/core/mount.h
src/core/service.c
src/core/service.h
src/core/socket.c
src/core/socket.h
src/core/swap.c
src/core/swap.h
src/core/unit.c
src/core/unit.h

index 79a6e1626c6a2b998c1b0b1ec7252a5f2743deab..1500670b094d8193d8c291fed0d8c22629dabfb0 100644 (file)
@@ -750,33 +750,21 @@ static void mount_dump(Unit *u, FILE *f, const char *prefix) {
 }
 
 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
-        pid_t pid;
-        int r;
+
         ExecParameters exec_params = {
                 .flags      = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
                 .stdin_fd   = -1,
                 .stdout_fd  = -1,
                 .stderr_fd  = -1,
         };
+        pid_t pid;
+        int r;
 
         assert(m);
         assert(c);
         assert(_pid);
 
-        (void) unit_realize_cgroup(UNIT(m));
-        if (m->reset_accounting) {
-                (void) unit_reset_cpu_accounting(UNIT(m));
-                (void) unit_reset_ip_accounting(UNIT(m));
-                m->reset_accounting = false;
-        }
-
-        unit_export_state_files(UNIT(m));
-
-        r = unit_setup_exec_runtime(UNIT(m));
-        if (r < 0)
-                return r;
-
-        r = unit_setup_dynamic_creds(UNIT(m));
+        r = unit_prepare_exec(UNIT(m));
         if (r < 0)
                 return r;
 
@@ -1091,7 +1079,8 @@ static int mount_start(Unit *u) {
 
         m->result = MOUNT_SUCCESS;
         m->reload_result = MOUNT_SUCCESS;
-        m->reset_accounting = true;
+
+        u->reset_accounting = true;
 
         mount_enter_mounting(m);
         return 1;
index 8309441a8d8b364758422e12ddfaa86592fcf01d..44fe3b889eaa092df86ae9e11a68f64e614f5371 100644 (file)
@@ -68,8 +68,6 @@ struct Mount {
         bool just_mounted:1;
         bool just_changed:1;
 
-        bool reset_accounting:1;
-
         bool sloppy_options;
 
         bool lazy_unmount;
index 445d1becc108bbdac24a2afb9903db1436a8b693..cd23eb5233eaad629ed2114fe1c3ca7837bb45bf 100644 (file)
@@ -1223,24 +1223,26 @@ static int service_spawn(
                 ExecFlags flags,
                 pid_t *_pid) {
 
-        _cleanup_strv_free_ char **final_env = NULL, **our_env = NULL, **fd_names = NULL;
-        _cleanup_free_ int *fds = NULL;
-        unsigned n_storage_fds = 0, n_socket_fds = 0, n_env = 0;
-        pid_t pid;
-
         ExecParameters exec_params = {
                 .flags      = flags,
                 .stdin_fd   = -1,
                 .stdout_fd  = -1,
                 .stderr_fd  = -1,
         };
-
+        _cleanup_strv_free_ char **final_env = NULL, **our_env = NULL, **fd_names = NULL;
+        unsigned n_storage_fds = 0, n_socket_fds = 0, n_env = 0;
+        _cleanup_free_ int *fds = NULL;
+        pid_t pid;
         int r;
 
         assert(s);
         assert(c);
         assert(_pid);
 
+        r = unit_prepare_exec(UNIT(s));
+        if (r < 0)
+                return r;
+
         if (flags & EXEC_IS_CONTROL) {
                 /* If this is a control process, mask the permissions/chroot application if this is requested. */
                 if (s->permissions_start_only)
@@ -1249,23 +1251,6 @@ static int service_spawn(
                         exec_params.flags &= ~EXEC_APPLY_CHROOT;
         }
 
-        (void) unit_realize_cgroup(UNIT(s));
-        if (s->reset_accounting) {
-                (void) unit_reset_cpu_accounting(UNIT(s));
-                (void) unit_reset_ip_accounting(UNIT(s));
-                s->reset_accounting = false;
-        }
-
-        unit_export_state_files(UNIT(s));
-
-        r = unit_setup_exec_runtime(UNIT(s));
-        if (r < 0)
-                return r;
-
-        r = unit_setup_dynamic_creds(UNIT(s));
-        if (r < 0)
-                return r;
-
         if ((flags & EXEC_PASS_FDS) ||
             s->exec_context.std_input == EXEC_INPUT_SOCKET ||
             s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
@@ -2188,7 +2173,8 @@ static int service_start(Unit *u) {
         s->main_pid_known = false;
         s->main_pid_alien = false;
         s->forbid_restart = false;
-        s->reset_accounting = true;
+
+        u->reset_accounting = true;
 
         s->status_text = mfree(s->status_text);
         s->status_errno = 0;
index a529f48a634c4d973e619a75f9a1663d843f96d1..d6be140c548c802b40c28aff0db31de75a2f632d 100644 (file)
@@ -166,8 +166,6 @@ struct Service {
         bool forbid_restart:1;
         bool start_timeout_defined:1;
 
-        bool reset_accounting:1;
-
         char *bus_name;
         char *bus_name_owner; /* unique name of the current owner */
 
index 6c0d799bd97d6cfe89395821a26dbdd3eba2cb56..15922d919f103fb7ee161e40b93ebe6749e67ff1 100644 (file)
@@ -1865,33 +1865,21 @@ static int socket_coldplug(Unit *u) {
 }
 
 static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
-        pid_t pid;
-        int r;
+
         ExecParameters exec_params = {
                 .flags      = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
                 .stdin_fd   = -1,
                 .stdout_fd  = -1,
                 .stderr_fd  = -1,
         };
+        pid_t pid;
+        int r;
 
         assert(s);
         assert(c);
         assert(_pid);
 
-        (void) unit_realize_cgroup(UNIT(s));
-        if (s->reset_accounting) {
-                (void) unit_reset_cpu_accounting(UNIT(s));
-                (void) unit_reset_ip_accounting(UNIT(s));
-                s->reset_accounting = false;
-        }
-
-        unit_export_state_files(UNIT(s));
-
-        r = unit_setup_exec_runtime(UNIT(s));
-        if (r < 0)
-                return r;
-
-        r = unit_setup_dynamic_creds(UNIT(s));
+        r = unit_prepare_exec(UNIT(s));
         if (r < 0)
                 return r;
 
@@ -2471,7 +2459,8 @@ static int socket_start(Unit *u) {
                 return r;
 
         s->result = SOCKET_SUCCESS;
-        s->reset_accounting = true;
+
+        u->reset_accounting = true;
 
         socket_enter_start_pre(s);
         return 1;
index 81cfc975784cc8f05a10d07a20699fff33f25c9e..e9e560e57e8360a45eef23639562aed13b1ccc43 100644 (file)
@@ -162,8 +162,6 @@ struct Socket {
 
         char *user, *group;
 
-        bool reset_accounting:1;
-
         char *fdname;
 
         RateLimit trigger_limit;
index f37a81f56c754369c1ce536fb9709a6dd1270119..115e59c5cabf89aa6127335fa31258016b31eeca 100644 (file)
@@ -603,35 +603,23 @@ static void swap_dump(Unit *u, FILE *f, const char *prefix) {
 }
 
 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
-        pid_t pid;
-        int r;
+
         ExecParameters exec_params = {
                 .flags     = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
                 .stdin_fd  = -1,
                 .stdout_fd = -1,
                 .stderr_fd = -1,
         };
+        pid_t pid;
+        int r;
 
         assert(s);
         assert(c);
         assert(_pid);
 
-        (void) unit_realize_cgroup(UNIT(s));
-        if (s->reset_accounting) {
-                (void) unit_reset_cpu_accounting(UNIT(s));
-                (void) unit_reset_ip_accounting(UNIT(s));
-                s->reset_accounting = false;
-        }
-
-        unit_export_state_files(UNIT(s));
-
-        r = unit_setup_exec_runtime(UNIT(s));
-        if (r < 0)
-                goto fail;
-
-        r = unit_setup_dynamic_creds(UNIT(s));
+        r = unit_prepare_exec(UNIT(s));
         if (r < 0)
-                goto fail;
+                return r;
 
         r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
         if (r < 0)
@@ -868,7 +856,8 @@ static int swap_start(Unit *u) {
                 return r;
 
         s->result = SWAP_SUCCESS;
-        s->reset_accounting = true;
+
+        u->reset_accounting = true;
 
         swap_enter_activating(s);
         return 1;
index d1ee73de8e0c2cc0e1105ebb033084ddd97e77d2..fa9d45ac0cecbd8721bb45d185d4f59f5bca00a4 100644 (file)
@@ -71,8 +71,6 @@ struct Swap {
         bool is_active:1;
         bool just_activated:1;
 
-        bool reset_accounting:1;
-
         SwapResult result;
 
         usec_t timeout_usec;
index d5e6b3891b48b1cffb960cd45b4366cb99755ebe..10b416a65b9df8b0c2d1dbeffdf2899aeb788d50 100644 (file)
@@ -5145,6 +5145,34 @@ void unit_unlink_state_files(Unit *u) {
         }
 }
 
+int unit_prepare_exec(Unit *u) {
+        int r;
+
+        assert(u);
+
+        /* Prepares everything so that we can fork of a process for this unit */
+
+        (void) unit_realize_cgroup(u);
+
+        if (u->reset_accounting) {
+                (void) unit_reset_cpu_accounting(u);
+                (void) unit_reset_ip_accounting(u);
+                u->reset_accounting = false;
+        }
+
+        unit_export_state_files(u);
+
+        r = unit_setup_exec_runtime(u);
+        if (r < 0)
+                return r;
+
+        r = unit_setup_dynamic_creds(u);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
 static const char* const collect_mode_table[_COLLECT_MODE_MAX] = {
         [COLLECT_INACTIVE] = "inactive",
         [COLLECT_INACTIVE_OR_FAILED] = "inactive-or-failed",
index 2b11a285d45e85cac27535236600df6bb8a65330..0f756dcda4d56f3b3d3ae28e6baa0e564c862bba 100644 (file)
@@ -342,6 +342,9 @@ struct Unit {
 
         UnitCGroupBPFState cgroup_bpf_state:2;
 
+        /* Reset cgroup accounting next time we fork something off */
+        bool reset_accounting:1;
+
         bool start_limit_hit:1;
 
         /* Did we already invoke unit_coldplug() for this unit? */
@@ -760,6 +763,8 @@ void unit_remove_dependencies(Unit *u, UnitDependencyMask mask);
 void unit_export_state_files(Unit *u);
 void unit_unlink_state_files(Unit *u);
 
+int unit_prepare_exec(Unit *u);
+
 /* Macros which append UNIT= or USER_UNIT= to the message */
 
 #define log_unit_full(unit, level, error, ...)                          \