Commit 0f8bcdfd92dc6342564192d707f339855c6fbc5f

Guillem Jover 2023-04-01T02:48:47

test: Fix closefrom() test to handle open file descriptor limits 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.

diff --git a/test/closefrom.c b/test/closefrom.c
index 160e6b1..aa8d3b2 100644
--- a/test/closefrom.c
+++ b/test/closefrom.c
@@ -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;