//#include #include #include #include #include namespace icl{ /// C++ Signal-Handler interface class \ingroup UTILS /** Just create an own signal handler class, implement its handleSignal() function and create a static object of that signal handler. example: \code class MySignalHandler : public SignalHandler{ public: MySignalHandler():SignalHandler("SIGINT,SIGSEGV"){} virtual void handleSignals(const string &signal){ if(signal == "SIGINT") printf("application interrupted! \n"); else printf("Oops something went wrong ...! \n"); killCurrentProcess(); } }; \endcode */ class SignalHandler{ public: /// Create a new Signal handler with a list of signals /** The default parameters can be used to catch some common signals that may occur, when the program is uncommonly killed. @param signals comma-separated list of string representations of the following signals: - SIGABRT ( process abort signal) - SIGALRM ( Alarm clock) - SIGBUS ( Access to an undefined portion of a memory object) - SIGCHLD ( Child process terminated, stopped or continued) - SIGCONT ( Continue executing, if stopped) - SIGFPE ( Erroneous arithmetic operation) - SIGHUP ( Hangup ) - SIGILL ( Illegal instruction ) - SIGINT ( Terminal interrupt signal ) - SIGKILL ( Kill (cannot be caught or ignored) - SIGPIPE ( Write on a pipe with no one to read it) - SIGQUIT ( Terminal quit signal ) - SIGSEGV ( Invalid memory reference ) - SIGSTOP ( Stop executing (cannot be caught or ignored) ) - SIGTERM ( Termination signal ) - SIGTSTP ( Terminal stop signal) - SIGTTIN ( Background process attempting read) - SIGTTOU ( Background process attempting write) - SIGUSR1 ( User-defined signal 1) - SIGUSR2 ( User-defined signal 2) - SIGPOLL ( Pollable event) - SIGPROF ( Profiling timer expired) - SIGSYS ( Bad system call) - SIGTRAP ( Trace/breakpoint trap ) - SIGURG ( High bandwidth data is available at a socket) - SIGVTALRM ( Virtual timer expired) - SIGXCPU ( CPU time limit exceeded) - SIGXFSZ ( File size limit exceeded) */ SignalHandler(const std::string &signals="SIGINT,SIGHUP,SIGTERM,SIGSEGV"); /// Destructor /** When the destructor is called the system default signal handlers are substituted instead of the handleSignals function */ virtual ~SignalHandler(); /// virtual signal handling function virtual void handleSignals(const std::string &signalAsString)=0; /// calls the original action which was associated to the corresponding signal /** This seems to be not practible, as the old actions are not defined by callable functions in the old action sigaction struct. */ void oldAction(const std::string &signal); /// kills the current process /** internally this function calls kill(getpid(),1) which is mutch stronger than calling exit(0) (see the signal.h manpage) */ static void killCurrentProcess(); private: /// internal storage of associated signals std::vector m_vecAssocitatedSignals; }; }