00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "plint.h"
00026 #include "error.h"
00027
00028 #include "signal.h"
00029 #include "sys.h"
00030
00031
00032 static void signal_handler(int s)
00033 {
00034 signal(s, signal_handler);
00035
00036 switch (s)
00037 {
00038 case SIGCHLD: eprintf("\n%s%sa child has left me !\n", PLINT_PLINT, PLINT_SIGN); wait(0); return;
00039 case SIGTSTP: eprintf("\n%s%sOk, I'll take a break to rest a while...\n", PLINT_PLINT, PLINT_SIGN); raise(SIGSTOP); return;
00040 case SIGPIPE OR SIGINT: eprintf("\n%s%swhat do you want ?\n", PLINT_PLINT, PLINT_SIGN); break;
00041 case SIGTERM: eprintf("\n%s%si follow your order!\n", PLINT_PLINT, PLINT_SIGN); break;
00042 case SIGQUIT: eprintf("\n%s%scowardly quitting...\n", PLINT_PLINT, PLINT_SIGN); break;
00043 case SIGSTKFLT OR SIGFPE : PLINT_ERROR(PLINT_ERR_MATH);
00044 case SIGSEGV: PLINT_ERROR(PLINT_ERR_MEM);
00045 default:
00046 assert(0);
00047 }
00048
00049 exit(0);
00050 }
00051
00052 static void signal_fatal_handler(int s)
00053 {
00054 plint->flag.jmp = 1;
00055 switch (s)
00056 {
00057 case SIGABRT OR SIGSYS OR SIGBUS:
00058 eprintf("\n%s%sbug!!!\n%s\n", PLINT_PLINT, PLINT_SIGN, PLINT_MAIL);
00059 PLINT_ERROR(PLINT_ERR_BUG);
00060 break;
00061 case SIGKILL:
00062 eprintf("\n%s%soh, no... shot from the backhhhaaooo.. ahhh...\n", PLINT_PLINT, PLINT_SIGN);
00063 break;
00064 default:
00065 }
00066 signal(s, SIG_DFL);
00067 signal(SIGABRT, SIG_DFL);
00068
00069 abort();
00070 }
00071
00072 void plint_sig_init()
00073 {
00074 sigfillset(&plint->ctx.sigblk);
00075 sigprocmask(SIG_SETMASK, &plint->ctx.sigblk, 0);
00076
00077 sigemptyset(&plint->ctx.sigblk);
00078
00079 #define PLINT_SIGNAL(__sig__) signal((__sig__), signal_handler), sigaddset(&plint->ctx.sigblk, (__sig__))
00080 PLINT_SIGNAL(SIGINT);
00081 PLINT_SIGNAL(SIGQUIT);
00082 PLINT_SIGNAL(SIGTERM);
00083 PLINT_SIGNAL(SIGSEGV);
00084 PLINT_SIGNAL(SIGFPE);
00085 PLINT_SIGNAL(SIGSTKFLT);
00086 PLINT_SIGNAL(SIGPIPE);
00087 PLINT_SIGNAL(SIGCHLD);
00088 PLINT_SIGNAL(SIGTSTP);
00089 #undef PLINT_SIGNAL
00090
00091 #define PLINT_FATAL_SIGNAL(__sig__) signal((__sig__), signal_fatal_handler)
00092 PLINT_FATAL_SIGNAL(SIGABRT);
00093 PLINT_FATAL_SIGNAL(SIGSYS);
00094 PLINT_FATAL_SIGNAL(SIGBUS);
00095 PLINT_FATAL_SIGNAL(SIGKILL);
00096 #undef PLINT_FATAL_SIGNAL
00097
00098 sigprocmask(SIG_SETMASK, &plint->ctx.sigblk, 0);
00099
00100 }
00101
00102 void plint_sig_finish()
00103 {
00104 sigprocmask(SIG_UNBLOCK, &plint->ctx.sigblk, 0);
00105 }