Kir Kolyshkin 9309bd
From d35551d8c6a9c46442500992abfb67774f9fa8d8 Mon Sep 17 00:00:00 2001
Kir Kolyshkin 9309bd
From: Jonas Witschel <diabonas@archlinux.org>
Kir Kolyshkin 9309bd
Date: Wed, 10 Nov 2021 22:46:35 +0100
Kir Kolyshkin 9309bd
Subject: [PATCH] scope: count successful cgroup additions when delegating via
Kir Kolyshkin 9309bd
 D-Bus
Kir Kolyshkin 9309bd
Kir Kolyshkin 9309bd
Since commit 8d3e4ac7cd37200d1431411a4b98925a24b7d9b3 ("scope: refuse
Kir Kolyshkin 9309bd
activation of scopes if no PIDs to add are left") all "systemd-run --scope
Kir Kolyshkin 9309bd
--user" calls fail because cgroup attachments delegated to the system instance
Kir Kolyshkin 9309bd
are not counted towards successful additions. Fix this by incrementing the
Kir Kolyshkin 9309bd
return value in case unit_attach_pid_to_cgroup_via_bus() succeeds, similar to
Kir Kolyshkin 9309bd
what happens when cg_attach() succeeds directly.
Kir Kolyshkin 9309bd
Kir Kolyshkin 9309bd
Note that this can *not* distinguish the case when
Kir Kolyshkin 9309bd
unit_attach_pid_to_cgroup_via_bus() has been run successfully, but all
Kir Kolyshkin 9309bd
processes to attach are gone in the meantime, unlike the checks that commit
Kir Kolyshkin 9309bd
8d3e4ac7cd37200d1431411a4b98925a24b7d9b3 adds for the system instance. This is
Kir Kolyshkin 9309bd
because even though unit_attach_pid_to_cgroup_via_bus() leads to an internal
Kir Kolyshkin 9309bd
unit_attach_pids_to_cgroup() call, the return value over D-Bus does not include
Kir Kolyshkin 9309bd
the number of successfully attached processes and is always NULL on success.
Kir Kolyshkin 9309bd
Kir Kolyshkin 9309bd
Fixes: #21297
Kir Kolyshkin 9309bd
---
Kir Kolyshkin 9309bd
 src/core/cgroup.c | 5 ++++-
Kir Kolyshkin 9309bd
 1 file changed, 4 insertions(+), 1 deletion(-)
Kir Kolyshkin 9309bd
Kir Kolyshkin 9309bd
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
Kir Kolyshkin 9309bd
index abc30e3990c4..c942db8d05eb 100644
Kir Kolyshkin 9309bd
--- a/src/core/cgroup.c
Kir Kolyshkin 9309bd
+++ b/src/core/cgroup.c
Kir Kolyshkin 9309bd
@@ -2283,8 +2283,11 @@ int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path) {
Kir Kolyshkin 9309bd
                                 z = unit_attach_pid_to_cgroup_via_bus(u, pid, suffix_path);
Kir Kolyshkin 9309bd
                                 if (z < 0)
Kir Kolyshkin 9309bd
                                         log_unit_info_errno(u, z, "Couldn't move process "PID_FMT" to requested cgroup '%s' (directly or via the system bus): %m", pid, empty_to_root(p));
Kir Kolyshkin 9309bd
-                                else
Kir Kolyshkin 9309bd
+                                else {
Kir Kolyshkin 9309bd
+                                        if (ret >= 0)
Kir Kolyshkin 9309bd
+                                                ret++; /* Count successful additions */
Kir Kolyshkin 9309bd
                                         continue; /* When the bus thing worked via the bus we are fully done for this PID. */
Kir Kolyshkin 9309bd
+                                }
Kir Kolyshkin 9309bd
                         }
Kir Kolyshkin 9309bd
 
Kir Kolyshkin 9309bd
                         if (ret >= 0)