From e16e51787973aa09fb05a49455c7c31e94b1e6e7 Mon Sep 17 00:00:00 2001 From: Per Hedbor <ph@opera.com> Date: Thu, 2 Oct 2014 18:27:13 +0200 Subject: [PATCH] Removed MAX_FD configure test It only returns the current max anyway, which is usually smaller than the max if you are running as root, and can also be changed at any time at runtime. Instead, use sysconf(_SC_OPEN_MAX) when available if needed, and as a last resort a hardcoded value (on Windows), but as a secondary change all uses except two was removed: o Stdio.get_all_active_fd - On systems without /dev/fd and /proc/self/fd. o On NT, the size of FD_SET is now set to a hardcoded value (64k). The rewrite of get_all_active_fd also speeds it up a lot. Not that it is a frequently used function. --- src/acconfig.h | 3 -- src/configure.in | 76 ------------------------------------- src/fdlib.c | 8 ++-- src/fdlib.h | 53 ++++++++++++++------------ src/global.h | 6 --- src/modules/_Stdio/file.c | 74 +++++++++++++++++++++++++++++++----- src/modules/spider/spider.c | 29 -------------- src/pike_embed.c | 23 ----------- src/signal_handler.c | 21 ---------- 9 files changed, 96 insertions(+), 197 deletions(-) diff --git a/src/acconfig.h b/src/acconfig.h index 2101e23a69..dba3f89b12 100644 --- a/src/acconfig.h +++ b/src/acconfig.h @@ -391,9 +391,6 @@ /* Assembler prefix for general purpose registers */ #undef PIKE_CPU_REG_PREFIX -/* Number of possible filedesriptors */ -#define MAX_OPEN_FILEDESCRIPTORS 1024 - /* define this if #include <time.h> provides an external int timezone */ #undef HAVE_EXTERNAL_TIMEZONE diff --git a/src/configure.in b/src/configure.in index 053b477c46..8e91694d68 100644 --- a/src/configure.in +++ b/src/configure.in @@ -20,7 +20,6 @@ If this does not work, please use an absolute path to the configure script.]) esac # Check that we can write to the source directory. -sleep 1 if touch "$srcdir/configure~"; then # Check that the system time is reasonably correct. if test `cd "$srcdir" && ls -1t configure configure~ | head -n 1` = "configure"; then @@ -6177,81 +6176,6 @@ else fi ######################################################################## - -AC_MSG_CHECKING(available file descriptors) -AC_CACHE_VAL(pike_cv_max_open_fd, -[ - if test "x$enable_binary" = "xno"; then - pike_cv_max_open_fd=256 - else - AC_TRY_RUN([ -#include <stdio.h> -#ifdef HAVE_SYS_TIME_H -#include <sys/time.h> -#endif -#ifdef HAVE_SYS_RESOURCE_H -#include <sys/resource.h> -#endif -#ifdef HAVE_LIMITS_H -#include <limits.h> -#endif - -#ifndef MAX_FD -#define MAX_FD 65536 -#endif - -int main() -{ - FILE *f; - long limit; - -#if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE) -#define RLIMIT_NOFILE RLIMIT_OFILE -#endif - -#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) - struct rlimit lim; - - if(getrlimit(RLIMIT_NOFILE,&lim)) - { - limit = MAX_FD; - }else{ - if(lim.rlim_max == RLIM_INFINITY) - { - limit = 1024 * 1024 * 2; /* Noone needs more :-) */ - -#if defined(OPEN_MAX) - /* On Darwin we can find a reasonable limit in OPEN_MAX which limits.h - will include from sys/syslimits.h. */ - limit = (OPEN_MAX < limit) ? OPEN_MAX : limit; -#endif - }else{ - limit=lim.rlim_max; - if(limit > 1024 * 1024 * 2) limit= 1024 * 1024 * 2; - } - } -#else - limit = MAX_FD; -#endif - - f=fopen("conftest.out.2","w"); - fprintf(f,"%ld\n",(long)limit); - fclose(f); - - return 0; -} - ], - pike_cv_max_open_fd=`cat conftest.out.2` - , - pike_cv_max_open_fd=256 - , - pike_cv_max_open_fd=256 - ) - fi -]) -AC_MSG_RESULT($pike_cv_max_open_fd) -AC_DEFINE_UNQUOTED(MAX_OPEN_FILEDESCRIPTORS,$pike_cv_max_open_fd) - AC_MSG_CHECKING(full availability of struct rusage members) AC_CACHE_VAL(pike_cv_func_getrusage_full, [ diff --git a/src/fdlib.c b/src/fdlib.c index 8952566805..65ecfa95aa 100644 --- a/src/fdlib.c +++ b/src/fdlib.c @@ -27,8 +27,8 @@ static MUTEX_T fd_mutex; -HANDLE da_handle[MAX_OPEN_FILEDESCRIPTORS]; -int fd_type[MAX_OPEN_FILEDESCRIPTORS]; +HANDLE da_handle[FD_SETSIZE]; +int fd_type[FD_SETSIZE]; int first_free_handle; /* #define FD_DEBUG */ @@ -123,7 +123,7 @@ PMOD_EXPORT char *debug_fd_info(int fd) if(fd<0) return "BAD"; - if(fd > MAX_OPEN_FILEDESCRIPTORS) + if(fd > FD_SETSIZE) return "OUT OF RANGE"; switch(fd_type[fd]) @@ -176,7 +176,7 @@ void fd_init(void) da_handle[2] = GetStdHandle(STD_ERROR_HANDLE); first_free_handle=3; - for(e=3;e<MAX_OPEN_FILEDESCRIPTORS-1;e++) + for(e=3;e<FD_SETSIZE-1;e++) fd_type[e]=e+1; fd_type[e]=FD_NO_MORE_FREE; mt_unlock(&fd_mutex); diff --git a/src/fdlib.h b/src/fdlib.h index d3775fffa5..200375862c 100644 --- a/src/fdlib.h +++ b/src/fdlib.h @@ -57,7 +57,10 @@ #define SOCKET_CAPABILITIES (fd_BIDIRECTIONAL | fd_CAN_NONBLOCK | fd_CAN_SHUTDOWN) #ifndef FD_SETSIZE -#define FD_SETSIZE MAX_OPEN_FILEDESCRIPTORS +/* + * in reality: almost unlimited actually. + */ +#define FD_SETSIZE 65536 #endif #include <winbase.h> @@ -203,7 +206,7 @@ PMOD_EXPORT const char *debug_fd_inet_ntop(int af, const void *addr, struct my_fd_set_s { - char bits[MAX_OPEN_FILEDESCRIPTORS/8]; + char bits[FD_SETSIZE/8]; }; typedef struct my_fd_set_s my_fd_set; @@ -218,29 +221,29 @@ typedef struct my_fd_set_s my_fd_set; #define my_FD_ISSET(FD,S) ((S)->bits[(FD)>>3]&(1<<(FD&7))) #define my_FD_ZERO(S) memset(& (S)->bits, 0, sizeof(my_fd_set)) -#define fd_copy_my_fd_set_to_fd_set(TO,FROM,max) do { \ - int e_,d_,max_=MINIMUM(MAX_OPEN_FILEDESCRIPTORS>>3,(max+7)>>3); \ - (TO)->fd_count=0; \ - for(e_=0;e_<max_;e_++) \ - { \ - int b_=(FROM)->bits[e_]; \ - if(b_) \ - { \ - for(d_=0;d_<8;d_++) \ - { \ - if(b_ & (1<<d_)) \ - { \ - int fd_=(e_<<3)+d_; \ - fd_check_fd(fd_); \ - (TO)->fd_array[(TO)->fd_count++]=(SOCKET)da_handle[fd_]; \ - } \ - } \ - } \ - } \ -}while(0) - -extern HANDLE da_handle[MAX_OPEN_FILEDESCRIPTORS]; -extern int fd_type[MAX_OPEN_FILEDESCRIPTORS]; +#define fd_copy_my_fd_set_to_fd_set(TO,FROM,max) do { \ + int e_,d_,max_=MINIMUM(FD_SETSIZE>>3,(max+7)>>3); \ + (TO)->fd_count=0; \ + for(e_=0;e_<max_;e_++) \ + { \ + int b_=(FROM)->bits[e_]; \ + if(b_) \ + { \ + for(d_=0;d_<8;d_++) \ + { \ + if(b_ & (1<<d_)) \ + { \ + int fd_=(e_<<3)+d_; \ + fd_check_fd(fd_); \ + (TO)->fd_array[(TO)->fd_count++]=(SOCKET)da_handle[fd_]; \ + } \ + } \ + } \ + } \ + }while(0) + +extern HANDLE da_handle[FD_SETSIZE]; +extern int fd_type[FD_SETSIZE]; #define fd_FD_CLR(X,Y) FD_CLR((SOCKET)da_handle[X],Y) #define fd_FD_SET(X,Y) \ diff --git a/src/global.h b/src/global.h index a700c7f4d6..7e349ec5ee 100644 --- a/src/global.h +++ b/src/global.h @@ -283,12 +283,6 @@ void *alloca(); # endif #endif -#ifdef __NT__ -/* We are running NT */ -#undef FD_SETSIZE -#define FD_SETSIZE MAX_OPEN_FILEDESCRIPTORS -#endif - #ifdef HAVE_DEVICES_TIMER_H /* On AmigaOS, struct timeval is defined in a variety of places and a variety of ways. Making sure <devices/timer.h> is included diff --git a/src/modules/_Stdio/file.c b/src/modules/_Stdio/file.c index 3f0e4abbe5..a1bd1bec42 100644 --- a/src/modules/_Stdio/file.c +++ b/src/modules/_Stdio/file.c @@ -5755,23 +5755,77 @@ static void exit_file_locking(void) */ static void f_get_all_active_fd(INT32 args) { - int i,fds,ne; + int i,fds=0; PIKE_STAT_T foo; - - ne = MAX_OPEN_FILEDESCRIPTORS; + struct svalue *sp; pop_n_elems(args); - for (i=fds=0; i<ne; i++) + sp = Pike_sp; { - int q; + DIR *tmp; THREADS_ALLOW(); - q = fd_fstat(i,&foo); - THREADS_DISALLOW(); - if(!q) +#ifndef __NT__ + if( (tmp = opendir( +#ifdef HAVE_DARWIN_XATTR + "/dev/fd" +#else + "/proc/self/fd" +#endif + )) ) { - push_int(i); - fds++; + INT_TYPE dfd = dirfd(tmp); + + while(1) + { + INT_TYPE fd; + char *ep; + struct dirent ent, *res; + /* solaris, linux, cygwin, darwin, netbsd et.al. */ + res = NULL; + while( UNLIKELY(readdir_r( tmp, &ent, &res )) + && UNLIKELY(errno==EINTR)) + ; + if( !res ) + break; + + fd = strtol(res->d_name, &ep, 10); + + if( LIKELY(ep != res->d_name) && (fd != dfd) ) + { + SET_SVAL_TYPE_SUBTYPE(*sp,PIKE_T_INT,0); + sp++->u.integer = fd; + fds++; + } + } + closedir(tmp); } + else +#endif /* __NT__ */ + { +#ifdef HAVE_SYSCONF + int max = sysconf(_SC_OPEN_MAX); + /* NOTE: This might have been lowered, so we might not actually + * get all FD:s. It is usually good, however. + * + * Also, this is not used on many systems + */ +#else + int max = 65535; +#endif + for (i=0; i<max; i++) + { + int q; + q = fd_fstat(i,&foo); + if(!q) + { + SET_SVAL_TYPE_SUBTYPE(*sp,PIKE_T_INT,0); + sp++->u.integer = i; + fds++; + } + } + } + THREADS_DISALLOW(); + Pike_sp = sp; } f_aggregate(fds); } diff --git a/src/modules/spider/spider.c b/src/modules/spider/spider.c index cb6cef4cbc..62f9704d30 100644 --- a/src/modules/spider/spider.c +++ b/src/modules/spider/spider.c @@ -986,31 +986,6 @@ void do_html_parse_lines(struct pike_string *ss, } } -/*! @decl array(int) get_all_active_fds() - */ -void f_get_all_active_fd(INT32 args) -{ - int i,fds,ne; - PIKE_STAT_T foo; - - ne = MAX_OPEN_FILEDESCRIPTORS; - - pop_n_elems(args); - for (i=fds=0; i<ne; i++) - { - int q; - THREADS_ALLOW(); - q = fd_fstat(i,&foo); - THREADS_DISALLOW(); - if(!q) - { - push_int(i); - fds++; - } - } - f_aggregate(fds); -} - /*! @decl string fd_info(int fd) */ void f_fd_info(INT32 args) @@ -1167,10 +1142,6 @@ PIKE_MODULE_INIT /* function(int,void|int:int) */ ADD_FUNCTION("stardate", f_stardate, tDeprecated(tFunc(tInt tInt,tInt)), 0); - /* function(:array(int)) */ - ADD_FUNCTION("get_all_active_fd", f_get_all_active_fd, - tDeprecated(tFunc(tNone,tArr(tInt))), OPT_EXTERNAL_DEPEND); - /* function(int:string) */ ADD_FUNCTION("fd_info", f_fd_info, tDeprecated(tFunc(tInt,tStr)), OPT_EXTERNAL_DEPEND); diff --git a/src/pike_embed.c b/src/pike_embed.c index 8ea8b03caf..559c30ab93 100644 --- a/src/pike_embed.c +++ b/src/pike_embed.c @@ -306,29 +306,6 @@ void init_pike_runtime(void (*exit_cb)(int)) #endif /* STACK_DEBUG */ #endif /* HAVE_GETRLIMIT && RLIMIT_STACK */ -#if 0 -#if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE) -#define RLIMIT_NOFILE RLIMIT_OFILE -#endif - -#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) - { - struct rlimit lim; - long tmp; - if(!getrlimit(RLIMIT_NOFILE, &lim)) - { -#ifdef RLIM_INFINITY - if(lim.rlim_max == RLIM_INFINITY) - lim.rlim_max=MAX_OPEN_FILEDESCRIPTORS; -#endif - tmp=MINIMUM(lim.rlim_max, MAX_OPEN_FILEDESCRIPTORS); - lim.rlim_cur=tmp; - setrlimit(RLIMIT_NOFILE, &lim); - } - } -#endif -#endif - TRACE((stderr, "Init time...\n")); UPDATE_CURRENT_TIME(); diff --git a/src/signal_handler.c b/src/signal_handler.c index f1dbae1046..2f8ed8303d 100644 --- a/src/signal_handler.c +++ b/src/signal_handler.c @@ -2143,27 +2143,6 @@ static HANDLE get_inheritable_handle(struct mapping *optional, if(fd == -1) Pike_error("File for %s is not open.\n",name); -#if 0 - { - int q; - for(q=0;q<MAX_OPEN_FILEDESCRIPTORS;q++) - { - if(fd_type[q]<-1) - { - DWORD flags; - fprintf(stderr,"%3d: %d %08x",q,fd_type[q],da_handle[q],flags); - GetHandleInformation((HANDLE)da_handle[q],&flags); - if(flags & HANDLE_FLAG_INHERIT) - fprintf(stderr," inheritable"); - if(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE) - fprintf(stderr," non-closable"); - fprintf(stderr,"\n"); - } - } - } -#endif - - if(!(fd_query_properties(fd, 0) & fd_INTERPROCESSABLE)) { void create_proxy_pipe(struct object *o, int for_reading); -- GitLab