From f37931bd78bbbc41590789368b9f8fc94cf8a68a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 29 Jan 2026 02:02:56 +0900 Subject: [PATCH] tree-wide: replace cg_{fd,path}_get_cgroupid() with {fd,path}_to_handle_u64() --- src/basic/cgroup-util.c | 21 --------------------- src/basic/cgroup-util.h | 7 ------- src/core/cgroup.c | 10 ++++++---- src/nsresourced/nsresourcework.c | 2 +- src/shared/cgroup-show.c | 3 ++- src/test/test-cgroup-util.c | 5 +++-- 6 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 6fb4d559589..5b213921543 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -47,9 +47,6 @@ typedef union { .file_handle.handle_type = FILEID_KERNFS, \ } -/* The .f_handle field is not aligned to 64bit on some archs, hence read it via an unaligned accessor */ -#define CG_FILE_HANDLE_CGROUPID(fh) unaligned_read_ne64(fh.file_handle.f_handle) - int cg_is_available(void) { struct statfs fs; @@ -112,24 +109,6 @@ int cg_path_from_cgroupid(int cgroupfs_fd, uint64_t id, char **ret) { return 0; } -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; -} - int cg_enumerate_processes(const char *path, FILE **ret) { _cleanup_free_ char *fs = NULL; FILE *f; diff --git a/src/basic/cgroup-util.h b/src/basic/cgroup-util.h index 641a1795269..68a771e5aed 100644 --- a/src/basic/cgroup-util.h +++ b/src/basic/cgroup-util.h @@ -125,13 +125,6 @@ int cg_path_open(const char *path); int cg_cgroupid_open(int cgroupfs_fd, uint64_t id); int cg_path_from_cgroupid(int cgroupfs_fd, uint64_t id, char **ret); -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, diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 35c0898e03c..ad889db3b98 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -34,6 +34,7 @@ #include "ip-protocol-list.h" #include "limits-util.h" #include "manager.h" +#include "mountpoint-util.h" #include "netlink-internal.h" #include "nulstr-util.h" #include "parse-util.h" @@ -2069,13 +2070,14 @@ static int unit_update_cgroup( uint64_t cgroup_id = 0; r = cg_get_path(crt->cgroup_path, /* suffix= */ NULL, &cgroup_full_path); - if (r == 0) { - r = cg_path_get_cgroupid(cgroup_full_path, &cgroup_id); + if (r < 0) + log_unit_warning_errno(u, r, "Failed to get full cgroup path on cgroup %s, ignoring: %m", empty_to_root(crt->cgroup_path)); + else { + r = path_to_handle_u64(cgroup_full_path, &cgroup_id); if (r < 0) log_unit_full_errno(u, ERRNO_IS_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r, "Failed to get cgroup ID of cgroup %s, ignoring: %m", cgroup_full_path); - } else - log_unit_warning_errno(u, r, "Failed to get full cgroup path on cgroup %s, ignoring: %m", empty_to_root(crt->cgroup_path)); + } crt->cgroup_id = cgroup_id; diff --git a/src/nsresourced/nsresourcework.c b/src/nsresourced/nsresourcework.c index 7004d03c482..60d3a01ce06 100644 --- a/src/nsresourced/nsresourcework.c +++ b/src/nsresourced/nsresourcework.c @@ -1354,7 +1354,7 @@ static int validate_cgroup(sd_varlink *link, int fd, uint64_t *ret_cgroup_id) { if (r == 0) return sd_varlink_error_invalid_parameter_name(link, "controlGroupFileDescriptor"); - r = cg_fd_get_cgroupid(fd, ret_cgroup_id); + r = fd_to_handle_u64(fd, ret_cgroup_id); if (r < 0) return log_debug_errno(r, "Failed to read cgroup ID from cgroupfs: %m"); diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c index 109fe016f53..cfc80d666f3 100644 --- a/src/shared/cgroup-show.c +++ b/src/shared/cgroup-show.c @@ -18,6 +18,7 @@ #include "glyph-util.h" #include "hostname-util.h" #include "log.h" +#include "mountpoint-util.h" #include "nulstr-util.h" #include "output-mode.h" #include "path-util.h" @@ -150,7 +151,7 @@ static int show_cgroup_name( delegate = r > 0; if (FLAGS_SET(flags, OUTPUT_CGROUP_ID)) { - r = cg_fd_get_cgroupid(fd, &cgroupid); + r = fd_to_handle_u64(fd, &cgroupid); if (r < 0) log_debug_errno(r, "Failed to determine cgroup ID of %s, ignoring: %m", path); } diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index 0fd81284e8e..b8393f33683 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -7,6 +7,7 @@ #include "errno-util.h" #include "fd-util.h" #include "format-util.h" +#include "mountpoint-util.h" #include "path-util.h" #include "pidref.h" #include "process-util.h" @@ -493,7 +494,7 @@ TEST(cgroupid) { ASSERT_OK(fd_get_path(fd, &p)); ASSERT_TRUE(path_equal(p, "/sys/fs/cgroup")); - ASSERT_OK(cg_fd_get_cgroupid(fd, &id)); + ASSERT_OK(fd_to_handle_u64(fd, &id)); fd2 = cg_cgroupid_open(fd, id); @@ -509,7 +510,7 @@ TEST(cgroupid) { ASSERT_OK(fd_get_path(fd2, &p2)); ASSERT_TRUE(path_equal(p2, "/sys/fs/cgroup")); - ASSERT_OK(cg_fd_get_cgroupid(fd2, &id2)); + ASSERT_OK(fd_to_handle_u64(fd2, &id2)); ASSERT_EQ(id, id2); -- 2.47.3