]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socketengines/socketengine_poll.cpp
Do not enable SO_LINGER on our sockets
[user/henk/code/inspircd.git] / src / socketengines / socketengine_poll.cpp
index a72d21d1ff9a15a26d00a1cb142f19d16bafe87d..5c361a0cbded453bd9a913f957f4ff31ff02ad73 100644 (file)
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
- * See: http://wiki.inspircd.org/Credits
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
+ *   Copyright (C) 2009 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
  *
- * This program is free but copyrighted software; see
- *    the file COPYING for details.
+ * 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
+ * License as published by the Free Software Foundation, version 2.
  *
- * ---------------------------------------------------
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+
 #include "inspircd.h"
 #include "exitcodes.h"
-#include "socketengines/socketengine_poll.h"
-#include <ulimit.h>
-#ifdef __FreeBSD__
+
+#ifndef SOCKETENGINE_POLL
+#define SOCKETENGINE_POLL
+
+#include <iostream>
+#include <vector>
+#include <string>
+#include <map>
+#include "inspircd_config.h"
+#include "inspircd.h"
+#include "socketengine.h"
+
+#ifndef _WIN32
+       #ifndef __USE_XOPEN
+           #define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/
+       #endif
+       #include <poll.h>
+       #include <sys/poll.h>
+#else
+       /* *grumble* */
+       #define struct pollfd WSAPOLLFD
+       #define poll WSAPoll
+#endif
+
+class InspIRCd;
+
+/** A specialisation of the SocketEngine class, designed to use poll().
+ */
+class PollEngine : public SocketEngine
+{
+private:
+       /** These are used by poll() to hold socket events
+        */
+       struct pollfd *events;
+       /** This map maps fds to an index in the events array.
+        */
+       std::map<int, unsigned int> fd_mappings;
+public:
+       /** Create a new PollEngine
+        */
+       PollEngine();
+       /** Delete a PollEngine
+        */
+       virtual ~PollEngine();
+       virtual bool AddFd(EventHandler* eh, int event_mask);
+       virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
+       virtual EventHandler* GetRef(int fd);
+       virtual void DelFd(EventHandler* eh);
+       virtual int DispatchEvents();
+       virtual std::string GetName();
+};
+
+#endif
+
+#ifdef BSD
        #include <sys/sysctl.h>
+#else
+       #include <ulimit.h>
 #endif
 
 PollEngine::PollEngine()
 {
        CurrentSetSize = 0;
-#ifndef __FreeBSD__
+#ifdef BSD
+       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);
+       sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
+#else
        int max = ulimit(4, 0);
        if (max > 0)
        {
@@ -31,17 +106,9 @@ PollEngine::PollEngine()
        else
        {
                ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
-               printf("ERROR: Can't determine maximum number of open sockets: %s\n", strerror(errno));
+               std::cout << "ERROR: Can't determine maximum number of open sockets: " << strerror(errno) << std::endl;
                ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
        }
-#else
-       int mib[2];
-       size_t len;
-
-       mib[0] = CTL_KERN;
-       mib[1] = KERN_MAXFILES;
-       len = sizeof(MAX_DESCRIPTORS);
-       sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
 #endif
 
        ref = new EventHandler* [GetMaxFds()];
@@ -116,20 +183,20 @@ void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
        events[it->second].events = mask_to_poll(new_mask);
 }
 
-bool PollEngine::DelFd(EventHandler* eh, bool force)
+void PollEngine::DelFd(EventHandler* eh)
 {
        int fd = eh->GetFd();
        if ((fd < 0) || (fd > MAX_DESCRIPTORS))
        {
                ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
-               return false;
+               return;
        }
 
        std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
        if (it == fd_mappings.end())
        {
                ServerInstance->Logs->Log("SOCKET",DEBUG,"DelFd() on unknown fd: %d", fd);
-               return false;
+               return;
        }
 
        unsigned int index = it->second;
@@ -161,7 +228,6 @@ bool PollEngine::DelFd(EventHandler* eh, bool force)
 
        ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d (index: %d) "
                        "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
-       return true;
 }
 
 int PollEngine::DispatchEvents()
@@ -171,6 +237,7 @@ int PollEngine::DispatchEvents()
        socklen_t codesize = sizeof(int);
        int errcode;
        int processed = 0;
+       ServerInstance->UpdateTime();
 
        if (i > 0)
        {
@@ -204,6 +271,9 @@ int PollEngine::DispatchEvents()
                        {
                                SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
                                eh->HandleEvent(EVENT_READ);
+                               if (eh != ref[index])
+                                       // whoops, deleted out from under us
+                                       continue;
                        }
                        
                        if (events[index].revents & POLLOUT)
@@ -225,3 +295,7 @@ std::string PollEngine::GetName()
        return "poll";
 }
 
+SocketEngine* CreateSocketEngine()
+{
+       return new PollEngine;
+}