00001
00013 #ifndef CXXUTILS_SEAL_SIGNAL_H // wlav SEAL_BASE_SIGNAL_H
00014 #define CXXUTILS_SEAL_SIGNAL_H // wlav SEAL_BASE_SIGNAL_H
00015
00016
00017
00018
00019
00020
00021
00022
00023 # include <signal.h>
00024 # include <sys/types.h>
00025 # include <climits>
00026
00027
00028
00029
00030 #ifdef si_utime // sss
00031 # define HAVE_SIGINFO_SI_UTIME 1 // sss
00032 #endif // sss
00033 #ifdef si_int // sss
00034 # define HAVE_SIGINFO_SI_INT 1 // sss
00035 #endif // sss
00036 # if !HAVE_SIGINFO_SI_UTIME // darwin
00037 # define si_utime si_signo ? 0 : 0
00038 # define si_stime si_signo ? 0 : 0
00039 # endif
00040 # if !HAVE_SIGINFO_SI_INT
00041 # if HAVE_SIGINFO_SI_VALUE // darwin
00042 # define si_int si_value.sigval_int
00043 # define si_ptr si_value.sigval_ptr
00044 # else // (none known)
00045 # define si_int si_signo ? 0 : 0
00046 # define si_ptr si_signo ? (void *) 0 : (void *) 0
00047 # endif
00048 # endif
00049
00050
00051
00052
00053
00054
00055
00056 # if !HAVE_POSIX_SIGNALS
00057
00058
00059
00060
00061 # ifdef _WIN32
00062 typedef struct _EXCEPTION_RECORD siginfo_t;
00063 #define SIGHUP 1
00064 #define SIGQUIT 3
00065 #define SIGTRAP 5
00066 #define SIGKILL 9
00067 #define SIGUSR1 10
00068 #define SIGUSR2 12
00069 #define SIGPIPE 13
00070 #define SIGALRM 14
00071 #define SIGCHLD 17
00072 #define SIGCONT 18
00073 #define SIGSTOP 19
00074 #define SIGTSTP 20
00075 #define SIGTTIN 21
00076 #define SIGTTOU 22
00077 # else
00078 struct siginfo_t {};
00079 # endif
00080
00081 typedef int sigset_t;
00082
00083 # define sigemptyset(x) (0)
00084 # define sigfillset(x) (0)
00085 # define sigaddset(x,y) (0)
00086 # define sigdelset(x,y) (0)
00087 # define sigismember(x,y) (0)
00088 # endif
00089
00090
00091 namespace Athena {
00092
00093
00095 template <class T>
00096 struct BitTraits
00097 {
00099 enum { Bits = sizeof (T) * CHAR_BIT };
00100
00102 enum { Bytes = Bits / 8 + ((Bits % 8) > 0) };
00103
00105 enum { Digits = (Bits * 30103) / 100000 + 1 };
00106
00107
00109 enum { HexDigits = Bits / 4 + ((Bits % 4) > 0) };
00110 };
00111
00112
00121 class Signal
00122 {
00123 public:
00132 static const int USR1_DUMP_CORE = 1;
00133
00136 static const int FATAL_ON_QUIT = 2;
00137
00140 static const int FATAL_ON_INT = 4;
00141
00143 static const int FATAL_DUMP_CORE = 8;
00144
00147 static const int FATAL_DUMP_SIG = 16;
00148
00151 static const int FATAL_DUMP_STACK = 32;
00152
00155 static const int FATAL_DUMP_LIBS = 64;
00156
00159 static const int FATAL_DUMP_CONTEXT = 128;
00160
00163 static const int FATAL_AUTO_EXIT = 256;
00164
00166 static const int FATAL_DEFAULT = (USR1_DUMP_CORE
00167 | FATAL_ON_INT
00168 | FATAL_DUMP_CORE
00169 | FATAL_DUMP_SIG
00170 | FATAL_DUMP_STACK
00171 | FATAL_DUMP_LIBS
00172 | FATAL_DUMP_CONTEXT
00173 | FATAL_AUTO_EXIT);
00174
00186 typedef bool (*QuitHook) (int sig, siginfo_t *info, void *x);
00187
00197 typedef bool (*FatalHook) (int sig, siginfo_t *info, void *x);
00198
00203 typedef void (*FatalReturn) (int sig, siginfo_t *info, void *x);
00204
00217 typedef void (*HandlerType) (int sig, siginfo_t *info, void *extra);
00218
00219
00220
00221
00222 static const char * name (int sig);
00223
00224
00225 static HandlerType handler (int sig, sigset_t *mask = 0);
00226 static HandlerType handle (int sig, HandlerType handler,
00227 const sigset_t *blockMask = 0);
00228 static void revert (int sig);
00229 static void ignore (int sig);
00230
00231 static void block (int sig, bool sense);
00232 static void block (const sigset_t *mask, bool sense);
00233 static void mask (const sigset_t *mask, sigset_t *old = 0);
00234
00235
00236 static int raise (int sig);
00237 static int kill (pid_t process, int sig);
00238 static int queue (int sig, int value = 0);
00239 static int queue (int sig, void *value);
00240 static int queue (pid_t process, int sig, int value = 0);
00241 static int queue (pid_t process, int sig, void *value);
00242
00243 static bool pending (int sig);
00244 static void pending (sigset_t *mask);
00245 static void suspend (const sigset_t *mask);
00246 static bool wait (int sig,
00247 siginfo_t *info = 0,
00248 long msecs = -1);
00249 static int wait (const sigset_t *mask,
00250 siginfo_t *info = 0,
00251 long msecs = -1);
00252
00253
00254 static void handleQuit (QuitHook hook = 0);
00255 static QuitHook handleQuitHook (void);
00256
00257 static void quit (int sig, siginfo_t *info, void *x);
00258
00259
00260 static void handleFatal (const char *applicationName = 0,
00261 IOFD fd = IOFD_INVALID,
00262 FatalHook hook = 0,
00263 FatalReturn mainreturn = 0,
00264 unsigned options = FATAL_DEFAULT);
00265 static IOFD handleFatalFd (void);
00266 static FatalHook handleFatalHook (void);
00267 static FatalReturn handleFatalReturn (void);
00268 static unsigned handleFatalOptions (void);
00269
00270 static void fatal (int sig, siginfo_t *info, void *x);
00271 static bool fatalDump (int sig, siginfo_t *info, void *x);
00272 static int fatalLevel (void);
00273 static bool crashed (void);
00274
00275 static void dumpInfo (IOFD fd, char *buf, int sig,
00276 const siginfo_t *info);
00277 static void dumpMemory (IOFD fd, char *buf,
00278 const void *data, size_t n);
00279 static void dumpContext (IOFD fd, char *buf,
00280 const void *context);
00281
00282 static const char * describe (int sig, int code);
00283
00284 private:
00285 static void trampoline (int sig);
00286
00287 static bool s_crashed;
00288 static int s_inFatal;
00289 static const char *s_applicationName;
00290 static IOFD s_fatalFd;
00291 static FatalHook s_fatalHook;
00292 static FatalReturn s_fatalReturn;
00293 static unsigned s_fatalOptions;
00294 static QuitHook s_quitHook;
00295 #if !HAVE_POSIX_SIGNALS
00296 static HandlerType s_trampolines [NSIG];
00297 #endif
00298 };
00299
00300
00301
00302
00303
00304 }
00305
00306
00307 extern "C" {
00310 void CxxUtils_installFatalHandler();
00311 }
00312
00313
00314 #endif // CXXUTILS_SEAL_SIGNAL_H wlav SEAL_BASE_SIGNAL_H