diff options
author | randomdan <randomdan@localhost> | 2013-09-07 13:49:14 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2013-09-07 13:49:14 +0000 |
commit | 5edb540785dbb4f4699e9cbd676c7dce44986647 (patch) | |
tree | dca8814eb8a7298e2819f754437fc0b51f3241a8 | |
parent | Basics for p2daemons (diff) | |
download | project2-5edb540785dbb4f4699e9cbd676c7dce44986647.tar.bz2 project2-5edb540785dbb4f4699e9cbd676c7dce44986647.tar.xz project2-5edb540785dbb4f4699e9cbd676c7dce44986647.zip |
Add signal handler to p2daemon
-rw-r--r-- | project2/daemon/lib/daemon.h | 3 | ||||
-rw-r--r-- | project2/daemon/p2daemonAppEngine.cpp | 30 | ||||
-rw-r--r-- | project2/daemon/p2daemonAppEngine.h | 3 |
3 files changed, 31 insertions, 5 deletions
diff --git a/project2/daemon/lib/daemon.h b/project2/daemon/lib/daemon.h index 106d3f4..ca57b5d 100644 --- a/project2/daemon/lib/daemon.h +++ b/project2/daemon/lib/daemon.h @@ -10,7 +10,8 @@ class Daemon : public IntrusivePtrBase { Daemon(); virtual ~Daemon(); - virtual void process() const = 0; + virtual void run() const = 0; + virtual void shutdown() const = 0; }; typedef boost::intrusive_ptr<Daemon> DaemonPtr; diff --git a/project2/daemon/p2daemonAppEngine.cpp b/project2/daemon/p2daemonAppEngine.cpp index 62bcff7..d00cd1d 100644 --- a/project2/daemon/p2daemonAppEngine.cpp +++ b/project2/daemon/p2daemonAppEngine.cpp @@ -5,6 +5,7 @@ std::string DaemonAppEngine::daemonType; boost::optional<std::string> DaemonAppEngine::libraryPath; Glib::ustring DaemonAppEngine::reqPlatform; +DaemonPtr DaemonAppEngine::daemon; DECLARE_OPTIONS(DaemonAppEngine, "Project2 Daemon options") ("daemon.type", Options::value(&daemonType), "The core daemon module to run") @@ -13,16 +14,38 @@ DECLARE_OPTIONS(DaemonAppEngine, "Project2 Daemon options") END_OPTIONS(DaemonAppEngine); DaemonAppEngine::DaemonAppEngine(int argc, char ** argv) : - library(!!libraryPath ? loadLibrary(*libraryPath) : boost::shared_ptr<void>()), - daemon(DaemonLoader::createNew(daemonType, argc, argv)) + library(!!libraryPath ? loadLibrary(*libraryPath) : boost::shared_ptr<void>()) { + daemon = DaemonLoader::createNew(daemonType, argc, argv); +} + +void +DaemonAppEngine::shutdown(int) +{ + if (signal(SIGINT, SIG_DFL) == SIG_ERR) { + Logger()->message(LOG_WARNING, "Failed to remove signal handler"); + } + if (signal(SIGTERM, SIG_DFL) == SIG_ERR) { + Logger()->message(LOG_WARNING, "Failed to remove signal handler"); + } + + Logger()->message(LOG_INFO, "Shutting down daemon."); + daemon->shutdown(); + Logger()->message(LOG_INFO, "Daemon shut down."); } void DaemonAppEngine::process() const { + if (signal(SIGINT, &shutdown) == SIG_ERR) { + Logger()->message(LOG_WARNING, "Failed to add signal handler"); + } + if (signal(SIGTERM, &shutdown) == SIG_ERR) { + Logger()->message(LOG_WARNING, "Failed to add signal handler"); + } + Logger()->message(LOG_INFO, "Starting daemon."); - daemon->process(); + daemon->run(); Logger()->message(LOG_INFO, "Daemon exitted."); } @@ -37,3 +60,4 @@ DaemonAppEngine::loadLibrary(const std::string & path) return boost::shared_ptr<void>(handle, &dlclose); } + diff --git a/project2/daemon/p2daemonAppEngine.h b/project2/daemon/p2daemonAppEngine.h index a74f34a..48ef572 100644 --- a/project2/daemon/p2daemonAppEngine.h +++ b/project2/daemon/p2daemonAppEngine.h @@ -22,7 +22,8 @@ class DaemonAppEngine { static boost::shared_ptr<void> loadLibrary(const std::string & path); boost::shared_ptr<void> library; - DaemonPtr daemon; + static void shutdown(int); + static DaemonPtr daemon; }; #endif |