CMS 3D CMS Logo

Signal.h

Go to the documentation of this file.
00001 #ifndef CLASSLIB_SIGNAL_H
00002 # define CLASSLIB_SIGNAL_H
00003 
00004 //<<<<<< INCLUDES                                                       >>>>>>
00005 
00006 # include "classlib/sysapi/IOTypes.h"
00007 
00008 // These should be hidden, but we can't do that for now: the clients
00009 // must be able to operate on sigset_t and pid_t.  Note that we do not
00010 // want to have <csignal> -- we need all the extra POSIX stuff.
00011 # include <signal.h>
00012 # include <sys/types.h>
00013 
00014 //<<<<<< PUBLIC DEFINES                                                 >>>>>>
00015 //<<<<<< PUBLIC CONSTANTS                                               >>>>>>
00016 //<<<<<< PUBLIC TYPES                                                   >>>>>>
00017 //<<<<<< PUBLIC VARIABLES                                               >>>>>>
00018 //<<<<<< PUBLIC FUNCTIONS                                               >>>>>>
00019 //<<<<<< CLASS DECLARATIONS                                             >>>>>>
00020 
00021 # if !HAVE_POSIX_SIGNALS
00022 // Forward declare POSIX signal handling stuff for platforms that
00023 // don't have them.  This allows them to be mentioned in the Signal
00024 // interface and minimally used in the clients.  Special kludge for
00025 // Windows.
00026 #  ifdef _WIN32
00027 typedef struct _EXCEPTION_RECORD siginfo_t;
00028 #define SIGHUP          1       /* Hangup (POSIX).  */
00029 #define SIGQUIT         3       /* Quit (POSIX).  */
00030 #define SIGTRAP         5       /* Trace trap (POSIX).  */
00031 #define SIGKILL         9       /* Kill, unblockable (POSIX).  */
00032 #define SIGUSR1         10      /* User-defined signal 1 (POSIX).  */
00033 #define SIGUSR2         12      /* User-defined signal 2 (POSIX).  */
00034 #define SIGPIPE         13      /* Broken pipe (POSIX).  */
00035 #define SIGALRM         14      /* Alarm clock (POSIX).  */
00036 #define SIGCHLD         17      /* Child status has changed (POSIX).  */
00037 #define SIGCONT         18      /* Continue (POSIX).  */
00038 #define SIGSTOP         19      /* Stop, unblockable (POSIX).  */
00039 #define SIGTSTP         20      /* Keyboard stop (POSIX).  */
00040 #define SIGTTIN         21      /* Background read from tty (POSIX).  */
00041 #define SIGTTOU         22      /* Background write to tty (POSIX).  */
00042 #  else
00043 struct siginfo_t {};
00044 #  endif
00045 
00046 typedef int sigset_t;
00047 
00048 #  define sigemptyset(x)         (0)
00049 #  define sigfillset(x)          (0)
00050 #  define sigaddset(x,y)         (0)
00051 #  define sigdelset(x,y)         (0)
00052 #  define sigismember(x,y) (0)
00053 # endif
00054 
00055 namespace lat {
00056 
00065 class Signal
00066 {
00067 public:
00076     static const int USR1_DUMP_CORE     = 1;
00077 
00080     static const int FATAL_ON_QUIT      = 2;
00081 
00084     static const int FATAL_ON_INT       = 4;
00085 
00087     static const int FATAL_DUMP_CORE    = 8;
00088 
00091     static const int FATAL_DUMP_SIG     = 16;
00092 
00095     static const int FATAL_DUMP_STACK   = 32;
00096 
00099     static const int FATAL_DUMP_LIBS    = 64;
00100 
00103     static const int FATAL_DUMP_CONTEXT = 128;
00104 
00107     static const int FATAL_AUTO_EXIT    = 256;
00108 
00110     static const int FATAL_DEFAULT  = (USR1_DUMP_CORE
00111                                        | FATAL_ON_INT
00112                                        | FATAL_DUMP_CORE
00113                                        | FATAL_DUMP_SIG
00114                                        | FATAL_DUMP_STACK
00115                                        | FATAL_DUMP_LIBS
00116                                        | FATAL_DUMP_CONTEXT
00117                                        | FATAL_AUTO_EXIT);
00118 
00130     typedef bool                (*QuitHook) (int sig, siginfo_t *info, void *x);
00131 
00141     typedef bool                (*FatalHook) (int sig, siginfo_t *info, void *x);
00142 
00147     typedef void                (*FatalReturn) (int sig, siginfo_t *info, void *x);
00148 
00161     typedef void (*HandlerType) (int sig, siginfo_t *info, void *extra);
00162 
00163 
00164     // Generic signal operations
00165     // - Signal names
00166     static const char *         name (int sig);
00167 
00168     // - Signal handlers and masks
00169     static HandlerType          handler (int sig, sigset_t *mask = 0);
00170     static HandlerType          handle (int sig, HandlerType handler,
00171                                         const sigset_t *blockMask = 0);
00172     static void                 revert (int sig);
00173     static void                 ignore (int sig);
00174 
00175     static void                 block (int sig, bool sense);
00176     static void                 block (const sigset_t *mask, bool sense);
00177     static void                 mask (const sigset_t *mask, sigset_t *old = 0);
00178 
00179     // - Sending and receiving signals
00180     static int                  raise (int sig);
00181     static int                  kill (pid_t process, int sig);
00182     static int                  queue (int sig, int value = 0);
00183     static int                  queue (int sig, void *value);
00184     static int                  queue (pid_t process, int sig, int value = 0);
00185     static int                  queue (pid_t process, int sig, void *value);
00186 
00187     static bool                 pending (int sig);
00188     static void                 pending (sigset_t *mask);
00189     static void                 suspend (const sigset_t *mask);
00190     static bool                 wait (int               sig,
00191                                       siginfo_t         *info = 0,
00192                                       long              msecs = -1);
00193     static int                  wait (const sigset_t    *mask,
00194                                       siginfo_t         *info = 0,
00195                                       long              msecs = -1);
00196 
00197     // Assisted handling of program termination signals
00198     static void                 handleQuit (QuitHook hook = 0);
00199     static QuitHook             handleQuitHook (void);
00200 
00201     static void                 quit (int sig, siginfo_t *info, void *x);
00202 
00203     // Assisted handling of fatal signals
00204     static void                 handleFatal (const char *applicationName = 0,
00205                                              IOFD fd = IOFD_INVALID,
00206                                              FatalHook hook = 0,
00207                                              FatalReturn mainreturn = 0,
00208                                              unsigned options = FATAL_DEFAULT);
00209     static IOFD                 handleFatalFd (void);
00210     static FatalHook            handleFatalHook (void);
00211     static FatalReturn          handleFatalReturn (void);
00212     static unsigned             handleFatalOptions (void);
00213 
00214     static void                 fatal (int sig, siginfo_t *info, void *x);
00215     static bool                 fatalDump (int sig, siginfo_t *info, void *x);
00216     static int                  fatalLevel (void);
00217     static bool                 crashed (void);
00218 
00219     static void                 dumpInfo    (IOFD fd, char *buf, int sig,
00220                                              const siginfo_t *info);
00221     static void                 dumpMemory  (IOFD fd, char *buf,
00222                                              const void *data, size_t n);
00223     static void                 dumpContext (IOFD fd, char *buf,
00224                                              const void *context);
00225 
00226 private:
00227     static void                 trampoline (int sig);
00228     static const char *         describe (int sig, int code);
00229 
00230     static bool                 s_crashed;
00231     static int                  s_inFatal;
00232     static const char           *s_applicationName;
00233     static IOFD                 s_fatalFd;
00234     static FatalHook            s_fatalHook;
00235     static FatalReturn          s_fatalReturn;
00236     static unsigned             s_fatalOptions;
00237     static QuitHook             s_quitHook;
00238 #if !HAVE_POSIX_SIGNALS
00239     static HandlerType          s_trampolines [NSIG];
00240 #endif
00241 };
00242 
00243 //<<<<<< INLINE PUBLIC FUNCTIONS                                        >>>>>>
00244 //<<<<<< INLINE MEMBER FUNCTIONS                                        >>>>>>
00245 
00246 } // namespace lat
00247 #endif // CLASSLIB_SIGNAL_H

Generated on Tue Jun 9 17:38:53 2009 for CMSSW by  doxygen 1.5.4