summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2011-05-23 04:41:51 -0400
committerAdam <Adam@anope.org>2011-05-23 04:41:51 -0400
commit8bf8832b70dea28e2048d5952ee03247f12bda92 (patch)
treef76f670690c105ec8f9ca079d868e930fd8f205b /src
parent4dd7e261f5704569856b5854dcc1f68010f0aaad (diff)
Rewrote the signal handling to use sigaction
Diffstat (limited to 'src')
-rw-r--r--src/init.cpp56
-rw-r--r--src/main.cpp98
-rw-r--r--src/sockets.cpp14
3 files changed, 92 insertions, 76 deletions
diff --git a/src/init.cpp b/src/init.cpp
index 7fa684336..0f3a5650d 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -209,6 +209,52 @@ static void write_pidfile()
/*************************************************************************/
+class SignalReload : public Signal
+{
+ public:
+ SignalReload(int sig) : Signal(sig) { }
+
+ void OnSignal()
+ {
+ Log() << "Received SIGHUP: Saving databases & rehashing configuration";
+
+ save_databases();
+
+ ServerConfig *old_config = Config;
+ try
+ {
+ Config = new ServerConfig();
+ FOREACH_MOD(I_OnReload, OnReload());
+ delete old_config;
+ }
+ catch (const ConfigException &ex)
+ {
+ Config = old_config;
+ Log() << "Error reloading configuration file: " << ex.GetReason();
+ }
+ }
+};
+
+class SignalExit : public Signal
+{
+ public:
+ SignalExit(int sig) : Signal(sig) { }
+
+ void OnSignal()
+ {
+#ifndef _WIN32
+ Log() << "Received " << strsignal(this->signal) << " signal (" << this->signal << "), exiting.";
+ quitmsg = Anope::string("Services terminating via signal ") + strsignal(this->signal) + " (" + stringify(this->signal) + ")";
+#else
+ Log() << "Received signal " << this->signal << ", exiting.";
+ quitmsg = Anope::string("Services terminating via signal ") + stringify(this->signal);
+#endif
+
+ save_databases();
+ quitting = true;
+ }
+};
+
void Init(int ac, char **av)
{
int started_from_term = isatty(0) && isatty(1) && isatty(2);
@@ -377,14 +423,8 @@ void Init(int ac, char **av)
/* Announce ourselves to the logfile. */
Log() << "Anope " << Anope::Version() << " starting up" << (debug || readonly ? " (options:" : "") << (debug ? " debug" : "") << (readonly ? " readonly" : "") << (debug || readonly ? ")" : "");
- /* Set signal handlers. Catch certain signals to let us do things or
- * panic as necessary, and ignore all others.
- */
-#ifndef _WIN32
- signal(SIGHUP, sighandler);
-#endif
- signal(SIGTERM, sighandler);
- signal(SIGINT, sighandler);
+ static SignalReload sig_hup(SIGHUP);
+ static SignalExit sig_term(SIGTERM), sig_int(SIGINT);
/* Initialize multi-language support */
Log(LOG_DEBUG) << "Loading Languages...";
diff --git a/src/main.cpp b/src/main.cpp
index 199b3162f..4a02e532d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -61,9 +61,6 @@ bool restarting = false;
/* Contains a message as to why services is terminating */
Anope::string quitmsg;
-/* Should we update the databases now? */
-bool save_data = false;
-
/* At what time were we started? */
time_t start_time = time(NULL);
@@ -83,8 +80,7 @@ class UpdateTimer : public Timer
void Tick(time_t)
{
- if (!readonly)
- save_databases();
+ save_databases();
}
};
@@ -212,66 +208,45 @@ void save_databases()
/*************************************************************************/
-/* If we get a weird signal, come here. */
+std::vector<Signal *> Signal::SignalHandlers;
-void sighandler(int signum)
+void Signal::SignalHandler(int signal)
{
- if (quitmsg.empty())
-#ifndef _WIN32
- quitmsg = Anope::string("Services terminating via signal ") + strsignal(signum) + " (" + stringify(signum) + ")";
-#else
- quitmsg = Anope::string("Services terminating via signal ") + stringify(signum);
-#endif
+ for (unsigned i = 0, j = SignalHandlers.size(); i < j; ++i)
+ if (SignalHandlers[i]->signal == signal)
+ SignalHandlers[i]->called = true;
+}
- if (started)
- {
- switch (signum)
+void Signal::Process()
+{
+ for (unsigned i = 0, j = SignalHandlers.size(); i < j; ++i)
+ if (SignalHandlers[i]->called == true)
{
-#ifndef _WIN32
- case SIGHUP:
- {
- Log() << "Received SIGHUP: Saving databases & rehashing configuration";
-
- save_databases();
-
- ServerConfig *old_config = Config;
- try
- {
- Config = new ServerConfig();
- FOREACH_MOD(I_OnReload, OnReload());
- delete old_config;
- }
- catch (const ConfigException &ex)
- {
- Config = old_config;
- Log() << "Error reloading configuration file: " << ex.GetReason();
- }
- break;
- }
-#endif
- case SIGINT:
- case SIGTERM:
- signal(signum, SIG_IGN);
-#ifndef _WIN32
- signal(SIGHUP, SIG_IGN);
-#endif
+ Signal *s = SignalHandlers[i];
+ s->called = false;
+ s->OnSignal();
+ }
+}
-#ifndef _WIN32
- Log() << "Received " << strsignal(signum) << " signal (" << signum << "), exiting.";
-#else
- Log() << "Received signal " << signum << ", exiting.";
-#endif
+Signal::Signal(int s) : called(false), signal(s)
+{
+ this->action.sa_flags = 0;
+ sigemptyset(&this->action.sa_mask);
+ this->action.sa_handler = SignalHandler;
+
+ sigaction(s, &this->action, NULL);
- save_databases();
- quitting = true;
- default:
- break;
- }
- }
+ SignalHandlers.push_back(this);
+}
- FOREACH_MOD(I_OnSignal, OnSignal(signum, quitmsg));
+Signal::~Signal()
+{
+ std::vector<Signal *>::iterator it = std::find(SignalHandlers.begin(), SignalHandlers.end(), this);
+ if (it != SignalHandlers.end())
+ SignalHandlers.erase(it);
}
+
/*************************************************************************/
/** The following comes from InspIRCd to get the full path of the Anope executable
@@ -388,6 +363,10 @@ int main(int ac, char **av, char **envp)
{
Log(LOG_DEBUG_2) << "Top of main loop";
+ /* Process signals */
+ Signal::Process();
+
+ /* Process timers */
if (Anope::CurTime - last_check >= Config->TimeoutCheck)
{
TimerManager::TickTimers(Anope::CurTime);
@@ -405,13 +384,6 @@ int main(int ac, char **av, char **envp)
SocketEngine::Process();
}
- if (save_data)
- {
- ircdproto->SendGlobops(NULL, "Updating databases on shutdown, please wait.");
- save_databases();
- save_data = false;
- }
-
if (restarting)
{
FOREACH_MOD(I_OnRestart, OnRestart());
diff --git a/src/sockets.cpp b/src/sockets.cpp
index c9758cb12..f255a4edf 100644
--- a/src/sockets.cpp
+++ b/src/sockets.cpp
@@ -467,11 +467,15 @@ bool BufferedSocket::ProcessRead()
{
char tbuffer[NET_BUFSIZE] = "";
- RecvLen = this->IO->Recv(this, tbuffer, sizeof(tbuffer) - 1);
- if (RecvLen == -2)
+ this->RecvLen = 0;
+
+ int len = this->IO->Recv(this, tbuffer, sizeof(tbuffer) - 1);
+ if (len == -2)
return true;
- else if (RecvLen <= 0)
+ else if (len <= 0)
return false;
+
+ this->RecvLen = len;
Anope::string sbuffer = this->extrabuf;
sbuffer += tbuffer;
@@ -559,7 +563,7 @@ void BufferedSocket::Write(const Anope::string &message)
/** Get the length of the read buffer
* @return The length of the read buffer
*/
-size_t BufferedSocket::ReadBufferLen() const
+int BufferedSocket::ReadBufferLen() const
{
return RecvLen;
}
@@ -567,7 +571,7 @@ size_t BufferedSocket::ReadBufferLen() const
/** Get the length of the write buffer
* @return The length of the write buffer
*/
-size_t BufferedSocket::WriteBufferLen() const
+int BufferedSocket::WriteBufferLen() const
{
return this->WriteBuffer.length();
}