]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: replace cg_{fd,path}_get_cgroupid() with {fd,path}_to_handle_u64()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 28 Jan 2026 17:02:56 +0000 (02:02 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 3 Feb 2026 17:45:17 +0000 (02:45 +0900)
src/basic/cgroup-util.c
src/basic/cgroup-util.h
src/core/cgroup.c
src/nsresourced/nsresourcework.c
src/shared/cgroup-show.c
src/test/test-cgroup-util.c

index 6fb4d559589838e48cd0c47b53f1df0ee2c7f191..5b213921543439cb47459eae0cc616c205cdd737 100644 (file)
@@ -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;
index 641a179526924c317f2c0da13bdd25e666b50afb..68a771e5aed6478d7dcac158c31b0a54e3ed1332 100644 (file)
@@ -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,
index 35c0898e03c71ad9683045a6e54dd623226cbdbd..ad889db3b987b4aae0edd105494e6116be08eef7 100644 (file)
@@ -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;
 
index 7004d03c4829873e435f56dcb038460d7ed03b3a..60d3a01ce06e4c44b2c85ad47c7948ef3557ebe3 100644 (file)
@@ -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");
 
index 109fe016f534d5b537242dc891e83ee0984a47db..cfc80d666f389b9dbd6e182b704cd79380192081 100644 (file)
@@ -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);
         }
index 0fd81284e8e3abd36f2a1bf150ac55155c376060..b8393f33683f209be3659d37a12d46319683c7c1 100644 (file)
@@ -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);