diff --git a/stuff/signals.hh b/stuff/signals.hh index 3729fc5627cb66a9ba0693488fb176c9799ea979..18011845eaba0f56e742c90495d7a4f49822811d 100644 --- a/stuff/signals.hh +++ b/stuff/signals.hh @@ -7,6 +7,8 @@ namespace Stuff { namespace Signals { + +//! reset given signal to default handler void resetSignal(int signal) { struct sigaction new_action; @@ -16,6 +18,7 @@ void resetSignal(int signal) sigaction(signal, &new_action, NULL); } +//! example signal handler void handleInterrupt(int signal) { Logger().Info() << "forcefully terminated at " << Logging::TimeString() << std::endl; @@ -25,16 +28,20 @@ void handleInterrupt(int signal) kill(getpid(), signal); } -void installSignalHandlers() +//! type of handler functions +typedef void handler_type(int); + +//! calling this from your main() will install handler as callback when signal is received +void installSignalHandler(int signal = SIGINT, handler_type handler = handleInterrupt) { struct sigaction new_action; /* Set up the structure to specify the new action. */ - new_action.sa_handler = handleInterrupt; + new_action.sa_handler = handler; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; - sigaction(SIGINT, &new_action, NULL); + sigaction(signal, &new_action, NULL); } } // end namespace signals } // end namepsace stuff