From 0e3af26b2ff6e9e22b6a8214d8d730a4d16c1f49 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Sat, 20 Feb 2021 01:31:05 +0100 Subject: [PATCH] cgroups: split out unified cgroup helpers Signed-off-by: Christian Brauner --- src/lxc/cgroups/cgfsng.c | 48 +++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 9d517a26e..8431007f4 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -3418,6 +3418,34 @@ static int cg_hybrid_init(struct cgroup_ops *ops, bool relative, bool unprivileg return 0; } +static inline bool __current_cgroup_unified_line(const char *line) +{ + return *line == '0'; +} + +static inline char *__current_cgroup_unified(bool relative, char *line) +{ + char *current_cgroup; + + line += STRLITERALLEN("0::"); + + if (!abspath(line)) + return ERR_PTR(-EINVAL); + + /* remove init.scope */ + if (!relative) + line = prune_init_scope(line); + + /* create a relative path */ + line = deabs(line); + + current_cgroup = strdup(line); + if (!current_cgroup) + return ERR_PTR(-ENOMEM); + + return current_cgroup; +} + /* Get current cgroup from /proc/self/cgroup for the cgroupfs v2 hierarchy. */ static char *current_cgroup_unified(bool relative) { @@ -3432,22 +3460,16 @@ static char *current_cgroup_unified(bool relative) return NULL; lxc_iterate_parts(it, cgroup_info, "\n") { - if (*it != '0') - continue; + char *current_cgroup; - it += STRLITERALLEN("0::"); - - if (!abspath(it)) - return log_error(NULL, "Corrupt cgroup info for %s process", relative ? "current" : "init"); - - /* remove init.scope */ - if (!relative) - it = prune_init_scope(it); + if (!__current_cgroup_unified_line(it)) + continue; - /* create a relative path */ - it = deabs(it); + current_cgroup = __current_cgroup_unified(relative, it); + if (IS_ERR(current_cgroup)) + return NULL; - return strdup(it); + return current_cgroup; } return log_error(NULL, "Failed to retrieve current cgroup for %s process", -- 2.47.2