**/
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.
/** 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
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");
#include "inspircd.h"
-
/** Reference table, contains all current handlers
**/
std::vector<EventHandler*> SocketEngine::ref;
*/
std::set<int> SocketEngine::trials;
-int SocketEngine::MAX_DESCRIPTORS;
+size_t SocketEngine::MaxSetSize = 0;
/** Socket engine statistics: count of various events, bandwidth usage
*/
{
}
+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;
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.
*/
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();
}
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()
void SocketEngine::Init()
{
- MAX_DESCRIPTORS = FD_SETSIZE;
+ MaxSetSize = FD_SETSIZE;
FD_ZERO(&ReadSet);
FD_ZERO(&WriteSet);
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))
{
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);