From: Lennart Poettering Date: Mon, 25 Aug 2025 09:05:48 +0000 (+0200) Subject: fileio: modernize xopendirat() a bit X-Git-Tag: v259-rc1~507 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93dea63fabe15231aa6decbbc8188387c9ac2a82;p=thirdparty%2Fsystemd.git fileio: modernize xopendirat() a bit --- diff --git a/src/basic/chase.c b/src/basic/chase.c index ddef1944874..3443721daac 100644 --- a/src/basic/chase.c +++ b/src/basic/chase.c @@ -842,7 +842,7 @@ int chase_and_opendir(const char *path, const char *root, ChaseFlags chase_flags return r; assert(path_fd >= 0); - d = xopendirat(path_fd, ".", O_NOFOLLOW); + d = xopendirat(path_fd, /* path= */ NULL, /* flags= */ 0); if (!d) return -errno; @@ -1045,7 +1045,7 @@ int chase_and_opendirat(int dir_fd, const char *path, ChaseFlags chase_flags, ch return r; assert(path_fd >= 0); - d = xopendirat(path_fd, ".", O_NOFOLLOW); + d = xopendirat(path_fd, /* path= */ NULL, /* flags= */ 0); if (!d) return -errno; diff --git a/src/basic/fileio.c b/src/basic/fileio.c index a2fb5c7649c..ca763a315b3 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -928,17 +928,22 @@ int get_proc_field(const char *path, const char *key, char **ret) { } } -DIR* xopendirat(int dir_fd, const char *name, int flags) { +DIR* xopendirat(int dir_fd, const char *path, int flags) { _cleanup_close_ int fd = -EBADF; assert(dir_fd >= 0 || dir_fd == AT_FDCWD); - assert(name); assert(!(flags & (O_CREAT|O_TMPFILE))); - if (dir_fd == AT_FDCWD && flags == 0) - return opendir(name); + if ((dir_fd == AT_FDCWD || path_is_absolute(path)) && + (flags &~ O_DIRECTORY) == 0) + return opendir(path); - fd = openat(dir_fd, name, O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags); + if (isempty(path)) { + path = "."; + flags |= O_NOFOLLOW; + } + + fd = openat(dir_fd, path, O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags); if (fd < 0) return NULL; diff --git a/src/basic/fileio.h b/src/basic/fileio.h index 0297cd07e98..4d798e7e646 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -92,7 +92,7 @@ int script_get_shebang_interpreter(const char *path, char **ret); int get_proc_field(const char *path, const char *key, char **ret); -DIR* xopendirat(int dir_fd, const char *name, int flags); +DIR* xopendirat(int dir_fd, const char *path, int flags); typedef enum XfopenFlags { XFOPEN_UNLOCKED = 1 << 0, /* call __fsetlocking(FSETLOCKING_BYCALLER) after opened */ diff --git a/src/udev/udev-node.c b/src/udev/udev-node.c index 412775b38b9..5abaf1eff39 100644 --- a/src/udev/udev-node.c +++ b/src/udev/udev-node.c @@ -190,7 +190,7 @@ static int stack_directory_find_prioritized_devnode(sd_device *dev, int dirfd, b return -ENOMEM; } - dir = xopendirat(dirfd, ".", O_NOFOLLOW); + dir = xopendirat(dirfd, /* path= */ NULL, /* flags= */ 0); if (!dir) return -errno;