From: Iago López Galeiras Date: Fri, 11 Dec 2020 12:15:25 +0000 (+0100) Subject: cgroup-util: add cg_path_get_cgroupid() X-Git-Tag: v250-rc1~561^2~13 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=535e3dd091aedaa47a5e349146d264f8769844c1;p=thirdparty%2Fsystemd.git cgroup-util: add cg_path_get_cgroupid() It returns the cgroupID from a cgroup path. --- diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 9ba9f375b05..221157d57aa 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -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; diff --git a/src/basic/cgroup-util.h b/src/basic/cgroup-util.h index 90ccd2c0326..eec13e18f17 100644 --- a/src/basic/cgroup-util.h +++ b/src/basic/cgroup-util.h @@ -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);