]> git.saurik.com Git - apple/shell_cmds.git/blame - sh/trap.c
shell_cmds-203.tar.gz
[apple/shell_cmds.git] / sh / trap.c
CommitLineData
71aad674
A
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>
deb63bfb 39__FBSDID("$FreeBSD: head/bin/sh/trap.c 297360 2016-03-28 18:58:40Z jilles $");
71aad674
A
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 "builtins.h"
59#include "myhistedit.h"
60
61#ifdef __APPLE__
62#define sys_nsig (NSIG)
63#endif /* __APPLE__ */
64
65/*
66 * Sigmode records the current value of the signal handlers for the various
67 * modes. A value of zero means that the current handler is not known.
68 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
69 */
70
71#define S_DFL 1 /* default signal handling (SIG_DFL) */
72#define S_CATCH 2 /* signal is caught */
73#define S_IGN 3 /* signal is ignored (SIG_IGN) */
74#define S_HARD_IGN 4 /* signal is ignored permanently */
75#define S_RESET 5 /* temporary - to reset a hard ignored sig */
76
77
78static char sigmode[NSIG]; /* current value of signal */
79volatile sig_atomic_t pendingsig; /* indicates some signal received */
80volatile sig_atomic_t pendingsig_waitcmd; /* indicates wait builtin should be interrupted */
81static int in_dotrap; /* do we execute in a trap handler? */
82static char *volatile trap[NSIG]; /* trap handler commands */
83static volatile sig_atomic_t gotsig[NSIG];
84 /* indicates specified signal received */
85static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
86static int last_trapsig;
87
88static int exiting; /* exitshell() has been called */
89static int exiting_exitstatus; /* value passed to exitshell() */
90
91static int getsigaction(int, sig_t *);
92
93
94/*
95 * Map a string to a signal number.
96 *
97 * Note: the signal number may exceed NSIG.
98 */
99static int
100sigstring_to_signum(char *sig)
101{
102
103 if (is_number(sig)) {
104 int signo;
105
106 signo = atoi(sig);
107 return ((signo >= 0 && signo < NSIG) ? signo : (-1));
108 } else if (strcasecmp(sig, "EXIT") == 0) {
109 return (0);
110 } else {
111 int n;
112
113 if (strncasecmp(sig, "SIG", 3) == 0)
114 sig += 3;
115 for (n = 1; n < sys_nsig; n++)
116 if (sys_signame[n] &&
117 strcasecmp(sys_signame[n], sig) == 0)
118 return (n);
119 }
120 return (-1);
121}
122
123
124/*
125 * Print a list of valid signal names.
126 */
127static void
128printsignals(void)
129{
130 int n, outlen;
131
132 outlen = 0;
133 for (n = 1; n < sys_nsig; n++) {
134 if (sys_signame[n]) {
135 out1fmt("%s", sys_signame[n]);
136 outlen += strlen(sys_signame[n]);
137 } else {
138 out1fmt("%d", n);
139 outlen += 3; /* good enough */
140 }
141 ++outlen;
142 if (outlen > 71 || n == sys_nsig - 1) {
143 out1str("\n");
144 outlen = 0;
145 } else {
146 out1c(' ');
147 }
148 }
149}
150
151
152/*
153 * The trap builtin.
154 */
155int
156trapcmd(int argc __unused, char **argv)
157{
158 char *action;
159 int signo;
160 int errors = 0;
161 int i;
162
163 while ((i = nextopt("l")) != '\0') {
164 switch (i) {
165 case 'l':
166 printsignals();
167 return (0);
168 }
169 }
170 argv = argptr;
171
172 if (*argv == NULL) {
173 for (signo = 0 ; signo < sys_nsig ; signo++) {
174 if (signo < NSIG && trap[signo] != NULL) {
175 out1str("trap -- ");
176 out1qstr(trap[signo]);
177 if (signo == 0) {
178 out1str(" EXIT\n");
179 } else if (sys_signame[signo]) {
180 out1fmt(" %s\n", sys_signame[signo]);
181 } else {
182 out1fmt(" %d\n", signo);
183 }
184 }
185 }
186 return 0;
187 }
188 action = NULL;
189 if (*argv && !is_number(*argv)) {
190 if (strcmp(*argv, "-") == 0)
191 argv++;
192 else {
193 action = *argv;
194 argv++;
195 }
196 }
197 for (; *argv; argv++) {
198 if ((signo = sigstring_to_signum(*argv)) == -1) {
199 warning("bad signal %s", *argv);
200 errors = 1;
201 continue;
202 }
203 INTOFF;
204 if (action)
205 action = savestr(action);
206 if (trap[signo])
207 ckfree(trap[signo]);
208 trap[signo] = action;
209 if (signo != 0)
210 setsignal(signo);
211 INTON;
212 }
213 return errors;
214}
215
216
217/*
218 * Clear traps on a fork.
219 */
220void
221clear_traps(void)
222{
223 char *volatile *tp;
224
225 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
226 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
227 INTOFF;
228 ckfree(*tp);
229 *tp = NULL;
230 if (tp != &trap[0])
231 setsignal(tp - trap);
232 INTON;
233 }
234 }
235}
236
237
238/*
239 * Check if we have any traps enabled.
240 */
241int
242have_traps(void)
243{
244 char *volatile *tp;
245
246 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
247 if (*tp && **tp) /* trap not NULL or SIG_IGN */
248 return 1;
249 }
250 return 0;
251}
252
253/*
254 * Set the signal handler for the specified signal. The routine figures
255 * out what it should be set to.
256 */
257void
258setsignal(int signo)
259{
260 int action;
261 sig_t sigact = SIG_DFL;
262 struct sigaction sa;
263 char *t;
264
265 if ((t = trap[signo]) == NULL)
266 action = S_DFL;
267 else if (*t != '\0')
268 action = S_CATCH;
269 else
270 action = S_IGN;
271 if (action == S_DFL) {
272 switch (signo) {
273 case SIGINT:
274 action = S_CATCH;
275 break;
276 case SIGQUIT:
277#ifdef DEBUG
278 {
279 extern int debug;
280
281 if (debug)
282 break;
283 }
284#endif
285 action = S_CATCH;
286 break;
287 case SIGTERM:
288 if (rootshell && iflag)
289 action = S_IGN;
290 break;
291#if JOBS
292 case SIGTSTP:
293 case SIGTTOU:
294 if (rootshell && mflag)
295 action = S_IGN;
296 break;
297#endif
298 }
299 }
300
301 t = &sigmode[signo];
302 if (*t == 0) {
303 /*
304 * current setting unknown
305 */
306 if (!getsigaction(signo, &sigact)) {
307 /*
308 * Pretend it worked; maybe we should give a warning
309 * here, but other shells don't. We don't alter
310 * sigmode, so that we retry every time.
311 */
312 return;
313 }
314 if (sigact == SIG_IGN) {
315 if (mflag && (signo == SIGTSTP ||
316 signo == SIGTTIN || signo == SIGTTOU)) {
317 *t = S_IGN; /* don't hard ignore these */
318 } else
319 *t = S_HARD_IGN;
320 } else {
321 *t = S_RESET; /* force to be set */
322 }
323 }
324 if (*t == S_HARD_IGN || *t == action)
325 return;
326 switch (action) {
327 case S_DFL: sigact = SIG_DFL; break;
328 case S_CATCH: sigact = onsig; break;
329 case S_IGN: sigact = SIG_IGN; break;
330 }
331 *t = action;
332 sa.sa_handler = sigact;
333 sa.sa_flags = 0;
334 sigemptyset(&sa.sa_mask);
335 sigaction(signo, &sa, NULL);
336}
337
338
339/*
340 * Return the current setting for sig w/o changing it.
341 */
342static int
343getsigaction(int signo, sig_t *sigact)
344{
345 struct sigaction sa;
346
347 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
348 return 0;
349 *sigact = (sig_t) sa.sa_handler;
350 return 1;
351}
352
353
354/*
355 * Ignore a signal.
356 */
357void
358ignoresig(int signo)
359{
360
361 if (sigmode[signo] == 0)
362 setsignal(signo);
363 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
364 signal(signo, SIG_IGN);
365 sigmode[signo] = S_IGN;
366 }
367}
368
369
370int
371issigchldtrapped(void)
372{
373
374 return (trap[SIGCHLD] != NULL && *trap[SIGCHLD] != '\0');
375}
376
377
378/*
379 * Signal handler.
380 */
381void
382onsig(int signo)
383{
384
385 if (signo == SIGINT && trap[SIGINT] == NULL) {
386 /*
387 * The !in_dotrap here is safe. The only way we can arrive
388 * here with in_dotrap set is that a trap handler set SIGINT to
389 * SIG_DFL and killed itself.
390 */
391 if (suppressint && !in_dotrap)
392 SET_PENDING_INT;
393 else
394 onint();
395 return;
396 }
397
398 /* If we are currently in a wait builtin, prepare to break it */
399 if (signo == SIGINT || signo == SIGQUIT)
400 pendingsig_waitcmd = signo;
401
402 if (trap[signo] != NULL && trap[signo][0] != '\0' &&
403 (signo != SIGCHLD || !ignore_sigchld)) {
404 gotsig[signo] = 1;
405 pendingsig = signo;
406 pendingsig_waitcmd = signo;
407 }
408}
409
410
411/*
412 * Called to execute a trap. Perhaps we should avoid entering new trap
413 * handlers while we are executing a trap handler.
414 */
415void
416dotrap(void)
417{
deb63bfb 418 struct stackmark smark;
71aad674
A
419 int i;
420 int savestatus, prev_evalskip, prev_skipcount;
421
422 in_dotrap++;
423 for (;;) {
424 pendingsig = 0;
425 pendingsig_waitcmd = 0;
426 for (i = 1; i < NSIG; i++) {
427 if (gotsig[i]) {
428 gotsig[i] = 0;
429 if (trap[i]) {
430 /*
431 * Ignore SIGCHLD to avoid infinite
432 * recursion if the trap action does
433 * a fork.
434 */
435 if (i == SIGCHLD)
436 ignore_sigchld++;
437
438 /*
439 * Backup current evalskip
440 * state and reset it before
441 * executing a trap, so that the
442 * trap is not disturbed by an
443 * ongoing break/continue/return
444 * statement.
445 */
446 prev_evalskip = evalskip;
447 prev_skipcount = skipcount;
448 evalskip = 0;
449
450 last_trapsig = i;
451 savestatus = exitstatus;
deb63bfb
A
452 setstackmark(&smark);
453 evalstring(stsavestr(trap[i]), 0);
454 popstackmark(&smark);
71aad674
A
455
456 /*
457 * If such a command was not
458 * already in progress, allow a
459 * break/continue/return in the
460 * trap action to have an effect
461 * outside of it.
462 */
463 if (evalskip == 0 ||
464 prev_evalskip != 0) {
465 evalskip = prev_evalskip;
466 skipcount = prev_skipcount;
467 exitstatus = savestatus;
468 }
469
470 if (i == SIGCHLD)
471 ignore_sigchld--;
472 }
473 break;
474 }
475 }
476 if (i >= NSIG)
477 break;
478 }
479 in_dotrap--;
480}
481
482
483/*
484 * Controls whether the shell is interactive or not.
485 */
486void
487setinteractive(int on)
488{
489 static int is_interactive = -1;
490
491 if (on == is_interactive)
492 return;
493 setsignal(SIGINT);
494 setsignal(SIGQUIT);
495 setsignal(SIGTERM);
496 is_interactive = on;
497}
498
499
500/*
501 * Called to exit the shell.
502 */
503void
504exitshell(int status)
505{
506 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
507 exiting = 1;
508 exiting_exitstatus = status;
509 exitshell_savedstatus();
510}
511
512void
513exitshell_savedstatus(void)
514{
515 struct jmploc loc1, loc2;
516 char *p;
517 int sig = 0;
518 sigset_t sigs;
519
520 if (!exiting) {
521 if (in_dotrap && last_trapsig) {
522 sig = last_trapsig;
523 exiting_exitstatus = sig + 128;
524 } else
525 exiting_exitstatus = oexitstatus;
526 }
527 exitstatus = oexitstatus = exiting_exitstatus;
528 if (!setjmp(loc1.loc)) {
529 handler = &loc1;
530 if ((p = trap[0]) != NULL && *p != '\0') {
531 /*
532 * Reset evalskip, or the trap on EXIT could be
533 * interrupted if the last command was a "return".
534 */
535 evalskip = 0;
536 trap[0] = NULL;
537 evalstring(p, 0);
538 }
539 }
540 if (!setjmp(loc2.loc)) {
541 handler = &loc2; /* probably unnecessary */
542 flushall();
543#if JOBS
544 setjobctl(0);
545#endif
546 }
547 if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN &&
548 sig != SIGTTOU) {
549 signal(sig, SIG_DFL);
550 sigemptyset(&sigs);
551 sigaddset(&sigs, sig);
552 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
553 kill(getpid(), sig);
554 /* If the default action is to ignore, fall back to _exit(). */
555 }
556 _exit(exiting_exitstatus);
557}