From 4668b2cac6354157346ae1a8cb97cb3f5b68506f Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Thu, 31 Oct 2024 23:59:45 +0000 Subject: [PATCH] imp: cgroup: fix relative cgroup paths in containers Problem: Relative paths for cgroups in /proc/self/cgroup may contain leading `/..` when the IMP is run inside some containers. This is because the path is relative to the container's mount point, not the actual mount point of the cgroupfs. Just strip the leading /.. from paths during discovery of the current cgroup path from `/proc/self/cgroup`. This should only apply in containers and thus CI. --- src/imp/cgroup.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/imp/cgroup.c b/src/imp/cgroup.c index 0a1c797..c19e783 100644 --- a/src/imp/cgroup.c +++ b/src/imp/cgroup.c @@ -44,6 +44,13 @@ #include "cgroup.h" #include "imp_log.h" +static char *remove_leading_dotdot (char *relpath) +{ + while (strncmp (relpath, "/..", 3) == 0) + relpath += 3; + return relpath; +} + /* * Looks up the 'pids'[*] subsystem relative cgroup path in * /proc/PID/cgroups and prepends `cgroup_mount_dir` to get the @@ -80,6 +87,11 @@ static int cgroup_init_path (struct cgroup_info *cgroup) /* Nullify subsys, relpath is already nul-terminated at newline */ *(relpath++) = '\0'; + /* Remove leading /.. in relpath. This could be due to cgroup + * mounted in a container. + */ + relpath = remove_leading_dotdot (relpath); + /* If unified cgroups are being used, then stop when we find * subsys="". Otherwise stop at subsys="name=systemd": */