]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socketengines/socketengine_kqueue.cpp
Convert the ISO 8859-2 nationalchars files to codepage configs.
[user/henk/code/inspircd.git] / src / socketengines / socketengine_kqueue.cpp
index d5a3bb793695ca3707c0e3b37304e4da69d5950f..e0a0535f4e8859da6de71de5ffa9cea42027a4c0 100644 (file)
@@ -1,9 +1,15 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2014-2015 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2014 Adam <Adam@anope.org>
+ *   Copyright (C) 2012-2013, 2017, 2019 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
- *   Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
- *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
+ *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
  *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
  * redistribute it and/or modify it under the terms of the GNU General Public
 
 
 #include "inspircd.h"
-#include "exitcodes.h"
+
 #include <sys/types.h>
 #include <sys/event.h>
 #include <sys/time.h>
-#include "socketengine.h"
-#include <iostream>
 #include <sys/sysctl.h>
 
 /** A specialisation of the SocketEngine class, designed to use BSD kqueue().
@@ -41,26 +45,27 @@ namespace
        /** Pending changes
         */
        std::vector<struct kevent> changelist(8);
+
+#if defined __NetBSD__ && __NetBSD_Version__ <= 999001400
+       inline intptr_t udata_cast(EventHandler* eh)
+       {
+               // On NetBSD <10 the last parameter of EV_SET is intptr_t.
+               return reinterpret_cast<intptr_t>(eh);
+       }
+#else
+       inline void* udata_cast(EventHandler* eh)
+       {
+               // On other platforms the last parameter of EV_SET is void*.
+               return static_cast<void*>(eh);
+       }
+#endif
 }
 
 /** Initialize the kqueue engine
  */
 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();
 }
 
@@ -73,13 +78,7 @@ void SocketEngine::RecoverFromFork()
         */
        EngineHandle = kqueue();
        if (EngineHandle == -1)
-       {
-               ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
-               ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: this is a fatal error, exiting now.");
-               std::cout << "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features." << std::endl;
-               std::cout << "ERROR: this is a fatal error, exiting now." << std::endl;
-               ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
-       }
+               InitError();
 }
 
 /** Shutdown the kqueue engine
@@ -108,7 +107,7 @@ bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
 
        // We always want to read from the socket...
        struct kevent* ke = GetChangeKE();
-       EV_SET(ke, fd, EVFILT_READ, EV_ADD, 0, 0, static_cast<void*>(eh));
+       EV_SET(ke, fd, EVFILT_READ, EV_ADD, 0, 0, udata_cast(eh));
 
        ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
 
@@ -149,7 +148,7 @@ void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
        {
                // new poll-style write
                struct kevent* ke = GetChangeKE();
-               EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, static_cast<void*>(eh));
+               EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, udata_cast(eh));
        }
        else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE))
        {
@@ -160,7 +159,7 @@ void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
        if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)))
        {
                struct kevent* ke = GetChangeKE();
-               EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, static_cast<void*>(eh));
+               EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, udata_cast(eh));
        }
 }
 
@@ -181,8 +180,9 @@ int SocketEngine::DispatchEvents()
 
        for (int j = 0; j < i; j++)
        {
+               // This can't be a static_cast because udata is intptr_t on NetBSD.
                struct kevent& kev = ke_list[j];
-               EventHandler* eh = static_cast<EventHandler*>(kev.udata);
+               EventHandler* eh = reinterpret_cast<EventHandler*>(kev.udata);
                if (!eh)
                        continue;
 
@@ -195,25 +195,23 @@ int SocketEngine::DispatchEvents()
                if (kev.flags & EV_EOF)
                {
                        stats.ErrorEvents++;
-                       eh->HandleEvent(EVENT_ERROR, kev.fflags);
+                       eh->OnEventHandlerError(kev.fflags);
                        continue;
                }
                if (filter == EVFILT_WRITE)
                {
-                       stats.WriteEvents++;
                        /* When mask is FD_WANT_FAST_WRITE or FD_WANT_SINGLE_WRITE,
                         * we set a one-shot write, so we need to clear that bit
                         * to detect when it set again.
                         */
                        const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
                        eh->SetEventMask(eh->GetEventMask() & ~bits_to_clr);
-                       eh->HandleEvent(EVENT_WRITE);
+                       eh->OnEventHandlerWrite();
                }
                else if (filter == EVFILT_READ)
                {
-                       stats.ReadEvents++;
                        eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
-                       eh->HandleEvent(EVENT_READ);
+                       eh->OnEventHandlerRead();
                }
        }