The branch main has been updated by des:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=c08e019c6ce4fc637b195f1bb0878763c3fefe9e

commit c08e019c6ce4fc637b195f1bb0878763c3fefe9e
Author:     Dag-Erling Smørgrav <d...@freebsd.org>
AuthorDate: 2025-07-08 19:40:55 +0000
Commit:     Dag-Erling Smørgrav <d...@freebsd.org>
CommitDate: 2025-07-08 19:41:21 +0000

    opendir, fdopendir: Add tests, clean up.
    
    * Add test cases for opendir() and fdopendir().
    * Drop O_NONBLOCK from opendir(); it was added a long time ago to avoid
      blocking if given a closed named pipe, but now we use O_DIRECTORY,
      which ensures that we get ENOTDIR in that case.
    * While here, remove unused #includes left over from the split.
    
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans, markj
    Differential Revision:  https://reviews.freebsd.org/D51126
---
 lib/libc/gen/fdopendir.c          |   7 +-
 lib/libc/gen/opendir.c            |   7 --
 lib/libc/gen/opendir2.c           |   6 +-
 lib/libc/tests/gen/Makefile       |   1 +
 lib/libc/tests/gen/opendir_test.c | 144 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 151 insertions(+), 14 deletions(-)

diff --git a/lib/libc/gen/fdopendir.c b/lib/libc/gen/fdopendir.c
index 67c0766b6d83..df6709fbcb85 100644
--- a/lib/libc/gen/fdopendir.c
+++ b/lib/libc/gen/fdopendir.c
@@ -30,14 +30,13 @@
  */
 
 #include "namespace.h"
-#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
+#include <stdbool.h>
 #include "un-namespace.h"
 
 #include "gen-private.h"
diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c
index 956c92c321e8..08d9eb10eaa2 100644
--- a/lib/libc/gen/opendir.c
+++ b/lib/libc/gen/opendir.c
@@ -30,14 +30,7 @@
  */
 
 #include "namespace.h"
-#include <sys/param.h>
-
 #include <dirent.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
 #include "un-namespace.h"
 
 #include "gen-private.h"
diff --git a/lib/libc/gen/opendir2.c b/lib/libc/gen/opendir2.c
index b9ac23e6d9fd..928145b468c1 100644
--- a/lib/libc/gen/opendir2.c
+++ b/lib/libc/gen/opendir2.c
@@ -30,11 +30,12 @@
  */
 
 #include "namespace.h"
-#include <sys/param.h>
+#include <sys/types.h>
 
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -52,8 +53,7 @@ __opendir2(const char *name, int flags)
 
        if ((flags & (__DTF_READALL | __DTF_SKIPREAD)) != 0)
                return (NULL);
-       if ((fd = _open(name,
-           O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC)) == -1)
+       if ((fd = _open(name, O_DIRECTORY | O_RDONLY | O_CLOEXEC)) == -1)
                return (NULL);
 
        dir = __opendir_common(fd, flags, false);
diff --git a/lib/libc/tests/gen/Makefile b/lib/libc/tests/gen/Makefile
index a967ad5ddf91..8c2151105209 100644
--- a/lib/libc/tests/gen/Makefile
+++ b/lib/libc/tests/gen/Makefile
@@ -20,6 +20,7 @@ ATF_TESTS_C+=         glob2_test
 ATF_TESTS_C+=          glob_blocks_test
 .endif
 ATF_TESTS_C+=          makecontext_test
+ATF_TESTS_C+=          opendir_test
 ATF_TESTS_C+=          popen_test
 ATF_TESTS_C+=          posix_spawn_test
 ATF_TESTS_C+=          realpath2_test
diff --git a/lib/libc/tests/gen/opendir_test.c 
b/lib/libc/tests/gen/opendir_test.c
new file mode 100644
index 000000000000..89be2becc607
--- /dev/null
+++ b/lib/libc/tests/gen/opendir_test.c
@@ -0,0 +1,144 @@
+/*-
+ * Copyright (c) 2025 Klara, Inc.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/stat.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <atf-c.h>
+
+/*
+ * Create a directory with a single subdirectory.
+ */
+static void
+opendir_prepare(const struct atf_tc *tc)
+{
+       ATF_REQUIRE_EQ(0, mkdir("dir", 0755));
+       ATF_REQUIRE_EQ(0, mkdir("dir/subdir", 0755));
+}
+
+/*
+ * Assuming dirp represents the directory created by opendir_prepare(),
+ * verify that readdir() returns what we expected to see there.
+ */
+static void
+opendir_check(const struct atf_tc *tc, DIR *dirp)
+{
+       struct dirent *ent;
+
+       ATF_REQUIRE((ent = readdir(dirp)) != NULL);
+       ATF_CHECK_EQ(1, ent->d_namlen);
+       ATF_CHECK_STREQ(".", ent->d_name);
+       ATF_CHECK_EQ(DT_DIR, ent->d_type);
+       ATF_REQUIRE((ent = readdir(dirp)) != NULL);
+       ATF_CHECK_EQ(2, ent->d_namlen);
+       ATF_CHECK_STREQ("..", ent->d_name);
+       ATF_CHECK_EQ(DT_DIR, ent->d_type);
+       ATF_REQUIRE((ent = readdir(dirp)) != NULL);
+       ATF_CHECK_EQ(sizeof("subdir") - 1, ent->d_namlen);
+       ATF_CHECK_STREQ("subdir", ent->d_name);
+       ATF_CHECK_EQ(DT_DIR, ent->d_type);
+       ATF_CHECK(readdir(dirp) == NULL);
+}
+
+ATF_TC(opendir_ok);
+ATF_TC_HEAD(opendir_ok, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Open a directory.");
+}
+ATF_TC_BODY(opendir_ok, tc)
+{
+       DIR *dirp;
+
+       opendir_prepare(tc);
+       ATF_REQUIRE((dirp = opendir("dir")) != NULL);
+       opendir_check(tc, dirp);
+       ATF_CHECK_EQ(0, closedir(dirp));
+}
+
+ATF_TC(opendir_fifo);
+ATF_TC_HEAD(opendir_fifo, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Do not hang if given a named pipe.");
+}
+ATF_TC_BODY(opendir_fifo, tc)
+{
+       DIR *dirp;
+       int fd;
+
+       ATF_REQUIRE((fd = mkfifo("fifo", 0644)) >= 0);
+       ATF_REQUIRE_EQ(0, close(fd));
+       ATF_REQUIRE((dirp = opendir("fifo")) == NULL);
+       ATF_CHECK_EQ(ENOTDIR, errno);
+}
+
+ATF_TC(fdopendir_ok);
+ATF_TC_HEAD(fdopendir_ok, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Open a directory from a directory descriptor.");
+}
+ATF_TC_BODY(fdopendir_ok, tc)
+{
+       DIR *dirp;
+       int dd;
+
+       opendir_prepare(tc);
+       ATF_REQUIRE((dd = open("dir", O_DIRECTORY | O_RDONLY)) >= 0);
+       ATF_REQUIRE((dirp = fdopendir(dd)) != NULL);
+       opendir_check(tc, dirp);
+       ATF_CHECK_EQ(dd, fdclosedir(dirp));
+       ATF_CHECK_EQ(0, close(dd));
+}
+
+ATF_TC(fdopendir_ebadf);
+ATF_TC_HEAD(fdopendir_ebadf, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Open a directory from an invalid descriptor.");
+}
+ATF_TC_BODY(fdopendir_ebadf, tc)
+{
+       DIR *dirp;
+       int dd;
+
+       ATF_REQUIRE_EQ(0, mkdir("dir", 0755));
+       ATF_REQUIRE((dd = open("dir", O_DIRECTORY | O_RDONLY)) >= 0);
+       ATF_CHECK_EQ(0, close(dd));
+       ATF_REQUIRE((dirp = fdopendir(dd)) == NULL);
+       ATF_CHECK_EQ(EBADF, errno);
+}
+
+ATF_TC(fdopendir_enotdir);
+ATF_TC_HEAD(fdopendir_enotdir, tc)
+{
+       atf_tc_set_md_var(tc, "descr",
+           "Open a directory from a non-directory descriptor.");
+}
+ATF_TC_BODY(fdopendir_enotdir, tc)
+{
+       DIR *dirp;
+       int fd;
+
+       ATF_REQUIRE((fd = open("file", O_CREAT | O_RDWR, 0644)) >= 0);
+       ATF_REQUIRE((dirp = fdopendir(fd)) == NULL);
+       ATF_CHECK_EQ(ENOTDIR, errno);
+       ATF_CHECK_EQ(0, close(fd));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+       ATF_TP_ADD_TC(tp, opendir_ok);
+       ATF_TP_ADD_TC(tp, fdopendir_ok);
+       ATF_TP_ADD_TC(tp, fdopendir_ebadf);
+       ATF_TP_ADD_TC(tp, fdopendir_enotdir);
+       ATF_TP_ADD_TC(tp, opendir_fifo);
+       return (atf_no_error());
+}

Reply via email to