]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
cgroups: add cgroup_get()
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 2 Feb 2021 15:59:38 +0000 (16:59 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 2 Feb 2021 16:10:10 +0000 (17:10 +0100)
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 <christian.brauner@ubuntu.com>
src/lxc/cgroups/cgfsng.c
src/lxc/cgroups/cgroup.h

index 6b89a4acc896e4f62f90f0ec697ea98507f08e84..b8c6e640e370fd657e9d78dcf24c67e80ce9d102 100644 (file)
@@ -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.
index 7d95dfd35356bc974d067c90d23e3d9557744eba..b47561daae2707959d38f77be90deda12460888a 100644 (file)
@@ -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)
 {