diff options
-rw-r--r-- | include/socketengine.h | 19 | ||||
-rw-r--r-- | src/inspircd.cpp | 2 | ||||
-rw-r--r-- | src/socketengine.cpp | 18 | ||||
-rw-r--r-- | src/socketengines/socketengine_epoll.cpp | 7 | ||||
-rw-r--r-- | src/socketengines/socketengine_kqueue.cpp | 15 | ||||
-rw-r--r-- | src/socketengines/socketengine_poll.cpp | 11 | ||||
-rw-r--r-- | src/socketengines/socketengine_select.cpp | 13 |
7 files changed, 41 insertions, 44 deletions
diff --git a/include/socketengine.h b/include/socketengine.h index fa2f26358..34dd306ba 100644 --- a/include/socketengine.h +++ b/include/socketengine.h @@ -266,20 +266,23 @@ class CoreExport SocketEngine **/ static std::vector<EventHandler*> ref; - protected: - /** Current number of descriptors in the engine - */ + /** Current number of descriptors in the engine. */ static size_t CurrentSetSize; + + /** The maximum number of descriptors in the engine. */ + static size_t MaxSetSize; + /** List of handlers that want a trial read/write */ static std::set<int> trials; - static int MAX_DESCRIPTORS; - /** Socket engine statistics: count of various events, bandwidth usage */ static Statistics stats; + /** Look up the fd limit using rlimit. */ + static void LookupMaxFds(); + static void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); /** Add an event handler to the base socket engine. AddFd(EventHandler*, int) should call this. @@ -344,10 +347,10 @@ public: /** Returns the number of file descriptors reported by the system this program may use * when it was started. - * @return If positive, the number of file descriptors that the system reported that we - * may use. Otherwise (<= 0) this number could not be determined. + * @return If non-zero the number of file descriptors that the system reported that we + * may use. */ - static int GetMaxFds() { return MAX_DESCRIPTORS; } + static size_t GetMaxFds() { return MaxSetSize; } /** Returns the number of file descriptors being queried * @return The set size 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); |