]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup-util: introduce generic cg_path_from_cgroupid() helper
authorMike Yuan <me@yhndnzj.com>
Tue, 19 Nov 2024 20:13:05 +0000 (21:13 +0100)
committerMike Yuan <me@yhndnzj.com>
Sat, 4 Jan 2025 16:48:22 +0000 (17:48 +0100)
Taken from nsresourced/userns-registry.c userns_destroy_cgroup()

src/basic/cgroup-util.c
src/basic/cgroup-util.h

index 8247b0c25fc8e53b7401e5fe8f124a7e19524d84..50f1f0e4df2c3e809e8e464962535354ec2c6980 100644 (file)
@@ -68,6 +68,27 @@ int cg_cgroupid_open(int cgroupfs_fd, uint64_t id) {
         return RET_NERRNO(open_by_handle_at(cgroupfs_fd, &fh.file_handle, O_DIRECTORY|O_CLOEXEC));
 }
 
+int cg_path_from_cgroupid(int cgroupfs_fd, uint64_t id, char **ret) {
+        _cleanup_close_ int cgfd = -EBADF;
+        int r;
+
+        cgfd = cg_cgroupid_open(cgroupfs_fd, id);
+        if (cgfd < 0)
+                return cgfd;
+
+        _cleanup_free_ char *path = NULL;
+        r = fd_get_path(cgfd, &path);
+        if (r < 0)
+                return r;
+
+        if (!path_startswith(path, "/sys/fs/cgroup/"))
+                return -EXDEV; /* recognizable error */
+
+        if (ret)
+                *ret = TAKE_PTR(path);
+        return 0;
+}
+
 int cg_get_cgroupid_at(int dfd, const char *path, uint64_t *ret) {
         cg_file_handle fh = CG_FILE_HANDLE_INIT;
         int mnt_id;
index d471aee0c3a0b9e6294a300541263e82e9e89d8d..49c3ea313860531946a1e6e17b0889d01f5d8708 100644 (file)
@@ -183,6 +183,7 @@ typedef enum CGroupUnified {
 int cg_path_open(const char *controller, const char *path);
 int cg_cgroupid_open(int fsfd, uint64_t id);
 
+int cg_path_from_cgroupid(int cgroupfs_fd, uint64_t id, char **ret);
 int cg_get_cgroupid_at(int dfd, const char *path, uint64_t *ret);
 static inline int cg_path_get_cgroupid(const char *path, uint64_t *ret) {
         return cg_get_cgroupid_at(AT_FDCWD, path, ret);