Branch: Tag:

2014-10-02

2014-10-02 16:36:41 by Per Hedbor <ph@opera.com>

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.

5755:    */   static void f_get_all_active_fd(INT32 args)   { -  int i,fds,ne; +  int i,fds=0;    PIKE_STAT_T foo; -  +  struct svalue *sp;    -  ne = MAX_OPEN_FILEDESCRIPTORS; -  +     pop_n_elems(args); -  for (i=fds=0; i<ne; i++) +  sp = Pike_sp;    { -  int q; +  DIR *tmp;    THREADS_ALLOW(); -  + #ifndef __NT__ +  if( (tmp = opendir( + #ifdef HAVE_DARWIN_XATTR +  "/dev/fd" + #else +  "/proc/self/fd" + #endif +  )) ) +  { +  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); -  THREADS_DISALLOW(); +     if(!q)    { -  push_int(i); +  SET_SVAL_TYPE_SUBTYPE(*sp,PIKE_T_INT,0); +  sp++->u.integer = i;    fds++;    }    } -  +  } +  THREADS_DISALLOW(); +  Pike_sp = sp; +  }    f_aggregate(fds);   }