Use /dev/fd instead of /proc/self/fd
authorAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
Thu, 18 Mar 2021 09:25:58 +0000 (09:25 +0000)
committerAlexander Richardson <alexander.richardson@cl.cam.ac.uk>
Fri, 10 Sep 2021 11:35:54 +0000 (11:35 +0000)
/dev/fd exists on all operating systems I can test (Linux, FreeBSD, macOS),
whereas /proc/self/fd only appears to exist on Linux.

Signed-off-by: Alex Richardson <Alexander.Richardson@cl.cam.ac.uk>
tests/test-helpers.c

index 20b6690..4cdd1fc 100644 (file)
@@ -48,8 +48,12 @@ count_open_fds(void)
        struct dirent *ent;
        int count = 0;
 
-       dir = opendir("/proc/self/fd");
-       assert(dir && "opening /proc/self/fd failed.");
+       /*
+        * Using /dev/fd instead of /proc/self/fd should allow this code to
+        * work on non-Linux operating systems.
+        */
+       dir = opendir("/dev/fd");
+       assert(dir && "opening /dev/fd failed.");
 
        errno = 0;
        while ((ent = readdir(dir))) {
@@ -58,7 +62,7 @@ count_open_fds(void)
                        continue;
                count++;
        }
-       assert(errno == 0 && "reading /proc/self/fd failed.");
+       assert(errno == 0 && "reading /dev/fd failed.");
 
        closedir(dir);