]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Convert GetMaxFds() to size_t and deduplicate setting code.
authorPeter Powell <petpow@saberuk.com>
Sun, 22 Oct 2017 01:27:25 +0000 (02:27 +0100)
committerPeter Powell <petpow@saberuk.com>
Sun, 22 Oct 2017 18:44:45 +0000 (19:44 +0100)
include/socketengine.h
src/inspircd.cpp
src/socketengine.cpp
src/socketengines/socketengine_epoll.cpp
src/socketengines/socketengine_kqueue.cpp
src/socketengines/socketengine_poll.cpp
src/socketengines/socketengine_select.cpp

index fa2f263583846b7746b20f547c706b09dd7055e1..34dd306ba45d5766501ade3a80c4dd075beab9f8 100644 (file)
@@ -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
index 3975169390336fdf7bff34d5ac624724bcd3e059..80d1df75d12117334606cd0560c3dd0dc63a3352 100644 (file)
@@ -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");
index 3735e7530aa899c65567953bce30f109aa39765c..bac97a6dc3eb6e81f14fb2375cdd38a18e0ee140 100644 (file)
@@ -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;
index c442e340dcd46abd7d8b6fa93c0f22d0daacfd93..dc10a3613ce7c5c22b117afa1a35ed621a5149f0 100644 (file)
@@ -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.
index 9db9023142c034dba9e30298a1714c1ecfaafb2e..c969af1fdd8b721ba8931eba19a86df2bb076508 100644 (file)
@@ -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();
 }
 
index a5e86ba82b9f7715a7034e0656a2e46433e47d10..c805935882badefa3e5fe3673a8f600a0ba41c21 100644 (file)
@@ -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()
index 42f634db18c35aa9c9db22f27b76261c6496b6e8..03f0aca62a04de54ab73137d0439dccc0f88888a 100644 (file)
@@ -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);