trap.c revision 1.39 1 1.39 kre /* $NetBSD: trap.c,v 1.39 2017/04/29 15:12:21 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.39 kre __RCSID("$NetBSD: trap.c,v 1.39 2017/04/29 15:12:21 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.13 christos
48 1.1 cgd #include "shell.h"
49 1.1 cgd #include "main.h"
50 1.1 cgd #include "nodes.h" /* for other headers */
51 1.1 cgd #include "eval.h"
52 1.1 cgd #include "jobs.h"
53 1.13 christos #include "show.h"
54 1.1 cgd #include "options.h"
55 1.35 christos #include "builtins.h"
56 1.1 cgd #include "syntax.h"
57 1.1 cgd #include "output.h"
58 1.1 cgd #include "memalloc.h"
59 1.1 cgd #include "error.h"
60 1.1 cgd #include "trap.h"
61 1.1 cgd #include "mystring.h"
62 1.31 christos #include "var.h"
63 1.1 cgd
64 1.1 cgd
65 1.1 cgd /*
66 1.1 cgd * Sigmode records the current value of the signal handlers for the various
67 1.1 cgd * modes. A value of zero means that the current handler is not known.
68 1.1 cgd * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
69 1.1 cgd */
70 1.1 cgd
71 1.1 cgd #define S_DFL 1 /* default signal handling (SIG_DFL) */
72 1.1 cgd #define S_CATCH 2 /* signal is caught */
73 1.1 cgd #define S_IGN 3 /* signal is ignored (SIG_IGN) */
74 1.1 cgd #define S_HARD_IGN 4 /* signal is ignored permenantly */
75 1.6 jtc #define S_RESET 5 /* temporary - to reset a hard ignored sig */
76 1.1 cgd
77 1.1 cgd
78 1.8 jtc char *trap[NSIG+1]; /* trap handler commands */
79 1.8 jtc MKINIT char sigmode[NSIG]; /* current value of signal */
80 1.36 christos static volatile char gotsig[NSIG];/* indicates specified signal received */
81 1.36 christos volatile int pendingsigs; /* indicates some signal received */
82 1.1 cgd
83 1.28 christos static int getsigaction(int, sig_t *);
84 1.26 wulf
85 1.26 wulf /*
86 1.26 wulf * return the signal number described by `p' (as a number or a name)
87 1.26 wulf * or -1 if it isn't one
88 1.26 wulf */
89 1.26 wulf
90 1.26 wulf static int
91 1.28 christos signame_to_signum(const char *p)
92 1.26 wulf {
93 1.26 wulf int i;
94 1.26 wulf
95 1.26 wulf if (is_number(p))
96 1.26 wulf return number(p);
97 1.26 wulf
98 1.26 wulf if (strcasecmp(p, "exit") == 0 )
99 1.26 wulf return 0;
100 1.26 wulf
101 1.26 wulf if (strncasecmp(p, "sig", 3) == 0)
102 1.26 wulf p += 3;
103 1.26 wulf
104 1.26 wulf for (i = 0; i < NSIG; ++i)
105 1.26 wulf if (strcasecmp (p, sys_signame[i]) == 0)
106 1.26 wulf return i;
107 1.26 wulf return -1;
108 1.26 wulf }
109 1.26 wulf
110 1.26 wulf /*
111 1.26 wulf * Print a list of valid signal names
112 1.26 wulf */
113 1.28 christos static void
114 1.26 wulf printsignals(void)
115 1.26 wulf {
116 1.26 wulf int n;
117 1.26 wulf
118 1.26 wulf out1str("EXIT ");
119 1.26 wulf
120 1.26 wulf for (n = 1; n < NSIG; n++) {
121 1.26 wulf out1fmt("%s", sys_signame[n]);
122 1.26 wulf if ((n == NSIG/2) || n == (NSIG - 1))
123 1.26 wulf out1str("\n");
124 1.26 wulf else
125 1.26 wulf out1c(' ');
126 1.26 wulf }
127 1.26 wulf }
128 1.15 christos
129 1.1 cgd /*
130 1.1 cgd * The trap builtin.
131 1.1 cgd */
132 1.1 cgd
133 1.10 cgd int
134 1.28 christos trapcmd(int argc, char **argv)
135 1.10 cgd {
136 1.1 cgd char *action;
137 1.1 cgd char **ap;
138 1.1 cgd int signo;
139 1.39 kre int errs = 0;
140 1.39 kre
141 1.39 kre ap = argv + 1;
142 1.39 kre
143 1.39 kre if (argc == 2 && strcmp(*ap, "-l") == 0) {
144 1.39 kre printsignals();
145 1.39 kre return 0;
146 1.39 kre }
147 1.39 kre
148 1.39 kre if (argc > 1 && strcmp(*ap, "--") == 0) {
149 1.39 kre argc--;
150 1.39 kre ap++;
151 1.39 kre }
152 1.1 cgd
153 1.1 cgd if (argc <= 1) {
154 1.31 christos for (signo = 0 ; signo <= NSIG ; signo++)
155 1.31 christos if (trap[signo] != NULL) {
156 1.31 christos out1fmt("trap -- ");
157 1.31 christos print_quoted(trap[signo]);
158 1.31 christos out1fmt(" %s\n",
159 1.26 wulf (signo) ? sys_signame[signo] : "EXIT");
160 1.31 christos }
161 1.1 cgd return 0;
162 1.1 cgd }
163 1.26 wulf
164 1.26 wulf action = NULL;
165 1.26 wulf
166 1.39 kre if (!is_number(*ap)) {
167 1.39 kre if ((*ap)[0] == '-' && (*ap)[1] == '\0')
168 1.39 kre ap++; /* reset to default */
169 1.39 kre else
170 1.39 kre action = *ap++; /* can be '' for "ignore" */
171 1.39 kre argc--;
172 1.39 kre }
173 1.26 wulf
174 1.39 kre if (argc < 2) { /* there must be at least 1 condition */
175 1.39 kre out2str("Usage: trap [-l]\n"
176 1.39 kre " trap action condition ...\n"
177 1.39 kre " trap N condition ...\n");
178 1.39 kre return 2;
179 1.26 wulf }
180 1.26 wulf
181 1.39 kre
182 1.1 cgd while (*ap) {
183 1.39 kre signo = signame_to_signum(*ap);
184 1.26 wulf
185 1.39 kre if (signo < 0 || signo > NSIG) {
186 1.39 kre /* This is not a fatal error, so sayeth posix */
187 1.39 kre outfmt(out2, "trap: '%s' bad condition\n", *ap);
188 1.39 kre errs = 1;
189 1.39 kre }
190 1.26 wulf
191 1.1 cgd INTOFF;
192 1.1 cgd if (action)
193 1.1 cgd action = savestr(action);
194 1.26 wulf
195 1.1 cgd if (trap[signo])
196 1.1 cgd ckfree(trap[signo]);
197 1.26 wulf
198 1.1 cgd trap[signo] = action;
199 1.26 wulf
200 1.1 cgd if (signo != 0)
201 1.27 christos setsignal(signo, 0);
202 1.1 cgd INTON;
203 1.1 cgd ap++;
204 1.1 cgd }
205 1.39 kre return errs;
206 1.1 cgd }
207 1.1 cgd
208 1.1 cgd
209 1.1 cgd
210 1.1 cgd /*
211 1.27 christos * Clear traps on a fork or vfork.
212 1.27 christos * Takes one arg vfork, to tell it to not be destructive of
213 1.27 christos * the parents variables.
214 1.1 cgd */
215 1.1 cgd
216 1.1 cgd void
217 1.28 christos clear_traps(int vforked)
218 1.27 christos {
219 1.1 cgd char **tp;
220 1.1 cgd
221 1.8 jtc for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
222 1.1 cgd if (*tp && **tp) { /* trap not NULL or SIG_IGN */
223 1.1 cgd INTOFF;
224 1.27 christos if (!vforked) {
225 1.27 christos ckfree(*tp);
226 1.27 christos *tp = NULL;
227 1.27 christos }
228 1.1 cgd if (tp != &trap[0])
229 1.27 christos setsignal(tp - trap, vforked);
230 1.1 cgd INTON;
231 1.1 cgd }
232 1.1 cgd }
233 1.1 cgd }
234 1.1 cgd
235 1.1 cgd
236 1.1 cgd
237 1.1 cgd /*
238 1.1 cgd * Set the signal handler for the specified signal. The routine figures
239 1.1 cgd * out what it should be set to.
240 1.1 cgd */
241 1.1 cgd
242 1.32 christos sig_t
243 1.28 christos setsignal(int signo, int vforked)
244 1.10 cgd {
245 1.1 cgd int action;
246 1.32 christos sig_t sigact = SIG_DFL, sig;
247 1.27 christos char *t, tsig;
248 1.1 cgd
249 1.1 cgd if ((t = trap[signo]) == NULL)
250 1.1 cgd action = S_DFL;
251 1.1 cgd else if (*t != '\0')
252 1.1 cgd action = S_CATCH;
253 1.1 cgd else
254 1.1 cgd action = S_IGN;
255 1.27 christos if (rootshell && !vforked && action == S_DFL) {
256 1.1 cgd switch (signo) {
257 1.1 cgd case SIGINT:
258 1.21 christos if (iflag || minusc || sflag == 0)
259 1.1 cgd action = S_CATCH;
260 1.1 cgd break;
261 1.1 cgd case SIGQUIT:
262 1.1 cgd #ifdef DEBUG
263 1.1 cgd if (debug)
264 1.1 cgd break;
265 1.1 cgd #endif
266 1.1 cgd /* FALLTHROUGH */
267 1.1 cgd case SIGTERM:
268 1.1 cgd if (iflag)
269 1.1 cgd action = S_IGN;
270 1.1 cgd break;
271 1.1 cgd #if JOBS
272 1.1 cgd case SIGTSTP:
273 1.1 cgd case SIGTTOU:
274 1.6 jtc if (mflag)
275 1.1 cgd action = S_IGN;
276 1.1 cgd break;
277 1.1 cgd #endif
278 1.1 cgd }
279 1.1 cgd }
280 1.14 christos
281 1.6 jtc t = &sigmode[signo - 1];
282 1.27 christos tsig = *t;
283 1.27 christos if (tsig == 0) {
284 1.16 christos /*
285 1.16 christos * current setting unknown
286 1.1 cgd */
287 1.15 christos if (!getsigaction(signo, &sigact)) {
288 1.15 christos /*
289 1.15 christos * Pretend it worked; maybe we should give a warning
290 1.15 christos * here, but other shells don't. We don't alter
291 1.15 christos * sigmode, so that we retry every time.
292 1.15 christos */
293 1.15 christos return 0;
294 1.15 christos }
295 1.1 cgd if (sigact == SIG_IGN) {
296 1.32 christos /*
297 1.33 christos * POSIX 3.14.13 states that non-interactive shells
298 1.33 christos * should ignore trap commands for signals that were
299 1.33 christos * ignored upon entry, and leaves the behavior
300 1.33 christos * unspecified for interactive shells. On interactive
301 1.33 christos * shells, or if job control is on, and we have a job
302 1.33 christos * control related signal, we allow the trap to work.
303 1.33 christos *
304 1.33 christos * This change allows us to be POSIX compliant, and
305 1.33 christos * at the same time override the default behavior if
306 1.33 christos * we need to by setting the interactive flag.
307 1.32 christos */
308 1.33 christos if ((mflag && (signo == SIGTSTP ||
309 1.33 christos signo == SIGTTIN || signo == SIGTTOU)) || iflag) {
310 1.33 christos tsig = S_IGN;
311 1.33 christos } else
312 1.33 christos tsig = S_HARD_IGN;
313 1.1 cgd } else {
314 1.27 christos tsig = S_RESET; /* force to be set */
315 1.1 cgd }
316 1.1 cgd }
317 1.27 christos if (tsig == S_HARD_IGN || tsig == action)
318 1.1 cgd return 0;
319 1.1 cgd switch (action) {
320 1.1 cgd case S_DFL: sigact = SIG_DFL; break;
321 1.1 cgd case S_CATCH: sigact = onsig; break;
322 1.1 cgd case S_IGN: sigact = SIG_IGN; break;
323 1.1 cgd }
324 1.32 christos sig = signal(signo, sigact);
325 1.32 christos if (sig != SIG_ERR) {
326 1.32 christos sigset_t ss;
327 1.32 christos if (!vforked)
328 1.32 christos *t = action;
329 1.32 christos if (action == S_CATCH)
330 1.32 christos (void)siginterrupt(signo, 1);
331 1.32 christos /*
332 1.32 christos * If our parent accidentally blocked signals for
333 1.32 christos * us make sure we unblock them
334 1.32 christos */
335 1.32 christos (void)sigemptyset(&ss);
336 1.32 christos (void)sigaddset(&ss, signo);
337 1.32 christos (void)sigprocmask(SIG_UNBLOCK, &ss, NULL);
338 1.32 christos }
339 1.32 christos return sig;
340 1.1 cgd }
341 1.1 cgd
342 1.6 jtc /*
343 1.6 jtc * Return the current setting for sig w/o changing it.
344 1.6 jtc */
345 1.15 christos static int
346 1.28 christos getsigaction(int signo, sig_t *sigact)
347 1.10 cgd {
348 1.6 jtc struct sigaction sa;
349 1.6 jtc
350 1.6 jtc if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
351 1.15 christos return 0;
352 1.15 christos *sigact = (sig_t) sa.sa_handler;
353 1.15 christos return 1;
354 1.6 jtc }
355 1.1 cgd
356 1.1 cgd /*
357 1.1 cgd * Ignore a signal.
358 1.1 cgd */
359 1.1 cgd
360 1.1 cgd void
361 1.28 christos ignoresig(int signo, int vforked)
362 1.10 cgd {
363 1.6 jtc if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
364 1.1 cgd signal(signo, SIG_IGN);
365 1.1 cgd }
366 1.27 christos if (!vforked)
367 1.27 christos sigmode[signo - 1] = S_HARD_IGN;
368 1.1 cgd }
369 1.1 cgd
370 1.1 cgd
371 1.1 cgd #ifdef mkinit
372 1.8 jtc INCLUDE <signal.h>
373 1.1 cgd INCLUDE "trap.h"
374 1.1 cgd
375 1.1 cgd SHELLPROC {
376 1.1 cgd char *sm;
377 1.1 cgd
378 1.27 christos clear_traps(0);
379 1.8 jtc for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
380 1.1 cgd if (*sm == S_IGN)
381 1.1 cgd *sm = S_HARD_IGN;
382 1.1 cgd }
383 1.1 cgd }
384 1.1 cgd #endif
385 1.1 cgd
386 1.1 cgd
387 1.1 cgd
388 1.1 cgd /*
389 1.1 cgd * Signal handler.
390 1.1 cgd */
391 1.1 cgd
392 1.1 cgd void
393 1.28 christos onsig(int signo)
394 1.10 cgd {
395 1.1 cgd signal(signo, onsig);
396 1.1 cgd if (signo == SIGINT && trap[SIGINT] == NULL) {
397 1.1 cgd onint();
398 1.1 cgd return;
399 1.1 cgd }
400 1.6 jtc gotsig[signo - 1] = 1;
401 1.1 cgd pendingsigs++;
402 1.1 cgd }
403 1.1 cgd
404 1.1 cgd
405 1.1 cgd
406 1.1 cgd /*
407 1.1 cgd * Called to execute a trap. Perhaps we should avoid entering new trap
408 1.1 cgd * handlers while we are executing a trap handler.
409 1.1 cgd */
410 1.1 cgd
411 1.1 cgd void
412 1.28 christos dotrap(void)
413 1.28 christos {
414 1.1 cgd int i;
415 1.1 cgd int savestatus;
416 1.38 kre char *tr;
417 1.1 cgd
418 1.1 cgd for (;;) {
419 1.1 cgd for (i = 1 ; ; i++) {
420 1.6 jtc if (gotsig[i - 1])
421 1.6 jtc break;
422 1.8 jtc if (i >= NSIG)
423 1.5 mycroft goto done;
424 1.1 cgd }
425 1.6 jtc gotsig[i - 1] = 0;
426 1.1 cgd savestatus=exitstatus;
427 1.38 kre tr = savestr(trap[i]); /* trap code may free trap[i] */
428 1.38 kre evalstring(tr, 0);
429 1.38 kre ckfree(tr);
430 1.1 cgd exitstatus=savestatus;
431 1.1 cgd }
432 1.1 cgd done:
433 1.1 cgd pendingsigs = 0;
434 1.1 cgd }
435 1.1 cgd
436 1.37 christos int
437 1.37 christos lastsig(void)
438 1.37 christos {
439 1.37 christos int i;
440 1.1 cgd
441 1.37 christos for (i = NSIG; i > 0; i--)
442 1.37 christos if (gotsig[i - 1])
443 1.37 christos return i;
444 1.37 christos return SIGINT; /* XXX */
445 1.37 christos }
446 1.1 cgd
447 1.1 cgd /*
448 1.1 cgd * Controls whether the shell is interactive or not.
449 1.1 cgd */
450 1.1 cgd
451 1.1 cgd
452 1.1 cgd void
453 1.28 christos setinteractive(int on)
454 1.10 cgd {
455 1.6 jtc static int is_interactive;
456 1.6 jtc
457 1.1 cgd if (on == is_interactive)
458 1.1 cgd return;
459 1.27 christos setsignal(SIGINT, 0);
460 1.27 christos setsignal(SIGQUIT, 0);
461 1.27 christos setsignal(SIGTERM, 0);
462 1.1 cgd is_interactive = on;
463 1.1 cgd }
464 1.1 cgd
465 1.1 cgd
466 1.1 cgd
467 1.1 cgd /*
468 1.1 cgd * Called to exit the shell.
469 1.1 cgd */
470 1.1 cgd
471 1.1 cgd void
472 1.28 christos exitshell(int status)
473 1.10 cgd {
474 1.1 cgd struct jmploc loc1, loc2;
475 1.1 cgd char *p;
476 1.1 cgd
477 1.28 christos TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
478 1.6 jtc if (setjmp(loc1.loc)) {
479 1.6 jtc goto l1;
480 1.6 jtc }
481 1.6 jtc if (setjmp(loc2.loc)) {
482 1.6 jtc goto l2;
483 1.6 jtc }
484 1.1 cgd handler = &loc1;
485 1.1 cgd if ((p = trap[0]) != NULL && *p != '\0') {
486 1.1 cgd trap[0] = NULL;
487 1.22 christos evalstring(p, 0);
488 1.1 cgd }
489 1.1 cgd l1: handler = &loc2; /* probably unnecessary */
490 1.1 cgd flushall();
491 1.1 cgd #if JOBS
492 1.1 cgd setjobctl(0);
493 1.1 cgd #endif
494 1.1 cgd l2: _exit(status);
495 1.18 mycroft /* NOTREACHED */
496 1.1 cgd }
497