]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
test: Fix closefrom() test to handle open file descriptor limits
authorGuillem Jover <guillem@hadrons.org>
Sat, 1 Apr 2023 00:48:47 +0000 (02:48 +0200)
committerGuillem Jover <guillem@hadrons.org>
Mon, 17 Apr 2023 02:12:42 +0000 (04:12 +0200)
If the system has configured a lower limit (either soft or hard) on the
number of open file descriptors, the test will fail. Make sure to check
whether we have exceeded that limit and adapt the max number of file
descriptors appropriately.

test/closefrom.c

index 160e6b1ede736877d3264e0cc2bf5bc234f72d18..aa8d3b28b5b250ace8bb4e3612aebc6604d73182 100644 (file)
@@ -35,18 +35,28 @@ main(int argc, char *argv[])
 {
        int i;
        int fd;
+       int fd_max;
 
        fd = open("/dev/null", O_RDONLY);
 
-       for (i = 4; i < 1024; i *= 2)
-               assert(dup2(fd, i) == i);
+       fd_max = 1024;
+       for (i = 4; i < fd_max; i *= 2) {
+               int fd_new = dup2(fd, i);
+
+               if (fd_new < 0 && (errno == EMFILE || errno == EBADF)) {
+                       fd_max = i - 1;
+                       break;
+               }
+               assert(fd_new == i);
+       }
 
        if (fd < 4)
                close(fd);
        closefrom(4);
 
-       for (i = 4; i < 1024; i++)
+       for (i = 4; i < fd_max; i++) {
                assert(fcntl(i, F_GETFL) == -1 && errno == EBADF);
+       }
        assert(fcntl(fd, F_GETFL) == -1 && errno == EBADF);
 
        return 0;