From: Christian Brauner Date: Fri, 19 Feb 2021 17:25:30 +0000 (+0100) Subject: cgroups: fix prune_init_scope() X-Git-Tag: lxc-5.0.0~274^2~31 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=37ac0b481d0f7420b632a7de829a44f7e72e2cb4;p=thirdparty%2Flxc.git cgroups: fix prune_init_scope() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/cgroups/cgroup.c b/src/lxc/cgroups/cgroup.c index d29aa483b..68e3e3e25 100644 --- a/src/lxc/cgroups/cgroup.c +++ b/src/lxc/cgroups/cgroup.c @@ -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; -} diff --git a/src/lxc/cgroups/cgroup.h b/src/lxc/cgroups/cgroup.h index c4a3afe86..3a8e3f3ef 100644 --- a/src/lxc/cgroups/cgroup.h +++ b/src/lxc/cgroups/cgroup.h @@ -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, diff --git a/src/lxc/cgroups/cgroup_utils.c b/src/lxc/cgroups/cgroup_utils.c index ac748fe6d..448fc9fa5 100644 --- a/src/lxc/cgroups/cgroup_utils.c +++ b/src/lxc/cgroups/cgroup_utils.c @@ -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//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; +} diff --git a/src/lxc/cgroups/cgroup_utils.h b/src/lxc/cgroups/cgroup_utils.h index 142b4db79..77bf40c15 100644 --- a/src/lxc/cgroups/cgroup_utils.h +++ b/src/lxc/cgroups/cgroup_utils.h @@ -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//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 */