2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2006-2007, 2009 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2006-2009 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /// $CompilerFlags: execute("mysql_config --include" "MYSQL_CXXFLAGS")
23 /// $LinkerFlags: execute("mysql_config --libs_r" "MYSQL_LDFLAGS" "-lmysqlclient")
25 /// $PackageInfo: require_system("arch") mariadb-libs
26 /// $PackageInfo: require_system("centos" "6.0" "6.99") mysql-devel
27 /// $PackageInfo: require_system("centos" "7.0") mariadb-devel
28 /// $PackageInfo: require_system("darwin") mysql-connector-c
29 /// $PackageInfo: require_system("debian") libmysqlclient-dev
30 /// $PackageInfo: require_system("ubuntu") libmysqlclient-dev
33 # pragma GCC diagnostic push
36 // Fix warnings about the use of `long long` on C++03.
38 # pragma clang diagnostic ignored "-Wc++11-long-long"
39 #elif defined __GNUC__
40 # pragma GCC diagnostic ignored "-Wlong-long"
45 #include "modules/sql.h"
48 # pragma GCC diagnostic pop
52 # pragma comment(lib, "libmysql.lib")
55 /* VERSION 3 API: With nonblocking (threaded) requests */
57 /* THE NONBLOCKING MYSQL API!
59 * MySQL provides no nonblocking (asyncronous) API of its own, and its developers recommend
60 * that instead, you should thread your program. This is what i've done here to allow for
61 * asyncronous SQL requests via mysql. The way this works is as follows:
63 * The module spawns a thread via class Thread, and performs its mysql queries in this thread,
64 * using a queue with priorities. There is a mutex on either end which prevents two threads
65 * adjusting the queue at the same time, and crashing the ircd. Every 50 milliseconds, the
66 * worker thread wakes up, and checks if there is a request at the head of its queue.
67 * If there is, it processes this request, blocking the worker thread but leaving the ircd
68 * thread to go about its business as usual. During this period, the ircd thread is able
69 * to insert futher pending requests into the queue.
71 * Once the processing of a request is complete, it is removed from the incoming queue to
72 * an outgoing queue, and initialized as a 'response'. The worker thread then signals the
73 * ircd thread (via a loopback socket) of the fact a result is available, by sending the
74 * connection ID through the connection.
76 * The ircd thread then mutexes the queue once more, reads the outbound response off the head
77 * of the queue, and sends it on its way to the original calling module.
79 * XXX: You might be asking "why doesnt it just send the response from within the worker thread?"
80 * The answer to this is simple. The majority of InspIRCd, and in fact most ircd's are not
81 * threadsafe. This module is designed to be threadsafe and is careful with its use of threads,
82 * however, if we were to call a module's OnRequest even from within a thread which was not the
83 * one the module was originally instantiated upon, there is a chance of all hell breaking loose
84 * if a module is ever put in a re-enterant state (stack corruption could occur, crashes, data
85 * corruption, and worse, so DONT think about it until the day comes when InspIRCd is 100%
86 * gauranteed threadsafe!)
91 class DispatcherThread;
95 // An SQL database which this query is executed on.
96 SQLConnection* connection;
98 // An object which handles the result of the query.
101 // The SQL query which is to be executed.
102 std::string querystr;
104 QueryQueueItem(SQL::Query* q, const std::string& s, SQLConnection* c)
112 struct ResultQueueItem
114 // An object which handles the result of the query.
117 // The result returned from executing the MySQL query.
120 ResultQueueItem(SQL::Query* q, MySQLresult* r)
127 typedef insp::flat_map<std::string, SQLConnection*> ConnMap;
128 typedef std::deque<QueryQueueItem> QueryQueue;
129 typedef std::deque<ResultQueueItem> ResultQueue;
133 class ModuleSQL : public Module
136 DispatcherThread* Dispatcher;
137 QueryQueue qq; // MUST HOLD MUTEX
138 ResultQueue rq; // MUST HOLD MUTEX
139 ConnMap connections; // main thread only
142 void init() CXX11_OVERRIDE;
144 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
145 void OnUnloadModule(Module* mod) CXX11_OVERRIDE;
146 Version GetVersion() CXX11_OVERRIDE;
149 class DispatcherThread : public SocketThread
152 ModuleSQL* const Parent;
154 DispatcherThread(ModuleSQL* CreatorModule) : Parent(CreatorModule) { }
155 ~DispatcherThread() { }
156 void Run() CXX11_OVERRIDE;
157 void OnNotify() CXX11_OVERRIDE;
160 /** Represents a mysql result set
162 class MySQLresult : public SQL::Result
168 std::vector<std::string> colnames;
169 std::vector<SQL::Row> fieldlists;
171 MySQLresult(MYSQL_RES* res, int affected_rows) : err(SQL::SUCCESS), currentrow(0), rows(0)
173 if (affected_rows >= 1)
175 rows = affected_rows;
176 fieldlists.resize(rows);
178 unsigned int field_count = 0;
183 while ((row = mysql_fetch_row(res)))
185 if (fieldlists.size() < (unsigned int)rows+1)
187 fieldlists.resize(fieldlists.size()+1);
190 MYSQL_FIELD *fields = mysql_fetch_fields(res);
191 if(mysql_num_fields(res) == 0)
193 if (fields && mysql_num_fields(res))
196 while (field_count < mysql_num_fields(res))
198 std::string a = (fields[field_count].name ? fields[field_count].name : "");
199 if (row[field_count])
200 fieldlists[n].push_back(SQL::Field(row[field_count]));
202 fieldlists[n].push_back(SQL::Field());
203 colnames.push_back(a);
210 mysql_free_result(res);
214 MySQLresult(SQL::Error& e) : err(e)
219 int Rows() CXX11_OVERRIDE
224 void GetCols(std::vector<std::string>& result) CXX11_OVERRIDE
226 result.assign(colnames.begin(), colnames.end());
229 bool HasColumn(const std::string& column, size_t& index) CXX11_OVERRIDE
231 for (size_t i = 0; i < colnames.size(); ++i)
233 if (colnames[i] == column)
242 SQL::Field GetValue(int row, int column)
244 if ((row >= 0) && (row < rows) && (column >= 0) && (column < (int)fieldlists[row].size()))
246 return fieldlists[row][column];
251 bool GetRow(SQL::Row& result) CXX11_OVERRIDE
253 if (currentrow < rows)
255 result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
267 /** Represents a connection to a mysql database
269 class SQLConnection : public SQL::Provider
272 bool EscapeString(SQL::Query* query, const std::string& in, std::string& out)
274 // In the worst case each character may need to be encoded as using two bytes and one
275 // byte is the NUL terminator.
276 std::vector<char> buffer(in.length() * 2 + 1);
278 // The return value of mysql_escape_string() is either an error or the length of the
279 // encoded string not including the NUL terminator.
281 // Unfortunately, someone genius decided that mysql_escape_string should return an
282 // unsigned type even though -1 is returned on error so checking whether an error
283 // happened is a bit cursed.
284 unsigned long escapedsize = mysql_escape_string(&buffer[0], in.c_str(), in.length());
285 if (escapedsize == static_cast<unsigned long>(-1))
287 SQL::Error err(SQL::QSEND_FAIL, InspIRCd::Format("%u: %s", mysql_errno(connection), mysql_error(connection)));
292 out.append(&buffer[0], escapedsize);
297 reference<ConfigTag> config;
301 // This constructor creates an SQLConnection object with the given credentials, but does not connect yet.
302 SQLConnection(Module* p, ConfigTag* tag)
303 : SQL::Provider(p, tag->getString("id"))
314 // This method connects to the database using the credentials supplied to the constructor, and returns
315 // true upon success.
318 connection = mysql_init(connection);
320 // Set the connection timeout.
321 unsigned int timeout = config->getDuration("timeout", 5, 1, 30);
322 mysql_options(connection, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);
324 // Attempt to connect to the database.
325 const std::string host = config->getString("host");
326 const std::string user = config->getString("user");
327 const std::string pass = config->getString("pass");
328 const std::string dbname = config->getString("name");
329 unsigned int port = config->getUInt("port", 3306, 1, 65535);
330 if (!mysql_real_connect(connection, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, NULL, CLIENT_IGNORE_SIGPIPE))
332 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Unable to connect to the %s MySQL server: %s",
333 GetId().c_str(), mysql_error(connection));
337 // Set the default character set.
338 const std::string charset = config->getString("charset");
339 if (!charset.empty() && mysql_set_character_set(connection, charset.c_str()))
341 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Could not set character set for %s to \"%s\": %s",
342 GetId().c_str(), charset.c_str(), mysql_error(connection));
346 // Execute the initial SQL query.
347 const std::string initialquery = config->getString("initialquery");
348 if (!initialquery.empty() && mysql_real_query(connection, initialquery.data(), initialquery.length()))
350 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Could not execute initial query \"%s\" for %s: %s",
351 initialquery.c_str(), name.c_str(), mysql_error(connection));
360 return (ModuleSQL*)(Module*)creator;
363 MySQLresult* DoBlockingQuery(const std::string& query)
366 /* Parse the command string and dispatch it to mysql */
367 if (CheckConnection() && !mysql_real_query(connection, query.data(), query.length()))
369 /* Successfull query */
370 MYSQL_RES* res = mysql_use_result(connection);
371 unsigned long rows = mysql_affected_rows(connection);
372 return new MySQLresult(res, rows);
376 /* XXX: See /usr/include/mysql/mysqld_error.h for a list of
377 * possible error numbers and error messages */
378 SQL::Error e(SQL::QREPLY_FAIL, InspIRCd::Format("%u: %s", mysql_errno(connection), mysql_error(connection)));
379 return new MySQLresult(e);
383 bool CheckConnection()
385 if (!connection || mysql_ping(connection) != 0)
392 mysql_close(connection);
395 void Submit(SQL::Query* q, const std::string& qs) CXX11_OVERRIDE
397 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Executing MySQL query: " + qs);
398 Parent()->Dispatcher->LockQueue();
399 Parent()->qq.push_back(QueryQueueItem(q, qs, this));
400 Parent()->Dispatcher->UnlockQueueWakeup();
403 void Submit(SQL::Query* call, const std::string& q, const SQL::ParamList& p) CXX11_OVERRIDE
406 unsigned int param = 0;
407 for(std::string::size_type i = 0; i < q.length(); i++)
411 else if (param < p.size() && !EscapeString(call, p[param++], res))
417 void Submit(SQL::Query* call, const std::string& q, const SQL::ParamMap& p) CXX11_OVERRIDE
420 for(std::string::size_type i = 0; i < q.length(); i++)
428 while (i < q.length() && isalnum(q[i]))
429 field.push_back(q[i++]);
432 SQL::ParamMap::const_iterator it = p.find(field);
433 if (it != p.end() && !EscapeString(call, it->second, res))
441 ModuleSQL::ModuleSQL()
446 void ModuleSQL::init()
448 if (mysql_library_init(0, NULL, NULL))
449 throw ModuleException("Unable to initialise the MySQL library!");
451 Dispatcher = new DispatcherThread(this);
452 ServerInstance->Threads.Start(Dispatcher);
455 ModuleSQL::~ModuleSQL()
460 Dispatcher->OnNotify();
464 for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
472 void ModuleSQL::ReadConfig(ConfigStatus& status)
475 ConfigTagList tags = ServerInstance->Config->ConfTags("database");
476 for(ConfigIter i = tags.first; i != tags.second; i++)
478 if (!stdalgo::string::equalsci(i->second->getString("module"), "mysql"))
480 std::string id = i->second->getString("id");
481 ConnMap::iterator curr = connections.find(id);
482 if (curr == connections.end())
484 SQLConnection* conn = new SQLConnection(this, i->second);
485 conns.insert(std::make_pair(id, conn));
486 ServerInstance->Modules->AddService(*conn);
491 connections.erase(curr);
495 // now clean up the deleted databases
496 Dispatcher->LockQueue();
497 SQL::Error err(SQL::BAD_DBID);
498 for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
500 ServerInstance->Modules->DelService(*i->second);
501 // it might be running a query on this database. Wait for that to complete
502 i->second->lock.Lock();
503 i->second->lock.Unlock();
504 // now remove all active queries to this DB
505 for (size_t j = qq.size(); j > 0; j--)
508 if (qq[k].connection == i->second)
510 qq[k].query->OnError(err);
512 qq.erase(qq.begin() + k);
515 // finally, nuke the connection
518 Dispatcher->UnlockQueue();
519 connections.swap(conns);
522 void ModuleSQL::OnUnloadModule(Module* mod)
524 SQL::Error err(SQL::BAD_DBID);
525 Dispatcher->LockQueue();
526 unsigned int i = qq.size();
530 if (qq[i].query->creator == mod)
534 // need to wait until the query is done
535 // (the result will be discarded)
536 qq[i].connection->lock.Lock();
537 qq[i].connection->lock.Unlock();
539 qq[i].query->OnError(err);
541 qq.erase(qq.begin() + i);
544 Dispatcher->UnlockQueue();
545 // clean up any result queue entries
546 Dispatcher->OnNotify();
549 Version ModuleSQL::GetVersion()
551 return Version("Provides MySQL support", VF_VENDOR);
554 void DispatcherThread::Run()
557 while (!this->GetExitFlag())
559 if (!Parent->qq.empty())
561 QueryQueueItem i = Parent->qq.front();
562 i.connection->lock.Lock();
564 MySQLresult* res = i.connection->DoBlockingQuery(i.querystr);
565 i.connection->lock.Unlock();
568 * At this point, the main thread could be working on:
569 * Rehash - delete i.connection out from under us. We don't care about that.
570 * UnloadModule - delete i.query and the qq item. Need to avoid reporting results.
574 if (!Parent->qq.empty() && Parent->qq.front().query == i.query)
576 Parent->qq.pop_front();
577 Parent->rq.push_back(ResultQueueItem(i.query, res));
582 // UnloadModule ate the query
588 /* We know the queue is empty, we can safely hang this thread until
591 this->WaitForQueue();
597 void DispatcherThread::OnNotify()
599 // this could unlock during the dispatch, but OnResult isn't expected to take that long
601 for(ResultQueue::iterator i = Parent->rq.begin(); i != Parent->rq.end(); i++)
603 MySQLresult* res = i->result;
604 if (res->err.code == SQL::SUCCESS)
605 i->query->OnResult(*res);
607 i->query->OnError(res->err);
615 MODULE_INIT(ModuleSQL)