From: Christian Brauner Date: Tue, 2 Feb 2021 15:59:38 +0000 (+0100) Subject: cgroups: add cgroup_get() X-Git-Tag: lxc-5.0.0~305^2~31 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b1356424886989139f83a63f0709007e5792d1f5;p=thirdparty%2Flxc.git cgroups: add cgroup_get() This is a unified hierarchy only method which doesn't need to initialize a full cgroup driver. Instead, it relies on the command socket to retrieve a cgroup2 file descriptor to the container's cgroup. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c index 6b89a4acc..b8c6e640e 100644 --- a/src/lxc/cgroups/cgfsng.c +++ b/src/lxc/cgroups/cgfsng.c @@ -2500,6 +2500,31 @@ __cgfsng_ops static bool cgfsng_attach(struct cgroup_ops *ops, return true; } +int cgroup_get(struct lxc_conf *conf, const char *filename, + char *buf, size_t len, + const char *name, const char *lxcpath) +{ + __do_close int unified_fd = -EBADF; + ssize_t ret; + + if (!conf || is_empty_string(filename) || is_empty_string(name) || + is_empty_string(lxcpath)) + return ret_errno(EINVAL); + + if ((buf && !len) || (len && !buf)) + return ret_errno(EINVAL); + + unified_fd = lxc_cmd_get_cgroup2_fd(name, lxcpath); + if (unified_fd < 0) + return ret_errno(ENOCGROUP2); + + ret = lxc_read_try_buf_at(unified_fd, filename, buf, len); + if (ret < 0) + SYSERROR("Failed to read cgroup value"); + + return ret; +} + /* Called externally (i.e. from 'lxc-cgroup') to query cgroup limits. Here we * don't have a cgroup_data set up, so we ask the running container through the * commands API for the cgroup path. diff --git a/src/lxc/cgroups/cgroup.h b/src/lxc/cgroups/cgroup.h index 7d95dfd35..b47561daa 100644 --- a/src/lxc/cgroups/cgroup.h +++ b/src/lxc/cgroups/cgroup.h @@ -191,6 +191,9 @@ __hidden extern void 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(struct lxc_conf *conf, const char *filename, + char *buf, size_t len, const char *name, + const char *lxcpath); static inline bool pure_unified_layout(const struct cgroup_ops *ops) {