1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* Stop mysql wanting to use long long */
15 #define NO_CLIENT_LONG_LONG
22 # pragma comment(lib, "mysqlclient.lib")
23 # pragma comment(lib, "advapi32.lib")
24 # pragma comment(linker, "/NODEFAULTLIB:LIBCMT")
27 /* VERSION 3 API: With nonblocking (threaded) requests */
29 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
30 /* $CompileFlags: exec("mysql_config --include") */
31 /* $LinkerFlags: exec("mysql_config --libs_r") rpath("mysql_config --libs_r") */
33 /* THE NONBLOCKING MYSQL API!
35 * MySQL provides no nonblocking (asyncronous) API of its own, and its developers recommend
36 * that instead, you should thread your program. This is what i've done here to allow for
37 * asyncronous SQL requests via mysql. The way this works is as follows:
39 * The module spawns a thread via class Thread, and performs its mysql queries in this thread,
40 * using a queue with priorities. There is a mutex on either end which prevents two threads
41 * adjusting the queue at the same time, and crashing the ircd. Every 50 milliseconds, the
42 * worker thread wakes up, and checks if there is a request at the head of its queue.
43 * If there is, it processes this request, blocking the worker thread but leaving the ircd
44 * thread to go about its business as usual. During this period, the ircd thread is able
45 * to insert futher pending requests into the queue.
47 * Once the processing of a request is complete, it is removed from the incoming queue to
48 * an outgoing queue, and initialized as a 'response'. The worker thread then signals the
49 * ircd thread (via a loopback socket) of the fact a result is available, by sending the
50 * connection ID through the connection.
52 * The ircd thread then mutexes the queue once more, reads the outbound response off the head
53 * of the queue, and sends it on its way to the original calling module.
55 * XXX: You might be asking "why doesnt he just send the response from within the worker thread?"
56 * The answer to this is simple. The majority of InspIRCd, and in fact most ircd's are not
57 * threadsafe. This module is designed to be threadsafe and is careful with its use of threads,
58 * however, if we were to call a module's OnRequest even from within a thread which was not the
59 * one the module was originally instantiated upon, there is a chance of all hell breaking loose
60 * if a module is ever put in a re-enterant state (stack corruption could occur, crashes, data
61 * corruption, and worse, so DONT think about it until the day comes when InspIRCd is 100%
62 * gauranteed threadsafe!)
64 * For a diagram of this system please see http://wiki.inspircd.org/Mysql2
69 class DispatcherThread;
76 QQueueItem(SQLQuery* Q, const std::string& S, SQLConnection* C) : q(Q), query(S), c(C) {}
83 RQueueItem(SQLQuery* Q, MySQLresult* R) : q(Q), r(R) {}
86 typedef std::map<std::string, SQLConnection*> ConnMap;
87 typedef std::deque<QQueueItem> QueryQueue;
88 typedef std::deque<RQueueItem> ResultQueue;
92 class ModuleSQL : public Module
95 DispatcherThread* Dispatcher;
96 QueryQueue qq; // MUST HOLD MUTEX
97 ResultQueue rq; // MUST HOLD MUTEX
98 ConnMap connections; // main thread only
103 void OnRehash(User* user);
104 void OnUnloadModule(Module* mod);
105 Version GetVersion();
108 class DispatcherThread : public SocketThread
111 ModuleSQL* const Parent;
113 DispatcherThread(ModuleSQL* CreatorModule) : Parent(CreatorModule) { }
114 ~DispatcherThread() { }
116 virtual void OnNotify();
119 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
120 #define mysql_field_count mysql_num_fields
123 /** Represents a mysql result set
125 class MySQLresult : public SQLResult
131 std::vector<std::string> colnames;
132 std::vector<SQLEntries> fieldlists;
134 MySQLresult(MYSQL_RES* res, int affected_rows) : err(SQL_NO_ERROR), currentrow(0), rows(0)
136 if (affected_rows >= 1)
138 rows = affected_rows;
139 fieldlists.resize(rows);
141 unsigned int field_count = 0;
146 while ((row = mysql_fetch_row(res)))
148 if (fieldlists.size() < (unsigned int)rows+1)
150 fieldlists.resize(fieldlists.size()+1);
153 MYSQL_FIELD *fields = mysql_fetch_fields(res);
154 if(mysql_num_fields(res) == 0)
156 if (fields && mysql_num_fields(res))
159 while (field_count < mysql_num_fields(res))
161 std::string a = (fields[field_count].name ? fields[field_count].name : "");
162 if (row[field_count])
163 fieldlists[n].push_back(SQLEntry(row[field_count]));
165 fieldlists[n].push_back(SQLEntry());
166 colnames.push_back(a);
173 mysql_free_result(res);
178 MySQLresult(SQLerror& e) : err(e)
192 virtual void GetCols(std::vector<std::string>& result)
194 result.assign(colnames.begin(), colnames.end());
197 virtual SQLEntry GetValue(int row, int column)
199 if ((row >= 0) && (row < rows) && (column >= 0) && (column < (int)fieldlists[row].size()))
201 return fieldlists[row][column];
206 virtual bool GetRow(SQLEntries& result)
208 if (currentrow < rows)
210 result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
222 /** Represents a connection to a mysql database
224 class SQLConnection : public SQLProvider
227 reference<ConfigTag> config;
231 // This constructor creates an SQLConnection object with the given credentials, but does not connect yet.
232 SQLConnection(Module* p, ConfigTag* tag) : SQLProvider(p, "SQL/" + tag->getString("id")),
233 config(tag), connection(NULL)
242 // This method connects to the database using the credentials supplied to the constructor, and returns
243 // true upon success.
246 unsigned int timeout = 1;
247 connection = mysql_init(connection);
248 mysql_options(connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
249 std::string host = config->getString("host");
250 std::string user = config->getString("user");
251 std::string pass = config->getString("pass");
252 std::string dbname = config->getString("name");
253 int port = config->getInt("port");
254 bool rv = mysql_real_connect(connection, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, NULL, 0);
257 std::string initquery;
258 if (config->readString("initialquery", initquery))
260 mysql_query(connection,initquery.c_str());
267 return (ModuleSQL*)(Module*)creator;
270 MySQLresult* DoBlockingQuery(const std::string& query)
273 /* Parse the command string and dispatch it to mysql */
274 if (CheckConnection() && !mysql_real_query(connection, query.data(), query.length()))
276 /* Successfull query */
277 MYSQL_RES* res = mysql_use_result(connection);
278 unsigned long rows = mysql_affected_rows(connection);
279 return new MySQLresult(res, rows);
283 /* XXX: See /usr/include/mysql/mysqld_error.h for a list of
284 * possible error numbers and error messages */
285 SQLerror e(SQL_QREPLY_FAIL, ConvToStr(mysql_errno(connection)) + std::string(": ") + mysql_error(connection));
286 return new MySQLresult(e);
290 bool CheckConnection()
292 if (!connection || mysql_ping(connection) != 0)
297 std::string GetError()
299 return mysql_error(connection);
304 mysql_close(connection);
307 void submit(SQLQuery* q, const std::string& qs)
309 Parent()->Dispatcher->LockQueue();
310 Parent()->qq.push_back(QQueueItem(q, qs, this));
311 Parent()->Dispatcher->UnlockQueueWakeup();
314 void submit(SQLQuery* call, const std::string& q, const ParamL& p)
317 unsigned int param = 0;
318 for(std::string::size_type i = 0; i < q.length(); i++)
324 if (param < p.size())
326 std::string parm = p[param++];
328 mysql_escape_string(buffer, parm.c_str(), parm.length());
329 // mysql_real_escape_string(connection, queryend, paramscopy[paramnum].c_str(), paramscopy[paramnum].length());
337 void submit(SQLQuery* call, const std::string& q, const ParamM& p)
340 for(std::string::size_type i = 0; i < q.length(); i++)
348 while (i < q.length() && isalnum(q[i]))
349 field.push_back(q[i++]);
352 ParamM::const_iterator it = p.find(field);
355 std::string parm = it->second;
357 mysql_escape_string(buffer, parm.c_str(), parm.length());
366 ModuleSQL::ModuleSQL()
371 void ModuleSQL::init()
373 Dispatcher = new DispatcherThread(this);
374 ServerInstance->Threads->Start(Dispatcher);
376 Implementation eventlist[] = { I_OnRehash, I_OnUnloadModule };
377 ServerInstance->Modules->Attach(eventlist, this, 2);
382 ModuleSQL::~ModuleSQL()
387 Dispatcher->OnNotify();
390 for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
396 void ModuleSQL::OnRehash(User* user)
399 ConfigTagList tags = ServerInstance->Config->ConfTags("database");
400 for(ConfigIter i = tags.first; i != tags.second; i++)
402 if (i->second->getString("module", "mysql") != "mysql")
404 std::string id = i->second->getString("id");
405 ConnMap::iterator curr = connections.find(id);
406 if (curr == connections.end())
408 SQLConnection* conn = new SQLConnection(this, i->second);
409 conns.insert(std::make_pair(id, conn));
410 ServerInstance->Modules->AddService(*conn);
415 connections.erase(curr);
419 // now clean up the deleted databases
420 Dispatcher->LockQueue();
421 SQLerror err(SQL_BAD_DBID);
422 for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
424 ServerInstance->Modules->DelService(*i->second);
425 // it might be running a query on this database. Wait for that to complete
426 i->second->lock.Lock();
427 i->second->lock.Unlock();
428 // now remove all active queries to this DB
429 for(unsigned int j = qq.size() - 1; j >= 0; j--)
431 if (qq[j].c == i->second)
433 qq[j].q->OnError(err);
435 qq.erase(qq.begin() + j);
438 // finally, nuke the connection
441 Dispatcher->UnlockQueue();
442 connections.swap(conns);
445 void ModuleSQL::OnUnloadModule(Module* mod)
447 SQLerror err(SQL_BAD_DBID);
448 Dispatcher->LockQueue();
449 unsigned int i = qq.size();
453 if (qq[i].q->creator == mod)
457 // need to wait until the query is done
458 // (the result will be discarded)
459 qq[i].c->lock.Lock();
460 qq[i].c->lock.Unlock();
462 qq[i].q->OnError(err);
464 qq.erase(qq.begin() + i);
467 Dispatcher->UnlockQueue();
468 // clean up any result queue entries
469 Dispatcher->OnNotify();
472 Version ModuleSQL::GetVersion()
474 return Version("MySQL support", VF_VENDOR);
477 void DispatcherThread::Run()
480 while (!this->GetExitFlag())
482 if (!Parent->qq.empty())
484 QQueueItem i = Parent->qq.front();
487 MySQLresult* res = i.c->DoBlockingQuery(i.query);
491 * At this point, the main thread could be working on:
492 * Rehash - delete i.c out from under us. We don't care about that.
493 * UnloadModule - delete i.q and the qq item. Need to avoid reporting results.
497 if (Parent->qq.front().q == i.q)
499 Parent->qq.pop_front();
500 Parent->rq.push_back(RQueueItem(i.q, res));
505 // UnloadModule ate the query
511 /* We know the queue is empty, we can safely hang this thread until
514 this->WaitForQueue();
520 void DispatcherThread::OnNotify()
522 // this could unlock during the dispatch, but OnResult isn't expected to take that long
524 for(ResultQueue::iterator i = Parent->rq.begin(); i != Parent->rq.end(); i++)
526 MySQLresult* res = i->r;
527 if (res->err.id == SQL_NO_ERROR)
528 i->q->OnResult(*res);
530 i->q->OnError(res->err);
538 MODULE_INIT(ModuleSQL)