]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
Auto loading of commands as shared objects via dlsym (very lightweight interface...
[user/henk/code/inspircd.git] / src / inspircd.cpp
index 3fddb2d8b47fdde47185968738302227840a6f2f..7efbaa54f02a3c078a7f4cf543c9ff593a647d23 100644 (file)
 #include "inspircd.h"
 #include "configreader.h"
 #include <signal.h>
-#include <time.h>
-#include <string>
 #include <exception>
-#include <stdexcept>
-#include <new>
-#include <map>
-#include <sstream>
 #include <fstream>
-#include <vector>
-#include <deque>
 #include "modules.h"
 #include "mode.h"
 #include "xline.h"
@@ -50,6 +42,7 @@
 #include "command_parse.h"
 
 using irc::sockets::NonBlocking;
+using irc::sockets::Blocking;
 using irc::sockets::insp_ntoa;
 using irc::sockets::insp_inaddr;
 using irc::sockets::insp_sockaddr;
@@ -107,22 +100,30 @@ void InspIRCd::Rehash(int status)
 
 void InspIRCd::SetSignals(bool SEGVHandler)
 {
-       signal (SIGALRM, SIG_IGN);
-       signal (SIGHUP, InspIRCd::Rehash);
-       signal (SIGPIPE, SIG_IGN);
-       signal (SIGTERM, InspIRCd::Exit);
+       signal(SIGALRM, SIG_IGN);
+       signal(SIGHUP, InspIRCd::Rehash);
+       signal(SIGPIPE, SIG_IGN);
+       signal(SIGTERM, InspIRCd::Exit);
+       signal(SIGCHLD, SIG_IGN);
 }
 
 bool InspIRCd::DaemonSeed()
 {
        int childpid;
        if ((childpid = fork ()) < 0)
-               return (ERROR);
+               return false;
        else if (childpid > 0)
        {
-               /* We wait a few seconds here, so that the shell prompt doesnt come back over the output */
-               sleep(6);
-               exit (0);
+               /* We wait here for the child process to kill us,
+                * so that the shell prompt doesnt come back over
+                * the output.
+                * Sending a kill with a signal of 0 just checks
+                * if the child pid is still around. If theyre not,
+                * they threw an error and we should give up.
+                */
+               while (kill(childpid, 0) != -1)
+                       sleep(1);
+               exit(ERROR);
        }
        setsid ();
        umask (007);
@@ -140,7 +141,7 @@ bool InspIRCd::DaemonSeed()
                if (setrlimit(RLIMIT_CORE, &rl) == -1)
                        this->Log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
        }
-  
+
        return true;
 }
 
@@ -174,8 +175,8 @@ InspIRCd::InspIRCd(int argc, char** argv)
        factory.resize(255);
        
        this->Config = new ServerConfig(this);
+       this->SNO = new SnomaskManager(this);
        this->Start();
-       this->module_sockets.clear();
        this->TIME = this->OLDTIME = this->startup_time = time(NULL);
        srand(this->TIME);
        this->Log(DEBUG,"*** InspIRCd starting up!");
@@ -293,7 +294,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
        this->Log(DEBUG,"%d listeners",stats->BoundPortCount);
        for (unsigned long count = 0; count < stats->BoundPortCount; count++)
        {
-               this->Log(DEBUG,"Add listener: %d",Config->openSockfd[count]);
+               this->Log(DEBUG,"Add listener: %d",Config->openSockfd[count]->GetFd());
                if (!SE->AddFd(Config->openSockfd[count]))
                {
                        printf("\nEH? Could not add listener to socketengine. You screwed up, aborting.\n");
@@ -303,9 +304,11 @@ InspIRCd::InspIRCd(int argc, char** argv)
 
        if (!Config->nofork)
        {
-               fclose(stdout);
-               fclose(stderr);
+               if (kill(getppid(), SIGTERM) == -1)
+                       printf("Error killing parent process: %s\n",strerror(errno));
                fclose(stdin);
+               fclose(stderr);
+               fclose(stdout);
        }
 
        printf("\nInspIRCd is now running!\n");
@@ -460,9 +463,9 @@ void InspIRCd::BuildISupport()
 {
        // the neatest way to construct the initial 005 numeric, considering the number of configure constants to go in it...
        std::stringstream v;
-       v << "WALLCHOPS WALLVOICES MODES=" << MAXMODES << " CHANTYPES=# PREFIX=(ohv)@%+ MAP MAXCHANNELS=" << MAXCHANS << " MAXBANS=60 VBANLIST NICKLEN=" << NICKMAX-1;
+       v << "WALLCHOPS WALLVOICES MODES=" << MAXMODES << " CHANTYPES=# PREFIX=" << this->Modes->BuildPrefixes() << " MAP MAXCHANNELS=" << MAXCHANS << " MAXBANS=60 VBANLIST NICKLEN=" << NICKMAX-1;
        v << " CASEMAPPING=rfc1459 STATUSMSG=@%+ CHARSET=ascii TOPICLEN=" << MAXTOPIC << " KICKLEN=" << MAXKICK << " MAXTARGETS=" << Config->MaxTargets << " AWAYLEN=";
-       v << MAXAWAY << " CHANMODES=b,k,l,psmnti FNC NETWORK=" << Config->Network << " MAXPARA=32";
+       v << MAXAWAY << " CHANMODES=" << this->Modes->ChanModes() << " FNC NETWORK=" << Config->Network << " MAXPARA=32";
        Config->data005 = v.str();
        FOREACH_MOD_I(this,I_On005Numeric,On005Numeric(Config->data005));
 }
@@ -660,8 +663,6 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
                        irc::whowas::MaintainWhoWas(TIME);
                }
                Timers->TickTimers(TIME);
-               if (process_module_sockets)
-                       this->DoSocketTimeouts(TIME);
                this->DoBackgroundUserStuff(TIME);
 
                if ((TIME % 5) == 0)
@@ -782,3 +783,71 @@ time_t InspIRCd::Time()
        return TIME;
 }
 
+bool FileLogger::Readable()
+{
+       return false;
+}
+
+void FileLogger::HandleEvent(EventType et)
+{
+       this->WriteLogLine("");
+       ServerInstance->SE->DelFd(this);
+}
+
+void FileLogger::WriteLogLine(const std::string &line)
+{
+       if (line.length())
+               buffer.append(line);
+
+       if (log)
+       {
+               int written = fprintf(log,"%s",buffer.c_str());
+               if ((written >= 0) && (written < (int)buffer.length()))
+               {
+                       buffer.erase(0, buffer.length());
+                       ServerInstance->SE->AddFd(this);
+               }
+               else if (written == -1)
+               {
+                       if (errno == EAGAIN)
+                               ServerInstance->SE->AddFd(this);
+               }
+               else
+               {
+                       /* Wrote the whole buffer, and no need for write callback */
+                       buffer = "";
+               }
+       }
+       if (writeops++ % 20)
+       {
+               fflush(log);
+       }
+}
+
+void FileLogger::Close()
+{
+       if (log)
+       {
+               int flags = fcntl(fileno(log), F_GETFL, 0);
+               fcntl(fileno(log), F_SETFL, flags ^ O_NONBLOCK);
+               if (buffer.size())
+                       fprintf(log,"%s",buffer.c_str());
+               fflush(log);
+               fclose(log);
+       }
+       buffer = "";
+       ServerInstance->SE->DelFd(this);
+}
+
+FileLogger::FileLogger(InspIRCd* Instance, FILE* logfile) : ServerInstance(Instance), log(logfile), writeops(0)
+{
+       irc::sockets::NonBlocking(fileno(log));
+       this->SetFd(fileno(log));
+       buffer = "";
+}
+
+FileLogger::~FileLogger()
+{
+       this->Close();
+}
+