]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
Extra error detection, thwap me if this comes up with false positives
[user/henk/code/inspircd.git] / src / inspircd.cpp
index 1df6ee2932f58274fa6fd0f6e80cca08c725a889..9b7d6fa1786d1b131dc6492ea8b396c619149e9c 100644 (file)
@@ -26,6 +26,9 @@ using namespace std;
 #include <sys/ioctl.h>
 #include <time.h>
 #include <string>
+#include <exception>
+#include <stdexcept>
+#include <new>
 #ifdef GCC3
 #include <ext/hash_map>
 #else
@@ -164,16 +167,20 @@ InspIRCd::InspIRCd(int argc, char** argv)
        if (argc > 1) {
                for (int i = 1; i < argc; i++)
                {
-                       if (!strcmp(argv[i],"-nofork")) {
+                       if (!strcmp(argv[i],"-nofork"))
+                       {
                                Config->nofork = true;
                        }
-                       if (!strcmp(argv[i],"-wait")) {
+                       if (!strcmp(argv[i],"-wait"))
+                       {
                                sleep(6);
                        }
-                       if (!strcmp(argv[i],"-nolimit")) {
-                               Config->unlimitcore = true;
+                       if (!strcmp(argv[i],"-nolimit"))
+                       {
+                               printf("WARNING: The `-nolimit' option is deprecated, and now on by default. This behaviour may change in the future.\n");
                        }
-                       if (!strcmp(argv[i],"-logfile")) {
+                       if (!strcmp(argv[i],"-logfile"))
+                       {
                                if (argc > i+1)
                                {
                                        strlcpy(LOG_FILE,argv[i+1],MAXBUF);
@@ -193,15 +200,15 @@ InspIRCd::InspIRCd(int argc, char** argv)
        this->MakeLowerMap();
 
         OpenLog(argv, argc);
+       this->stats = new serverstats();
         Config->ClearStack();
         Config->Read(true,NULL);
         CheckRoot();
        this->ModeGrok = new ModeParser();
        this->Parser = new CommandParser();
-       this->stats = new serverstats();
         AddServerName(Config->ServerName);
         CheckDie();
-        stats->BoundPortCount = BindPorts();
+        stats->BoundPortCount = BindPorts(true);
 
        for(int t = 0; t < 255; t++)
                Config->global_implementation[t] = 0;
@@ -212,7 +219,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
        SetSignals();
         if (!Config->nofork)
         {
-                if (DaemonSeed() == ERROR)
+               if (!DaemonSeed())
                 {
                         printf("ERROR: could not go into daemon mode. Shutting down.\n");
                         Exit(ERROR);
@@ -586,7 +593,7 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
         sockaddr_in sock_us;     // our port number
         socklen_t uslen;         // length of our port number
 
-       if (yield_depth > 3)
+       if (yield_depth > 100)
                return;
 
        yield_depth++;
@@ -676,6 +683,8 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
          * listening ports or module sockets though, things could get
          * ugly.
          */
+       log(DEBUG,"There are %d fd's to process.",numberactive);
+
         for (unsigned int activefd = 0; activefd < numberactive; activefd++)
         {
                 int socket_type = SE->GetType(activefds[activefd]);
@@ -683,6 +692,7 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
                 {
                         case X_ESTAB_CLIENT:
 
+                               log(DEBUG,"Type: X_ESTAB_CLIENT: fd=%d",activefds[activefd]);
                                 cu = fd_ref_table[activefds[activefd]];
                                 if (cu)
                                         ProcessUser(cu);  
@@ -691,6 +701,8 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
         
                         case X_ESTAB_MODULE:
 
+                               log(DEBUG,"Type: X_ESTAB_MODULE: fd=%d",activefds[activefd]);
+
                                if (!process_module_sockets)
                                        break;
 
@@ -717,6 +729,11 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
                                         s->Close();
                                         delete s;
                                 }
+                               else if (!s)
+                               {
+                                       log(DEBUG,"WTF, X_ESTAB_MODULE for nonexistent InspSocket, removed!");
+                                       SE->DelFd(s->GetFd());
+                               }
                         break;
 
                         case X_ESTAB_DNS:
@@ -728,12 +745,15 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
                                  * and independent of the mainloop.
                                  */
 #ifndef THREADED_DNS
+                               log(DEBUG,"Type: X_ESTAB_DNS: fd=%d",activefds[activefd]);
                                 dns_poll(activefds[activefd]);
 #endif
                         break;
 
                        case X_LISTEN:
 
+                               log(DEBUG,"Type: X_LISTEN_MODULE: fd=%d",activefds[activefd]);
+
                                 /* It's a listener */
                                 uslen = sizeof(sock_us);
                                 length = sizeof(client);
@@ -778,6 +798,7 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
                                  * what we would do, so for now, its going
                                  * to safely do bugger all.
                                  */
+                               log(DEBUG,"Type: X_WHAT_THE_FUCK_BBQ: fd=%d",activefds[activefd]);
                                SE->DelFd(activefds[activefd]);
                         break;
                 }
@@ -791,6 +812,9 @@ int InspIRCd::Run()
        
         LoadAllModules(this);
 
+       /* Just in case no modules were loaded - fix for bug #101 */
+       this->BuildISupport();
+
         printf("\nInspIRCd is now running!\n");
        
        if (!Config->nofork)
@@ -829,9 +853,18 @@ int InspIRCd::Run()
 
 int main(int argc, char** argv)
 {
-        ServerInstance = new InspIRCd(argc, argv);
-        ServerInstance->Run();
-        delete ServerInstance;
-        return 0;
+       try
+       {
+               ServerInstance = new InspIRCd(argc, argv);
+               ServerInstance->Run();
+               delete ServerInstance;
+       }
+       catch (std::bad_alloc)
+       {
+               log(DEFAULT,"You are out of memory! (got exception std::bad_alloc!)");
+               send_error("**** OUT OF MEMORY **** We're gonna need a bigger boat!");
+               printf("Out of memory! (got exception std::bad_alloc!");
+       }
+       return 0;
 }