]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
cgroups: fix prune_init_scope()
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 19 Feb 2021 17:25:30 +0000 (18:25 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 19 Feb 2021 17:25:30 +0000 (18:25 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/cgroups/cgroup.c
src/lxc/cgroups/cgroup.h
src/lxc/cgroups/cgroup_utils.c
src/lxc/cgroups/cgroup_utils.h

index d29aa483b4b55de112eac9e9ef866e92c1bf65e9..68e3e3e2504a1dd3309335345aced8a1e480498b 100644 (file)
@@ -106,15 +106,3 @@ void cgroup_exit(struct cgroup_ops *ops)
 
        return;
 }
-
-#define INIT_SCOPE "/init.scope"
-char *prune_init_scope(char *cg)
-{
-       if (is_empty_string(cg))
-               return NULL;
-
-       if (strnequal(cg, INIT_SCOPE, STRLITERALLEN(INIT_SCOPE)))
-               return cg + STRLITERALLEN(INIT_SCOPE);
-
-       return cg;
-}
index c4a3afe86c73b3f8d3298386577a31bc03720360..3a8e3f3ef348cbb40df3eec6e13a405a656c312f 100644 (file)
@@ -207,8 +207,6 @@ __hidden extern struct cgroup_ops *cgroup_init(struct lxc_conf *conf);
 __hidden extern void cgroup_exit(struct cgroup_ops *ops);
 define_cleanup_function(struct cgroup_ops *, cgroup_exit);
 
-__hidden extern char *prune_init_scope(char *cg);
-
 __hidden extern int cgroup_attach(const struct lxc_conf *conf, const char *name,
                                  const char *lxcpath, pid_t pid);
 __hidden extern int cgroup_get(const char *name, const char *lxcpath,
index ac748fe6df74b5a77a1d025231a9380de6e34b30..448fc9fa576945e7acb1fd6c94eae9e17b416b22 100644 (file)
@@ -159,3 +159,32 @@ int cgroup_tree_prune(int dfd, const char *path)
 
        return 0;
 }
+
+#define INIT_SCOPE "/init.scope"
+char *prune_init_scope(char *path)
+{
+       char *slash = path;
+       size_t len;
+
+       /*
+        * This function can only be called on information parsed from
+        * /proc/<pid>/cgroup. The file displays the current cgroup of the
+        * process as absolute paths. So if we are passed a non-absolute path
+        * things are way wrong.
+        */
+       if (!abspath(path))
+               return ret_set_errno(NULL, EINVAL);
+
+       len = strlen(path);
+       if (len < STRLITERALLEN(INIT_SCOPE))
+               return path;
+
+       slash += (len - STRLITERALLEN(INIT_SCOPE));
+       if (strequal(slash, INIT_SCOPE)) {
+               if (slash == path)
+                       slash++;
+               *slash = '\0';
+       }
+
+       return path;
+}
index 142b4db79a21cc8755a0f36e0090a8478a24efd3..77bf40c15a214ccff070261f3836e1925f8ecdd2 100644 (file)
@@ -43,4 +43,11 @@ static inline bool cgns_supported(void)
 
 __hidden extern int cgroup_tree_prune(int dfd, const char *path);
 
+/*
+ * This function can only be called on information parsed from
+ * /proc/<pid>/cgroup or on absolute paths and it will verify the latter and
+ * return NULL if a relative path is passed.
+ */
+__hidden extern char *prune_init_scope(char *path);
+
 #endif /* __LXC_CGROUP_UTILS_H */