tree-wide: replace cg_{fd,path}_get_cgroupid() with {fd,path}_to_handle_u64()

This commit is contained in:
Yu Watanabe
2026-02-04 02:45:17 +09:00
parent 2eab8fa1e4
commit f37931bd78
6 changed files with 12 additions and 36 deletions
-21
View 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;
-7
View 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,
+6 -4
View 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;
+1 -1
View 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");
+2 -1
View 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);
}
+3 -2
View 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);