From: Lennart Poettering Date: Wed, 6 Oct 2021 15:02:08 +0000 (+0200) Subject: cgroup-util: add reusable union type for cgroupfs file_handle structs X-Git-Tag: v250-rc1~555^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a5edf95ec2e38c01abbc237ca4c906f102c224d8;p=thirdparty%2Fsystemd.git cgroup-util: add reusable union type for cgroupfs file_handle structs That way we can easily call name_to_handle_at() on cgroupfs2 elsewhere. --- diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 1d577a24eca..e60db5e48c2 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -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; } diff --git a/src/basic/cgroup-util.h b/src/basic/cgroup-util.h index 43801ee0f44..21275e9eaa6 100644 --- a/src/basic/cgroup-util.h +++ b/src/basic/cgroup-util.h @@ -2,6 +2,7 @@ #pragma once #include +#include #include #include #include @@ -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)