libutil: add O_NOCTTY back to old pty open code
[minix.git] / commands / ash / trap.c
bloba1daa08ebb7ff0df8a093d42ced07c603937695f
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 #if defined(__minix)
66 #if !defined(__NBSD_LIBC)
67 #define NO_SIGINTERRUPT
68 #endif
69 #define NO_SYS_SIGNAME
70 #define NO_SYS_SIGLIST
71 #endif
73 typedef void (*sig_T)(int);
76 * Sigmode records the current value of the signal handlers for the various
77 * modes. A value of zero means that the current handler is not known.
78 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
81 #define S_DFL 1 /* default signal handling (SIG_DFL) */
82 #define S_CATCH 2 /* signal is caught */
83 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
84 #define S_HARD_IGN 4 /* signal is ignored permanently */
85 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
88 MKINIT char sigmode[_NSIG]; /* current value of signal */
89 int pendingsigs; /* indicates some signal received */
90 int is_interactive= -1; /* Shell is interactive */
91 int in_dotrap; /* do we execute in a trap handler? */
92 static char *volatile trap[_NSIG]; /* trap handler commands */
93 static volatile sig_atomic_t gotsig[_NSIG];
94 /* indicates specified signal received */
95 static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
96 volatile sig_atomic_t gotwinch;
98 static int sigstring_to_signum (char *);
99 static void printsignals (void);
100 static int getsigaction(int, sig_T *);
101 static void onsig (int);
102 #ifdef NO_SIGINTERRUPT
103 static int siginterrupt (int,int);
104 #endif
105 static char *strsigname (int);
109 * Map a string to a signal number.
111 static int
112 sigstring_to_signum(char *sig)
115 if (is_number(sig)) {
116 int signo;
118 signo = atoi(sig);
119 return ((signo >= 0 && signo < _NSIG) ? signo : (-1));
120 } else if (strcasecmp(sig, "exit") == 0) {
121 return (0);
122 } else {
123 int n;
125 if (strncasecmp(sig, "sig", 3) == 0)
126 sig += 3;
127 for (n = 1; n < _NSIG; n++)
128 if (strcasecmp(strsigname(n), sig) == 0)
129 return (n);
131 return (-1);
136 * Print a list of valid signal names.
138 static void
139 printsignals(void)
141 int n, outlen;
143 outlen = 0;
144 for (n = 1; n < _NSIG; n++) {
145 if (strsigname(n)) {
146 out1fmt("%s", strsigname(n));
147 outlen += strlen(strsigname(n));
148 } else {
149 out1fmt("%d", n);
150 outlen += 3; /* good enough */
152 ++outlen;
153 if (outlen > 70 || n == _NSIG - 1) {
154 out1str("\n");
155 outlen = 0;
156 } else {
157 out1c(' ');
164 * The trap builtin.
167 trapcmd(int argc, char **argv)
169 char *action;
170 int signo;
172 if (argc <= 1) {
173 for (signo = 0 ; signo < _NSIG ; signo++) {
174 if (trap[signo] != NULL) {
175 if (signo == 0) {
176 out1fmt("trap -- '%s' %s\n",
177 trap[signo], "exit");
178 } else if (strsigname(signo)) {
179 out1fmt("trap -- '%s' %s\n",
180 trap[signo], strsigname(signo));
181 } else {
182 out1fmt("trap -- '%s' %d\n",
183 trap[signo], signo);
187 return 0;
189 action = NULL;
190 if (*++argv && strcmp(*argv, "--") == 0)
191 argv++;
192 if (*argv && sigstring_to_signum(*argv) == -1) {
193 if ((*argv)[0] != '-') {
194 action = *argv;
195 argv++;
196 } else if ((*argv)[1] == '\0') {
197 argv++;
198 } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
199 printsignals();
200 return 0;
201 } else {
202 error("bad option %s", *argv);
205 while (*argv) {
206 if ((signo = sigstring_to_signum(*argv)) == -1)
207 error("bad signal %s", *argv);
208 INTOFF;
209 if (action)
210 action = savestr(action);
211 if (trap[signo])
212 ckfree(trap[signo]);
213 trap[signo] = action;
214 if (signo != 0)
215 setsignal(signo);
216 INTON;
217 argv++;
219 return 0;
224 * Clear traps on a fork.
226 void
227 clear_traps(void)
229 char *volatile *tp;
231 for (tp = trap ; tp <= &trap[_NSIG - 1] ; tp++) {
232 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
233 INTOFF;
234 ckfree(*tp);
235 *tp = NULL;
236 if (tp != &trap[0])
237 setsignal(tp - trap);
238 INTON;
245 * Set the signal handler for the specified signal. The routine figures
246 * out what it should be set to.
248 void
249 setsignal(int signo)
251 int action;
252 sig_T sig, sigact = SIG_DFL;
253 char *t;
255 if ((t = trap[signo]) == NULL)
256 action = S_DFL;
257 else if (*t != '\0')
258 action = S_CATCH;
259 else
260 action = S_IGN;
261 if (action == S_DFL) {
262 switch (signo) {
263 case SIGINT:
264 action = S_CATCH;
265 break;
266 case SIGQUIT:
267 #if DEBUG
269 extern int debug;
271 if (debug)
272 break;
274 #endif
275 action = S_CATCH;
276 break;
277 case SIGTERM:
278 if (rootshell && iflag)
279 action = S_IGN;
280 break;
281 #if JOBS
282 case SIGTSTP:
283 case SIGTTOU:
284 if (rootshell && mflag)
285 action = S_IGN;
286 break;
287 #endif
288 #ifndef NO_HISTORY
289 case SIGWINCH:
290 if (rootshell && iflag)
291 action = S_CATCH;
292 break;
293 #endif
297 t = &sigmode[signo];
298 if (*t == 0) {
300 * current setting unknown
302 if (!getsigaction(signo, &sigact)) {
304 * Pretend it worked; maybe we should give a warning
305 * here, but other shells don't. We don't alter
306 * sigmode, so that we retry every time.
308 return;
310 if (sigact == SIG_IGN) {
311 if (mflag && (signo == SIGTSTP ||
312 signo == SIGTTIN || signo == SIGTTOU)) {
313 *t = S_IGN; /* don't hard ignore these */
314 } else
315 *t = S_HARD_IGN;
316 } else {
317 *t = S_RESET; /* force to be set */
320 if (*t == S_HARD_IGN || *t == action)
321 return;
322 switch (action) {
323 case S_DFL: sigact = SIG_DFL; break;
324 case S_CATCH: sigact = onsig; break;
325 case S_IGN: sigact = SIG_IGN; break;
327 *t = action;
328 sig = signal(signo, sigact);
329 if (sig != SIG_ERR && action == S_CATCH)
330 siginterrupt(signo, 1);
335 * Return the current setting for sig w/o changing it.
337 static int
338 getsigaction(int signo, sig_T *sigact)
340 struct sigaction sa;
342 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
343 return 0;
344 *sigact = (sig_T) sa.sa_handler;
345 return 1;
350 * Ignore a signal.
352 void
353 ignoresig(int signo)
356 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
357 signal(signo, SIG_IGN);
359 sigmode[signo] = S_HARD_IGN;
363 #ifdef mkinit
364 INCLUDE <signal.h>
365 INCLUDE "trap.h"
367 SHELLPROC {
368 char *sm;
370 clear_traps();
371 for (sm = sigmode ; sm < sigmode + _NSIG ; sm++) {
372 if (*sm == S_IGN)
373 *sm = S_HARD_IGN;
376 #endif
380 * Signal handler.
382 static void
383 onsig(int signo)
386 #ifndef BSD
387 signal(signo, onsig);
388 #endif
389 if (signo == SIGINT && trap[SIGINT] == NULL) {
390 onint();
391 return;
394 if (signo != SIGCHLD || !ignore_sigchld)
395 gotsig[signo] = 1;
396 pendingsigs++;
398 /* If we are currently in a wait builtin, prepare to break it */
399 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
400 breakwaitcmd = 1;
402 * If a trap is set, not ignored and not the null command, we need
403 * to make sure traps are executed even when a child blocks signals.
405 if (Tflag &&
406 trap[signo] != NULL &&
407 ! trap[signo][0] == '\0' &&
408 ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
409 breakwaitcmd = 1;
411 #ifndef NO_HISTORY
412 if (signo == SIGWINCH)
413 gotwinch = 1;
414 #endif
419 * Called to execute a trap. Perhaps we should avoid entering new trap
420 * handlers while we are executing a trap handler.
422 void
423 dotrap(void)
425 int i;
426 int savestatus;
428 in_dotrap++;
429 for (;;) {
430 for (i = 1; i < _NSIG; i++) {
431 if (gotsig[i]) {
432 gotsig[i] = 0;
433 if (trap[i]) {
435 * Ignore SIGCHLD to avoid infinite
436 * recursion if the trap action does
437 * a fork.
439 if (i == SIGCHLD)
440 ignore_sigchld++;
441 savestatus = exitstatus;
442 evalstring(trap[i]);
443 exitstatus = savestatus;
444 if (i == SIGCHLD)
445 ignore_sigchld--;
447 break;
450 if (i >= _NSIG)
451 break;
453 in_dotrap--;
454 pendingsigs = 0;
459 * Controls whether the shell is interactive or not.
461 void
462 setinteractive(int on)
464 if (on == is_interactive)
465 return;
466 setsignal(SIGINT);
467 setsignal(SIGQUIT);
468 setsignal(SIGTERM);
469 #ifndef NO_HISTORY
470 setsignal(SIGWINCH);
471 #endif
472 is_interactive = on;
477 * Called to exit the shell.
479 void
480 exitshell(int status)
482 struct jmploc loc1, loc2;
483 char *p;
485 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
486 if (setjmp(loc1.loc)) {
487 goto l1;
489 if (setjmp(loc2.loc)) {
490 goto l2;
492 handler = &loc1;
493 if ((p = trap[0]) != NULL && *p != '\0') {
494 trap[0] = NULL;
495 evalstring(p);
497 l1: handler = &loc2; /* probably unnecessary */
498 flushall();
499 #if JOBS
500 setjobctl(0);
501 #endif
502 l2: _exit(status);
505 #ifdef NO_SIGINTERRUPT
506 static int siginterrupt(sig, flag)
507 int sig;
508 int flag;
510 return 0;
512 #endif
514 #ifdef NO_SYS_SIGNAME
515 static char *strsigname(sig)
516 int sig;
518 switch(sig)
520 case 0: return "Signal 0"; /* 0 */
521 case SIGHUP: return "hup"; /* 1 */
522 case SIGINT: return "int"; /* 2 */
523 case SIGQUIT: return "quit"; /* 3 */
524 case SIGILL: return "ill"; /* 4 */
525 case SIGTRAP: return "trap"; /* 5 */
526 case SIGABRT: return "abrt"; /* 6 */
527 #ifdef __minix_vmd
528 case SIGEMT: return "emt"; /* 7 */
529 #else
530 case SIGBUS: return "bus"; /* 7 */
531 #endif
532 case SIGFPE: return "fpe"; /* 8 */
533 case SIGKILL: return "kill"; /* 9 */
534 case SIGUSR1: return "usr1"; /* 10 */
535 case SIGSEGV: return "segv"; /* 11 */
536 case SIGUSR2: return "usr2"; /* 12 */
537 case SIGPIPE: return "pipe"; /* 13 */
538 case SIGALRM: return "alrm"; /* 14 */
539 case SIGTERM: return "term"; /* 15 */
540 #ifdef __minix_vmd
541 case 16: return "Signal 16"; /* 16 */
542 #else
543 case SIGEMT: return "emt"; /* 16 */
544 #endif
545 case SIGCHLD: return "chld"; /* 17 */
546 case SIGCONT: return "cont"; /* 18 */
547 case SIGSTOP: return "stop"; /* 19 */
548 case SIGTSTP: return "tstp"; /* 20 */
549 case SIGTTIN: return "ttin"; /* 21 */
550 case SIGTTOU: return "ttou"; /* 22 */
551 case SIGWINCH: return "winch"; /* 23 */
552 case SIGVTALRM: return "vtalrm"; /* 24 */
553 case SIGPROF: return "prof"; /* 25 */
554 #ifdef __minix_vmd
555 case SIGFPEMU: return "fpemu"; /* 30 */
556 #endif
557 default: return "Signal n";
560 #else
561 static char *strsigname(sig)
562 int sig;
564 return (char *)sys_signame[sig];
566 #endif
568 #ifdef NO_SYS_SIGLIST
569 #include "signames.h"
570 char *strsiglist(sig)
571 int sig;
573 if (sig > MAXSIG)
574 return NULL;
575 return sigmesg[sig];
577 #else
578 char *strsiglist(sig)
579 int sig;
581 return (char *)sys_siglist[sig];
583 #endif
586 * $PchId: trap.c,v 1.7 2006/05/23 11:56:21 philip Exp $