]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup-util: introduce cg_get_cgroupid_at()
authorMike Yuan <me@yhndnzj.com>
Fri, 29 Nov 2024 12:43:02 +0000 (13:43 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 11 Dec 2024 20:19:07 +0000 (05:19 +0900)
Suggested in https://github.com/systemd/systemd/pull/35242#discussion_r1862658163

src/basic/cgroup-util.c
src/basic/cgroup-util.h

index 309dccb45ab22e89222c0dc7ee9a4cdc57ed2d76..8247b0c25fc8e53b7401e5fe8f124a7e19524d84 100644 (file)
@@ -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;
index 9d7573d9442e323a6ea1ce5f63d648182266bc40..d471aee0c3a0b9e6294a300541263e82e9e89d8d 100644 (file)
@@ -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);