select on fds higher then 255 do not check if the fd is open. Here is my example code:
#include <stdio.h> #include <errno.h> #include <unistd.h> #include <sys/select.h> int main() { fd_set set; for(int i = 5;i<FD_SETSIZE;i++) { printf("--> i is %d\n", i); FD_ZERO(&set); FD_SET(i, &set); close(i); int retval = select(FD_SETSIZE, &set, NULL, NULL, NULL); if(-1 == retval) { perror("select"); } } }
This results in:
--> i is 5 select: Bad file descriptor ... --> i is 255 select: Bad file descriptor --> i is 256
Then the application blocks. Why does this not create a EBADF
on 256 till FD_SETSIZE?
Requested Information from comments:
The result of prlimit
is:
NOFILE max number of open files 1024 1048576
This is the result of strace ./test_select
:
select(1024, [127], NULL, NULL, NULL) = -1 EBADF (Bad file descriptor) dup(2) = 3 fcntl(3, F_GETFL) = 0x8402 (flags O_RDWR|O_APPEND|O_LARGEFILE) fstat(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0 write(3, "select: Bad file descriptor\n", 28select: Bad file descriptor ) = 28 close(3) = 0 write(1, "--> i is 128\n", 13--> i is 128 ) = 13 close(128) = -1 EBADF (Bad file descriptor) select(1024, [128], NULL, NULL, NULL
Debunking thoughts from the comments:
#include <stdio.h> #include <errno.h> #include <unistd.h> #include <sys/select.h> #include <fcntl.h> int main() { char filename[80]; int fd; for(int i = 5;i<500;i++) { snprintf(filename, 80, "/tmp/file%d", i); fd = open(filename, O_RDWR | O_APPEND | O_CREAT); } printf("--> fd is %d, FD_SETSIZE is %d\n", fd, FD_SETSIZE); fd_set set; FD_ZERO(&set); FD_SET(fd, &set); int retval = select(FD_SETSIZE, NULL, &set, NULL, NULL); if(-1 == retval) { perror("select"); } }
Results in:
$ ./test_select --> fd is 523, FD_SETSIZE is 1024
Process exits normally, no blocking.