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.
{
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;