Blob


1 #include <u.h>
2 #include <signal.h>
3 #define NOPLAN9DEFINES
4 #include <libc.h>
5 #include "9proc.h"
7 extern char *_p9sigstr(int, char*);
9 static struct {
10 int sig;
11 int restart;
12 } sigs[] = {
13 SIGHUP, 0,
14 SIGINT, 0,
15 SIGQUIT, 0,
16 SIGILL, 0,
17 SIGTRAP, 0,
18 /* SIGABRT, */
19 #ifdef SIGEMT
20 SIGEMT, 0,
21 #endif
22 SIGFPE, 0,
23 SIGBUS, 0,
24 /* SIGSEGV, */
25 SIGCHLD, 1,
26 SIGSYS, 0,
27 SIGPIPE, 0,
28 SIGALRM, 0,
29 SIGTERM, 0,
30 SIGTSTP, 1,
31 SIGTTIN, 1,
32 SIGTTOU, 1,
33 SIGXCPU, 0,
34 SIGXFSZ, 0,
35 SIGVTALRM, 0,
36 SIGUSR1, 0,
37 SIGUSR2, 0,
38 #ifdef SIGINFO
39 SIGINFO, 0,
40 #endif
41 };
43 static void (*notifyf)(void*, char*);
45 static void
46 notifysigf(int sig)
47 {
48 int v;
49 char tmp[64];
50 Uproc *up;
52 up = _p9uproc();
53 v = p9setjmp(up->notejb);
54 if(v == 0 && notifyf)
55 (*notifyf)(nil, _p9sigstr(sig, tmp));
56 else if(v == 2){
57 if(0)print("HANDLED %d\n", sig);
58 return;
59 }
60 if(0)print("DEFAULT %d\n", sig);
61 signal(sig, SIG_DFL);
62 kill(getpid(), sig);
63 }
65 int
66 notify(void (*f)(void*, char*))
67 {
68 int i;
69 struct sigaction sa;
71 memset(&sa, 0, sizeof sa);
72 if(f == 0)
73 sa.sa_handler = SIG_DFL;
74 else{
75 notifyf = f;
76 sa.sa_handler = notifysigf;
77 }
78 for(i=0; i<nelem(sigs); i++){
79 if(sigs[i].restart)
80 sa.sa_flags |= SA_RESTART;
81 else
82 sa.sa_flags &= ~SA_RESTART;
83 sigaction(sigs[i].sig, &sa, 0);
84 }
85 return 0;
86 }
88 int
89 noted(int v)
90 {
91 Uproc *up;
93 up = _p9uproc();
94 p9longjmp(up->notejb, v==NCONT ? 2 : 1);
95 abort();
96 return 0;
97 }