]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_mysql.cpp
Fix memory leak if we send STARTTLS twice, thanks special and psychon
[user/henk/code/inspircd.git] / src / modules / extra / m_mysql.cpp
index a033d13e78008ab2b592dafb513479bbd29301e9..be6f4bc4c89e957c7314bc9e285afda795526e20 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
  * See: http://www.inspircd.org/wiki/index.php/Credits
  *
  * This program is free but copyrighted software; see
@@ -11,6 +11,9 @@
  * ---------------------------------------------------
  */
 
+/* Stop mysql wanting to use long long */
+#define NO_CLIENT_LONG_LONG
+
 #include "inspircd.h"
 #include <mysql.h>
 #include <pthread.h>
@@ -25,7 +28,6 @@
 /* $CompileFlags: exec("mysql_config --include") */
 /* $LinkerFlags: exec("mysql_config --libs_r") rpath("mysql_config --libs_r") */
 /* $ModDep: m_sqlv2.h */
-/* $NoPedantic */
 
 /* THE NONBLOCKING MYSQL API!
  * 
@@ -68,6 +70,7 @@ class Notifier;
 
 typedef std::map<std::string, SQLConnection*> ConnMap;
 bool giveup = false;
+bool threadfinished = false;
 static Module* SQLModule = NULL;
 static Notifier* MessagePipe = NULL;
 int QueueFD = -1;
@@ -99,7 +102,7 @@ class MySQLresult : public SQLresult
        int rows;
  public:
 
-       MySQLresult(Module* self, Module* to, MYSQL_RES* res, int affected_rows, unsigned int id) : SQLresult(self, to, id), currentrow(0), fieldmap(NULL)
+       MySQLresult(Module* self, Module* to, MYSQL_RES* res, int affected_rows, unsigned int rid) : SQLresult(self, to, rid), currentrow(0), fieldmap(NULL)
        {
                /* A number of affected rows from from mysql_affected_rows.
                 */
@@ -145,7 +148,7 @@ class MySQLresult : public SQLresult
                }
        }
 
-       MySQLresult(Module* self, Module* to, SQLerror e, unsigned int id) : SQLresult(self, to, id), currentrow(0)
+       MySQLresult(Module* self, Module* to, SQLerror e, unsigned int rid) : SQLresult(self, to, rid), currentrow(0)
        {
                rows = 0;
                error = e;
@@ -512,7 +515,7 @@ void ClearOldConnections(ConfigReader* conf)
        {
                if (!HostInConf(conf, i->second->GetConfHost()))
                {
-                       DELETE(i->second);
+                       delete i->second;
                        safei = i;
                        --i;
                        Connections.erase(safei);
@@ -526,7 +529,7 @@ void ClearAllConnections()
        while ((i = Connections.begin()) != Connections.end())
        {
                Connections.erase(i);
-               DELETE(i->second);
+               delete i->second;
        }
 }
 
@@ -542,7 +545,7 @@ void ConnectDatabases(InspIRCd* ServerInstance)
                {
                        /* XXX: MUTEX */
                        pthread_mutex_lock(&logging_mutex);
-                       ServerInstance->Log(DEFAULT,"SQL: Failed to connect database "+i->second->GetHost()+": Error: "+i->second->GetError());
+                       ServerInstance->Logs->Log("m_mysql",DEFAULT,"SQL: Failed to connect database "+i->second->GetHost()+": Error: "+i->second->GetError());
                        i->second->SetEnable(false);
                        pthread_mutex_unlock(&logging_mutex);
                }
@@ -725,16 +728,24 @@ class ModuleSQL : public Module
                
                pthread_attr_t attribs;
                pthread_attr_init(&attribs);
-               pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_DETACHED);
+               pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
                if (pthread_create(&this->Dispatcher, &attribs, DispatcherThread, (void *)this) != 0)
                {
                        throw ModuleException("m_mysql: Failed to create dispatcher thread: " + std::string(strerror(errno)));
                }
+               pthread_attr_destroy(&attribs);
 
                if (!ServerInstance->Modules->PublishFeature("SQL", this))
                {
                        /* Tell worker thread to exit NOW */
+                       int rc;
+                       void *status;
                        giveup = true;
+                       rc = pthread_join(Dispatcher, &status);
+                       if (rc)
+                       {
+                               ServerInstance->Logs->Log("m_mysql",DEFAULT,"SQL: Error code from pthread_join() is %d", rc);
+                       }
                        throw ModuleException("m_mysql: Unable to publish feature 'SQL'");
                }
 
@@ -745,7 +756,14 @@ class ModuleSQL : public Module
 
        virtual ~ModuleSQL()
        {
+               int rc;
+               void *status;
                giveup = true;
+               rc = pthread_join(Dispatcher, &status);
+               if (rc)
+               {
+                       ServerInstance->Logs->Log("m_mysql",DEFAULT,"SQL: Error code from pthread_join() is %d", rc);
+               }
                ClearAllConnections();
                delete Conf;
                ServerInstance->Modules->UnpublishInterface("SQL", this);
@@ -754,10 +772,6 @@ class ModuleSQL : public Module
        }
 
 
-       void Implements(char* List)
-       {
-               List[I_OnRehash] = List[I_OnRequest] = 1;
-       }
 
        unsigned long NewID()
        {
@@ -766,7 +780,7 @@ class ModuleSQL : public Module
                return ++currid;
        }
 
-       char* OnRequest(Request* request)
+       virtual const char* OnRequest(Request* request)
        {
                if(strcmp(SQLREQID, request->GetId()) == 0)
                {
@@ -777,7 +791,7 @@ class ModuleSQL : public Module
 
                        ConnMap::iterator iter;
 
-                       char* returnval = NULL;
+                       const char* returnval = NULL;
 
                        if((iter = Connections.find(req->dbid)) != Connections.end())
                        {
@@ -806,7 +820,7 @@ class ModuleSQL : public Module
        
        virtual Version GetVersion()
        {
-               return Version(1,1,0,0,VF_VENDOR|VF_SERVICEPROVIDER,API_VERSION);
+               return Version(1,2,0,0,VF_VENDOR|VF_SERVICEPROVIDER,API_VERSION);
        }
        
 };
@@ -882,10 +896,10 @@ void* DispatcherThread(void* arg)
                        /* XXX: Unlock */
                }
 
-               usleep(50);
+               usleep(1000);
        }
 
-       return NULL;
+       pthread_exit((void *) 0);
 }
 
 MODULE_INIT(ModuleSQL)