]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: add generic resolve_xat_fdroot()
authorMike Yuan <me@yhndnzj.com>
Thu, 29 Jan 2026 10:27:40 +0000 (11:27 +0100)
committerMike Yuan <me@yhndnzj.com>
Thu, 5 Feb 2026 13:14:39 +0000 (14:14 +0100)
As suggested in
https://github.com/systemd/systemd/pull/40500#discussion_r2740921215

src/basic/fd-util.c
src/basic/fd-util.h
src/basic/stat-util.c

index f80ce0ae470e12e8f423c041f26c037ef48450ca..197d015f37ead5f100ae4f4c60c77178a89d4755 100644 (file)
@@ -1105,6 +1105,32 @@ int fds_are_same_mount(int fd1, int fd2) {
         return statx_inode_same(&sx1, &sx2) && statx_mount_same(&sx1, &sx2);
 }
 
+int resolve_xat_fdroot(int *fd, const char **path, char **ret_buffer) {
+        assert(fd);
+        assert(path);
+        assert(ret_buffer);
+
+        if (*fd != XAT_FDROOT) {
+                *ret_buffer = NULL;
+                return 0;
+        }
+
+        if (isempty(*path)) {
+                *path = "/";
+                *ret_buffer = NULL;
+        } else if (!path_is_absolute(*path)) {
+                char *p = strjoin("/", *path);
+                if (!p)
+                        return -ENOMEM;
+
+                *path = *ret_buffer = p;
+        }
+
+        *fd = AT_FDCWD;
+
+        return 1;
+}
+
 char* format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) {
         assert(buf);
         assert(fd >= 0);
index 707445d73d89c4d37213b43d896e5301c4c4bf93..ee1dc870df85940b8dd464e54ba988524c9dd0d9 100644 (file)
@@ -181,6 +181,8 @@ static inline int dir_fd_is_root_or_cwd(int dir_fd) {
 
 int fds_are_same_mount(int fd1, int fd2);
 
+int resolve_xat_fdroot(int *fd, const char **path, char **ret_buffer);
+
 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
 #define PROC_FD_PATH_MAX \
         (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
index 6d1796c315bb59e98b22f315d13b9b45ec24edf5..2e2f929fc08eeb0536538156c22b92ca714abcde 100644 (file)
@@ -36,19 +36,9 @@ static int verify_stat_at(
         assert(verify_func);
 
         _cleanup_free_ char *p = NULL;
-        if (fd == XAT_FDROOT) {
-                fd = AT_FDCWD;
-
-                if (isempty(path))
-                        path = "/";
-                else if (!path_is_absolute(path)) {
-                        p = strjoin("/", path);
-                        if (!p)
-                                return -ENOMEM;
-
-                        path = p;
-                }
-        }
+        r = resolve_xat_fdroot(&fd, &path, &p);
+        if (r < 0)
+                return r;
 
         if (fstatat(fd, strempty(path), &st,
                     (isempty(path) ? AT_EMPTY_PATH : 0) | (follow ? 0 : AT_SYMLINK_NOFOLLOW)) < 0)