Index: include/asterisk/select.h =================================================================== --- include/asterisk/select.h (revision 313097) +++ include/asterisk/select.h (working copy) @@ -33,6 +33,16 @@ extern "C" { #endif +/*!\note + * Number of file descriptors supported. This needs to reduce the field size + * to the actual field size that we support. Otherwise, we could end up with + * some select(2) call weirdness (either calls that error out immediately, + * because the size is too high, or calls that don't actually monitor anything, + * because the bits are higher than the maximum that the system will monitor). + * + * Initially defined in main/poll.c, though it may be modified in + * main/asterisk.c (main() function). + */ extern unsigned int ast_FD_SETSIZE; #if !defined(HAVE_VARIABLE_FDSET) && defined(CONFIGURE_RAN_AS_ROOT) @@ -56,10 +66,12 @@ #define FD_SET(fd, fds) \ do { \ TYPEOF_FD_SET_FDS_BITS *bytes = (TYPEOF_FD_SET_FDS_BITS *) fds; \ - if (fd / sizeof(*bytes) + ((fd + 1) % sizeof(*bytes) ? 1 : 0) < sizeof(*(fds))) { \ + if (fd >= ast_FD_SETSIZE) { \ + fprintf(stderr, "FD %d exceeds the maximum file descriptor permitted by this process (%d)\n", fd, ast_FD_SETSIZE); \ + } else if (fd / sizeof(*bytes) + ((fd + 1) % sizeof(*bytes) ? 1 : 0) < sizeof(*(fds))) { \ bytes[fd / (sizeof(*bytes) * 8)] |= ((TYPEOF_FD_SET_FDS_BITS) 1) << (fd % (sizeof(*bytes) * 8)); \ } else { \ - fprintf(stderr, "FD %d exceeds the maximum size of ast_fdset!\n", fd); \ + fprintf(stderr, "FD %d exceeds the maximum size of ast_fdset! (increase ast_FDMAX in include/asterisk/select.h and recompile)\n", fd); \ } \ } while (0) #endif /* HAVE_VARIABLE_FDSET */ Index: main/asterisk.c =================================================================== --- main/asterisk.c (revision 313097) +++ main/asterisk.c (working copy) @@ -3362,12 +3362,14 @@ break; /* XXX Should we exit() here? XXX */ } - fd2 = (l.rlim_cur > sizeof(readers) * 8 ? sizeof(readers) * 8 : l.rlim_cur) - 1; + fd2 = (l.rlim_cur >= sizeof(readers) * 8 ? sizeof(readers) * 8 : l.rlim_cur) - 1; if (dup2(fd, fd2) < 0) { ast_log(LOG_WARNING, "Cannot open maximum file descriptor %d at boot? %s\n", fd2, strerror(errno)); close(fd); break; } + /* Set here, because we reference the value in the FD_SET macro */ + ast_FD_SETSIZE = fd2 + 1; FD_ZERO(&readers); FD_SET(fd2, &readers);