- simplify findhole() for use for 1 page only
[minix.git] / commands / ash / trap.c
blob9cddb6f37c3eb89feacabb9298f4d3b1907b5d7c
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
36 #endif
37 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/trap.c,v 1.29 2004/04/06 20:06:51 markm Exp $");
43 #include <signal.h>
44 #include <unistd.h>
45 #include <stdlib.h>
47 #include "shell.h"
48 #include "main.h"
49 #include "nodes.h" /* for other headers */
50 #include "eval.h"
51 #include "jobs.h"
52 #include "show.h"
53 #include "options.h"
54 #include "syntax.h"
55 #include "output.h"
56 #include "memalloc.h"
57 #include "error.h"
58 #include "trap.h"
59 #include "mystring.h"
60 #if !defined(NO_HISTORY) && !defined(EDITLINE)
61 #include "myhistedit.h"
62 #endif
63 #include "builtins.h"
65 #ifdef __minix
66 #define NO_SIGINTERRUPT
67 #define NO_SYS_SIGNAME
68 #define NO_SYS_SIGLIST
70 #endif
72 typedef void (*sig_T)(int);
75 * Sigmode records the current value of the signal handlers for the various
76 * modes. A value of zero means that the current handler is not known.
77 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
80 #define S_DFL 1 /* default signal handling (SIG_DFL) */
81 #define S_CATCH 2 /* signal is caught */
82 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
83 #define S_HARD_IGN 4 /* signal is ignored permanently */
84 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
87 MKINIT char sigmode[_NSIG]; /* current value of signal */
88 int pendingsigs; /* indicates some signal received */
89 int is_interactive= -1; /* Shell is interactive */
90 int in_dotrap; /* do we execute in a trap handler? */
91 static char *volatile trap[_NSIG]; /* trap handler commands */
92 static volatile sig_atomic_t gotsig[_NSIG];
93 /* indicates specified signal received */
94 static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
95 volatile sig_atomic_t gotwinch;
97 static int sigstring_to_signum (char *);
98 static void printsignals (void);
99 static int getsigaction(int, sig_T *);
100 static void onsig (int);
101 #ifdef NO_SIGINTERRUPT
102 static int siginterrupt (int,int);
103 #endif
104 static char *strsigname (int);
108 * Map a string to a signal number.
110 static int
111 sigstring_to_signum(char *sig)
114 if (is_number(sig)) {
115 int signo;
117 signo = atoi(sig);
118 return ((signo >= 0 && signo < _NSIG) ? signo : (-1));
119 } else if (strcasecmp(sig, "exit") == 0) {
120 return (0);
121 } else {
122 int n;
124 if (strncasecmp(sig, "sig", 3) == 0)
125 sig += 3;
126 for (n = 1; n < _NSIG; n++)
127 if (strcasecmp(strsigname(n), sig) == 0)
128 return (n);
130 return (-1);
135 * Print a list of valid signal names.
137 static void
138 printsignals(void)
140 int n, outlen;
142 outlen = 0;
143 for (n = 1; n < _NSIG; n++) {
144 if (strsigname(n)) {
145 out1fmt("%s", strsigname(n));
146 outlen += strlen(strsigname(n));
147 } else {
148 out1fmt("%d", n);
149 outlen += 3; /* good enough */
151 ++outlen;
152 if (outlen > 70 || n == _NSIG - 1) {
153 out1str("\n");
154 outlen = 0;
155 } else {
156 out1c(' ');
163 * The trap builtin.
166 trapcmd(int argc, char **argv)
168 char *action;
169 int signo;
171 if (argc <= 1) {
172 for (signo = 0 ; signo < _NSIG ; signo++) {
173 if (trap[signo] != NULL) {
174 if (signo == 0) {
175 out1fmt("trap -- '%s' %s\n",
176 trap[signo], "exit");
177 } else if (strsigname(signo)) {
178 out1fmt("trap -- '%s' %s\n",
179 trap[signo], strsigname(signo));
180 } else {
181 out1fmt("trap -- '%s' %d\n",
182 trap[signo], signo);
186 return 0;
188 action = NULL;
189 if (*++argv && strcmp(*argv, "--") == 0)
190 argv++;
191 if (*argv && sigstring_to_signum(*argv) == -1) {
192 if ((*argv)[0] != '-') {
193 action = *argv;
194 argv++;
195 } else if ((*argv)[1] == '\0') {
196 argv++;
197 } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
198 printsignals();
199 return 0;
200 } else {
201 error("bad option %s", *argv);
204 while (*argv) {
205 if ((signo = sigstring_to_signum(*argv)) == -1)
206 error("bad signal %s", *argv);
207 INTOFF;
208 if (action)
209 action = savestr(action);
210 if (trap[signo])
211 ckfree(trap[signo]);
212 trap[signo] = action;
213 if (signo != 0)
214 setsignal(signo);
215 INTON;
216 argv++;
218 return 0;
223 * Clear traps on a fork.
225 void
226 clear_traps(void)
228 char *volatile *tp;
230 for (tp = trap ; tp <= &trap[_NSIG - 1] ; tp++) {
231 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
232 INTOFF;
233 ckfree(*tp);
234 *tp = NULL;
235 if (tp != &trap[0])
236 setsignal(tp - trap);
237 INTON;
244 * Set the signal handler for the specified signal. The routine figures
245 * out what it should be set to.
247 void
248 setsignal(int signo)
250 int action;
251 sig_T sig, sigact = SIG_DFL;
252 char *t;
254 if ((t = trap[signo]) == NULL)
255 action = S_DFL;
256 else if (*t != '\0')
257 action = S_CATCH;
258 else
259 action = S_IGN;
260 if (action == S_DFL) {
261 switch (signo) {
262 case SIGINT:
263 action = S_CATCH;
264 break;
265 case SIGQUIT:
266 #if DEBUG
268 extern int debug;
270 if (debug)
271 break;
273 #endif
274 action = S_CATCH;
275 break;
276 case SIGTERM:
277 if (rootshell && iflag)
278 action = S_IGN;
279 break;
280 #if JOBS
281 case SIGTSTP:
282 case SIGTTOU:
283 if (rootshell && mflag)
284 action = S_IGN;
285 break;
286 #endif
287 #ifndef NO_HISTORY
288 case SIGWINCH:
289 if (rootshell && iflag)
290 action = S_CATCH;
291 break;
292 #endif
296 t = &sigmode[signo];
297 if (*t == 0) {
299 * current setting unknown
301 if (!getsigaction(signo, &sigact)) {
303 * Pretend it worked; maybe we should give a warning
304 * here, but other shells don't. We don't alter
305 * sigmode, so that we retry every time.
307 return;
309 if (sigact == SIG_IGN) {
310 if (mflag && (signo == SIGTSTP ||
311 signo == SIGTTIN || signo == SIGTTOU)) {
312 *t = S_IGN; /* don't hard ignore these */
313 } else
314 *t = S_HARD_IGN;
315 } else {
316 *t = S_RESET; /* force to be set */
319 if (*t == S_HARD_IGN || *t == action)
320 return;
321 switch (action) {
322 case S_DFL: sigact = SIG_DFL; break;
323 case S_CATCH: sigact = onsig; break;
324 case S_IGN: sigact = SIG_IGN; break;
326 *t = action;
327 sig = signal(signo, sigact);
328 if (sig != SIG_ERR && action == S_CATCH)
329 siginterrupt(signo, 1);
334 * Return the current setting for sig w/o changing it.
336 static int
337 getsigaction(int signo, sig_T *sigact)
339 struct sigaction sa;
341 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
342 return 0;
343 *sigact = (sig_T) sa.sa_handler;
344 return 1;
349 * Ignore a signal.
351 void
352 ignoresig(int signo)
355 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
356 signal(signo, SIG_IGN);
358 sigmode[signo] = S_HARD_IGN;
362 #ifdef mkinit
363 INCLUDE <signal.h>
364 INCLUDE "trap.h"
366 SHELLPROC {
367 char *sm;
369 clear_traps();
370 for (sm = sigmode ; sm < sigmode + _NSIG ; sm++) {
371 if (*sm == S_IGN)
372 *sm = S_HARD_IGN;
375 #endif
379 * Signal handler.
381 static void
382 onsig(int signo)
385 #ifndef BSD
386 signal(signo, onsig);
387 #endif
388 if (signo == SIGINT && trap[SIGINT] == NULL) {
389 onint();
390 return;
393 if (signo != SIGCHLD || !ignore_sigchld)
394 gotsig[signo] = 1;
395 pendingsigs++;
397 /* If we are currently in a wait builtin, prepare to break it */
398 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
399 breakwaitcmd = 1;
401 * If a trap is set, not ignored and not the null command, we need
402 * to make sure traps are executed even when a child blocks signals.
404 if (Tflag &&
405 trap[signo] != NULL &&
406 ! trap[signo][0] == '\0' &&
407 ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
408 breakwaitcmd = 1;
410 #ifndef NO_HISTORY
411 if (signo == SIGWINCH)
412 gotwinch = 1;
413 #endif
418 * Called to execute a trap. Perhaps we should avoid entering new trap
419 * handlers while we are executing a trap handler.
421 void
422 dotrap(void)
424 int i;
425 int savestatus;
427 in_dotrap++;
428 for (;;) {
429 for (i = 1; i < _NSIG; i++) {
430 if (gotsig[i]) {
431 gotsig[i] = 0;
432 if (trap[i]) {
434 * Ignore SIGCHLD to avoid infinite
435 * recursion if the trap action does
436 * a fork.
438 if (i == SIGCHLD)
439 ignore_sigchld++;
440 savestatus = exitstatus;
441 evalstring(trap[i]);
442 exitstatus = savestatus;
443 if (i == SIGCHLD)
444 ignore_sigchld--;
446 break;
449 if (i >= _NSIG)
450 break;
452 in_dotrap--;
453 pendingsigs = 0;
458 * Controls whether the shell is interactive or not.
460 void
461 setinteractive(int on)
463 if (on == is_interactive)
464 return;
465 setsignal(SIGINT);
466 setsignal(SIGQUIT);
467 setsignal(SIGTERM);
468 #ifndef NO_HISTORY
469 setsignal(SIGWINCH);
470 #endif
471 is_interactive = on;
476 * Called to exit the shell.
478 void
479 exitshell(int status)
481 struct jmploc loc1, loc2;
482 char *p;
484 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
485 if (setjmp(loc1.loc)) {
486 goto l1;
488 if (setjmp(loc2.loc)) {
489 goto l2;
491 handler = &loc1;
492 if ((p = trap[0]) != NULL && *p != '\0') {
493 trap[0] = NULL;
494 evalstring(p);
496 l1: handler = &loc2; /* probably unnecessary */
497 flushall();
498 #if JOBS
499 setjobctl(0);
500 #endif
501 l2: _exit(status);
504 #ifdef NO_SIGINTERRUPT
505 static int siginterrupt(sig, flag)
506 int sig;
507 int flag;
509 return 0;
511 #endif
513 #ifdef NO_SYS_SIGNAME
514 static char *strsigname(sig)
515 int sig;
517 switch(sig)
519 case 0: return "Signal 0"; /* 0 */
520 case SIGHUP: return "hup"; /* 1 */
521 case SIGINT: return "int"; /* 2 */
522 case SIGQUIT: return "quit"; /* 3 */
523 case SIGILL: return "ill"; /* 4 */
524 case SIGTRAP: return "trap"; /* 5 */
525 case SIGABRT: return "abrt"; /* 6 */
526 #ifdef __minix_vmd
527 case SIGEMT: return "emt"; /* 7 */
528 #else
529 case SIGBUS: return "bus"; /* 7 */
530 #endif
531 case SIGFPE: return "fpe"; /* 8 */
532 case SIGKILL: return "kill"; /* 9 */
533 case SIGUSR1: return "usr1"; /* 10 */
534 case SIGSEGV: return "segv"; /* 11 */
535 case SIGUSR2: return "usr2"; /* 12 */
536 case SIGPIPE: return "pipe"; /* 13 */
537 case SIGALRM: return "alrm"; /* 14 */
538 case SIGTERM: return "term"; /* 15 */
539 #ifdef __minix_vmd
540 case 16: return "Signal 16"; /* 16 */
541 #else
542 case SIGEMT: return "emt"; /* 16 */
543 #endif
544 case SIGCHLD: return "chld"; /* 17 */
545 case SIGCONT: return "cont"; /* 18 */
546 case SIGSTOP: return "stop"; /* 19 */
547 case SIGTSTP: return "tstp"; /* 20 */
548 case SIGTTIN: return "ttin"; /* 21 */
549 case SIGTTOU: return "ttou"; /* 22 */
550 case SIGWINCH: return "winch"; /* 23 */
551 case SIGVTALRM: return "vtalrm"; /* 24 */
552 case SIGPROF: return "prof"; /* 25 */
553 #ifdef __minix_vmd
554 case SIGFPEMU: return "fpemu"; /* 30 */
555 #endif
556 default: return "Signal n";
559 #else
560 static char *strsigname(sig)
561 int sig;
563 return sys_signame[sig];
565 #endif
567 #ifdef NO_SYS_SIGLIST
568 #include "signames.h"
569 char *strsiglist(sig)
570 int sig;
572 if (sig > MAXSIG)
573 return NULL;
574 return sigmesg[sig];
576 #else
577 char *strsiglist(sig)
578 int sig;
580 return sys_siglist[sig];
582 #endif
585 * $PchId: trap.c,v 1.7 2006/05/23 11:56:21 philip Exp $