]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_mssql.cpp
Handle file not found, and tidy up the perl.
[user/henk/code/inspircd.git] / src / modules / extra / m_mssql.cpp
index ab4d2aa717e6af1d90beaafaa33390716e35e149..3083dcec2f105e876dc1a1507ba08b869942f412 100644 (file)
@@ -1,12 +1,12 @@
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*              +------------------------------------+
+ *              | Inspire Internet Relay Chat Daemon |
+ *              +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2008 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
- *            the file COPYING for details.
+ *                       the file COPYING for details.
  *
  * ---------------------------------------------------
  */
 #include "m_sqlv2.h"
 
 /* $ModDesc: MsSQL provider */
+/* $CompileFlags: exec("grep VERSION_NO /usr/include/tdsver.h 2>/dev/null | perl -e 'print "-D_TDSVER=".((<> =~ /freetds v(\d+\.\d+)/i) ? $1*100 : 0);'") */
 /* $LinkerFlags: -ltds */
 /* $ModDep: m_sqlv2.h */
 
 class SQLConn;
 class MsSQLResult;
 class ResultNotifier;
+class MsSQLListener;
+class ModuleMsSQL;
 
 typedef std::map<std::string, SQLConn*> ConnMap;
-typedef std::deque<classbase*> paramlist;
 typedef std::deque<MsSQLResult*> ResultQueue;
 
-ResultNotifier* resultnotify = NULL;
+ResultNotifier* notifier = NULL;
+MsSQLListener* listener = NULL;
+int QueueFD = -1;
 
+ConnMap connections;
+Mutex* QueueMutex;
+Mutex* ResultsMutex;
+Mutex* LoggingMutex;
+
+class QueryThread : public Thread
+{
+  private:
+       ModuleMsSQL* Parent;
+       InspIRCd* ServerInstance;
+  public:
+       QueryThread(InspIRCd* si, ModuleMsSQL* mod)
+       : Thread(), Parent(mod), ServerInstance(si)
+       {
+       }
+       ~QueryThread() { }
+       virtual void Run();
+};
 
 class ResultNotifier : public BufferedSocket
 {
-       Module* mod;
-       insp_sockaddr sock_us;
+       ModuleMsSQL* mod;
+
+ public:
+       ResultNotifier(ModuleMsSQL* m, InspIRCd* SI, int newfd, char* ip) : BufferedSocket(SI, newfd, ip), mod(m)
+       {
+       }
+
+       virtual bool OnDataReady()
+       {
+               char data = 0;
+               if (ServerInstance->SE->Recv(this, &data, 1, 0) > 0)
+               {
+                       Dispatch();
+                       return true;
+               }
+               return false;
+       }
+
+       void Dispatch();
+};
+
+class MsSQLListener : public ListenSocketBase
+{
+       ModuleMsSQL* Parent;
+       irc::sockets::insp_sockaddr sock_us;
        socklen_t uslen;
+       FileReader* index;
 
  public:
-       /* Create a socket on a random port. Let the tcp stack allocate us an available port */
-#ifdef IPV6
-       ResultNotifier(InspIRCd* SI, Module* m) : BufferedSocket(SI, "::1", 0, true, 3000), mod(m)
-#else
-       ResultNotifier(InspIRCd* SI, Module* m) : BufferedSocket(SI, "127.0.0.1", 0, true, 3000), mod(m)
-#endif
+       MsSQLListener(ModuleMsSQL* 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))
                {
-                       throw ModuleException("Could not create random listening port on localhost");
+                       throw ModuleException("Could not getsockname() to find out port number for ITC port");
                }
        }
 
-       ResultNotifier(InspIRCd* SI, Module* m, int newfd, char* ip) : BufferedSocket(SI, newfd, ip), mod(m)
+       virtual void OnAcceptReady(const std::string &ipconnectedto, int nfd, const std::string &incomingip)
        {
+               new ResultNotifier(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 */
@@ -69,20 +111,12 @@ class ResultNotifier : public BufferedSocket
                return ntohs(sock_us.sin_port);
 #endif
        }
-
-       virtual int OnIncomingConnection(int newsock, char* ip)
-       {
-               Dispatch();
-               return false;
-       }
-
-       void Dispatch();
 };
 
 
 class MsSQLResult : public SQLresult
 {
 private:
+ private:
        int currentrow;
        int rows;
        int cols;
@@ -94,7 +128,7 @@ class MsSQLResult : public SQLresult
        SQLfieldList* fieldlist;
        SQLfieldMap* fieldmap;
 
 public:
+ public:
        MsSQLResult(Module* self, Module* to, unsigned int rid)
        : SQLresult(self, to, rid), currentrow(0), rows(0), cols(0), fieldlist(NULL), fieldmap(NULL)
        {
@@ -250,39 +284,47 @@ class MsSQLResult : public SQLresult
 
 class SQLConn : public classbase
 {
 private:
+ private:
        ResultQueue results;
-       InspIRCd* Instance;
+       InspIRCd* ServerInstance;
        Module* mod;
        SQLhost host;
        TDSLOGIN* login;
        TDSSOCKET* sock;
-       TDSCONNECTION* conn;
+       TDSCONTEXT* context;
+
+ public:
+       QueryQueue queue;
 
-  public:
        SQLConn(InspIRCd* SI, Module* m, const SQLhost& hi)
-       : Instance(SI), mod(m), host(hi)
+       : ServerInstance(SI), mod(m), host(hi), login(NULL), sock(NULL), context(NULL)
        {
-               if (OpenDB() == TDS_SUCCEED)
+               if (OpenDB())
                {
                        std::string query("USE " + host.name);
                        if (tds_submit_query(sock, query.c_str()) == TDS_SUCCEED)
                        {
                                if (tds_process_simple_query(sock) != TDS_SUCCEED)
                                {
-                                       Instance->Logs->Log("m_mssql",DEFAULT, "WARNING: Could not select database " + host.name + " for DB with id: " + host.id);
+                                       LoggingMutex->Lock();
+                                       ServerInstance->Logs->Log("m_mssql",DEFAULT, "WARNING: Could not select database " + host.name + " for DB with id: " + host.id);
+                                       LoggingMutex->Unlock();
                                        CloseDB();
                                }
                        }
                        else
                        {
-                               Instance->Logs->Log("m_mssql",DEFAULT, "WARNING: Could not select database " + host.name + " for DB with id: " + host.id);
+                               LoggingMutex->Lock();
+                               ServerInstance->Logs->Log("m_mssql",DEFAULT, "WARNING: Could not select database " + host.name + " for DB with id: " + host.id);
+                               LoggingMutex->Unlock();
                                CloseDB();
                        }
                }
                else
                {
-                       Instance->Logs->Log("m_mssql",DEFAULT, "WARNING: Could not connect to DB with id: " + host.id);
+                       LoggingMutex->Lock();
+                       ServerInstance->Logs->Log("m_mssql",DEFAULT, "WARNING: Could not connect to DB with id: " + host.id);
+                       LoggingMutex->Unlock();
                        CloseDB();
                }
        }
@@ -295,8 +337,8 @@ class SQLConn : public classbase
        SQLerror Query(SQLrequest &req)
        {
                if (!sock)
-                       return SQLerror(BAD_CONN, "Socket was NULL, check if SQL server is running.");
-                       
+                       return SQLerror(SQL_BAD_CONN, "Socket was NULL, check if SQL server is running.");
+
                /* Pointer to the buffer we screw around with substitution in */
                char* query;
 
@@ -306,7 +348,7 @@ class SQLConn : public classbase
                /* Total length of the unescaped parameters */
                unsigned long paramlen;
 
-               /* Total length of query, used for binary-safety in mysql_real_query */
+               /* Total length of query, used for binary-safety */
                unsigned long querylength = 0;
 
                paramlen = 0;
@@ -345,7 +387,7 @@ class SQLConn : public classbase
                                                escend++;
                                        }
                                        *escend = 0;
-                                       
+
                                        for (char* n = escaped; *n; n++)
                                        {
                                                *queryend = *n;
@@ -367,28 +409,30 @@ class SQLConn : public classbase
                *queryend = 0;
                req.query.q = query;
 
-               MsSQLResult* res = new MsSQLResult(mod, req.GetSource(), req.id);
+               MsSQLResult* res = new MsSQLResult((Module*)mod, req.GetSource(), req.id);
                res->dbid = host.id;
                res->query = req.query.q;
 
                char* msquery = strdup(req.query.q.data());
-               Instance->Logs->Log("m_mssql",DEBUG,"doing Query: %s",msquery);
+               LoggingMutex->Lock();
+               ServerInstance->Logs->Log("m_mssql",DEBUG,"doing Query: %s",msquery);
+               LoggingMutex->Unlock();
                if (tds_submit_query(sock, msquery) != TDS_SUCCEED)
                {
                        std::string error("failed to execute: "+std::string(req.query.q.data()));
                        delete[] query;
                        delete res;
                        free(msquery);
-                       return SQLerror(QSEND_FAIL, error);
+                       return SQLerror(SQL_QSEND_FAIL, error);
                }
                delete[] query;
                free(msquery);
-               
+
                int tds_res;
                while (tds_process_tokens(sock, &tds_res, NULL, TDS_TOKEN_RESULTS) == TDS_SUCCEED)
                {
-                       //Instance->Logs->Log("m_mssql",DEBUG,"<******> result type: %d", tds_res);
-                       //Instance->Logs->Log("m_mssql",DEBUG,"AFFECTED ROWS: %d", sock->rows_affected);
+                       //ServerInstance->Logs->Log("m_mssql",DEBUG,"<******> result type: %d", tds_res);
+                       //ServerInstance->Logs->Log("m_mssql",DEBUG,"AFFECTED ROWS: %d", sock->rows_affected);
                        switch (tds_res)
                        {
                                case TDS_ROWFMT_RESULT:
@@ -426,7 +470,11 @@ class SQLConn : public classbase
                                                                unsigned char* src;
                                                                CONV_RESULT dres;
                                                                ctype = tds_get_conversion_type(col->column_type, col->column_size);
-                                                               src = &(sock->current_results->current_row[col->column_offset]);
+#if _TDSVER >= 82
+                                                                       src = col->column_data;
+#else
+                                                                       src = &(sock->current_results->current_row[col->column_offset]);
+#endif
                                                                srclen = col->column_cur_size;
                                                                tds_convert(sock->tds_ctx, ctype, (TDS_CHAR *) src, srclen, SYBCHAR, &dres);
                                                                data[j] = (char*)dres.ib;
@@ -438,9 +486,11 @@ class SQLConn : public classbase
 
                                default:
                                        break;
-                       }       
+                       }
                }
+               ResultsMutex->Lock();
                results.push_back(res);
+               ResultsMutex->Unlock();
                SendNotify();
                return SQLerror();
        }
@@ -448,14 +498,18 @@ class SQLConn : public classbase
        static int HandleMessage(const TDSCONTEXT * pContext, TDSSOCKET * pTdsSocket, TDSMESSAGE * pMessage)
        {
                SQLConn* sc = (SQLConn*)pContext->parent;
-               sc->Instance->Logs->Log("m_mssql", DEBUG, "Message for DB with id: %s -> %s", sc->host.id.c_str(), pMessage->message);
+               LoggingMutex->Lock();
+               sc->ServerInstance->Logs->Log("m_mssql", DEBUG, "Message for DB with id: %s -> %s", sc->host.id.c_str(), pMessage->message);
+               LoggingMutex->Unlock();
                return 0;
        }
 
        static int HandleError(const TDSCONTEXT * pContext, TDSSOCKET * pTdsSocket, TDSMESSAGE * pMessage)
        {
                SQLConn* sc = (SQLConn*)pContext->parent;
-               sc->Instance->Logs->Log("m_mssql", DEFAULT, "Error for DB with id: %s -> %s", sc->host.id.c_str(), pMessage->message);
+               LoggingMutex->Lock();
+               sc->ServerInstance->Logs->Log("m_mssql", DEFAULT, "Error for DB with id: %s -> %s", sc->host.id.c_str(), pMessage->message);
+               LoggingMutex->Unlock();
                return 0;
        }
 
@@ -469,16 +523,14 @@ class SQLConn : public classbase
                res->UpdateAffectedCount();
        }
 
-       int OpenDB()
+       bool OpenDB()
        {
                CloseDB();
 
-               TDSCONTEXT* cont;
-               cont = tds_alloc_context(this);
-               cont->msg_handler = HandleMessage;
-               cont->err_handler = HandleError;
+               TDSCONNECTION* conn = NULL;
 
                login = tds_alloc_login();
+               tds_set_app(login, "TSQL");
                tds_set_library(login,"TDS-Library");
                tds_set_host(login, "");
                tds_set_server(login, host.host.c_str());
@@ -488,22 +540,41 @@ class SQLConn : public classbase
                tds_set_port(login, host.port);
                tds_set_packet(login, 512);
 
-               sock = tds_alloc_socket(cont, 512);
-               conn = tds_read_config_info(NULL, login, cont->locale);
-               return tds_connect(sock, conn);
+               context = tds_alloc_context(this);
+               context->msg_handler = HandleMessage;
+               context->err_handler = HandleError;
+
+               sock = tds_alloc_socket(context, 512);
+               tds_set_parent(sock, NULL);
+
+               conn = tds_read_config_info(NULL, login, context->locale);
+
+               if (tds_connect(sock, conn) == TDS_SUCCEED)
+               {
+                       tds_free_connection(conn);
+                       return 1;
+               }
+               tds_free_connection(conn);
+               return 0;
        }
 
        void CloseDB()
        {
-               if (login)
-                       tds_free_login(login);
                if (sock)
+               {
                        tds_free_socket(sock);
-               if (conn)
-                       tds_free_connection(conn);
-               login = NULL;
-               sock = NULL;
-               conn = NULL;
+                       sock = NULL;
+               }
+               if (context)
+               {
+                       tds_free_context(context);
+                       context = NULL;
+               }
+               if (login)
+               {
+                       tds_free_login(login);
+                       login = NULL;
+               }
        }
 
        SQLhost GetConfHost()
@@ -516,6 +587,7 @@ class SQLConn : public classbase
                while (results.size())
                {
                        MsSQLResult* res = results[0];
+                       ResultsMutex->Lock();
                        if (res->GetDest())
                        {
                                res->Send();
@@ -529,6 +601,7 @@ class SQLConn : public classbase
                                delete res;
                        }
                        results.pop_front();
+                       ResultsMutex->Unlock();
                }
        }
 
@@ -544,32 +617,42 @@ class SQLConn : public classbase
 
        void SendNotify()
        {
-               int QueueFD;
-               if ((QueueFD = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
+               if (QueueFD < 0)
                {
-                       /* crap, we're out of sockets... */
-                       return;
-               }
+                       if ((QueueFD = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
+                       {
+                               /* crap, we're out of sockets... */
+                               return;
+                       }
 
-               insp_sockaddr addr;
+                       irc::sockets::insp_sockaddr addr;
 
 #ifdef IPV6
-               insp_aton("::1", &addr.sin6_addr);
-               addr.sin6_family = AF_FAMILY;
-               addr.sin6_port = htons(resultnotify->GetPort());
+                       irc::sockets::insp_aton("::1", &addr.sin6_addr);
+                       addr.sin6_family = AF_FAMILY;
+                       addr.sin6_port = htons(listener->GetPort());
 #else
-               insp_inaddr ia;
-               insp_aton("127.0.0.1", &ia);
-               addr.sin_family = AF_FAMILY;
-               addr.sin_addr = ia;
-               addr.sin_port = htons(resultnotify->GetPort());
+                       irc::sockets::insp_inaddr ia;
+                       irc::sockets::insp_aton("127.0.0.1", &ia);
+                       addr.sin_family = AF_FAMILY;
+                       addr.sin_addr = ia;
+                       addr.sin_port = htons(listener->GetPort());
 #endif
 
-               if (connect(QueueFD, (sockaddr*)&addr,sizeof(addr)) == -1)
-               {
-                       /* wtf, we cant connect to it, but we just created it! */
-                       return;
+                       if (connect(QueueFD, (sockaddr*)&addr,sizeof(addr)) == -1)
+                       {
+                               /* wtf, we cant connect to it, but we just created it! */
+                               return;
+                       }
                }
+               char id = 0;
+               send(QueueFD, &id, 1, 0);
+       }
+
+       void DoLeadingQuery()
+       {
+               SQLrequest& req = queue.front();
+               req.error = Query(req);
        }
 
 };
@@ -577,14 +660,18 @@ class SQLConn : public classbase
 
 class ModuleMsSQL : public Module
 {
-  private:
-       ConnMap connections;
+ private:
        unsigned long currid;
+       QueryThread* queryDispatcher;
 
 public:
+ public:
        ModuleMsSQL(InspIRCd* Me)
-       : Module::Module(Me), currid(0)
+       : Module(Me), currid(0)
        {
+               LoggingMutex = ServerInstance->Mutexes->CreateMutex();
+               ResultsMutex = ServerInstance->Mutexes->CreateMutex();
+               QueueMutex = ServerInstance->Mutexes->CreateMutex();
+
                ServerInstance->Modules->UseInterface("SQLutils");
 
                if (!ServerInstance->Modules->PublishFeature("SQL", this))
@@ -592,10 +679,30 @@ class ModuleMsSQL : public Module
                        throw ModuleException("m_mssql: Unable to publish feature 'SQL'");
                }
 
-               resultnotify = new ResultNotifier(ServerInstance, this);
+               /* Create a socket on a random port. Let the tcp stack allocate us an available port */
+#ifdef IPV6
+               listener = new MsSQLListener(this, ServerInstance, 0, "::1");
+#else
+               listener = new MsSQLListener(this, ServerInstance, 0, "127.0.0.1");
+#endif
+
+               if (listener->GetFd() == -1)
+               {
+                       ServerInstance->Modules->DoneWithInterface("SQLutils");
+                       throw ModuleException("m_mssql: unable to create ITC pipe");
+               }
+               else
+               {
+                       LoggingMutex->Lock();
+                       ServerInstance->Logs->Log("m_mssql", DEBUG, "MsSQL: Interthread comms port is %d", listener->GetPort());
+                       LoggingMutex->Unlock();
+               }
 
                ReadConf();
 
+               queryDispatcher = new QueryThread(ServerInstance, this);
+               ServerInstance->Threads->Create(queryDispatcher);
+
                ServerInstance->Modules->PublishInterface("SQL", this);
                Implementation eventlist[] = { I_OnRequest, I_OnRehash };
                ServerInstance->Modules->Attach(eventlist, this, 2);
@@ -603,16 +710,33 @@ class ModuleMsSQL : public Module
 
        virtual ~ModuleMsSQL()
        {
+               delete queryDispatcher;
                ClearQueue();
                ClearAllConnections();
-               resultnotify->SetFd(-1);
-               resultnotify->state = I_ERROR;
-               resultnotify->OnError(I_ERR_SOCKET);
-               resultnotify->ClosePending = true;
-               delete resultnotify;
+
+               ServerInstance->SE->DelFd(listener);
+               ServerInstance->BufferedSocketCull();
+
+               if (QueueFD >= 0)
+               {
+                       shutdown(QueueFD, 2);
+                       close(QueueFD);
+               }
+
+               if (notifier)
+               {
+                       ServerInstance->SE->DelFd(notifier);
+                       notifier->Close();
+                       ServerInstance->BufferedSocketCull();
+               }
+
                ServerInstance->Modules->UnpublishInterface("SQL", this);
                ServerInstance->Modules->UnpublishFeature("SQL");
                ServerInstance->Modules->DoneWithInterface("SQLutils");
+
+               delete LoggingMutex;
+               delete ResultsMutex;
+               delete QueueMutex;
        }
 
 
@@ -659,7 +783,7 @@ class ModuleMsSQL : public Module
                }
                return false;
        }
-    
+
        void ReadConf()
        {
                ClearOldConnections();
@@ -687,7 +811,9 @@ class ModuleMsSQL : public Module
        {
                if (HasHost(hi))
                {
+                       LoggingMutex->Lock();
                        ServerInstance->Logs->Log("m_mssql",DEFAULT, "WARNING: A MsSQL connection with id: %s already exists. Aborting database open attempt.", hi.id.c_str());
+                       LoggingMutex->Unlock();
                        return;
                }
 
@@ -725,7 +851,9 @@ class ModuleMsSQL : public Module
 
        virtual void OnRehash(User* user, const std::string &parameter)
        {
+               QueueMutex->Lock();
                ReadConf();
+               QueueMutex->Unlock();
        }
 
        virtual const char* OnRequest(Request* request)
@@ -733,18 +861,27 @@ class ModuleMsSQL : public Module
                if(strcmp(SQLREQID, request->GetId()) == 0)
                {
                        SQLrequest* req = (SQLrequest*)request;
+
+                       QueueMutex->Lock();
+
                        ConnMap::iterator iter;
+
+                       const char* returnval = NULL;
+
                        if((iter = connections.find(req->dbid)) != connections.end())
                        {
                                req->id = NewID();
-                               req->error = iter->second->Query(*req);
-                               return SQLSUCCESS;
+                               iter->second->queue.push(*req);
+                               returnval= SQLSUCCESS;
                        }
                        else
                        {
-                               req->error.Id(BAD_DBID);
-                               return NULL;
+                               req->error.Id(SQL_BAD_DBID);
                        }
+
+                       QueueMutex->Unlock();
+
+                       return returnval;
                }
                return NULL;
        }
@@ -759,14 +896,40 @@ class ModuleMsSQL : public Module
 
        virtual Version GetVersion()
        {
-               return Version(1,0,0,0,VF_VENDOR|VF_SERVICEPROVIDER,API_VERSION);
+               return Version("$Id$", VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
        }
 
 };
 
 void ResultNotifier::Dispatch()
 {
-       ((ModuleMsSQL*)mod)->SendQueue();
+       mod->SendQueue();
+}
+
+void QueryThread::Run()
+{
+       while (this->GetExitFlag() == false)
+       {
+               SQLConn* conn = NULL;
+               QueueMutex->Lock();
+               for (ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
+               {
+                       if (i->second->queue.totalsize())
+                       {
+                               conn = i->second;
+                               break;
+                       }
+               }
+               QueueMutex->Unlock();
+               if (conn)
+               {
+                       conn->DoLeadingQuery();
+                       QueueMutex->Lock();
+                       conn->queue.pop();
+                       QueueMutex->Unlock();
+               }
+               usleep(1000);
+       }
 }
 
 MODULE_INIT(ModuleMsSQL)