]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_mysql.cpp
Remember to free some stuff when failing in the ctor
[user/henk/code/inspircd.git] / src / modules / extra / m_mysql.cpp
index ae66e44bb5377eb7ed26b535e3c07adcbc07c004..7e24202679ac772ac0414ef0c6a350b90b29ae6f 100644 (file)
@@ -91,6 +91,7 @@ class ModuleSQL : public Module
         Mutex* QueueMutex;
         Mutex* ResultsMutex;
         Mutex* LoggingMutex;
+        Mutex* ConnMutex;
 
         ModuleSQL(InspIRCd* Me);
         ~ModuleSQL();
@@ -574,6 +575,7 @@ void ConnectDatabases(InspIRCd* ServerInstance, ModuleSQL* Parent)
 
 void LoadDatabases(ConfigReader* conf, InspIRCd* ServerInstance, ModuleSQL* Parent)
 {
+       Parent->ConnMutex->Lock();
        ClearOldConnections(conf);
        for (int j =0; j < conf->Enumerate("database"); j++)
        {
@@ -596,6 +598,7 @@ void LoadDatabases(ConfigReader* conf, InspIRCd* ServerInstance, ModuleSQL* Pare
                }
        }
        ConnectDatabases(ServerInstance, Parent);
+       Parent->ConnMutex->Unlock();
 }
 
 char FindCharId(const std::string &id)
@@ -664,7 +667,7 @@ class Notifier : public BufferedSocket
        ModuleSQL* Parent;
 
  public:
-       Notifier(InspIRCd* SI, int newfd, char* ip) : BufferedSocket(SI, newfd, ip) { }
+       Notifier(ModuleSQL* P, InspIRCd* SI, int newfd, char* ip) : BufferedSocket(SI, newfd, ip), Parent(P) { }
 
        virtual bool OnDataReady()
        {
@@ -675,8 +678,10 @@ class Notifier : public BufferedSocket
                 * The function GetCharId translates a single character
                 * back into an iterator.
                 */
+
                if (Instance->SE->Recv(this, &data, 1, 0) > 0)
                {
+                       Parent->ConnMutex->Lock();
                        ConnMap::iterator iter = GetCharId(data);
                        if (iter != Connections.end())
                        {
@@ -687,9 +692,11 @@ class Notifier : public BufferedSocket
                                delete (*n);
                                iter->second->rq.pop_front();
                                Parent->ResultsMutex->Unlock();
+                               Parent->ConnMutex->Unlock();
                                return true;
                        }
                        /* No error, but unknown id */
+                       Parent->ConnMutex->Unlock();
                        return true;
                }
 
@@ -702,12 +709,13 @@ class Notifier : public BufferedSocket
  */
 class MySQLListener : public ListenSocketBase
 {
+       ModuleSQL* Parent;
        insp_sockaddr sock_us;
        socklen_t uslen;
        FileReader* index;
 
  public:
-       MySQLListener(InspIRCd* Instance, int port, const std::string &addr) : ListenSocketBase(Instance, port, addr)
+       MySQLListener(ModuleSQL* P, InspIRCd* Instance, int port, const std::string &addr) : ListenSocketBase(Instance, port, addr), Parent(P)
        {
                uslen = sizeof(sock_us);
                if (getsockname(this->fd,(sockaddr*)&sock_us,&uslen))
@@ -718,7 +726,7 @@ class MySQLListener : public ListenSocketBase
 
        virtual void OnAcceptReady(const std::string &ipconnectedto, int nfd, const std::string &incomingip)
        {
-               new Notifier(this->ServerInstance, nfd, (char *)ipconnectedto.c_str()); // XXX unsafe casts suck
+               new Notifier(this->Parent, this->ServerInstance, nfd, (char *)ipconnectedto.c_str()); // XXX unsafe casts suck
        }
 
        /* Using getsockname and ntohs, we can determine which port number we were allocated */
@@ -742,20 +750,30 @@ ModuleSQL::ModuleSQL(InspIRCd* Me) : Module(Me), rehashing(false)
 
        /* Create a socket on a random port. Let the tcp stack allocate us an available port */
 #ifdef IPV6
-       MessagePipe = new MySQLListener(ServerInstance, 0, "::1");
+       MessagePipe = new MySQLListener(this, ServerInstance, 0, "::1");
 #else
-       MessagePipe = new MySQLListener(ServerInstance, 0, "127.0.0.1");
+       MessagePipe = new MySQLListener(this, ServerInstance, 0, "127.0.0.1");
 #endif
 
+       LoggingMutex = ServerInstance->Mutexes->CreateMutex();
+       ConnMutex = ServerInstance->Mutexes->CreateMutex();
+
        if (MessagePipe->GetFd() == -1)
+       {
+               delete ConnMutex;
+               ServerInstance->Modules->DoneWithInterface("SQLutils");
                throw ModuleException("m_mysql: unable to create ITC pipe");
+       }
        else
+       {
+               LoggingMutex->Lock();
                ServerInstance->Logs->Log("m_mysql", DEBUG, "MySQL: Interthread comms port is %d", MessagePipe->GetPort());
+               LoggingMutex->Unlock();
+       }
 
        Dispatcher = new DispatcherThread(ServerInstance, this);
        ServerInstance->Threads->Create(Dispatcher);
 
-       LoggingMutex = ServerInstance->Mutexes->CreateMutex();
        ResultsMutex = ServerInstance->Mutexes->CreateMutex();
        QueueMutex = ServerInstance->Mutexes->CreateMutex();
 
@@ -764,6 +782,11 @@ ModuleSQL::ModuleSQL(InspIRCd* Me) : Module(Me), rehashing(false)
                /* Tell worker thread to exit NOW,
                 * Automatically joins */
                delete Dispatcher;
+               delete LoggingMutex;
+               delete ResultsMutex;
+               delete QueueMutex;
+               delete ConnMutex;
+               ServerInstance->Modules->DoneWithInterface("SQLutils");
                throw ModuleException("m_mysql: Unable to publish feature 'SQL'");
        }
 
@@ -783,6 +806,7 @@ ModuleSQL::~ModuleSQL()
        delete LoggingMutex;
        delete ResultsMutex;
        delete QueueMutex;
+       delete ConnMutex;
 }
 
 unsigned long ModuleSQL::NewID()
@@ -805,6 +829,7 @@ const char* ModuleSQL::OnRequest(Request* request)
 
                const char* returnval = NULL;
 
+               ConnMutex->Lock();
                if((iter = Connections.find(req->dbid)) != Connections.end())
                {
                        req->id = NewID();
@@ -816,8 +841,8 @@ const char* ModuleSQL::OnRequest(Request* request)
                        req->error.Id(SQL_BAD_DBID);
                }
 
+               ConnMutex->Unlock();
                QueueMutex->Unlock();
-               /* XXX: Unlock */
 
                return returnval;
        }
@@ -882,6 +907,7 @@ void DispatcherThread::Run()
                SQLConnection* conn = NULL;
                /* XXX: Lock here for safety */
                Parent->QueueMutex->Lock();
+               Parent->ConnMutex->Lock();
                for (ConnMap::iterator i = Connections.begin(); i != Connections.end(); i++)
                {
                        if (i->second->queue.totalsize())
@@ -890,6 +916,7 @@ void DispatcherThread::Run()
                                break;
                        }
                }
+               Parent->ConnMutex->Unlock();
                Parent->QueueMutex->Unlock();
                /* XXX: Unlock */