summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPeter Powell <petpow@saberuk.com>2017-10-22 02:27:25 +0100
committerPeter Powell <petpow@saberuk.com>2017-10-22 19:44:45 +0100
commitb1098712771ab823042fcf8614a706c76c2ff401 (patch)
tree7feb35687035b5ac6e1dd4181501400c31a54efc /src
parentd4414f54910aeaa4809a7eacea75a089a0820f40 (diff)
Convert GetMaxFds() to size_t and deduplicate setting code.
Diffstat (limited to 'src')
-rw-r--r--src/inspircd.cpp2
-rw-r--r--src/socketengine.cpp18
-rw-r--r--src/socketengines/socketengine_epoll.cpp7
-rw-r--r--src/socketengines/socketengine_kqueue.cpp15
-rw-r--r--src/socketengines/socketengine_poll.cpp11
-rw-r--r--src/socketengines/socketengine_select.cpp13
6 files changed, 30 insertions, 36 deletions
diff --git a/src/inspircd.cpp b/src/inspircd.cpp
index 397516939..80d1df75d 100644
--- a/src/inspircd.cpp
+++ b/src/inspircd.cpp
@@ -498,7 +498,7 @@ InspIRCd::InspIRCd(int argc, char** argv) :
QueryPerformanceFrequency(&stats.QPFrequency);
#endif
- Logs->Log("STARTUP", LOG_DEFAULT, "Startup complete as '%s'[%s], %d max open sockets", Config->ServerName.c_str(),Config->GetSID().c_str(), SocketEngine::GetMaxFds());
+ Logs->Log("STARTUP", LOG_DEFAULT, "Startup complete as '%s'[%s], %lu max open sockets", Config->ServerName.c_str(),Config->GetSID().c_str(), SocketEngine::GetMaxFds());
#ifndef _WIN32
ConfigTag* security = Config->ConfValue("security");
diff --git a/src/socketengine.cpp b/src/socketengine.cpp
index 3735e7530..bac97a6dc 100644
--- a/src/socketengine.cpp
+++ b/src/socketengine.cpp
@@ -23,7 +23,6 @@
#include "inspircd.h"
-
/** Reference table, contains all current handlers
**/
std::vector<EventHandler*> SocketEngine::ref;
@@ -36,7 +35,7 @@ size_t SocketEngine::CurrentSetSize = 0;
*/
std::set<int> SocketEngine::trials;
-int SocketEngine::MAX_DESCRIPTORS;
+size_t SocketEngine::MaxSetSize = 0;
/** Socket engine statistics: count of various events, bandwidth usage
*/
@@ -61,6 +60,21 @@ void EventHandler::OnEventHandlerError(int errornum)
{
}
+void SocketEngine::LookupMaxFds()
+{
+ struct rlimit limits;
+ if (!getrlimit(RLIMIT_NOFILE, &limits))
+ MaxSetSize = limits.rlim_cur;
+
+#if defined __APPLE__
+ limits.rlim_cur = limits.rlim_max == RLIM_INFINITY ? OPEN_MAX : limits.rlim_max;
+#else
+ limits.rlim_cur = limits.rlim_max;
+#endif
+ if (!setrlimit(RLIMIT_NOFILE, &limits))
+ MaxSetSize = limits.rlim_cur;
+}
+
void SocketEngine::ChangeEventMask(EventHandler* eh, int change)
{
int old_m = eh->event_mask;
diff --git a/src/socketengines/socketengine_epoll.cpp b/src/socketengines/socketengine_epoll.cpp
index c442e340d..dc10a3613 100644
--- a/src/socketengines/socketengine_epoll.cpp
+++ b/src/socketengines/socketengine_epoll.cpp
@@ -38,12 +38,7 @@ namespace
void SocketEngine::Init()
{
- // MAX_DESCRIPTORS is mainly used for display purposes, no problem if getrlimit() fails
- struct rlimit limit;
- if (!getrlimit(RLIMIT_NOFILE, &limit))
- {
- MAX_DESCRIPTORS = limit.rlim_cur;
- }
+ LookupMaxFds();
// 128 is not a maximum, just a hint at the eventual number of sockets that may be polled,
// and it is completely ignored by 2.6.8 and later kernels, except it must be larger than zero.
diff --git a/src/socketengines/socketengine_kqueue.cpp b/src/socketengines/socketengine_kqueue.cpp
index 9db902314..c969af1fd 100644
--- a/src/socketengines/socketengine_kqueue.cpp
+++ b/src/socketengines/socketengine_kqueue.cpp
@@ -46,20 +46,7 @@ namespace
*/
void SocketEngine::Init()
{
- MAX_DESCRIPTORS = 0;
- int mib[2];
- size_t len;
-
- mib[0] = CTL_KERN;
-#ifdef KERN_MAXFILESPERPROC
- mib[1] = KERN_MAXFILESPERPROC;
-#else
- mib[1] = KERN_MAXFILES;
-#endif
- len = sizeof(MAX_DESCRIPTORS);
- // MAX_DESCRIPTORS is mainly used for display purposes, no problem if the sysctl() below fails
- sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
-
+ LookupMaxFds();
RecoverFromFork();
}
diff --git a/src/socketengines/socketengine_poll.cpp b/src/socketengines/socketengine_poll.cpp
index a5e86ba82..c80593588 100644
--- a/src/socketengines/socketengine_poll.cpp
+++ b/src/socketengines/socketengine_poll.cpp
@@ -41,16 +41,7 @@ namespace
void SocketEngine::Init()
{
- struct rlimit limits;
- if (!getrlimit(RLIMIT_NOFILE, &limits))
- {
- MAX_DESCRIPTORS = limits.rlim_cur;
- }
- else
- {
- // MAX_DESCRIPTORS is mainly used for display purposes, it's not a problem that getrlimit() failed
- MAX_DESCRIPTORS = -1;
- }
+ LookupMaxFds();
}
void SocketEngine::Deinit()
diff --git a/src/socketengines/socketengine_select.cpp b/src/socketengines/socketengine_select.cpp
index 42f634db1..03f0aca62 100644
--- a/src/socketengines/socketengine_select.cpp
+++ b/src/socketengines/socketengine_select.cpp
@@ -35,7 +35,7 @@ namespace
void SocketEngine::Init()
{
- MAX_DESCRIPTORS = FD_SETSIZE;
+ MaxSetSize = FD_SETSIZE;
FD_ZERO(&ReadSet);
FD_ZERO(&WriteSet);
@@ -53,7 +53,11 @@ void SocketEngine::RecoverFromFork()
bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
{
int fd = eh->GetFd();
- if ((fd < 0) || (fd > GetMaxFds() - 1))
+
+ if (fd < 0)
+ return false;
+
+ if (static_cast<size_t>(fd) >= GetMaxFds())
return false;
if (!SocketEngine::AddFdRef(eh))
@@ -73,7 +77,10 @@ void SocketEngine::DelFd(EventHandler* eh)
{
int fd = eh->GetFd();
- if ((fd < 0) || (fd > GetMaxFds() - 1))
+ if (fd < 0)
+ return;
+
+ if (static_cast<size_t>(fd) >= GetMaxFds())
return;
SocketEngine::DelFdRef(eh);