]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
cgroups: split out unified cgroup helpers
authorChristian Brauner <christian.brauner@ubuntu.com>
Sat, 20 Feb 2021 00:31:05 +0000 (01:31 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sat, 20 Feb 2021 02:17:47 +0000 (03:17 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/cgroups/cgfsng.c

index 9d517a26e5b66bd499ddf241b6f6eff17c42056f..8431007f4b3db854b57b1823d6e258084996ec41 100644 (file)
@@ -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",