]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup-util: add cg_path_get_cgroupid()
authorIago López Galeiras <iagol@microsoft.com>
Fri, 11 Dec 2020 12:15:25 +0000 (13:15 +0100)
committerIago Lopez Galeiras <iagol@microsoft.com>
Wed, 6 Oct 2021 08:52:14 +0000 (10:52 +0200)
It returns the cgroupID from a cgroup path.

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

index 9ba9f375b05a3c18e6b7bcd5b7e4d4967fe6d8a1..221157d57aaa8225f9d65310cdde0ab4f725bcf8 100644 (file)
@@ -1367,6 +1367,29 @@ int cg_pid_get_machine_name(pid_t pid, char **machine) {
         return cg_path_get_machine_name(cgroup, machine);
 }
 
+int cg_path_get_cgroupid(const char *path, uint64_t *ret) {
+        int mnt_id = -1;
+
+        assert(path);
+        assert(ret);
+
+        union {
+                struct file_handle f_handle;
+                uint8_t space[offsetof(struct file_handle, f_handle) + sizeof(uint64_t)];
+        } buf = {
+                .f_handle.handle_bytes = sizeof(uint64_t),
+        };
+
+        /* This is cgroupfs so we know the size of the handle, thus no need to loop around like
+         * name_to_handle_at_loop() does in mountpoint-util.c */
+        if (name_to_handle_at(AT_FDCWD, path, &buf.f_handle, &mnt_id, 0) < 0)
+                return -errno;
+
+        *ret = *(uint64_t *) buf.f_handle.f_handle;
+
+        return 0;
+}
+
 int cg_path_get_session(const char *path, char **session) {
         _cleanup_free_ char *unit = NULL;
         char *start, *end;
index 90ccd2c032652b1cde79b9f8a9001f36880cb06f..eec13e18f17e3ac97f6841060b3ddb28285c3b8d 100644 (file)
@@ -236,6 +236,7 @@ int cg_is_empty_recursive(const char *controller, const char *path);
 
 int cg_get_root_path(char **path);
 
+int cg_path_get_cgroupid(const char *path, uint64_t *ret);
 int cg_path_get_session(const char *path, char **session);
 int cg_path_get_owner_uid(const char *path, uid_t *uid);
 int cg_path_get_unit(const char *path, char **unit);