From: Mike Yuan Date: Fri, 29 Nov 2024 12:43:02 +0000 (+0100) Subject: cgroup-util: introduce cg_get_cgroupid_at() X-Git-Tag: v258-rc1~1898 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=eded4272d2197cbd582d50da2cb09cb384fd0749;p=thirdparty%2Fsystemd.git cgroup-util: introduce cg_get_cgroupid_at() Suggested in https://github.com/systemd/systemd/pull/35242#discussion_r1862658163 --- diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 309dccb45ab..8247b0c25fc 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -68,6 +68,24 @@ int cg_cgroupid_open(int cgroupfs_fd, uint64_t id) { return RET_NERRNO(open_by_handle_at(cgroupfs_fd, &fh.file_handle, O_DIRECTORY|O_CLOEXEC)); } +int cg_get_cgroupid_at(int dfd, const char *path, uint64_t *ret) { + cg_file_handle fh = CG_FILE_HANDLE_INIT; + int mnt_id; + + assert(dfd >= 0 || (dfd == AT_FDCWD && path_is_absolute(path))); + assert(ret); + + /* 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(dfd, strempty(path), &fh.file_handle, &mnt_id, isempty(path) ? AT_EMPTY_PATH : 0) < 0) { + assert(errno != EOVERFLOW); + return -errno; + } + + *ret = CG_FILE_HANDLE_CGROUPID(fh); + return 0; +} + static int cg_enumerate_items(const char *controller, const char *path, FILE **ret, const char *item) { _cleanup_free_ char *fs = NULL; FILE *f; @@ -1345,36 +1363,6 @@ int cg_pid_get_machine_name(pid_t pid, char **ret_machine) { return cg_path_get_machine_name(cgroup, ret_machine); } -int cg_path_get_cgroupid(const char *path, uint64_t *ret) { - cg_file_handle fh = CG_FILE_HANDLE_INIT; - int mnt_id; - - assert(path); - assert(ret); - - /* 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, &fh.file_handle, &mnt_id, 0) < 0) - return -errno; - - *ret = CG_FILE_HANDLE_CGROUPID(fh); - return 0; -} - -int cg_fd_get_cgroupid(int fd, uint64_t *ret) { - cg_file_handle fh = CG_FILE_HANDLE_INIT; - int mnt_id = -1; - - assert(fd >= 0); - assert(ret); - - if (name_to_handle_at(fd, "", &fh.file_handle, &mnt_id, AT_EMPTY_PATH) < 0) - return -errno; - - *ret = CG_FILE_HANDLE_CGROUPID(fh); - return 0; -} - int cg_path_get_session(const char *path, char **ret_session) { _cleanup_free_ char *unit = NULL; char *start, *end; diff --git a/src/basic/cgroup-util.h b/src/basic/cgroup-util.h index 9d7573d9442..d471aee0c3a 100644 --- a/src/basic/cgroup-util.h +++ b/src/basic/cgroup-util.h @@ -183,6 +183,14 @@ typedef enum CGroupUnified { int cg_path_open(const char *controller, const char *path); int cg_cgroupid_open(int fsfd, uint64_t id); +int cg_get_cgroupid_at(int dfd, const char *path, uint64_t *ret); +static inline int cg_path_get_cgroupid(const char *path, uint64_t *ret) { + return cg_get_cgroupid_at(AT_FDCWD, path, ret); +} +static inline int cg_fd_get_cgroupid(int fd, uint64_t *ret) { + return cg_get_cgroupid_at(fd, NULL, ret); +} + typedef enum CGroupFlags { CGROUP_SIGCONT = 1 << 0, CGROUP_IGNORE_SELF = 1 << 1, @@ -265,8 +273,6 @@ 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_fd_get_cgroupid(int fd, uint64_t *ret); int cg_path_get_session(const char *path, char **ret_session); int cg_path_get_owner_uid(const char *path, uid_t *ret_uid); int cg_path_get_unit(const char *path, char **ret_unit);