#include #ifndef _LP64 #error "not using 64bit mode" #endif #include #if ! defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE) #define RLIMIT_NOFILE RLIMIT_OFILE #endif #include #ifndef STDERR_FILENO #define STDERR_FILENO 2 #endif #include #include #include #define MAX 65536 int main( int argc, char* argv[] ) { struct rlimit limit; FILE** fd; char fn[256]; long i, maxfd = ( argc != 2 ) ? MAX : strtol(argv[1],0,0); /* obtain hard/soft limit */ if ( getrlimit( RLIMIT_NOFILE, &limit ) == -1 ) { perror( "unable to obtain limits" ); return 1; } else { printf( "FD limit = { hard : %d, soft : %d }\n", limit.rlim_max, limit.rlim_cur ); } /* check back with soft/hard limit of system */ if ( maxfd > limit.rlim_cur ) { /* check with hardlimit, complain if exceeded */ if ( maxfd > limit.rlim_max ) { fputs( "hardlimit exceeded!\n", stderr ); return 1; } else { limit.rlim_cur = maxfd; if ( setrlimit( RLIMIT_NOFILE, &limit ) == -1 ) { perror( "unable to set new soft limit" ); return 1; } } } assert( (fd = malloc( sizeof(FILE*) * maxfd )) != NULL ); for (i=STDERR_FILENO+1; i