1 /* $Id: fdlimit.c 4511 2001-02-07 23:41:08Z rra $
3 ** Portably determine or set the limit on open file descriptors.
5 ** Pretty much all platforms these days have getrlimit and setrlimit, so
6 ** prefer those, but for determining the current limit preserve the old
7 ** portability (if we don't have getrlimit, try sysconf, then
8 ** getdtablesize, then ulimit, and then try to find a hard-coded constant
9 ** in <sys/param.h> and failing that fall back to the POSIX-guaranteed
12 ** For setting the limit, only setrlimit is supported; if it isn't
13 ** available, return -1 always. We also refuse to set the limit to
14 ** something higher than select can handle, checking against FD_SETSIZE.
16 ** Note that on some versions of Linux (2.2.x reported), sysconf may return
17 ** the wrong value for the maximum file descriptors. getrlimit is correct,
18 ** so always prefer it.
25 # include <sys/select.h>
28 /* FreeBSD 3.4 RELEASE needs <sys/time.h> before <sys/resource.h>. */
29 #if HAVE_GETRLIMIT || HAVE_SETRLIMIT
31 # include <sys/time.h>
33 # include <sys/resource.h>
38 #if HAVE_SETRLIMIT && defined(RLIMIT_NOFILE)
41 setfdlimit(unsigned int limit)
46 if (limit > FD_SETSIZE) {
56 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
63 if (limit > rl.rlim_max)
65 return setrlimit(RLIMIT_NOFILE, &rl);
68 #else /* !(HAVE_SETRLIMIT && RLIMIT_NOFILE) */
71 setfdlimit(unsigned int limit UNUSED)
73 /* Unimplemented system call is close enough. */
78 #endif /* !(HAVE_SETRLIMIT && RLIMIT_NOFILE) */
80 #if HAVE_GETRLIMIT && defined(RLIMIT_NOFILE)
87 if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
97 return sysconf(_SC_OPEN_MAX);
100 #elif HAVE_GETDTABLESIZE
105 return getdtablesize();
114 return ulimit(UL_GDESLIM, 0);
120 #else /* no function mechanism available */
124 # include <sys/param.h>