Skip to content

Commit 68ee5d7

Browse files
committed
core: support user manager with Condition[Memory/CPU/IO]Pressure
Get the cgroup root path from the current PID, so that when ran by the user manager we can get to the right path. Eg: foo.slice:10% will check under: /sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/foo.slice/cpu.pressure Follow-up for 81513b3
1 parent 8feb9fa commit 68ee5d7

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/shared/condition.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "psi-util.h"
4646
#include "selinux-util.h"
4747
#include "smack-util.h"
48+
#include "special.h"
4849
#include "stat-util.h"
4950
#include "string-table.h"
5051
#include "string-util.h"
@@ -992,14 +993,14 @@ static int condition_test_psi(Condition *c, char **env) {
992993
if (r == 1) {
993994
pressure_path = path_join("/proc/pressure", pressure_type);
994995
if (!pressure_path)
995-
return log_oom();
996+
return log_oom_debug();
996997

997998
value = first;
998999
} else {
9991000
const char *controller = strjoina(pressure_type, ".pressure");
1000-
_cleanup_free_ char *slice_path = NULL;
1001+
_cleanup_free_ char *slice_path = NULL, *root_scope = NULL;
10011002
CGroupMask mask, required_mask;
1002-
char *slice;
1003+
char *slice, *e;
10031004

10041005
required_mask = c->type == CONDITION_MEMORY_PRESSURE ? CGROUP_MASK_MEMORY :
10051006
c->type == CONDITION_CPU_PRESSURE ? CGROUP_MASK_CPU :
@@ -1030,6 +1031,26 @@ static int condition_test_psi(Condition *c, char **env) {
10301031
if (r < 0)
10311032
return log_debug_errno(r, "Cannot determine slice \"%s\" cgroup path: %m", slice);
10321033

1034+
/* We might be running under the user manager, so get the root path and prefix it accordingly. */
1035+
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &root_scope);
1036+
if (r < 0)
1037+
return log_debug_errno(r, "Failed to get root cgroup path: %m");
1038+
1039+
/* Drop init.scope, we want the parent. We could get an empty or / path, but that's fine,
1040+
* just skip it in that case. */
1041+
e = endswith(root_scope, "/" SPECIAL_INIT_SCOPE);
1042+
if (e)
1043+
*e = 0;
1044+
if (!empty_or_root(root_scope)) {
1045+
_cleanup_free_ char *slice_joined = NULL;
1046+
1047+
slice_joined = path_join(root_scope, slice_path);
1048+
if (!slice_joined)
1049+
return log_oom_debug();
1050+
1051+
free_and_replace(slice_path, slice_joined);
1052+
}
1053+
10331054
r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, slice_path, controller, &pressure_path);
10341055
if (r < 0)
10351056
return log_debug_errno(r, "Error getting cgroup pressure path from %s: %m", slice_path);

0 commit comments

Comments
 (0)