diff --git a/nsock/src/netutils.c b/nsock/src/netutils.c index 6410838f0..8cb75ae05 100644 --- a/nsock/src/netutils.c +++ b/nsock/src/netutils.c @@ -93,13 +93,14 @@ static int netutils_debugging = 0; * process and return that maximum value (note -- you better not actually open * this many -- stdin, stdout, other files opened by libraries you use, etc. all * count toward this limit. Leave a little slack */ -int maximize_fdlimit(void) { +rlim_t maximize_fdlimit(void) { #ifndef WIN32 struct rlimit r; - static int maxfds = -1; + static int maxfds_set = 0; + static rlim_t maxfds = 0; - if (maxfds > 0) + if (maxfds_set) return maxfds; #ifndef RLIMIT_NOFILE @@ -110,17 +111,17 @@ int maximize_fdlimit(void) { #endif #endif if (!getrlimit(RLIMIT_NOFILE, &r)) { + maxfds = r.rlim_cur; r.rlim_cur = r.rlim_max; - if (setrlimit(RLIMIT_NOFILE, &r)) + if (!setrlimit(RLIMIT_NOFILE, &r)) if (netutils_debugging) perror("setrlimit RLIMIT_NOFILE failed"); if (!getrlimit(RLIMIT_NOFILE, &r)) { maxfds = r.rlim_cur; - return maxfds; - } else { - return 0; } + maxfds_set = 1; + return maxfds; } #endif /* !WIN32 */ return 0; diff --git a/nsock/src/netutils.h b/nsock/src/netutils.h index 009e7c60a..363d82318 100644 --- a/nsock/src/netutils.h +++ b/nsock/src/netutils.h @@ -79,11 +79,18 @@ #include #endif +#if HAVE_SYS_RESOURCE_H +#include +#else +#ifndef rlim_t +#define rlim_t int +#endif +#endif /* Maximize the number of file descriptors (including sockets) allowed for this * process and return that maximum value (note -- you better not actually open * this many -- stdin, stdout, other files opened by libraries you use, etc. all * count toward this limit. Leave a little slack */ -int maximize_fdlimit(void); +rlim_t maximize_fdlimit(void); /* Get the UNIX domain socket path or empty string if the address family != AF_UNIX. */ const char *get_unixsock_path(const struct sockaddr_storage *addr); diff --git a/nsock/src/nsock_pool.c b/nsock/src/nsock_pool.c index 8231870da..92bf981ba 100644 --- a/nsock/src/nsock_pool.c +++ b/nsock/src/nsock_pool.c @@ -71,6 +71,9 @@ #endif #include +#if HAVE_SYS_RESOURCE_H +#include +#endif extern struct timeval nsock_tod; @@ -294,17 +297,16 @@ void nsock_pool_delete(nsock_pool ms_pool) { } void nsock_library_initialize(void) { - int res; +#ifndef WIN32 + rlim_t res; /* We want to make darn sure the evil SIGPIPE is ignored */ -#ifndef WIN32 signal(SIGPIPE, SIG_IGN); -#endif /* And we're gonna need sockets -- LOTS of sockets ... */ res = maximize_fdlimit(); -#ifndef WIN32 assert(res > 7); #endif + return; }