]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup-util: add reusable union type for cgroupfs file_handle structs
authorLennart Poettering <lennart@poettering.net>
Wed, 6 Oct 2021 15:02:08 +0000 (17:02 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 7 Oct 2021 09:49:44 +0000 (11:49 +0200)
That way we can easily call name_to_handle_at() on cgroupfs2 elsewhere.

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

index 1d577a24eca6b7fb88be9d6b76877fd871336d01..e60db5e48c2e7c942567e1db3cbd30695033831e 100644 (file)
@@ -1368,25 +1368,18 @@ int cg_pid_get_machine_name(pid_t pid, char **machine) {
 }
 
 int cg_path_get_cgroupid(const char *path, uint64_t *ret) {
+        cg_file_handle fh = CG_FILE_HANDLE_INIT;
         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)
+        if (name_to_handle_at(AT_FDCWD, path, &fh.file_handle, &mnt_id, 0) < 0)
                 return -errno;
 
-        *ret = *(uint64_t *) buf.f_handle.f_handle;
-
+        *ret = CG_FILE_HANDLE_CGROUPID(fh);
         return 0;
 }
 
index 43801ee0f44f89d85aec5d815254146f6ec89df5..21275e9eaa6f6f146607b33f32ed57563621e840 100644 (file)
@@ -2,6 +2,7 @@
 #pragma once
 
 #include <dirent.h>
+#include <fcntl.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -315,3 +316,12 @@ typedef enum ManagedOOMPreference {
 
 const char* managed_oom_preference_to_string(ManagedOOMPreference a) _const_;
 ManagedOOMPreference managed_oom_preference_from_string(const char *s) _pure_;
+
+/* The structure to pass to name_to_handle_at() on cgroupfs2 */
+typedef union {
+        struct file_handle file_handle;
+        uint8_t space[offsetof(struct file_handle, f_handle) + sizeof(uint64_t)];
+} cg_file_handle;
+
+#define CG_FILE_HANDLE_INIT { .file_handle.handle_bytes = sizeof(uint64_t) }
+#define CG_FILE_HANDLE_CGROUPID(fh) (*(uint64_t*) (fh).file_handle.f_handle)