X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fextra%2Fm_pgsql.cpp;h=e1db996c896d918867bd8677b6190a174df03daa;hb=b4a174ee9c32d62ea6bf010e837e8c5b1c3d36a3;hp=c454befaed1db36867e41487769536184d6a0834;hpb=426d1c8e7ef6186fa122477bb51f8e6023dda56b;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/extra/m_pgsql.cpp b/src/modules/extra/m_pgsql.cpp index c454befae..e1db996c8 100644 --- a/src/modules/extra/m_pgsql.cpp +++ b/src/modules/extra/m_pgsql.cpp @@ -3,15 +3,15 @@ * * Copyright (C) 2016 Adam * Copyright (C) 2015 Daniel Vassdal - * Copyright (C) 2013, 2016-2019 Sadie Powell + * Copyright (C) 2013, 2016-2020 Sadie Powell * Copyright (C) 2012-2015 Attila Molnar * Copyright (C) 2012 Robby * Copyright (C) 2009-2010 Daniel De Graaf * Copyright (C) 2009 Uli Schlachter * Copyright (C) 2008 Thomas Stagner * Copyright (C) 2007, 2009-2010 Craig Edwards + * Copyright (C) 2007, 2009 Dennis Friis * Copyright (C) 2007 Robin Burchell - * Copyright (C) 2007 Dennis Friis * Copyright (C) 2006 Oliver Lupton * * This file is part of InspIRCd. InspIRCd is free software: you can @@ -57,6 +57,9 @@ typedef insp::flat_map ConnMap; enum SQLstatus { + // The connection has died. + DEAD, + // Connecting and wants read event. CREAD, @@ -183,7 +186,7 @@ class SQLConn : public SQL::Provider, public EventHandler public: reference conf; /* The entry */ std::deque queue; - PGconn* sql; /* PgSQL database connection handle */ + PGconn* sql; /* PgSQL database connection handle */ SQLstatus status; /* PgSQL database connection status */ QueueItem qinprog; /* If there is currently a query in progress */ @@ -195,10 +198,7 @@ class SQLConn : public SQL::Provider, public EventHandler , qinprog(NULL, "") { if (!DoConnect()) - { - ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not connect to database " + tag->getString("id")); DelayReconnect(); - } } CullResult cull() CXX11_OVERRIDE @@ -222,6 +222,7 @@ class SQLConn : public SQL::Provider, public EventHandler q->OnError(err); delete q; } + Close(); } void OnEventHandlerRead() CXX11_OVERRIDE @@ -267,34 +268,40 @@ class SQLConn : public SQL::Provider, public EventHandler return conninfo.str(); } + bool HandleConnectError(const char* reason) + { + ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Could not connect to the \"%s\" database: %s", + GetId().c_str(), reason); + return false; + } + bool DoConnect() { sql = PQconnectStart(GetDSN().c_str()); if (!sql) - return false; + return HandleConnectError("PQconnectStart returned NULL"); if(PQstatus(sql) == CONNECTION_BAD) - return false; + return HandleConnectError("connection status is bad"); if(PQsetnonblocking(sql, 1) == -1) - return false; + return HandleConnectError("unable to mark fd as non-blocking"); /* OK, we've initialised the connection, now to get it hooked into the socket engine * and then start polling it. */ - this->fd = PQsocket(sql); - - if(this->fd <= -1) - return false; + SetFd(PQsocket(sql)); + if(!HasFd()) + return HandleConnectError("PQsocket returned an invalid fd"); if (!SocketEngine::AddFd(this, FD_WANT_NO_WRITE | FD_WANT_NO_READ)) - { - ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BUG: Couldn't add pgsql socket to socket engine"); - return false; - } + return HandleConnectError("could not add the pgsql socket to the socket engine"); /* Socket all hooked into the engine, now to tell PgSQL to start connecting */ - return DoPoll(); + if (!DoPoll()) + return HandleConnectError("could not poll the connection state"); + + return true; } bool DoPoll() @@ -310,6 +317,8 @@ class SQLConn : public SQL::Provider, public EventHandler status = CREAD; return true; case PGRES_POLLING_FAILED: + SocketEngine::ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE); + status = DEAD; return false; case PGRES_POLLING_OK: SocketEngine::ChangeEventMask(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE); @@ -399,7 +408,7 @@ restart: { DoPoll(); } - else + else if (status == WREAD || status == WWRITE) { DoConnectedPoll(); } @@ -500,7 +509,10 @@ restart: void Close() { - SocketEngine::DelFd(this); + status = DEAD; + + if (HasFd() && SocketEngine::HasFd(GetFd())) + SocketEngine::DelFd(this); if(sql) { @@ -545,8 +557,13 @@ class ModulePgSQL : public Module if (curr == connections.end()) { SQLConn* conn = new SQLConn(this, i->second); - conns.insert(std::make_pair(id, conn)); - ServerInstance->Modules->AddService(*conn); + if (conn->status != DEAD) + { + conns.insert(std::make_pair(id, conn)); + ServerInstance->Modules->AddService(*conn); + } + // If the connection is dead it has already been queued for culling + // at the end of the main loop so we don't need to delete it here. } else { @@ -612,17 +629,17 @@ bool ReconnectTimer::Tick(time_t time) void SQLConn::DelayReconnect() { + status = DEAD; ModulePgSQL* mod = (ModulePgSQL*)(Module*)creator; + ConnMap::iterator it = mod->connections.find(conf->getString("id")); if (it != mod->connections.end()) - { mod->connections.erase(it); - ServerInstance->GlobalCulls.AddItem((EventHandler*)this); - if (!mod->retimer) - { - mod->retimer = new ReconnectTimer(mod); - ServerInstance->Timers.AddTimer(mod->retimer); - } + ServerInstance->GlobalCulls.AddItem((EventHandler*)this); + if (!mod->retimer) + { + mod->retimer = new ReconnectTimer(mod); + ServerInstance->Timers.AddTimer(mod->retimer); } }