00001 #ifndef CLASSLIB_SIGNAL_H
00002 # define CLASSLIB_SIGNAL_H
00003
00004
00005
00006 # include "classlib/sysapi/IOTypes.h"
00007
00008
00009
00010
00011 # include <signal.h>
00012 # include <sys/types.h>
00013
00014
00015
00016
00017
00018
00019
00020
00021 # if !HAVE_POSIX_SIGNALS
00022
00023
00024
00025
00026 # ifdef _WIN32
00027 typedef struct _EXCEPTION_RECORD siginfo_t;
00028 #define SIGHUP 1
00029 #define SIGQUIT 3
00030 #define SIGTRAP 5
00031 #define SIGKILL 9
00032 #define SIGUSR1 10
00033 #define SIGUSR2 12
00034 #define SIGPIPE 13
00035 #define SIGALRM 14
00036 #define SIGCHLD 17
00037 #define SIGCONT 18
00038 #define SIGSTOP 19
00039 #define SIGTSTP 20
00040 #define SIGTTIN 21
00041 #define SIGTTOU 22
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
00165
00166 static const char * name (int sig);
00167
00168
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
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
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
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
00244
00245
00246 }
00247 #endif // CLASSLIB_SIGNAL_H