summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/posix/signalHandler.hpp94
-rw-r--r--src/signalController.cpp34
-rw-r--r--src/signalController.h36
3 files changed, 59 insertions, 105 deletions
diff --git a/src/posix/signalHandler.hpp b/src/posix/signalHandler.hpp
index 92a66bd..d52b4d6 100644
--- a/src/posix/signalHandler.hpp
+++ b/src/posix/signalHandler.hpp
@@ -37,73 +37,43 @@
#include <boost/thread.hpp>
#include <boost/bind.hpp>
-class SigIntHandler : public SignalHandler
+int SigIntHandler(const SigNum& /*sig*/, const std::string& /*msg*/)
{
-public:
- SigIntHandler() : SignalHandler(SIGINT) {}
- int handle()
- {
- cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught, exiting";
- return 1;
- }
-};
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught, exiting";
+ return 1;
+}
-class SigQuitHandler : public SignalHandler
+int SigQuitHandler(const SigNum& /*sig*/, const std::string& /*msg*/)
{
-public:
- SigQuitHandler() : SignalHandler(SIGQUIT) {}
- int handle()
- {
- cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught, exiting";
- return 1;
- }
-};
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught, exiting";
+ return 1;
+}
-class SigHupHandler : public SignalHandler
+int SigHupHandler(const SigNum& /*sig*/, const std::string& /*msg*/)
{
-public:
- SigHupHandler() : SignalHandler(SIGHUP) {}
- int handle()
- {
- cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught";
- return 0;
- }
-};
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught";
+ return 0;
+}
-class SigUsr1Handler : public SignalHandler
+int SigTermHandler(const SigNum& /*sig*/, const std::string& /*msg*/)
{
-public:
- SigUsr1Handler() : SignalHandler(SIGUSR1) {}
- int handle()
- {
- cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caught, exiting";
- return 1;
- }
-};
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caught, exiting";
+ return 1;
+}
-class SigUsr2Handler : public SignalHandler
+int SigUsr1Handler(const SigNum& /*sig*/, const std::string& /*msg*/)
{
-public:
- SigUsr2Handler() : SignalHandler(SIGUSR2) {}
- int handle()
- {
- cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught";
- return 0;
- }
-};
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught";
+ return 0;
+}
-class SigTermHandler : public SignalHandler
+int SigUsr2Handler(const SigNum& /*sig*/, const std::string& /*msg*/)
{
-public:
- SigTermHandler() : SignalHandler(SIGTERM) {}
- int handle()
- {
- cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught";
- return 0;
- }
-};
+ cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught";
+ return 0;
+}
-void handle()
+void handleSignal()
{
sigset_t signal_set;
int sigNum;
@@ -138,14 +108,14 @@ void registerSignalHandler(SignalController& ctrl)
#error The signalhandler works only with pthreads
#endif
- boost::thread(boost::bind(handle));
+ boost::thread(boost::bind(handleSignal));
- ctrl.handler[SIGINT] = new SigIntHandler;
- ctrl.handler[SIGQUIT] = new SigQuitHandler;
- ctrl.handler[SIGHUP] = new SigHupHandler;
- ctrl.handler[SIGTERM] = new SigTermHandler;
- ctrl.handler[SIGUSR1] = new SigUsr1Handler;
- ctrl.handler[SIGUSR2] = new SigUsr2Handler;
+ ctrl.handler[SIGINT] = boost::bind(SigIntHandler, _1, _2);
+ ctrl.handler[SIGQUIT] = boost::bind(SigQuitHandler, _1, _2);
+ ctrl.handler[SIGHUP] = boost::bind(SigHupHandler, _1, _2);
+ ctrl.handler[SIGTERM] = boost::bind(SigTermHandler, _1, _2);
+ ctrl.handler[SIGUSR1] = boost::bind(SigUsr1Handler, _1, _2);
+ ctrl.handler[SIGUSR2] = boost::bind(SigUsr2Handler, _1, _2);
cLog.msg(Log::PRIO_DEBUG) << "signal handlers are now registered";
}
diff --git a/src/signalController.cpp b/src/signalController.cpp
index cc31c75..f0191fe 100644
--- a/src/signalController.cpp
+++ b/src/signalController.cpp
@@ -32,13 +32,13 @@
#include <map>
#include <iostream>
+#include <boost/bind.hpp>
#include "signalController.h"
#include "log.h"
#include "anytunError.h"
#include "threadUtils.hpp"
-
SignalController* SignalController::inst = NULL;
Mutex SignalController::instMutex;
SignalController& gSignalController = SignalController::instance();
@@ -53,30 +53,24 @@ SignalController& SignalController::instance()
return *inst;
}
-int SigErrorHandler::handle(const std::string& msg)
+int SigErrorHandler(const SigNum& /*sig*/, const std::string& msg)
{
AnytunError::throwErr() << msg;
return 0;
}
-// use system specific signal handler
+//use system specific signal handler
#ifndef _MSC_VER
#include "signalHandler.hpp"
#else
#include "win32/signalHandler.hpp"
#endif
-SignalController::~SignalController()
-{
- for(HandlerMap::iterator it = handler.begin(); it != handler.end(); ++it)
- delete it->second;
-}
-
void SignalController::init()
{
registerSignalHandler(*this);
- handler[SIGERROR] = new SigErrorHandler;
+ handler[SIGERROR] = boost::bind(SigErrorHandler, _1, _2);
}
void SignalController::inject(int sig, const std::string& msg)
@@ -90,6 +84,11 @@ void SignalController::inject(int sig, const std::string& msg)
int SignalController::run()
{
+ for(CallbackMap::iterator it = callbacks.begin(); it != callbacks.end(); ++it)
+ if(it->first == CALLB_RUNNING)
+ it->second(CALLB_RUNNING);
+
+ int ret = 0;
while(1) {
sigQueueSem.down();
SigPair sig;
@@ -102,18 +101,19 @@ int SignalController::run()
HandlerMap::iterator it = handler.find(sig.first);
if(it != handler.end())
{
- int ret;
- if(sig.second == "")
- ret = it->second->handle();
- else
- ret = it->second->handle(sig.second);
+ ret = it->second(sig.first, sig.second);
if(ret)
- return ret;
+ break;
}
else
cLog.msg(Log::PRIO_NOTICE) << "SIG " << sig.first << " caught with message '" << sig.second << "' - ignoring";
}
- return 0;
+
+ for(CallbackMap::iterator it = callbacks.begin(); it != callbacks.end(); ++it)
+ if(it->first == CALLB_STOPPING)
+ it->second(CALLB_STOPPING);
+
+ return ret;
}
diff --git a/src/signalController.h b/src/signalController.h
index fdf5034..2602cc0 100644
--- a/src/signalController.h
+++ b/src/signalController.h
@@ -35,33 +35,16 @@
#include <map>
#include <queue>
+#include <boost/function.hpp>
#include "threadUtils.hpp"
#define SIGERROR -1
-class SignalHandler
-{
-public:
- virtual ~SignalHandler() {}
-
- virtual int handle() { return 0; }
- virtual int handle(const std::string& msg) { return 0; }
-
-protected:
- SignalHandler(int s) : sigNum(s) {}
-
-private:
- int sigNum;
- friend class SignalController;
-};
-
-class SigErrorHandler : public SignalHandler
-{
-public:
- SigErrorHandler() : SignalHandler(SIGERROR) {}
- int handle(const std::string& msg);
-};
+typedef int SigNum;
+typedef boost::function<int (SigNum const&, std::string const&)> SignalHandler;
+typedef enum { CALLB_RUNNING, CALLB_STOPPING } CallbackType;
+typedef boost::function<void (CallbackType const&)> ServiceCallback;
class SignalController
{
@@ -73,10 +56,8 @@ public:
void inject(int sig, const std::string& msg = "");
private:
- typedef std::map<int, SignalHandler*> HandlerMap;
-
SignalController() {};
- ~SignalController();
+ ~SignalController() {};
SignalController(const SignalController &s);
void operator=(const SignalController &s);
@@ -95,8 +76,11 @@ private:
Mutex sigQueueMutex;
Semaphore sigQueueSem;
+ typedef std::map<SigNum, SignalHandler> HandlerMap;
HandlerMap handler;
-
+ typedef std::map<CallbackType, ServiceCallback> CallbackMap;
+ CallbackMap callbacks;
+
friend void registerSignalHandler(SignalController& ctrl);
};