diff --git a/libnetutil/netutil.cc b/libnetutil/netutil.cc index 006a877ea..db10e49af 100644 --- a/libnetutil/netutil.cc +++ b/libnetutil/netutil.cc @@ -131,6 +131,10 @@ #define NETINET_IP_H #endif +#if HAVE_SYS_RESOURCE_H +#include +#endif + #include "netutil.h" #define NBASE_MAX_ERR_STR_LEN 1024 /* Max length of an error message */ @@ -3725,3 +3729,82 @@ char *grab_next_host_spec(FILE *inputfd, bool random, int argc, char **fakeargv) return host_spec; } + + +/** Tries to increase the open file descriptor limit for this process. + * @param "desired" is the number of desired max open descriptors. Pass a + * negative value to set the maximum allowed. + * @return the number of max open descriptors that could be set, or 0 in case + * of failure. + * @warning if "desired" is less than the current limit, no action is + * performed. This function may only be used to increase the limit, not to + * decrease it. */ +int set_max_open_descriptors(int desired_max) { + #ifndef WIN32 + struct rlimit r; + int maxfds=-1; + int flag=0; + + #if (defined(RLIMIT_OFILE) || defined(RLIMIT_NOFILE)) + + #ifdef RLIMIT_NOFILE + flag=RLIMIT_NOFILE; /* Linux */ + #else + flag=RLIMIT_OFILE; /* BSD */ + #endif + + if (!getrlimit(flag, &r)) { + /* If current limit is less than the desired, try to increase it */ + if(r.rlim_cur < (rlim_t)desired_max){ + if(desired_max<0){ + r.rlim_cur=r.rlim_max; /* Set maximum */ + }else{ + r.rlim_cur = MIN( (int)r.rlim_max, desired_max ); + } + if (setrlimit(flag, &r)) + ; // netutil_debug("setrlimit(%d, %p) failed", flag, r); + if (!getrlimit(flag, &r)) { + maxfds = r.rlim_cur; + return maxfds; + }else { + return 0; + } + } + } + + #endif /* (defined(RLIMIT_OFILE) || defined(RLIMIT_NOFILE)) */ + #endif /* !WIN32 */ + return 0; +} + + +/** Returns the open file descriptor limit for this process. + * @return the number of max open descriptors or 0 in case of failure. */ +int get_max_open_descriptors() { + #ifndef WIN32 + struct rlimit r; + int flag=0; + + #if (defined(RLIMIT_OFILE) || defined(RLIMIT_NOFILE)) + + #ifdef RLIMIT_NOFILE + flag=RLIMIT_NOFILE; /* Linux */ + #else + flag=RLIMIT_OFILE; /* BSD */ + #endif + + if (!getrlimit(flag, &r)) { + return (int)r.rlim_cur; + } + + #endif /* (defined(RLIMIT_OFILE) || defined(RLIMIT_NOFILE)) */ + #endif /* !WIN32 */ + return 0; +} + + +/* Maximize the open file descriptor limit for this process go up to the + max allowed */ +int max_sd() { + return set_max_open_descriptors(-1); +} diff --git a/libnetutil/netutil.h b/libnetutil/netutil.h index 5e5b55f89..b396db2a8 100644 --- a/libnetutil/netutil.h +++ b/libnetutil/netutil.h @@ -474,4 +474,22 @@ char *grab_next_host_spec(FILE *inputfd, bool random, int argc, char **fakeargv) int DnetName2PcapName(const char *dnetdev, char *pcapdev, int pcapdevlen); #endif +/** Tries to increase the open file descriptor limit for this process. + * @param "desired" is the number of desired max open descriptors. Pass a + * negative value to set the maximum allowed. + * @return the number of max open descriptors that could be set, or 0 in case + * of failure. + * @warning if "desired" is less than the current limit, no action is + * performed. This function may only be used to increase the limit, not to + * decrease it. */ +int set_max_open_descriptors(int desired_max); + +/** Returns the open file descriptor limit for this process. + * @return the number of max open descriptors or 0 in case of failure. */ +int get_max_open_descriptors(); + +/* Maximize the open file descriptor limit for this process go up to the + max allowed */ +int max_sd(); + #endif /* _NETUTIL_H_ */ diff --git a/tcpip.cc b/tcpip.cc index 2f56188df..d54acf807 100644 --- a/tcpip.cc +++ b/tcpip.cc @@ -108,10 +108,6 @@ #include #endif -#if HAVE_SYS_RESOURCE_H -#include -#endif - #if HAVE_UNISTD_H /* #include */ #include @@ -1665,45 +1661,6 @@ void max_rcvbuf(int sd) { #endif /* WIN32 */ } -/* Maximize the open file descriptor limit for this process go up to the - max allowed */ -int max_sd() { -#ifndef WIN32 - struct rlimit r; - static int maxfds = -1; - - if (maxfds > 0) - return maxfds; - -#if(defined(RLIMIT_NOFILE)) - if (!getrlimit(RLIMIT_NOFILE, &r)) { - r.rlim_cur = r.rlim_max; - if (setrlimit(RLIMIT_NOFILE, &r)) - if (o.debugging) - perror("setrlimit RLIMIT_NOFILE failed"); - if (!getrlimit(RLIMIT_NOFILE, &r)) { - maxfds = r.rlim_cur; - return maxfds; - } else - return 0; - } -#endif -#if(defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE)) - if (!getrlimit(RLIMIT_OFILE, &r)) { - r.rlim_cur = r.rlim_max; - if (setrlimit(RLIMIT_OFILE, &r)) - if (o.debugging) - perror("setrlimit RLIMIT_OFILE failed"); - if (!getrlimit(RLIMIT_OFILE, &r)) { - maxfds = r.rlim_cur; - return maxfds; - } else - return 0; - } -#endif -#endif /* WIN32 */ - return 0; -} /* Give broadcast permission to a socket */ void broadcast_socket(int sd) { diff --git a/tcpip.h b/tcpip.h index da78c9e2a..9ab6df069 100644 --- a/tcpip.h +++ b/tcpip.h @@ -639,10 +639,6 @@ int gettcpopt_ts(struct tcp_hdr *tcp, u32 *timestamp, u32 *echots); /* Maximize the receive buffer of a socket descriptor (up to 500K) */ void max_rcvbuf(int sd); -/* Maximize the open file descriptor limit for this process go up to the - max allowed */ -int max_sd(); - /* Give broadcast permission to a socket */ void broadcast_socket(int sd);