Deleted Added
full compact
trap.c (194127) trap.c (199205)
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
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.
19 *
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.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
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.
19 *
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.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/trap.c 194127 2009-06-13 21:10:41Z jilles $");
39__FBSDID("$FreeBSD: head/bin/sh/trap.c 199205 2009-11-11 23:13:24Z jilles $");
40
41#include <signal.h>
42#include <unistd.h>
43#include <stdlib.h>
44
45#include "shell.h"
46#include "main.h"
47#include "nodes.h" /* for other headers */
48#include "eval.h"
49#include "jobs.h"
50#include "show.h"
51#include "options.h"
52#include "syntax.h"
53#include "output.h"
54#include "memalloc.h"
55#include "error.h"
56#include "trap.h"
57#include "mystring.h"
58#include "myhistedit.h"
59
60
61/*
62 * Sigmode records the current value of the signal handlers for the various
63 * modes. A value of zero means that the current handler is not known.
64 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
65 */
66
67#define S_DFL 1 /* default signal handling (SIG_DFL) */
68#define S_CATCH 2 /* signal is caught */
69#define S_IGN 3 /* signal is ignored (SIG_IGN) */
70#define S_HARD_IGN 4 /* signal is ignored permanently */
71#define S_RESET 5 /* temporary - to reset a hard ignored sig */
72
73
74MKINIT char sigmode[NSIG]; /* current value of signal */
75int pendingsigs; /* indicates some signal received */
76int in_dotrap; /* do we execute in a trap handler? */
77static char *volatile trap[NSIG]; /* trap handler commands */
78static volatile sig_atomic_t gotsig[NSIG];
79 /* indicates specified signal received */
80static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
81volatile sig_atomic_t gotwinch;
82
83static int getsigaction(int, sig_t *);
84
85
86/*
87 * Map a string to a signal number.
88 *
89 * Note: the signal number may exceed NSIG.
90 */
91static int
92sigstring_to_signum(char *sig)
93{
94
95 if (is_number(sig)) {
96 int signo;
97
98 signo = atoi(sig);
99 return ((signo >= 0 && signo < NSIG) ? signo : (-1));
100 } else if (strcasecmp(sig, "exit") == 0) {
101 return (0);
102 } else {
103 int n;
104
105 if (strncasecmp(sig, "sig", 3) == 0)
106 sig += 3;
107 for (n = 1; n < sys_nsig; n++)
108 if (sys_signame[n] &&
109 strcasecmp(sys_signame[n], sig) == 0)
110 return (n);
111 }
112 return (-1);
113}
114
115
116/*
117 * Print a list of valid signal names.
118 */
119static void
120printsignals(void)
121{
122 int n, outlen;
123
124 outlen = 0;
125 for (n = 1; n < sys_nsig; n++) {
126 if (sys_signame[n]) {
127 out1fmt("%s", sys_signame[n]);
128 outlen += strlen(sys_signame[n]);
129 } else {
130 out1fmt("%d", n);
131 outlen += 3; /* good enough */
132 }
133 ++outlen;
134 if (outlen > 70 || n == sys_nsig - 1) {
135 out1str("\n");
136 outlen = 0;
137 } else {
138 out1c(' ');
139 }
140 }
141}
142
143
144/*
145 * The trap builtin.
146 */
147int
148trapcmd(int argc, char **argv)
149{
150 char *action;
151 int signo;
152
153 if (argc <= 1) {
154 for (signo = 0 ; signo < sys_nsig ; signo++) {
155 if (signo < NSIG && trap[signo] != NULL) {
156 out1str("trap -- ");
157 out1qstr(trap[signo]);
158 if (signo == 0) {
159 out1str(" exit\n");
160 } else if (sys_signame[signo]) {
161 out1fmt(" %s\n", sys_signame[signo]);
162 } else {
163 out1fmt(" %d\n", signo);
164 }
165 }
166 }
167 return 0;
168 }
169 action = NULL;
170 if (*++argv && strcmp(*argv, "--") == 0)
171 argv++;
172 if (*argv && sigstring_to_signum(*argv) == -1) {
173 if ((*argv)[0] != '-') {
174 action = *argv;
175 argv++;
176 } else if ((*argv)[1] == '\0') {
177 argv++;
178 } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
179 printsignals();
180 return 0;
181 } else {
182 error("bad option %s", *argv);
183 }
184 }
185 while (*argv) {
186 if ((signo = sigstring_to_signum(*argv)) == -1)
187 error("bad signal %s", *argv);
188 INTOFF;
189 if (action)
190 action = savestr(action);
191 if (trap[signo])
192 ckfree(trap[signo]);
193 trap[signo] = action;
194 if (signo != 0)
195 setsignal(signo);
196 INTON;
197 argv++;
198 }
199 return 0;
200}
201
202
203/*
204 * Clear traps on a fork.
205 */
206void
207clear_traps(void)
208{
209 char *volatile *tp;
210
211 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
212 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
213 INTOFF;
214 ckfree(*tp);
215 *tp = NULL;
216 if (tp != &trap[0])
217 setsignal(tp - trap);
218 INTON;
219 }
220 }
221}
222
223
224/*
225 * Check if we have any traps enabled.
226 */
227int
228have_traps(void)
229{
230 char *volatile *tp;
231
232 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
233 if (*tp && **tp) /* trap not NULL or SIG_IGN */
234 return 1;
235 }
236 return 0;
237}
238
239/*
240 * Set the signal handler for the specified signal. The routine figures
241 * out what it should be set to.
242 */
243void
244setsignal(int signo)
245{
246 int action;
40
41#include <signal.h>
42#include <unistd.h>
43#include <stdlib.h>
44
45#include "shell.h"
46#include "main.h"
47#include "nodes.h" /* for other headers */
48#include "eval.h"
49#include "jobs.h"
50#include "show.h"
51#include "options.h"
52#include "syntax.h"
53#include "output.h"
54#include "memalloc.h"
55#include "error.h"
56#include "trap.h"
57#include "mystring.h"
58#include "myhistedit.h"
59
60
61/*
62 * Sigmode records the current value of the signal handlers for the various
63 * modes. A value of zero means that the current handler is not known.
64 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
65 */
66
67#define S_DFL 1 /* default signal handling (SIG_DFL) */
68#define S_CATCH 2 /* signal is caught */
69#define S_IGN 3 /* signal is ignored (SIG_IGN) */
70#define S_HARD_IGN 4 /* signal is ignored permanently */
71#define S_RESET 5 /* temporary - to reset a hard ignored sig */
72
73
74MKINIT char sigmode[NSIG]; /* current value of signal */
75int pendingsigs; /* indicates some signal received */
76int in_dotrap; /* do we execute in a trap handler? */
77static char *volatile trap[NSIG]; /* trap handler commands */
78static volatile sig_atomic_t gotsig[NSIG];
79 /* indicates specified signal received */
80static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
81volatile sig_atomic_t gotwinch;
82
83static int getsigaction(int, sig_t *);
84
85
86/*
87 * Map a string to a signal number.
88 *
89 * Note: the signal number may exceed NSIG.
90 */
91static int
92sigstring_to_signum(char *sig)
93{
94
95 if (is_number(sig)) {
96 int signo;
97
98 signo = atoi(sig);
99 return ((signo >= 0 && signo < NSIG) ? signo : (-1));
100 } else if (strcasecmp(sig, "exit") == 0) {
101 return (0);
102 } else {
103 int n;
104
105 if (strncasecmp(sig, "sig", 3) == 0)
106 sig += 3;
107 for (n = 1; n < sys_nsig; n++)
108 if (sys_signame[n] &&
109 strcasecmp(sys_signame[n], sig) == 0)
110 return (n);
111 }
112 return (-1);
113}
114
115
116/*
117 * Print a list of valid signal names.
118 */
119static void
120printsignals(void)
121{
122 int n, outlen;
123
124 outlen = 0;
125 for (n = 1; n < sys_nsig; n++) {
126 if (sys_signame[n]) {
127 out1fmt("%s", sys_signame[n]);
128 outlen += strlen(sys_signame[n]);
129 } else {
130 out1fmt("%d", n);
131 outlen += 3; /* good enough */
132 }
133 ++outlen;
134 if (outlen > 70 || n == sys_nsig - 1) {
135 out1str("\n");
136 outlen = 0;
137 } else {
138 out1c(' ');
139 }
140 }
141}
142
143
144/*
145 * The trap builtin.
146 */
147int
148trapcmd(int argc, char **argv)
149{
150 char *action;
151 int signo;
152
153 if (argc <= 1) {
154 for (signo = 0 ; signo < sys_nsig ; signo++) {
155 if (signo < NSIG && trap[signo] != NULL) {
156 out1str("trap -- ");
157 out1qstr(trap[signo]);
158 if (signo == 0) {
159 out1str(" exit\n");
160 } else if (sys_signame[signo]) {
161 out1fmt(" %s\n", sys_signame[signo]);
162 } else {
163 out1fmt(" %d\n", signo);
164 }
165 }
166 }
167 return 0;
168 }
169 action = NULL;
170 if (*++argv && strcmp(*argv, "--") == 0)
171 argv++;
172 if (*argv && sigstring_to_signum(*argv) == -1) {
173 if ((*argv)[0] != '-') {
174 action = *argv;
175 argv++;
176 } else if ((*argv)[1] == '\0') {
177 argv++;
178 } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
179 printsignals();
180 return 0;
181 } else {
182 error("bad option %s", *argv);
183 }
184 }
185 while (*argv) {
186 if ((signo = sigstring_to_signum(*argv)) == -1)
187 error("bad signal %s", *argv);
188 INTOFF;
189 if (action)
190 action = savestr(action);
191 if (trap[signo])
192 ckfree(trap[signo]);
193 trap[signo] = action;
194 if (signo != 0)
195 setsignal(signo);
196 INTON;
197 argv++;
198 }
199 return 0;
200}
201
202
203/*
204 * Clear traps on a fork.
205 */
206void
207clear_traps(void)
208{
209 char *volatile *tp;
210
211 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
212 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
213 INTOFF;
214 ckfree(*tp);
215 *tp = NULL;
216 if (tp != &trap[0])
217 setsignal(tp - trap);
218 INTON;
219 }
220 }
221}
222
223
224/*
225 * Check if we have any traps enabled.
226 */
227int
228have_traps(void)
229{
230 char *volatile *tp;
231
232 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
233 if (*tp && **tp) /* trap not NULL or SIG_IGN */
234 return 1;
235 }
236 return 0;
237}
238
239/*
240 * Set the signal handler for the specified signal. The routine figures
241 * out what it should be set to.
242 */
243void
244setsignal(int signo)
245{
246 int action;
247 sig_t sig, sigact = SIG_DFL;
247 sig_t sigact = SIG_DFL;
248 struct sigaction sa;
248 char *t;
249
250 if ((t = trap[signo]) == NULL)
251 action = S_DFL;
252 else if (*t != '\0')
253 action = S_CATCH;
254 else
255 action = S_IGN;
256 if (action == S_DFL) {
257 switch (signo) {
258 case SIGINT:
259 action = S_CATCH;
260 break;
261 case SIGQUIT:
262#ifdef DEBUG
263 {
264 extern int debug;
265
266 if (debug)
267 break;
268 }
269#endif
270 action = S_CATCH;
271 break;
272 case SIGTERM:
273 if (rootshell && iflag)
274 action = S_IGN;
275 break;
276#if JOBS
277 case SIGTSTP:
278 case SIGTTOU:
279 if (rootshell && mflag)
280 action = S_IGN;
281 break;
282#endif
283#ifndef NO_HISTORY
284 case SIGWINCH:
285 if (rootshell && iflag)
286 action = S_CATCH;
287 break;
288#endif
289 }
290 }
291
292 t = &sigmode[signo];
293 if (*t == 0) {
294 /*
295 * current setting unknown
296 */
297 if (!getsigaction(signo, &sigact)) {
298 /*
299 * Pretend it worked; maybe we should give a warning
300 * here, but other shells don't. We don't alter
301 * sigmode, so that we retry every time.
302 */
303 return;
304 }
305 if (sigact == SIG_IGN) {
306 if (mflag && (signo == SIGTSTP ||
307 signo == SIGTTIN || signo == SIGTTOU)) {
308 *t = S_IGN; /* don't hard ignore these */
309 } else
310 *t = S_HARD_IGN;
311 } else {
312 *t = S_RESET; /* force to be set */
313 }
314 }
315 if (*t == S_HARD_IGN || *t == action)
316 return;
317 switch (action) {
318 case S_DFL: sigact = SIG_DFL; break;
319 case S_CATCH: sigact = onsig; break;
320 case S_IGN: sigact = SIG_IGN; break;
321 }
322 *t = action;
249 char *t;
250
251 if ((t = trap[signo]) == NULL)
252 action = S_DFL;
253 else if (*t != '\0')
254 action = S_CATCH;
255 else
256 action = S_IGN;
257 if (action == S_DFL) {
258 switch (signo) {
259 case SIGINT:
260 action = S_CATCH;
261 break;
262 case SIGQUIT:
263#ifdef DEBUG
264 {
265 extern int debug;
266
267 if (debug)
268 break;
269 }
270#endif
271 action = S_CATCH;
272 break;
273 case SIGTERM:
274 if (rootshell && iflag)
275 action = S_IGN;
276 break;
277#if JOBS
278 case SIGTSTP:
279 case SIGTTOU:
280 if (rootshell && mflag)
281 action = S_IGN;
282 break;
283#endif
284#ifndef NO_HISTORY
285 case SIGWINCH:
286 if (rootshell && iflag)
287 action = S_CATCH;
288 break;
289#endif
290 }
291 }
292
293 t = &sigmode[signo];
294 if (*t == 0) {
295 /*
296 * current setting unknown
297 */
298 if (!getsigaction(signo, &sigact)) {
299 /*
300 * Pretend it worked; maybe we should give a warning
301 * here, but other shells don't. We don't alter
302 * sigmode, so that we retry every time.
303 */
304 return;
305 }
306 if (sigact == SIG_IGN) {
307 if (mflag && (signo == SIGTSTP ||
308 signo == SIGTTIN || signo == SIGTTOU)) {
309 *t = S_IGN; /* don't hard ignore these */
310 } else
311 *t = S_HARD_IGN;
312 } else {
313 *t = S_RESET; /* force to be set */
314 }
315 }
316 if (*t == S_HARD_IGN || *t == action)
317 return;
318 switch (action) {
319 case S_DFL: sigact = SIG_DFL; break;
320 case S_CATCH: sigact = onsig; break;
321 case S_IGN: sigact = SIG_IGN; break;
322 }
323 *t = action;
323 sig = signal(signo, sigact);
324 if (sig != SIG_ERR && action == S_CATCH)
325 siginterrupt(signo, 1);
324 sa.sa_handler = sigact;
325 sa.sa_flags = 0;
326 sigemptyset(&sa.sa_mask);
327 sigaction(signo, &sa, NULL);
326}
327
328
329/*
330 * Return the current setting for sig w/o changing it.
331 */
332static int
333getsigaction(int signo, sig_t *sigact)
334{
335 struct sigaction sa;
336
337 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
338 return 0;
339 *sigact = (sig_t) sa.sa_handler;
340 return 1;
341}
342
343
344/*
345 * Ignore a signal.
346 */
347void
348ignoresig(int signo)
349{
350
351 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
352 signal(signo, SIG_IGN);
353 }
354 sigmode[signo] = S_HARD_IGN;
355}
356
357
358#ifdef mkinit
359INCLUDE <signal.h>
360INCLUDE "trap.h"
361
362SHELLPROC {
363 char *sm;
364
365 clear_traps();
366 for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
367 if (*sm == S_IGN)
368 *sm = S_HARD_IGN;
369 }
370}
371#endif
372
373
374/*
375 * Signal handler.
376 */
377void
378onsig(int signo)
379{
380
381 if (signo == SIGINT && trap[SIGINT] == NULL) {
382 onint();
383 return;
384 }
385
386 if (signo != SIGCHLD || !ignore_sigchld)
387 gotsig[signo] = 1;
388 pendingsigs++;
389
390 /* If we are currently in a wait builtin, prepare to break it */
391 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
392 breakwaitcmd = 1;
393 /*
394 * If a trap is set, not ignored and not the null command, we need
395 * to make sure traps are executed even when a child blocks signals.
396 */
397 if (Tflag &&
398 trap[signo] != NULL &&
399 ! (trap[signo][0] == '\0') &&
400 ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
401 breakwaitcmd = 1;
402
403#ifndef NO_HISTORY
404 if (signo == SIGWINCH)
405 gotwinch = 1;
406#endif
407}
408
409
410/*
411 * Called to execute a trap. Perhaps we should avoid entering new trap
412 * handlers while we are executing a trap handler.
413 */
414void
415dotrap(void)
416{
417 int i;
418 int savestatus;
419
420 in_dotrap++;
421 for (;;) {
422 for (i = 1; i < NSIG; i++) {
423 if (gotsig[i]) {
424 gotsig[i] = 0;
425 if (trap[i]) {
426 /*
427 * Ignore SIGCHLD to avoid infinite
428 * recursion if the trap action does
429 * a fork.
430 */
431 if (i == SIGCHLD)
432 ignore_sigchld++;
433 savestatus = exitstatus;
434 evalstring(trap[i], 0);
435 exitstatus = savestatus;
436 if (i == SIGCHLD)
437 ignore_sigchld--;
438 }
439 break;
440 }
441 }
442 if (i >= NSIG)
443 break;
444 }
445 in_dotrap--;
446 pendingsigs = 0;
447}
448
449
450/*
451 * Controls whether the shell is interactive or not.
452 */
453void
454setinteractive(int on)
455{
456 static int is_interactive = -1;
457
458 if (on == is_interactive)
459 return;
460 setsignal(SIGINT);
461 setsignal(SIGQUIT);
462 setsignal(SIGTERM);
463#ifndef NO_HISTORY
464 setsignal(SIGWINCH);
465#endif
466 is_interactive = on;
467}
468
469
470/*
471 * Called to exit the shell.
472 */
473void
474exitshell(int status)
475{
476 struct jmploc loc1, loc2;
477 char *p;
478
479 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
480 if (setjmp(loc1.loc)) {
481 goto l1;
482 }
483 if (setjmp(loc2.loc)) {
484 goto l2;
485 }
486 handler = &loc1;
487 if ((p = trap[0]) != NULL && *p != '\0') {
488 trap[0] = NULL;
489 evalstring(p, 0);
490 }
491l1: handler = &loc2; /* probably unnecessary */
492 flushall();
493#if JOBS
494 setjobctl(0);
495#endif
496l2: _exit(status);
497}
328}
329
330
331/*
332 * Return the current setting for sig w/o changing it.
333 */
334static int
335getsigaction(int signo, sig_t *sigact)
336{
337 struct sigaction sa;
338
339 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
340 return 0;
341 *sigact = (sig_t) sa.sa_handler;
342 return 1;
343}
344
345
346/*
347 * Ignore a signal.
348 */
349void
350ignoresig(int signo)
351{
352
353 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
354 signal(signo, SIG_IGN);
355 }
356 sigmode[signo] = S_HARD_IGN;
357}
358
359
360#ifdef mkinit
361INCLUDE <signal.h>
362INCLUDE "trap.h"
363
364SHELLPROC {
365 char *sm;
366
367 clear_traps();
368 for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
369 if (*sm == S_IGN)
370 *sm = S_HARD_IGN;
371 }
372}
373#endif
374
375
376/*
377 * Signal handler.
378 */
379void
380onsig(int signo)
381{
382
383 if (signo == SIGINT && trap[SIGINT] == NULL) {
384 onint();
385 return;
386 }
387
388 if (signo != SIGCHLD || !ignore_sigchld)
389 gotsig[signo] = 1;
390 pendingsigs++;
391
392 /* If we are currently in a wait builtin, prepare to break it */
393 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
394 breakwaitcmd = 1;
395 /*
396 * If a trap is set, not ignored and not the null command, we need
397 * to make sure traps are executed even when a child blocks signals.
398 */
399 if (Tflag &&
400 trap[signo] != NULL &&
401 ! (trap[signo][0] == '\0') &&
402 ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
403 breakwaitcmd = 1;
404
405#ifndef NO_HISTORY
406 if (signo == SIGWINCH)
407 gotwinch = 1;
408#endif
409}
410
411
412/*
413 * Called to execute a trap. Perhaps we should avoid entering new trap
414 * handlers while we are executing a trap handler.
415 */
416void
417dotrap(void)
418{
419 int i;
420 int savestatus;
421
422 in_dotrap++;
423 for (;;) {
424 for (i = 1; i < NSIG; i++) {
425 if (gotsig[i]) {
426 gotsig[i] = 0;
427 if (trap[i]) {
428 /*
429 * Ignore SIGCHLD to avoid infinite
430 * recursion if the trap action does
431 * a fork.
432 */
433 if (i == SIGCHLD)
434 ignore_sigchld++;
435 savestatus = exitstatus;
436 evalstring(trap[i], 0);
437 exitstatus = savestatus;
438 if (i == SIGCHLD)
439 ignore_sigchld--;
440 }
441 break;
442 }
443 }
444 if (i >= NSIG)
445 break;
446 }
447 in_dotrap--;
448 pendingsigs = 0;
449}
450
451
452/*
453 * Controls whether the shell is interactive or not.
454 */
455void
456setinteractive(int on)
457{
458 static int is_interactive = -1;
459
460 if (on == is_interactive)
461 return;
462 setsignal(SIGINT);
463 setsignal(SIGQUIT);
464 setsignal(SIGTERM);
465#ifndef NO_HISTORY
466 setsignal(SIGWINCH);
467#endif
468 is_interactive = on;
469}
470
471
472/*
473 * Called to exit the shell.
474 */
475void
476exitshell(int status)
477{
478 struct jmploc loc1, loc2;
479 char *p;
480
481 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
482 if (setjmp(loc1.loc)) {
483 goto l1;
484 }
485 if (setjmp(loc2.loc)) {
486 goto l2;
487 }
488 handler = &loc1;
489 if ((p = trap[0]) != NULL && *p != '\0') {
490 trap[0] = NULL;
491 evalstring(p, 0);
492 }
493l1: handler = &loc2; /* probably unnecessary */
494 flushall();
495#if JOBS
496 setjobctl(0);
497#endif
498l2: _exit(status);
499}