]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - lib/fs.c
ss: introduce cgroup2 cache and helper functions
[mirror_iproute2.git] / lib / fs.c
index af36bea0987fa97f2452d71b5db9f1c81be650d1..e265fc04eaec5ae62ce524500a5448145b2ecd54 100644 (file)
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -20,9 +20,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#ifdef HAVE_LIBBSD
-#include <bsd/string.h>
-#endif
 #include <errno.h>
 #include <limits.h>
 
@@ -62,13 +59,18 @@ static char *find_fs_mount(const char *fs_to_find)
 }
 
 /* caller needs to free string returned */
-char *find_cgroup2_mount(void)
+char *find_cgroup2_mount(bool do_mount)
 {
        char *mnt = find_fs_mount(CGROUP2_FS_NAME);
 
        if (mnt)
                return mnt;
 
+       if (!do_mount) {
+               fprintf(stderr, "Failed to find cgroup2 mount\n");
+               return NULL;
+       }
+
        mnt = strdup(MNT_CGRP2_PATH);
        if (!mnt) {
                fprintf(stderr, "Failed to allocate memory for cgroup2 path\n");
@@ -77,7 +79,7 @@ char *find_cgroup2_mount(void)
        }
 
        if (make_path(mnt, 0755)) {
-               fprintf(stderr, "Failed to setup vrf cgroup2 directory\n");
+               fprintf(stderr, "Failed to setup cgroup2 directory\n");
                free(mnt);
                return NULL;
        }
@@ -102,6 +104,134 @@ out:
        return mnt;
 }
 
+__u64 get_cgroup2_id(const char *path)
+{
+       char fh_buf[sizeof(struct file_handle) + sizeof(__u64)] = { 0 };
+       struct file_handle *fhp = (struct file_handle *)fh_buf;
+       union {
+               __u64 id;
+               unsigned char bytes[sizeof(__u64)];
+       } cg_id = { .id = 0 };
+       char *mnt = NULL;
+       int mnt_fd = -1;
+       int mnt_id;
+
+       if (!path) {
+               fprintf(stderr, "Invalid cgroup2 path\n");
+               return 0;
+       }
+
+       fhp->handle_bytes = sizeof(__u64);
+       if (name_to_handle_at(AT_FDCWD, path, fhp, &mnt_id, 0) < 0) {
+               /* try at cgroup2 mount */
+
+               while (*path == '/')
+                       path++;
+               if (*path == '\0') {
+                       fprintf(stderr, "Invalid cgroup2 path\n");
+                       goto out;
+               }
+
+               mnt = find_cgroup2_mount(false);
+               if (!mnt)
+                       goto out;
+
+               mnt_fd = open(mnt, O_RDONLY);
+               if (mnt_fd < 0) {
+                       fprintf(stderr, "Failed to open cgroup2 mount\n");
+                       goto out;
+               }
+
+               fhp->handle_bytes = sizeof(__u64);
+               if (name_to_handle_at(mnt_fd, path, fhp, &mnt_id, 0) < 0) {
+                       fprintf(stderr, "Failed to get cgroup2 ID: %s\n",
+                                       strerror(errno));
+                       goto out;
+               }
+               if (fhp->handle_bytes != sizeof(__u64)) {
+                       fprintf(stderr, "Invalid size of cgroup2 ID\n");
+                       goto out;
+               }
+       }
+
+       memcpy(cg_id.bytes, fhp->f_handle, sizeof(__u64));
+
+out:
+       close(mnt_fd);
+       free(mnt);
+
+       return cg_id.id;
+}
+
+#define FILEID_INO32_GEN 1
+
+/* caller needs to free string returned */
+char *get_cgroup2_path(__u64 id, bool full)
+{
+       char fh_buf[sizeof(struct file_handle) + sizeof(__u64)] = { 0 };
+       struct file_handle *fhp = (struct file_handle *)fh_buf;
+       union {
+               __u64 id;
+               unsigned char bytes[sizeof(__u64)];
+       } cg_id = { .id = id };
+       int mnt_fd = -1, fd = -1;
+       char link_buf[PATH_MAX];
+       char *path = NULL;
+       char fd_path[64];
+       int link_len;
+       char *mnt;
+
+       if (!id) {
+               fprintf(stderr, "Invalid cgroup2 ID\n");
+               return NULL;
+       }
+
+       mnt = find_cgroup2_mount(false);
+       if (!mnt)
+               return NULL;
+
+       mnt_fd = open(mnt, O_RDONLY);
+       if (mnt_fd < 0) {
+               fprintf(stderr, "Failed to open cgroup2 mount\n");
+               goto out;
+       }
+
+       fhp->handle_bytes = sizeof(__u64);
+       fhp->handle_type = FILEID_INO32_GEN;
+       memcpy(fhp->f_handle, cg_id.bytes, sizeof(__u64));
+
+       fd = open_by_handle_at(mnt_fd, fhp, 0);
+       if (fd < 0) {
+               fprintf(stderr, "Failed to open cgroup2 by ID\n");
+               goto out;
+       }
+
+       snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", fd);
+       link_len = readlink(fd_path, link_buf, sizeof(link_buf) - 1);
+       if (link_len < 0) {
+               fprintf(stderr,
+                       "Failed to read value of symbolic link %s\n",
+                       fd_path);
+               goto out;
+       }
+       link_buf[link_len] = '\0';
+
+       if (full)
+               path = strdup(link_buf);
+       else
+               path = strdup(link_buf + strlen(mnt));
+       if (!path)
+               fprintf(stderr,
+                       "Failed to allocate memory for cgroup2 path\n");
+
+out:
+       close(fd);
+       close(mnt_fd);
+       free(mnt);
+
+       return path;
+}
+
 int make_path(const char *path, mode_t mode)
 {
        char *dir, *delim;