/* * Signal handling for Plan 9 programs. * We stubbornly use the strings from Plan 9 instead * of the enumerated Unix constants. * There are some weird translations. In particular, * a "kill" note is the same as SIGTERM in Unix. * There is no equivalent note to Unix's SIGKILL, since * it's not a deliverable signal anyway. * * We do not handle SIGABRT or SIGSEGV, mainly because * the thread library queues its notes for later, and we want * to dump core with the state at time of delivery. * * We have to add some extra entry points to provide the * ability to tweak which signals are deliverable and which * are acted upon. Notifydisable and notifyenable play with * the process signal mask. Notifyignore enables the signal * but will not call notifyf when it comes in. This is occasionally * useful. */ #include #include #include #define NOPLAN9DEFINES #include #include "util.h" static struct { int sig; char *str; } tab[] = { SIGHUP, "hangup", SIGINT, "interrupt", SIGILL, "sys: illegal instruction", SIGABRT, "sys: abort", SIGFPE, "sys: fp: trap", SIGSEGV, "sys: segmentation violation", SIGTERM, "kill", }; static char* _p9sigstr(int sig, char *tmp) { int i; for(i=0; ib)){ case 0: // Beep( 200, 200 ); if(notifyf) (*notifyf)(nil, _p9sigstr(sig, tmp)); /* fall through */ case 1: /* noted(NDFLT) */ // Beep( 7000, 200 ); if(0)print("DEFAULT %d\n", sig); s = findsig(sig); signal(sig, SIG_DFL); raise(sig); _exit(1); case 2: /* noted(NCONT) */ if(0)print("HANDLED %d\n", sig); return; } } static void signonotify(int sig) { USED(sig); } int noted(int v) { p9longjmp(onejmp.b, v==NCONT ? 2 : 1); abort(); return 0; } int notify(void (*f)(void*, char*)) { static int init; notifyf = f; if(!init){ init = 1; noteinit(); } return 0; } /* * Nonsense about enabling and disabling signals. */ typedef void Sighandler(int); static Sighandler* handler(int s) { Sighandler *h; h = signal(s, SIG_DFL); signal(s, h); return h; } static int notesetenable(int sig, int enabled) { if(sig == 0) return -1; /* not implemented yet */ return -1; } int noteenable(char *msg) { return notesetenable(_p9strsig(msg), 1); } int notedisable(char *msg) { return notesetenable(_p9strsig(msg), 0); } static int notifyseton(int s, int on) { Sig *sig; Sighandler *h, *oh; sig = findsig(s); if(sig == nil) return -1; h = on ? signotify : signonotify; /* * Install handler. */ oh = signal(sig->sig, h); return oh == signotify; } int notifyon(char *msg) { return notifyseton(_p9strsig(msg), 1); } int notifyoff(char *msg) { return notifyseton(_p9strsig(msg), 0); } static BOOL WINAPI ctrlhandler(DWORD type) { switch(type){ case CTRL_BREAK_EVENT: raise(SIGINT); return TRUE; } return FALSE; } /* * Initialization follows sigs table. */ static void noteinit(void) { int i; Sig *sig; for(i=0; isig) != SIG_DFL) continue; notifyseton(sig->sig, 1); } if(winhascons()) SetConsoleCtrlHandler(ctrlhandler, 1); }