1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-18 21:49:01 +00:00

Make maximize_fdlimit return rlim_t on appropriate platforms. Closes #2085. Fixes #2079

This commit is contained in:
dmiller
2020-07-19 05:00:13 +00:00
parent 3421d01d2e
commit 05763b620d
3 changed files with 22 additions and 12 deletions

View File

@@ -93,13 +93,14 @@ static int netutils_debugging = 0;
* process and return that maximum value (note -- you better not actually open * 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 * this many -- stdin, stdout, other files opened by libraries you use, etc. all
* count toward this limit. Leave a little slack */ * count toward this limit. Leave a little slack */
int maximize_fdlimit(void) { rlim_t maximize_fdlimit(void) {
#ifndef WIN32 #ifndef WIN32
struct rlimit r; 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; return maxfds;
#ifndef RLIMIT_NOFILE #ifndef RLIMIT_NOFILE
@@ -110,17 +111,17 @@ int maximize_fdlimit(void) {
#endif #endif
#endif #endif
if (!getrlimit(RLIMIT_NOFILE, &r)) { if (!getrlimit(RLIMIT_NOFILE, &r)) {
maxfds = r.rlim_cur;
r.rlim_cur = r.rlim_max; r.rlim_cur = r.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &r)) if (!setrlimit(RLIMIT_NOFILE, &r))
if (netutils_debugging) if (netutils_debugging)
perror("setrlimit RLIMIT_NOFILE failed"); perror("setrlimit RLIMIT_NOFILE failed");
if (!getrlimit(RLIMIT_NOFILE, &r)) { if (!getrlimit(RLIMIT_NOFILE, &r)) {
maxfds = r.rlim_cur; maxfds = r.rlim_cur;
return maxfds;
} else {
return 0;
} }
maxfds_set = 1;
return maxfds;
} }
#endif /* !WIN32 */ #endif /* !WIN32 */
return 0; return 0;

View File

@@ -79,11 +79,18 @@
#include <sys/un.h> #include <sys/un.h>
#endif #endif
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#else
#ifndef rlim_t
#define rlim_t int
#endif
#endif
/* Maximize the number of file descriptors (including sockets) allowed for this /* Maximize the number of file descriptors (including sockets) allowed for this
* process and return that maximum value (note -- you better not actually open * 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 * this many -- stdin, stdout, other files opened by libraries you use, etc. all
* count toward this limit. Leave a little slack */ * 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. */ /* 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); const char *get_unixsock_path(const struct sockaddr_storage *addr);

View File

@@ -71,6 +71,9 @@
#endif #endif
#include <signal.h> #include <signal.h>
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
extern struct timeval nsock_tod; extern struct timeval nsock_tod;
@@ -294,17 +297,16 @@ void nsock_pool_delete(nsock_pool ms_pool) {
} }
void nsock_library_initialize(void) { void nsock_library_initialize(void) {
int res; #ifndef WIN32
rlim_t res;
/* We want to make darn sure the evil SIGPIPE is ignored */ /* We want to make darn sure the evil SIGPIPE is ignored */
#ifndef WIN32
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
#endif
/* And we're gonna need sockets -- LOTS of sockets ... */ /* And we're gonna need sockets -- LOTS of sockets ... */
res = maximize_fdlimit(); res = maximize_fdlimit();
#ifndef WIN32
assert(res > 7); assert(res > 7);
#endif #endif
return;
} }