]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
manager: move idle_pipe allocation to manager.c and make it atomic
authorLennart Poettering <lennart@poettering.net>
Wed, 18 Oct 2023 11:37:45 +0000 (13:37 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 18 Oct 2023 15:07:41 +0000 (16:07 +0100)
Let's make sure it either fails or suceeds, but never fails half-way
leaving a half-initialized array around.

src/core/manager.c
src/core/manager.h
src/core/transaction.c

index 9307a13a79ffd71785ea3e22b7c77be86f14833a..55321d1638a4c10875bb5141930d9d8ed1f0ce30 100644 (file)
@@ -4952,6 +4952,35 @@ ManagerTimestamp manager_timestamp_initrd_mangle(ManagerTimestamp s) {
         return s;
 }
 
+int manager_allocate_idle_pipe(Manager *m) {
+        int r;
+
+        assert(m);
+
+        if (m->idle_pipe[0] >= 0) {
+                assert(m->idle_pipe[1] >= 0);
+                assert(m->idle_pipe[2] >= 0);
+                assert(m->idle_pipe[3] >= 0);
+                return 0;
+        }
+
+        assert(m->idle_pipe[1] < 0);
+        assert(m->idle_pipe[2] < 0);
+        assert(m->idle_pipe[3] < 0);
+
+        r = RET_NERRNO(pipe2(m->idle_pipe + 0, O_NONBLOCK|O_CLOEXEC));
+        if (r < 0)
+                return r;
+
+        r = RET_NERRNO(pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC));
+        if (r < 0) {
+                safe_close_pair(m->idle_pipe + 0);
+                return r;
+        }
+
+        return 1;
+}
+
 void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) {
         assert(defaults);
         assert(scope >= 0);
index 6321a353a5a1525ac82d2f7ce724abdb4e98cb67..3b3920078e6da37fd9792bfa905a9763ebbb60a8 100644 (file)
@@ -635,6 +635,8 @@ int manager_override_watchdog_pretimeout_governor(Manager *m, const char *govern
 
 LogTarget manager_get_executor_log_target(Manager *m);
 
+int manager_allocate_idle_pipe(Manager *m);
+
 const char* oom_policy_to_string(OOMPolicy i) _const_;
 OOMPolicy oom_policy_from_string(const char *s) _pure_;
 
index 85f673f447ddc3c6c3cf7f5e2accf768ec2b9783..a81c40fb062a3a14dbb46502d89eb358654bc32f 100644 (file)
@@ -781,19 +781,11 @@ int transaction_activate(
 
         assert(hashmap_isempty(tr->jobs));
 
-        if (!hashmap_isempty(m->jobs)) {
-                /* Are there any jobs now? Then make sure we have the
-                 * idle pipe around. We don't really care too much
-                 * whether this works or not, as the idle pipe is a
-                 * feature for cosmetics, not actually useful for
-                 * anything beyond that. */
-
-                if (m->idle_pipe[0] < 0 && m->idle_pipe[1] < 0 &&
-                    m->idle_pipe[2] < 0 && m->idle_pipe[3] < 0) {
-                        (void) pipe2(m->idle_pipe, O_NONBLOCK|O_CLOEXEC);
-                        (void) pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC);
-                }
-        }
+        /* Are there any jobs now? Then make sure we have the idle pipe around. We don't really care too much
+         * whether this works or not, as the idle pipe is a feature for cosmetics, not actually useful for
+         * anything beyond that. */
+        if (!hashmap_isempty(m->jobs))
+                (void) manager_allocate_idle_pipe(m);
 
         return 0;
 }