[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <20250130-nolibc-dir-v1-2-ea9950b52e29@weissschuh.net>
Date: Thu, 30 Jan 2025 20:54:03 +0100
From: Thomas Weißschuh <linux@...ssschuh.net>
To: Willy Tarreau <w@....eu>, Shuah Khan <shuah@...nel.org>
Cc: linux-kernel@...r.kernel.org, linux-kselftest@...r.kernel.org,
Thomas Weißschuh <thomas.weissschuh@...utronix.de>,
Thomas Weißschuh <linux@...ssschuh.net>
Subject: [PATCH 2/2] tools/nolibc: add support for directory access
From: Thomas Weißschuh <thomas.weissschuh@...utronix.de>
Add an allocation-free implementation of readdir() and related
functions. The implementation is modelled after the one for FILE.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@...utronix.de>
Signed-off-by: Thomas Weißschuh <linux@...ssschuh.net>
---
I'm not entirely sure where to put it. It doesn't really belong into
stdio.h, but that's where the FILE stuff is.
sys.h wants alphabetical ordering, but IMO these functions should stick
together.
---
tools/include/nolibc/stdio.h | 76 ++++++++++++++++++++++++++++
tools/include/nolibc/types.h | 5 ++
tools/testing/selftests/nolibc/nolibc-test.c | 37 ++++++++++++++
3 files changed, 118 insertions(+)
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 3892034198dd566d21a5cc0a9f67cf097d428393..1f275a0a7b6b2c6f1c15405d027c282bb77aa618 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -387,6 +387,82 @@ const char *strerror(int errno)
return buf;
}
+/* See comment of FILE above */
+typedef struct {
+ char dummy[1];
+} DIR;
+
+static __attribute__((unused))
+DIR *fdopendir(int fd)
+{
+ if (fd < 0) {
+ SET_ERRNO(EBADF);
+ return NULL;
+ }
+ return (DIR *)(intptr_t)~fd;
+}
+
+static __attribute__((unused))
+DIR *opendir(const char *name)
+{
+ int fd;
+
+ fd = open(name, O_RDONLY);
+ if (fd == -1)
+ return NULL;
+ return fdopendir(fd);
+}
+
+static __attribute__((unused))
+int closedir(DIR *dirp)
+{
+ intptr_t i = (intptr_t)dirp;
+
+ if (i >= 0) {
+ SET_ERRNO(EBADF);
+ return -1;
+ }
+ return close(~i);
+}
+
+static __attribute__((unused))
+struct dirent *readdir(DIR *dirp)
+{
+ static struct dirent dirent;
+
+ char buf[sizeof(struct linux_dirent64) + NAME_MAX];
+ struct linux_dirent64 *ldir = (void *)buf;
+ intptr_t i = (intptr_t)dirp;
+ int fd, ret;
+
+ if (i >= 0) {
+ SET_ERRNO(EBADF);
+ return NULL;
+ }
+
+ fd = ~i;
+
+ ret = getdents64(fd, ldir, sizeof(buf));
+ if (ret == -1 || ret == 0)
+ return NULL;
+
+ /*
+ * getdents64() returns as many entries as fit the buffer.
+ * readdir() can only return one entry at a time.
+ * Make sure the non-returned ones are not skipped.
+ */
+ ret = lseek(fd, ldir->d_off, SEEK_SET);
+ if (ret == -1)
+ return NULL;
+
+ dirent = (struct dirent) {
+ .d_ino = ldir->d_ino,
+ };
+ strlcpy(dirent.d_name, ldir->d_name, sizeof(dirent.d_name));
+
+ return &dirent;
+}
+
/* make sure to include all global symbols */
#include "nolibc.h"
diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h
index b26a5d0c417c7c3b232f28e78cdd6d94a759b7bc..67885731fff3b2008efffe7c02f93f978ef7b078 100644
--- a/tools/include/nolibc/types.h
+++ b/tools/include/nolibc/types.h
@@ -179,6 +179,11 @@ struct linux_dirent64 {
char d_name[];
};
+struct dirent {
+ ino_t d_ino;
+ char d_name[256];
+};
+
/* The format of the struct as returned by the libc to the application, which
* significantly differs from the format returned by the stat() syscall flavours.
*/
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 0e0e3b48a8c3a6802c6989954b6f3a7c7258db43..18f769bcf68dfb190b157aeeba14f7904965377b 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -767,6 +767,42 @@ int test_getdents64(const char *dir)
return ret;
}
+int test_directories(void)
+{
+ int comm = 0, cmdline = 0;
+ struct dirent *dirent;
+ DIR *dir;
+ int ret;
+
+ dir = opendir("/proc/self");
+ if (!dir)
+ return 1;
+
+ while (1) {
+ errno = 0;
+ dirent = readdir(dir);
+ if (!dirent)
+ break;
+
+ if (strcmp(dirent->d_name, "comm") == 0)
+ comm++;
+ else if (strcmp(dirent->d_name, "cmdline") == 0)
+ cmdline++;
+ }
+
+ if (errno)
+ return 1;
+
+ ret = closedir(dir);
+ if (ret)
+ return 1;
+
+ if (comm != 1 || cmdline != 1)
+ return 1;
+
+ return 0;
+}
+
int test_getpagesize(void)
{
int x = getpagesize();
@@ -1059,6 +1095,7 @@ int run_syscall(int min, int max)
CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); break;
CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
+ CASE_TEST(directories); EXPECT_SYSZR(proc, test_directories()); break;
CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break;
--
2.48.1
Powered by blists - more mailing lists