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.
This commit is contained in:
Guillem Jover 2023-04-01 02:48:47 +02:00
parent 07192b31e3
commit 0f8bcdfd92

View 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;