/******************************************************************** ** Image Component Library (ICL) ** ** ** ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** ** Neuroinformatics Group ** ** Website: www.iclcv.org and ** ** http://opensource.cit-ec.de/projects/icl ** ** ** ** File : ICLUtils/src/ICLUtils/SignalHandler.h ** ** Module : ICLUtils ** ** Authors: Christof Elbrechter, Viktor Richter ** ** ** ** ** ** GNU LESSER GENERAL PUBLIC LICENSE ** ** This file may be used under the terms of the GNU Lesser General ** ** Public License version 3.0 as published by the ** ** ** ** Free Software Foundation and appearing in the file LICENSE.LGPL ** ** included in the packaging of this file. Please review the ** ** following information to ensure the license requirements will ** ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** ** ** ** The development of this software was supported by the ** ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** ** Forschungsgemeinschaft (DFG) in the context of the German ** ** Excellence Initiative. ** ** ** ********************************************************************/ #pragma once #include #include #include #include #include #include namespace icl{ namespace utils{ /// 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 icl::utils::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"); } }; \endcode The handleSignal() function must not exit the program. This will be done auomatically. */ class ICLUtils_API 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 Q_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) */ private: SignalHandler(); // todo: later the constructor should be made private! //public: /// this cannot be instantiated manually! Use SignalHandler::install instead //SignalHandler(const std::string &signalsList="SIGINT,SIGHUP,SIGTERM,SIGSEGV"); public: //friend class NamedCallbackHandler; /// installs a handler to the given signals! /** several handlers can be installed to the same signals. If a handler is installed twice under the same ID, the handler installation is skipped! @param orderPercent can be used to define an execution order of signal handlers Handlers with a higher value of orderPercent are triggered later The value 100 is used for shutting down the global QApplication, which usually ends Qt's event loop, so it should not be used. */ static void install(const std::string &id, Function handler, const std::string &signalList="SIGINT,SIGTERM,SIGSEGV", int orderPercent = 0); static void uninstall(const std::string &id); /// Destructor /** When the destructor is called the system default signal handlers are substituted instead of the handleSignals function */ // virtual ~SignalHandler(); /// virtual signal handling function /** The SignalHandler implementation will track all instantiated subclassed and, on signal, call the handleSignals functions and in the end kill the process with the Hangup-signal. **/ // virtual void handleSignals(const std::string &signalAsString)=0; /// removes the signal handle for this instance /** If this is the last instance registered to a certain signal, the sygnal handle will be released and the default signal handlers are registered. **/ // void removeHandle(std::string signalName); /// removes all handles for this instance /** Basically calls removeHandle with all registeded handle names **/ //void removeAllHandles(); /// 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); //private: /// internal storage of associated signals //std::vector m_vecAssocitatedSignals; }; } // namespace utils } // namespace icl