2 * Signal handling for Plan 9 programs.
3 * We stubbornly use the strings from Plan 9 instead
4 * of the enumerated Unix constants.
5 * There are some weird translations. In particular,
6 * a "kill" note is the same as SIGTERM in Unix.
7 * There is no equivalent note to Unix's SIGKILL, since
8 * it's not a deliverable signal anyway.
10 * We do not handle SIGABRT or SIGSEGV, mainly because
11 * the thread library queues its notes for later, and we want
12 * to dump core with the state at time of delivery.
14 * We have to add some extra entry points to provide the
15 * ability to tweak which signals are deliverable and which
16 * are acted upon. Notifydisable and notifyenable play with
17 * the process signal mask. Notifyignore enables the signal
18 * but will not call notifyf when it comes in. This is occasionally
24 #define NOPLAN9DEFINES
27 extern char *_p9sigstr(int, char*);
28 extern int _p9strsig(char*);
30 typedef struct Sig Sig;
33 int sig; /* signal number */
57 SIGCHLD, Restart|Ignore,
62 SIGTSTP, Restart|Ignore|NoNotify,
63 /* SIGTTIN, Restart|Ignore, */
64 /* SIGTTOU, Restart|Ignore, */
71 SIGWINCH, Restart|Ignore|NoNotify,
74 SIGINFO, Restart|Ignore|NoNotify,
83 for(i=0; i<nelem(sigs); i++)
90 * The thread library initializes _notejmpbuf to its own
91 * routine which provides a per-pthread jump buffer.
92 * If we're not using the thread library, we assume we are
95 typedef struct Jmp Jmp;
109 Jmp *(*_notejmpbuf)(void) = getonejmp;
110 static void noteinit(void);
113 * Actual signal handler.
116 static void (*notifyf)(void*, char*); /* Plan 9 handler */
125 j = (*_notejmpbuf)();
126 switch(p9setjmp(j->b)){
129 (*notifyf)(nil, _p9sigstr(sig, tmp));
131 case 1: /* noted(NDFLT) */
132 if(0)print("DEFAULT %d\n", sig);
134 if(s && (s->flags&Ignore))
136 signal(sig, SIG_DFL);
139 case 2: /* noted(NCONT) */
140 if(0)print("HANDLED %d\n", sig);
154 p9longjmp((*_notejmpbuf)()->b, v==NCONT ? 2 : 1);
160 notify(void (*f)(void*, char*))
173 * Nonsense about enabling and disabling signals.
175 typedef void Sighandler(int);
181 sigaction(s, nil, &sa);
182 return sa.sa_handler;
186 notesetenable(int sig, int enabled)
188 sigset_t mask, omask;
194 sigaddset(&mask, sig);
195 sigprocmask(enabled ? SIG_UNBLOCK : SIG_BLOCK, &mask, &omask);
196 return !sigismember(&omask, sig);
200 noteenable(char *msg)
202 return notesetenable(_p9strsig(msg), 1);
206 notedisable(char *msg)
208 return notesetenable(_p9strsig(msg), 0);
212 notifyseton(int s, int on)
215 struct sigaction sa, osa;
220 memset(&sa, 0, sizeof sa);
221 sa.sa_handler = on ? signotify : signonotify;
222 if(sig->flags&Restart)
223 sa.sa_flags |= SA_RESTART;
226 * We can't allow signals within signals because there's
227 * only one jump buffer.
229 sigfillset(&sa.sa_mask);
234 sigaction(sig->sig, &sa, &osa);
235 return osa.sa_handler == signotify;
241 return notifyseton(_p9strsig(msg), 1);
247 return notifyseton(_p9strsig(msg), 0);
251 * Initialization follows sigs table.
259 for(i=0; i<nelem(sigs); i++){
262 * If someone has already installed a handler,
263 * It's probably some ld preload nonsense,
264 * like pct (a SIGVTALRM-based profiler).
265 * Or maybe someone has already called notifyon/notifyoff.
268 if(handler(sig->sig) != SIG_DFL)
270 notifyseton(sig->sig, !(sig->flags&NoNotify));