From 4931dc5dcb8d520377d72965e51c778f6c51cf51 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Sun, 2 Aug 2009 22:26:07 +0200 Subject: [PATCH] Added call functionality to figure out the maximum number of sockets (file descriptors) available on system. --- src/network/network.c | 15 +++++++++++++++ src/network/network.h | 5 +++++ src/uhub.h | 2 ++ 3 files changed, 22 insertions(+) diff --git a/src/network/network.c b/src/network/network.c index e68bb8b..90955b4 100644 --- a/src/network/network.c +++ b/src/network/network.c @@ -61,6 +61,21 @@ int net_initialize() return -1; } +size_t net_get_max_sockets() +{ +#ifdef HAVE_GETRLIMIT + struct rlimit limits; + if (getrlimit(RLIMIT_NOFILE, &limits) == 0) + { + return limits.rlim_max; + } + LOG_ERROR("getrlimit() failed"); +#else + LOG_ERROR("System does not have getrlimit(): constrained to 1024 sockets"); +#endif /* HAVE_GETRLIMIT */ + return 1024; +} + int net_destroy() { diff --git a/src/network/network.h b/src/network/network.h index a3e034f..fbb02d3 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -87,6 +87,11 @@ extern const char* net_error_string(int code); */ extern int net_socket_create(int af, int type, int protocol); +/** + * Returns the maximum number of file/socket descriptors. + */ +extern size_t net_get_max_sockets(); + /** * A wrapper for the close() function call. */ diff --git a/src/uhub.h b/src/uhub.h index 9d25431..3fa1445 100644 --- a/src/uhub.h +++ b/src/uhub.h @@ -83,8 +83,10 @@ #ifndef WIN32 #include #include +#include #define HAVE_STRNDUP #define HAVE_MEMMEM +#define HAVE_GETRLIMIT #endif #ifdef SSL_SUPPORT