X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fextra%2Fm_mysql.cpp;h=be6f4bc4c89e957c7314bc9e285afda795526e20;hb=813bc55a8496875cef52e6da42d608e4d2fa35da;hp=fc804d50ef9dcbf91faa6068ed41eeefafd37542;hpb=802abc2bff1052062d77cf99109115db768193fd;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/extra/m_mysql.cpp b/src/modules/extra/m_mysql.cpp index fc804d50e..be6f4bc4c 100644 --- a/src/modules/extra/m_mysql.cpp +++ b/src/modules/extra/m_mysql.cpp @@ -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,14 +11,15 @@ * --------------------------------------------------- */ -#include -#include +/* Stop mysql wanting to use long long */ +#define NO_CLIENT_LONG_LONG + +#include "inspircd.h" #include #include #include "users.h" #include "channels.h" #include "modules.h" -#include "inspircd.h" #include "m_sqlv2.h" /* VERSION 2 API: With nonblocking (threaded) requests */ @@ -69,6 +70,7 @@ class Notifier; typedef std::map ConnMap; bool giveup = false; +bool threadfinished = false; static Module* SQLModule = NULL; static Notifier* MessagePipe = NULL; int QueueFD = -1; @@ -100,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. */ @@ -146,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; @@ -513,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); @@ -527,7 +529,7 @@ void ClearAllConnections() while ((i = Connections.begin()) != Connections.end()) { Connections.erase(i); - DELETE(i->second); + delete i->second; } } @@ -543,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); } @@ -576,6 +578,30 @@ void LoadDatabases(ConfigReader* conf, InspIRCd* ServerInstance) ConnectDatabases(ServerInstance); } +char FindCharId(const std::string &id) +{ + char i = 1; + for (ConnMap::iterator iter = Connections.begin(); iter != Connections.end(); ++iter, ++i) + { + if (iter->first == id) + { + return i; + } + } + return 0; +} + +ConnMap::iterator GetCharId(char id) +{ + char i = 1; + for (ConnMap::iterator iter = Connections.begin(); iter != Connections.end(); ++iter, ++i) + { + if (i == id) + return iter; + } + return Connections.end(); +} + void NotifyMainThread(SQLConnection* connection_with_new_result) { /* Here we write() to the socket the main thread has open @@ -586,15 +612,23 @@ void NotifyMainThread(SQLConnection* connection_with_new_result) * thread, we can just use standard connect(), and we can * block if we like. We just send the connection id of the * connection back. + * + * NOTE: We only send a single char down the connection, this + * way we know it wont get a partial read at the other end if + * the system is especially congested (see bug #263). + * The function FindCharId translates a connection name into a + * one character id, and GetCharId translates a character id + * back into an iterator. */ - send(QueueFD, connection_with_new_result->GetID().c_str(), connection_with_new_result->GetID().length()+1, 0); + char id = FindCharId(connection_with_new_result->GetID()); + send(QueueFD, &id, 1, 0); } void* DispatcherThread(void* arg); /** Used by m_mysql to notify one thread when the other has a result */ -class Notifier : public InspSocket +class Notifier : public BufferedSocket { insp_sockaddr sock_us; socklen_t uslen; @@ -604,9 +638,9 @@ class Notifier : public InspSocket /* Create a socket on a random port. Let the tcp stack allocate us an available port */ #ifdef IPV6 - Notifier(InspIRCd* SI) : InspSocket(SI, "::1", 0, true, 3000) + Notifier(InspIRCd* SI) : BufferedSocket(SI, "::1", 0, true, 3000) #else - Notifier(InspIRCd* SI) : InspSocket(SI, "127.0.0.1", 0, true, 3000) + Notifier(InspIRCd* SI) : BufferedSocket(SI, "127.0.0.1", 0, true, 3000) #endif { uslen = sizeof(sock_us); @@ -616,7 +650,7 @@ class Notifier : public InspSocket } } - Notifier(InspIRCd* SI, int newfd, char* ip) : InspSocket(SI, newfd, ip) + Notifier(InspIRCd* SI, int newfd, char* ip) : BufferedSocket(SI, newfd, ip) { } @@ -639,13 +673,17 @@ class Notifier : public InspSocket virtual bool OnDataReady() { - char* data = this->Read(); - ConnMap::iterator iter; - - if (data && *data) + char data = 0; + /* NOTE: Only a single character is read so we know we + * cant get a partial read. (We've been told that theres + * data waiting, so we wont ever get EAGAIN) + * The function GetCharId translates a single character + * back into an iterator. + */ + if (Instance->SE->Recv(this, &data, 1, 0) > 0) { - /* We expect to be sent a null terminated string */ - if((iter = Connections.find(data)) != Connections.end()) + ConnMap::iterator iter = GetCharId(data); + if (iter != Connections.end()) { /* Lock the mutex, send back the data */ pthread_mutex_lock(&results_mutex); @@ -655,8 +693,11 @@ class Notifier : public InspSocket pthread_mutex_unlock(&results_mutex); return true; } + /* No error, but unknown id */ + return true; } + /* Erk, error on descriptor! */ return false; } }; @@ -676,7 +717,7 @@ class ModuleSQL : public Module ModuleSQL(InspIRCd* Me) : Module::Module(Me), rehashing(false) { - ServerInstance->UseInterface("SQLutils"); + ServerInstance->Modules->UseInterface("SQLutils"); Conf = new ConfigReader(ServerInstance); PublicServerInstance = ServerInstance; @@ -687,37 +728,50 @@ 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->PublishFeature("SQL", this)) + 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'"); } - ServerInstance->PublishInterface("SQL", this); + ServerInstance->Modules->PublishInterface("SQL", this); + Implementation eventlist[] = { I_OnRehash, I_OnRequest }; + ServerInstance->Modules->Attach(eventlist, this, 2); } 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->UnpublishInterface("SQL", this); - ServerInstance->UnpublishFeature("SQL"); - ServerInstance->DoneWithInterface("SQLutils"); + delete Conf; + ServerInstance->Modules->UnpublishInterface("SQL", this); + ServerInstance->Modules->UnpublishFeature("SQL"); + ServerInstance->Modules->DoneWithInterface("SQLutils"); } - void Implements(char* List) - { - List[I_OnRehash] = List[I_OnRequest] = 1; - } unsigned long NewID() { @@ -726,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) { @@ -737,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()) { @@ -759,14 +813,14 @@ class ModuleSQL : public Module return NULL; } - virtual void OnRehash(userrec* user, const std::string ¶meter) + virtual void OnRehash(User* user, const std::string ¶meter) { rehashing = true; } 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); } }; @@ -842,35 +896,10 @@ void* DispatcherThread(void* arg) /* XXX: Unlock */ } - usleep(50); + usleep(1000); } - return NULL; + pthread_exit((void *) 0); } - -// stuff down here is the module-factory stuff. For basic modules you can ignore this. - -class ModuleSQLFactory : public ModuleFactory -{ - public: - ModuleSQLFactory() - { - } - - ~ModuleSQLFactory() - { - } - - virtual Module * CreateModule(InspIRCd* Me) - { - return new ModuleSQL(Me); - } - -}; - - -extern "C" void * init_module( void ) -{ - return new ModuleSQLFactory; -} +MODULE_INIT(ModuleSQL)