trap.c revision 1.51 1 1.51 kre /* $NetBSD: trap.c,v 1.51 2019/01/18 06:28:09 kre Exp $ */
2 1.12 cgd
3 1.1 cgd /*-
4 1.6 jtc * Copyright (c) 1991, 1993
5 1.6 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.29 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.17 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.12 cgd #if 0
38 1.15 christos static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
39 1.12 cgd #else
40 1.51 kre __RCSID("$NetBSD: trap.c,v 1.51 2019/01/18 06:28:09 kre Exp $");
41 1.12 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.13 christos #include <signal.h>
45 1.13 christos #include <unistd.h>
46 1.13 christos #include <stdlib.h>
47 1.40 kre #include <stdio.h>
48 1.47 kre #include <errno.h>
49 1.50 kre #include <termios.h>
50 1.47 kre
51 1.50 kre #undef CEOF /* from <termios.h> but concflicts with sh use */
52 1.50 kre
53 1.50 kre #include <sys/ioctl.h>
54 1.47 kre #include <sys/resource.h>
55 1.13 christos
56 1.1 cgd #include "shell.h"
57 1.1 cgd #include "main.h"
58 1.1 cgd #include "nodes.h" /* for other headers */
59 1.1 cgd #include "eval.h"
60 1.1 cgd #include "jobs.h"
61 1.13 christos #include "show.h"
62 1.1 cgd #include "options.h"
63 1.35 christos #include "builtins.h"
64 1.1 cgd #include "syntax.h"
65 1.1 cgd #include "output.h"
66 1.1 cgd #include "memalloc.h"
67 1.1 cgd #include "error.h"
68 1.1 cgd #include "trap.h"
69 1.1 cgd #include "mystring.h"
70 1.31 christos #include "var.h"
71 1.1 cgd
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * Sigmode records the current value of the signal handlers for the various
75 1.1 cgd * modes. A value of zero means that the current handler is not known.
76 1.1 cgd * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
77 1.1 cgd */
78 1.1 cgd
79 1.1 cgd #define S_DFL 1 /* default signal handling (SIG_DFL) */
80 1.1 cgd #define S_CATCH 2 /* signal is caught */
81 1.1 cgd #define S_IGN 3 /* signal is ignored (SIG_IGN) */
82 1.1 cgd #define S_HARD_IGN 4 /* signal is ignored permenantly */
83 1.6 jtc #define S_RESET 5 /* temporary - to reset a hard ignored sig */
84 1.1 cgd
85 1.1 cgd
86 1.8 jtc MKINIT char sigmode[NSIG]; /* current value of signal */
87 1.47 kre static volatile sig_atomic_t gotsig[NSIG];/* indicates specified signal received */
88 1.47 kre volatile sig_atomic_t pendingsigs; /* indicates some signal received */
89 1.47 kre
90 1.47 kre int traps_invalid; /* in a subshell, but trap[] not yet cleared */
91 1.47 kre static char * volatile trap[NSIG]; /* trap handler commands */
92 1.47 kre static int in_dotrap;
93 1.47 kre static int last_trapsig;
94 1.47 kre
95 1.47 kre static int exiting; /* exitshell() has been done */
96 1.47 kre static int exiting_status; /* the status to use for exit() */
97 1.1 cgd
98 1.28 christos static int getsigaction(int, sig_t *);
99 1.40 kre STATIC const char *trap_signame(int);
100 1.46 kre void printsignals(struct output *, int);
101 1.26 wulf
102 1.26 wulf /*
103 1.26 wulf * return the signal number described by `p' (as a number or a name)
104 1.26 wulf * or -1 if it isn't one
105 1.26 wulf */
106 1.26 wulf
107 1.26 wulf static int
108 1.28 christos signame_to_signum(const char *p)
109 1.26 wulf {
110 1.26 wulf int i;
111 1.26 wulf
112 1.26 wulf if (is_number(p))
113 1.26 wulf return number(p);
114 1.26 wulf
115 1.26 wulf if (strcasecmp(p, "exit") == 0 )
116 1.26 wulf return 0;
117 1.26 wulf
118 1.46 kre i = signalnumber(p);
119 1.46 kre if (i == 0)
120 1.46 kre i = -1;
121 1.46 kre return i;
122 1.26 wulf }
123 1.26 wulf
124 1.26 wulf /*
125 1.40 kre * return the name of a signal used by the "trap" command
126 1.40 kre */
127 1.40 kre STATIC const char *
128 1.40 kre trap_signame(int signo)
129 1.40 kre {
130 1.40 kre static char nbuf[12];
131 1.46 kre const char *p;
132 1.40 kre
133 1.40 kre if (signo == 0)
134 1.40 kre return "EXIT";
135 1.46 kre p = signalname(signo);
136 1.40 kre if (p != NULL)
137 1.40 kre return p;
138 1.40 kre (void)snprintf(nbuf, sizeof nbuf, "%d", signo);
139 1.40 kre return nbuf;
140 1.40 kre }
141 1.40 kre
142 1.50 kre #ifdef SMALL
143 1.40 kre /*
144 1.26 wulf * Print a list of valid signal names
145 1.26 wulf */
146 1.46 kre void
147 1.46 kre printsignals(struct output *out, int len)
148 1.26 wulf {
149 1.26 wulf int n;
150 1.26 wulf
151 1.46 kre if (len != 0)
152 1.46 kre outc(' ', out);
153 1.26 wulf for (n = 1; n < NSIG; n++) {
154 1.46 kre outfmt(out, "%s", trap_signame(n));
155 1.26 wulf if ((n == NSIG/2) || n == (NSIG - 1))
156 1.46 kre outstr("\n", out);
157 1.26 wulf else
158 1.46 kre outc(' ', out);
159 1.26 wulf }
160 1.26 wulf }
161 1.50 kre #else /* !SMALL */
162 1.50 kre /*
163 1.50 kre * Print the names of all the signals (neatly) to fp
164 1.50 kre * "len" gives the number of chars already printed to
165 1.50 kre * the current output line (in kill.c, always 0)
166 1.50 kre */
167 1.50 kre void
168 1.50 kre printsignals(struct output *out, int len)
169 1.50 kre {
170 1.50 kre int sig;
171 1.50 kre int nl, pad;
172 1.50 kre const char *name;
173 1.50 kre int termwidth = 80;
174 1.50 kre
175 1.50 kre if ((name = bltinlookup("COLUMNS", 1)) != NULL)
176 1.50 kre termwidth = (int)strtol(name, NULL, 10);
177 1.50 kre else if (isatty(1)) {
178 1.50 kre struct winsize win;
179 1.50 kre
180 1.50 kre if (ioctl(1, TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
181 1.50 kre termwidth = win.ws_col;
182 1.50 kre }
183 1.50 kre
184 1.50 kre if (posix)
185 1.50 kre pad = 1;
186 1.50 kre else
187 1.50 kre pad = (len | 7) + 1 - len;
188 1.50 kre
189 1.50 kre for (sig = 0; (sig = signalnext(sig)) != 0; ) {
190 1.50 kre name = signalname(sig);
191 1.50 kre if (name == NULL)
192 1.50 kre continue;
193 1.50 kre
194 1.50 kre nl = strlen(name);
195 1.50 kre
196 1.50 kre if (len > 0 && nl + len + pad >= termwidth) {
197 1.50 kre outc('\n', out);
198 1.50 kre len = 0;
199 1.50 kre pad = 0;
200 1.50 kre } else if (pad > 0 && len != 0)
201 1.50 kre outfmt(out, "%*s", pad, "");
202 1.50 kre else
203 1.50 kre pad = 0;
204 1.50 kre
205 1.50 kre len += nl + pad;
206 1.50 kre if (!posix)
207 1.50 kre pad = (nl | 7) + 1 - nl;
208 1.50 kre else
209 1.50 kre pad = 1;
210 1.50 kre
211 1.50 kre outstr(name, out);
212 1.50 kre }
213 1.50 kre if (len != 0)
214 1.50 kre outc('\n', out);
215 1.50 kre }
216 1.50 kre #endif /* SMALL */
217 1.15 christos
218 1.1 cgd /*
219 1.1 cgd * The trap builtin.
220 1.1 cgd */
221 1.1 cgd
222 1.10 cgd int
223 1.28 christos trapcmd(int argc, char **argv)
224 1.10 cgd {
225 1.1 cgd char *action;
226 1.1 cgd char **ap;
227 1.1 cgd int signo;
228 1.39 kre int errs = 0;
229 1.40 kre int printonly = 0;
230 1.39 kre
231 1.39 kre ap = argv + 1;
232 1.39 kre
233 1.47 kre CTRACE(DBG_TRAP, ("trapcmd: "));
234 1.39 kre if (argc == 2 && strcmp(*ap, "-l") == 0) {
235 1.47 kre CTRACE(DBG_TRAP, ("-l\n"));
236 1.46 kre out1str("EXIT");
237 1.46 kre printsignals(out1, 4);
238 1.39 kre return 0;
239 1.39 kre }
240 1.40 kre if (argc == 2 && strcmp(*ap, "-") == 0) {
241 1.47 kre CTRACE(DBG_TRAP, ("-\n"));
242 1.40 kre for (signo = 0; signo < NSIG; signo++) {
243 1.40 kre if (trap[signo] == NULL)
244 1.40 kre continue;
245 1.40 kre INTOFF;
246 1.40 kre ckfree(trap[signo]);
247 1.40 kre trap[signo] = NULL;
248 1.40 kre if (signo != 0)
249 1.40 kre setsignal(signo, 0);
250 1.40 kre INTON;
251 1.40 kre }
252 1.47 kre traps_invalid = 0;
253 1.40 kre return 0;
254 1.40 kre }
255 1.40 kre if (argc >= 2 && strcmp(*ap, "-p") == 0) {
256 1.47 kre CTRACE(DBG_TRAP, ("-p "));
257 1.40 kre printonly = 1;
258 1.40 kre ap++;
259 1.40 kre argc--;
260 1.40 kre }
261 1.39 kre
262 1.39 kre if (argc > 1 && strcmp(*ap, "--") == 0) {
263 1.39 kre argc--;
264 1.39 kre ap++;
265 1.39 kre }
266 1.1 cgd
267 1.1 cgd if (argc <= 1) {
268 1.40 kre int count;
269 1.40 kre
270 1.47 kre CTRACE(DBG_TRAP, ("*all*\n"));
271 1.40 kre if (printonly) {
272 1.40 kre for (count = 0, signo = 0 ; signo < NSIG ; signo++)
273 1.40 kre if (trap[signo] == NULL) {
274 1.40 kre if (count == 0)
275 1.40 kre out1str("trap -- -");
276 1.40 kre out1fmt(" %s", trap_signame(signo));
277 1.40 kre /* oh! unlucky 13 */
278 1.40 kre if (++count >= 13) {
279 1.40 kre out1str("\n");
280 1.40 kre count = 0;
281 1.40 kre }
282 1.40 kre }
283 1.40 kre if (count)
284 1.40 kre out1str("\n");
285 1.40 kre }
286 1.40 kre
287 1.40 kre for (count = 0, signo = 0 ; signo < NSIG ; signo++)
288 1.40 kre if (trap[signo] != NULL && trap[signo][0] == '\0') {
289 1.40 kre if (count == 0)
290 1.40 kre out1str("trap -- ''");
291 1.40 kre out1fmt(" %s", trap_signame(signo));
292 1.40 kre /*
293 1.40 kre * the prefix is 10 bytes, with 4 byte
294 1.40 kre * signal names (common) we have room in
295 1.40 kre * the 70 bytes left on a normal line for
296 1.40 kre * 70/(4+1) signals, that's 14, but to
297 1.40 kre * allow for the occasional longer sig name
298 1.40 kre * we output one less...
299 1.40 kre */
300 1.40 kre if (++count >= 13) {
301 1.40 kre out1str("\n");
302 1.40 kre count = 0;
303 1.40 kre }
304 1.40 kre }
305 1.40 kre if (count)
306 1.40 kre out1str("\n");
307 1.40 kre
308 1.40 kre for (signo = 0 ; signo < NSIG ; signo++)
309 1.40 kre if (trap[signo] != NULL && trap[signo][0] != '\0') {
310 1.40 kre out1str("trap -- ");
311 1.31 christos print_quoted(trap[signo]);
312 1.40 kre out1fmt(" %s\n", trap_signame(signo));
313 1.31 christos }
314 1.40 kre
315 1.1 cgd return 0;
316 1.1 cgd }
317 1.47 kre CTRACE(DBG_TRAP, ("\n"));
318 1.26 wulf
319 1.26 wulf action = NULL;
320 1.26 wulf
321 1.47 kre if (!printonly && traps_invalid)
322 1.47 kre free_traps();
323 1.47 kre
324 1.40 kre if (!printonly && !is_number(*ap)) {
325 1.39 kre if ((*ap)[0] == '-' && (*ap)[1] == '\0')
326 1.39 kre ap++; /* reset to default */
327 1.39 kre else
328 1.39 kre action = *ap++; /* can be '' for "ignore" */
329 1.39 kre argc--;
330 1.39 kre }
331 1.26 wulf
332 1.39 kre if (argc < 2) { /* there must be at least 1 condition */
333 1.39 kre out2str("Usage: trap [-l]\n"
334 1.40 kre " trap -p [condition ...]\n"
335 1.39 kre " trap action condition ...\n"
336 1.39 kre " trap N condition ...\n");
337 1.39 kre return 2;
338 1.26 wulf }
339 1.26 wulf
340 1.39 kre
341 1.1 cgd while (*ap) {
342 1.39 kre signo = signame_to_signum(*ap);
343 1.26 wulf
344 1.40 kre if (signo < 0 || signo >= NSIG) {
345 1.39 kre /* This is not a fatal error, so sayeth posix */
346 1.39 kre outfmt(out2, "trap: '%s' bad condition\n", *ap);
347 1.39 kre errs = 1;
348 1.40 kre ap++;
349 1.40 kre continue;
350 1.40 kre }
351 1.40 kre ap++;
352 1.40 kre
353 1.40 kre if (printonly) {
354 1.40 kre out1str("trap -- ");
355 1.40 kre if (trap[signo] == NULL)
356 1.40 kre out1str("-");
357 1.40 kre else
358 1.40 kre print_quoted(trap[signo]);
359 1.40 kre out1fmt(" %s\n", trap_signame(signo));
360 1.40 kre continue;
361 1.39 kre }
362 1.26 wulf
363 1.1 cgd INTOFF;
364 1.1 cgd if (action)
365 1.1 cgd action = savestr(action);
366 1.26 wulf
367 1.47 kre VTRACE(DBG_TRAP, ("trap for %d from %s%s%s to %s%s%s\n", signo,
368 1.47 kre trap[signo] ? "'" : "", trap[signo] ? trap[signo] : "-",
369 1.47 kre trap[signo] ? "'" : "", action ? "'" : "",
370 1.47 kre action ? action : "-", action ? "'" : ""));
371 1.47 kre
372 1.1 cgd if (trap[signo])
373 1.1 cgd ckfree(trap[signo]);
374 1.26 wulf
375 1.1 cgd trap[signo] = action;
376 1.26 wulf
377 1.1 cgd if (signo != 0)
378 1.27 christos setsignal(signo, 0);
379 1.1 cgd INTON;
380 1.1 cgd }
381 1.39 kre return errs;
382 1.1 cgd }
383 1.1 cgd
384 1.1 cgd
385 1.1 cgd
386 1.1 cgd /*
387 1.27 christos * Clear traps on a fork or vfork.
388 1.27 christos * Takes one arg vfork, to tell it to not be destructive of
389 1.27 christos * the parents variables.
390 1.1 cgd */
391 1.1 cgd void
392 1.28 christos clear_traps(int vforked)
393 1.27 christos {
394 1.47 kre char * volatile *tp;
395 1.47 kre
396 1.47 kre VTRACE(DBG_TRAP, ("clear_traps(%d)\n", vforked));
397 1.47 kre if (!vforked)
398 1.47 kre traps_invalid = 1;
399 1.1 cgd
400 1.47 kre for (tp = &trap[1] ; tp < &trap[NSIG] ; tp++) {
401 1.1 cgd if (*tp && **tp) { /* trap not NULL or SIG_IGN */
402 1.1 cgd INTOFF;
403 1.47 kre setsignal(tp - trap, vforked == 1);
404 1.1 cgd INTON;
405 1.1 cgd }
406 1.1 cgd }
407 1.47 kre if (vforked == 2)
408 1.47 kre free_traps();
409 1.47 kre }
410 1.47 kre
411 1.47 kre void
412 1.47 kre free_traps(void)
413 1.47 kre {
414 1.47 kre char * volatile *tp;
415 1.47 kre
416 1.47 kre VTRACE(DBG_TRAP, ("free_traps%s\n", traps_invalid ? "(invalid)" : ""));
417 1.47 kre INTOFF;
418 1.47 kre for (tp = trap ; tp < &trap[NSIG] ; tp++)
419 1.47 kre if (*tp && **tp) {
420 1.47 kre ckfree(*tp);
421 1.47 kre *tp = NULL;
422 1.47 kre }
423 1.47 kre traps_invalid = 0;
424 1.47 kre INTON;
425 1.1 cgd }
426 1.1 cgd
427 1.45 kre /*
428 1.45 kre * See if there are any defined traps
429 1.45 kre */
430 1.45 kre int
431 1.45 kre have_traps(void)
432 1.45 kre {
433 1.47 kre char * volatile *tp;
434 1.47 kre
435 1.47 kre if (traps_invalid)
436 1.47 kre return 0;
437 1.1 cgd
438 1.45 kre for (tp = trap ; tp < &trap[NSIG] ; tp++)
439 1.45 kre if (*tp && **tp) /* trap not NULL or SIG_IGN */
440 1.45 kre return 1;
441 1.45 kre return 0;
442 1.45 kre }
443 1.1 cgd
444 1.1 cgd /*
445 1.1 cgd * Set the signal handler for the specified signal. The routine figures
446 1.1 cgd * out what it should be set to.
447 1.1 cgd */
448 1.47 kre void
449 1.28 christos setsignal(int signo, int vforked)
450 1.10 cgd {
451 1.1 cgd int action;
452 1.32 christos sig_t sigact = SIG_DFL, sig;
453 1.27 christos char *t, tsig;
454 1.1 cgd
455 1.51 kre if (traps_invalid || (t = trap[signo]) == NULL)
456 1.1 cgd action = S_DFL;
457 1.1 cgd else if (*t != '\0')
458 1.1 cgd action = S_CATCH;
459 1.1 cgd else
460 1.1 cgd action = S_IGN;
461 1.47 kre
462 1.47 kre VTRACE(DBG_TRAP, ("setsignal(%d%s) -> %d", signo,
463 1.47 kre vforked ? ", VF" : "", action));
464 1.27 christos if (rootshell && !vforked && action == S_DFL) {
465 1.1 cgd switch (signo) {
466 1.1 cgd case SIGINT:
467 1.21 christos if (iflag || minusc || sflag == 0)
468 1.1 cgd action = S_CATCH;
469 1.1 cgd break;
470 1.1 cgd case SIGQUIT:
471 1.1 cgd #ifdef DEBUG
472 1.1 cgd if (debug)
473 1.1 cgd break;
474 1.1 cgd #endif
475 1.1 cgd /* FALLTHROUGH */
476 1.1 cgd case SIGTERM:
477 1.47 kre if (rootshell && iflag)
478 1.1 cgd action = S_IGN;
479 1.1 cgd break;
480 1.1 cgd #if JOBS
481 1.1 cgd case SIGTSTP:
482 1.1 cgd case SIGTTOU:
483 1.47 kre if (rootshell && mflag)
484 1.1 cgd action = S_IGN;
485 1.1 cgd break;
486 1.1 cgd #endif
487 1.1 cgd }
488 1.1 cgd }
489 1.14 christos
490 1.47 kre /*
491 1.47 kre * Never let users futz with SIGCHLD
492 1.47 kre * instead we will give them pseudo SIGCHLD's
493 1.47 kre * when background jobs complete.
494 1.47 kre */
495 1.47 kre if (signo == SIGCHLD)
496 1.47 kre action = S_DFL;
497 1.47 kre
498 1.47 kre VTRACE(DBG_TRAP, (" -> %d", action));
499 1.47 kre
500 1.47 kre t = &sigmode[signo];
501 1.27 christos tsig = *t;
502 1.27 christos if (tsig == 0) {
503 1.16 christos /*
504 1.16 christos * current setting unknown
505 1.1 cgd */
506 1.15 christos if (!getsigaction(signo, &sigact)) {
507 1.15 christos /*
508 1.15 christos * Pretend it worked; maybe we should give a warning
509 1.15 christos * here, but other shells don't. We don't alter
510 1.15 christos * sigmode, so that we retry every time.
511 1.15 christos */
512 1.47 kre VTRACE(DBG_TRAP, (" getsigaction (%d)\n", errno));
513 1.47 kre return;
514 1.15 christos }
515 1.47 kre VTRACE(DBG_TRAP, (" [%s]%s%s", sigact==SIG_IGN ? "IGN" :
516 1.47 kre sigact==SIG_DFL ? "DFL" : "caught",
517 1.47 kre iflag ? "i" : "", mflag ? "m" : ""));
518 1.47 kre
519 1.1 cgd if (sigact == SIG_IGN) {
520 1.32 christos /*
521 1.33 christos * POSIX 3.14.13 states that non-interactive shells
522 1.33 christos * should ignore trap commands for signals that were
523 1.33 christos * ignored upon entry, and leaves the behavior
524 1.33 christos * unspecified for interactive shells. On interactive
525 1.33 christos * shells, or if job control is on, and we have a job
526 1.33 christos * control related signal, we allow the trap to work.
527 1.33 christos *
528 1.33 christos * This change allows us to be POSIX compliant, and
529 1.33 christos * at the same time override the default behavior if
530 1.33 christos * we need to by setting the interactive flag.
531 1.32 christos */
532 1.33 christos if ((mflag && (signo == SIGTSTP ||
533 1.33 christos signo == SIGTTIN || signo == SIGTTOU)) || iflag) {
534 1.33 christos tsig = S_IGN;
535 1.33 christos } else
536 1.33 christos tsig = S_HARD_IGN;
537 1.1 cgd } else {
538 1.27 christos tsig = S_RESET; /* force to be set */
539 1.1 cgd }
540 1.1 cgd }
541 1.47 kre VTRACE(DBG_TRAP, (" tsig=%d\n", tsig));
542 1.47 kre
543 1.27 christos if (tsig == S_HARD_IGN || tsig == action)
544 1.47 kre return;
545 1.47 kre
546 1.1 cgd switch (action) {
547 1.1 cgd case S_DFL: sigact = SIG_DFL; break;
548 1.1 cgd case S_CATCH: sigact = onsig; break;
549 1.1 cgd case S_IGN: sigact = SIG_IGN; break;
550 1.1 cgd }
551 1.47 kre
552 1.32 christos sig = signal(signo, sigact);
553 1.47 kre
554 1.32 christos if (sig != SIG_ERR) {
555 1.32 christos sigset_t ss;
556 1.47 kre
557 1.32 christos if (!vforked)
558 1.32 christos *t = action;
559 1.47 kre
560 1.32 christos if (action == S_CATCH)
561 1.32 christos (void)siginterrupt(signo, 1);
562 1.32 christos /*
563 1.32 christos * If our parent accidentally blocked signals for
564 1.32 christos * us make sure we unblock them
565 1.32 christos */
566 1.32 christos (void)sigemptyset(&ss);
567 1.32 christos (void)sigaddset(&ss, signo);
568 1.32 christos (void)sigprocmask(SIG_UNBLOCK, &ss, NULL);
569 1.32 christos }
570 1.47 kre return;
571 1.1 cgd }
572 1.1 cgd
573 1.6 jtc /*
574 1.6 jtc * Return the current setting for sig w/o changing it.
575 1.6 jtc */
576 1.15 christos static int
577 1.28 christos getsigaction(int signo, sig_t *sigact)
578 1.10 cgd {
579 1.6 jtc struct sigaction sa;
580 1.6 jtc
581 1.6 jtc if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
582 1.15 christos return 0;
583 1.15 christos *sigact = (sig_t) sa.sa_handler;
584 1.15 christos return 1;
585 1.6 jtc }
586 1.1 cgd
587 1.1 cgd /*
588 1.1 cgd * Ignore a signal.
589 1.1 cgd */
590 1.1 cgd
591 1.1 cgd void
592 1.28 christos ignoresig(int signo, int vforked)
593 1.10 cgd {
594 1.47 kre if (sigmode[signo] == 0)
595 1.47 kre setsignal(signo, vforked);
596 1.47 kre
597 1.47 kre VTRACE(DBG_TRAP, ("ignoresig(%d%s)\n", signo, vforked ? ", VF" : ""));
598 1.47 kre if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
599 1.1 cgd signal(signo, SIG_IGN);
600 1.47 kre if (!vforked)
601 1.47 kre sigmode[signo] = S_IGN;
602 1.1 cgd }
603 1.47 kre }
604 1.47 kre
605 1.47 kre char *
606 1.47 kre child_trap(void)
607 1.47 kre {
608 1.47 kre char * p;
609 1.47 kre
610 1.47 kre p = trap[SIGCHLD];
611 1.47 kre
612 1.51 kre if (traps_invalid || (p != NULL && *p == '\0'))
613 1.47 kre p = NULL;
614 1.47 kre
615 1.47 kre return p;
616 1.1 cgd }
617 1.1 cgd
618 1.1 cgd
619 1.1 cgd #ifdef mkinit
620 1.8 jtc INCLUDE <signal.h>
621 1.1 cgd INCLUDE "trap.h"
622 1.47 kre INCLUDE "shell.h"
623 1.47 kre INCLUDE "show.h"
624 1.1 cgd
625 1.1 cgd SHELLPROC {
626 1.1 cgd char *sm;
627 1.1 cgd
628 1.47 kre INTOFF;
629 1.47 kre clear_traps(2);
630 1.8 jtc for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
631 1.47 kre if (*sm == S_IGN) {
632 1.1 cgd *sm = S_HARD_IGN;
633 1.49 kre VTRACE(DBG_TRAP, ("SHELLPROC: %d -> hard_ign\n",
634 1.49 kre (sm - sigmode)));
635 1.47 kre }
636 1.1 cgd }
637 1.47 kre INTON;
638 1.1 cgd }
639 1.1 cgd #endif
640 1.1 cgd
641 1.1 cgd
642 1.1 cgd
643 1.1 cgd /*
644 1.1 cgd * Signal handler.
645 1.1 cgd */
646 1.1 cgd
647 1.1 cgd void
648 1.28 christos onsig(int signo)
649 1.10 cgd {
650 1.41 kre CTRACE(DBG_SIG, ("Signal %d, had: pending %d, gotsig[%d]=%d\n",
651 1.41 kre signo, pendingsigs, signo, gotsig[signo]));
652 1.41 kre
653 1.47 kre /* This should not be needed.
654 1.1 cgd signal(signo, onsig);
655 1.47 kre */
656 1.47 kre
657 1.51 kre if (signo == SIGINT && (traps_invalid || trap[SIGINT] == NULL)) {
658 1.1 cgd onint();
659 1.1 cgd return;
660 1.1 cgd }
661 1.47 kre
662 1.47 kre /*
663 1.47 kre * if the signal will do nothing, no point reporting it
664 1.47 kre */
665 1.51 kre if (!traps_invalid && trap[signo] != NULL && trap[signo][0] != '\0' &&
666 1.47 kre signo != SIGCHLD) {
667 1.47 kre gotsig[signo] = 1;
668 1.47 kre pendingsigs++;
669 1.47 kre }
670 1.1 cgd }
671 1.1 cgd
672 1.1 cgd
673 1.1 cgd
674 1.1 cgd /*
675 1.1 cgd * Called to execute a trap. Perhaps we should avoid entering new trap
676 1.1 cgd * handlers while we are executing a trap handler.
677 1.1 cgd */
678 1.1 cgd
679 1.1 cgd void
680 1.28 christos dotrap(void)
681 1.28 christos {
682 1.1 cgd int i;
683 1.47 kre char *tr;
684 1.1 cgd int savestatus;
685 1.47 kre struct skipsave saveskip;
686 1.47 kre
687 1.47 kre in_dotrap++;
688 1.1 cgd
689 1.47 kre CTRACE(DBG_TRAP, ("dotrap[%d]: %d pending, traps %sinvalid\n",
690 1.47 kre in_dotrap, pendingsigs, traps_invalid ? "" : "not "));
691 1.1 cgd for (;;) {
692 1.47 kre pendingsigs = 0;
693 1.1 cgd for (i = 1 ; ; i++) {
694 1.47 kre if (i >= NSIG)
695 1.40 kre return;
696 1.40 kre if (gotsig[i])
697 1.6 jtc break;
698 1.1 cgd }
699 1.40 kre gotsig[i] = 0;
700 1.47 kre
701 1.47 kre if (traps_invalid)
702 1.47 kre continue;
703 1.47 kre
704 1.47 kre tr = trap[i];
705 1.47 kre
706 1.47 kre CTRACE(DBG_TRAP|DBG_SIG, ("dotrap %d: %s%s%s\n", i,
707 1.47 kre tr ? "\"" : "", tr ? tr : "NULL", tr ? "\"" : ""));
708 1.47 kre
709 1.47 kre if (tr != NULL) {
710 1.47 kre last_trapsig = i;
711 1.47 kre save_skipstate(&saveskip);
712 1.47 kre savestatus = exitstatus;
713 1.47 kre
714 1.44 kre tr = savestr(tr); /* trap code may free trap[i] */
715 1.44 kre evalstring(tr, 0);
716 1.44 kre ckfree(tr);
717 1.47 kre
718 1.47 kre if (current_skipstate() == SKIPNONE ||
719 1.47 kre saveskip.state != SKIPNONE) {
720 1.47 kre restore_skipstate(&saveskip);
721 1.47 kre exitstatus = savestatus;
722 1.47 kre }
723 1.44 kre }
724 1.1 cgd }
725 1.47 kre
726 1.47 kre in_dotrap--;
727 1.1 cgd }
728 1.1 cgd
729 1.37 christos int
730 1.37 christos lastsig(void)
731 1.37 christos {
732 1.37 christos int i;
733 1.1 cgd
734 1.40 kre for (i = NSIG; --i > 0; )
735 1.40 kre if (gotsig[i])
736 1.37 christos return i;
737 1.37 christos return SIGINT; /* XXX */
738 1.37 christos }
739 1.1 cgd
740 1.1 cgd /*
741 1.1 cgd * Controls whether the shell is interactive or not.
742 1.1 cgd */
743 1.1 cgd
744 1.1 cgd
745 1.1 cgd void
746 1.28 christos setinteractive(int on)
747 1.10 cgd {
748 1.6 jtc static int is_interactive;
749 1.6 jtc
750 1.1 cgd if (on == is_interactive)
751 1.1 cgd return;
752 1.27 christos setsignal(SIGINT, 0);
753 1.27 christos setsignal(SIGQUIT, 0);
754 1.27 christos setsignal(SIGTERM, 0);
755 1.1 cgd is_interactive = on;
756 1.1 cgd }
757 1.1 cgd
758 1.1 cgd
759 1.1 cgd
760 1.1 cgd /*
761 1.1 cgd * Called to exit the shell.
762 1.1 cgd */
763 1.47 kre void
764 1.47 kre exitshell(int status)
765 1.47 kre {
766 1.47 kre CTRACE(DBG_ERRS|DBG_PROCS|DBG_CMDS|DBG_TRAP,
767 1.47 kre ("pid %d: exitshell(%d)\n", getpid(), status));
768 1.47 kre
769 1.47 kre exiting = 1;
770 1.47 kre exiting_status = status;
771 1.47 kre exitshell_savedstatus();
772 1.47 kre }
773 1.1 cgd
774 1.1 cgd void
775 1.47 kre exitshell_savedstatus(void)
776 1.10 cgd {
777 1.47 kre struct jmploc loc;
778 1.1 cgd char *p;
779 1.47 kre volatile int sig = 0;
780 1.47 kre int s;
781 1.47 kre sigset_t sigs;
782 1.1 cgd
783 1.41 kre CTRACE(DBG_ERRS|DBG_PROCS|DBG_CMDS|DBG_TRAP,
784 1.47 kre ("pid %d: exitshell_savedstatus()%s $?=%d xs=%d dt=%d ts=%d\n",
785 1.47 kre getpid(), exiting ? " exiting" : "", exitstatus,
786 1.47 kre exiting_status, in_dotrap, last_trapsig));
787 1.47 kre
788 1.47 kre if (!exiting) {
789 1.47 kre if (in_dotrap && last_trapsig) {
790 1.47 kre sig = last_trapsig;
791 1.47 kre exiting_status = sig + 128;
792 1.47 kre } else
793 1.47 kre exiting_status = exitstatus;
794 1.47 kre }
795 1.47 kre exitstatus = exiting_status;
796 1.47 kre
797 1.47 kre if (!setjmp(loc.loc)) {
798 1.47 kre handler = &loc;
799 1.47 kre
800 1.47 kre if (!traps_invalid && (p = trap[0]) != NULL && *p != '\0') {
801 1.47 kre reset_eval();
802 1.47 kre trap[0] = NULL;
803 1.47 kre VTRACE(DBG_TRAP, ("exit trap: \"%s\"\n", p));
804 1.47 kre evalstring(p, 0);
805 1.47 kre }
806 1.47 kre }
807 1.41 kre
808 1.47 kre INTOFF; /* we're done, no more interrupts. */
809 1.47 kre
810 1.47 kre if (!setjmp(loc.loc)) {
811 1.47 kre handler = &loc; /* probably unnecessary */
812 1.47 kre flushall();
813 1.47 kre #if JOBS
814 1.47 kre setjobctl(0);
815 1.47 kre #endif
816 1.6 jtc }
817 1.47 kre
818 1.47 kre if ((s = sig) != 0 && s != SIGSTOP && s != SIGTSTP && s != SIGTTIN &&
819 1.47 kre s != SIGTTOU) {
820 1.47 kre struct rlimit nocore;
821 1.47 kre
822 1.47 kre /*
823 1.47 kre * if the signal is of the core dump variety, don't...
824 1.47 kre */
825 1.47 kre nocore.rlim_cur = nocore.rlim_max = 0;
826 1.47 kre (void) setrlimit(RLIMIT_CORE, &nocore);
827 1.47 kre
828 1.47 kre signal(s, SIG_DFL);
829 1.47 kre sigemptyset(&sigs);
830 1.47 kre sigaddset(&sigs, s);
831 1.47 kre sigprocmask(SIG_UNBLOCK, &sigs, NULL);
832 1.47 kre
833 1.47 kre kill(getpid(), s);
834 1.1 cgd }
835 1.47 kre _exit(exiting_status);
836 1.18 mycroft /* NOTREACHED */
837 1.1 cgd }
838