1*0a6a1f1dSLionel Sambuc /* $NetBSD: trap.c,v 1.37 2015/08/22 12:12:47 christos Exp $ */
2d90bee97SLionel Sambuc
3d90bee97SLionel Sambuc /*-
4d90bee97SLionel Sambuc * Copyright (c) 1991, 1993
5d90bee97SLionel Sambuc * The Regents of the University of California. All rights reserved.
6d90bee97SLionel Sambuc *
7d90bee97SLionel Sambuc * This code is derived from software contributed to Berkeley by
8d90bee97SLionel Sambuc * Kenneth Almquist.
9d90bee97SLionel Sambuc *
10d90bee97SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11d90bee97SLionel Sambuc * modification, are permitted provided that the following conditions
12d90bee97SLionel Sambuc * are met:
13d90bee97SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14d90bee97SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15d90bee97SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16d90bee97SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17d90bee97SLionel Sambuc * documentation and/or other materials provided with the distribution.
18d90bee97SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
19d90bee97SLionel Sambuc * may be used to endorse or promote products derived from this software
20d90bee97SLionel Sambuc * without specific prior written permission.
21d90bee97SLionel Sambuc *
22d90bee97SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23d90bee97SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24d90bee97SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25d90bee97SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26d90bee97SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27d90bee97SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28d90bee97SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29d90bee97SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30d90bee97SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31d90bee97SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32d90bee97SLionel Sambuc * SUCH DAMAGE.
33d90bee97SLionel Sambuc */
34d90bee97SLionel Sambuc
35d90bee97SLionel Sambuc #include <sys/cdefs.h>
36d90bee97SLionel Sambuc #ifndef lint
37d90bee97SLionel Sambuc #if 0
38d90bee97SLionel Sambuc static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
39d90bee97SLionel Sambuc #else
40*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: trap.c,v 1.37 2015/08/22 12:12:47 christos Exp $");
41d90bee97SLionel Sambuc #endif
42d90bee97SLionel Sambuc #endif /* not lint */
43d90bee97SLionel Sambuc
44d90bee97SLionel Sambuc #include <signal.h>
45d90bee97SLionel Sambuc #include <unistd.h>
46d90bee97SLionel Sambuc #include <stdlib.h>
47d90bee97SLionel Sambuc
48d90bee97SLionel Sambuc #include "shell.h"
49d90bee97SLionel Sambuc #include "main.h"
50d90bee97SLionel Sambuc #include "nodes.h" /* for other headers */
51d90bee97SLionel Sambuc #include "eval.h"
52d90bee97SLionel Sambuc #include "jobs.h"
53d90bee97SLionel Sambuc #include "show.h"
54d90bee97SLionel Sambuc #include "options.h"
55d90bee97SLionel Sambuc #include "builtins.h"
56d90bee97SLionel Sambuc #include "syntax.h"
57d90bee97SLionel Sambuc #include "output.h"
58d90bee97SLionel Sambuc #include "memalloc.h"
59d90bee97SLionel Sambuc #include "error.h"
60d90bee97SLionel Sambuc #include "trap.h"
61d90bee97SLionel Sambuc #include "mystring.h"
62d90bee97SLionel Sambuc #include "var.h"
63d90bee97SLionel Sambuc
64d90bee97SLionel Sambuc
65d90bee97SLionel Sambuc /*
66d90bee97SLionel Sambuc * Sigmode records the current value of the signal handlers for the various
67d90bee97SLionel Sambuc * modes. A value of zero means that the current handler is not known.
68d90bee97SLionel Sambuc * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
69d90bee97SLionel Sambuc */
70d90bee97SLionel Sambuc
71d90bee97SLionel Sambuc #define S_DFL 1 /* default signal handling (SIG_DFL) */
72d90bee97SLionel Sambuc #define S_CATCH 2 /* signal is caught */
73d90bee97SLionel Sambuc #define S_IGN 3 /* signal is ignored (SIG_IGN) */
74d90bee97SLionel Sambuc #define S_HARD_IGN 4 /* signal is ignored permenantly */
75d90bee97SLionel Sambuc #define S_RESET 5 /* temporary - to reset a hard ignored sig */
76d90bee97SLionel Sambuc
77d90bee97SLionel Sambuc
78d90bee97SLionel Sambuc char *trap[NSIG+1]; /* trap handler commands */
79d90bee97SLionel Sambuc MKINIT char sigmode[NSIG]; /* current value of signal */
80*0a6a1f1dSLionel Sambuc static volatile char gotsig[NSIG];/* indicates specified signal received */
81*0a6a1f1dSLionel Sambuc volatile int pendingsigs; /* indicates some signal received */
82d90bee97SLionel Sambuc
83d90bee97SLionel Sambuc static int getsigaction(int, sig_t *);
84d90bee97SLionel Sambuc
85d90bee97SLionel Sambuc /*
86d90bee97SLionel Sambuc * return the signal number described by `p' (as a number or a name)
87d90bee97SLionel Sambuc * or -1 if it isn't one
88d90bee97SLionel Sambuc */
89d90bee97SLionel Sambuc
90d90bee97SLionel Sambuc static int
signame_to_signum(const char * p)91d90bee97SLionel Sambuc signame_to_signum(const char *p)
92d90bee97SLionel Sambuc {
93d90bee97SLionel Sambuc int i;
94d90bee97SLionel Sambuc
95d90bee97SLionel Sambuc if (is_number(p))
96d90bee97SLionel Sambuc return number(p);
97d90bee97SLionel Sambuc
98d90bee97SLionel Sambuc if (strcasecmp(p, "exit") == 0 )
99d90bee97SLionel Sambuc return 0;
100d90bee97SLionel Sambuc
101d90bee97SLionel Sambuc if (strncasecmp(p, "sig", 3) == 0)
102d90bee97SLionel Sambuc p += 3;
103d90bee97SLionel Sambuc
104d90bee97SLionel Sambuc for (i = 0; i < NSIG; ++i)
105d90bee97SLionel Sambuc if (strcasecmp (p, sys_signame[i]) == 0)
106d90bee97SLionel Sambuc return i;
107d90bee97SLionel Sambuc return -1;
108d90bee97SLionel Sambuc }
109d90bee97SLionel Sambuc
110d90bee97SLionel Sambuc /*
111d90bee97SLionel Sambuc * Print a list of valid signal names
112d90bee97SLionel Sambuc */
113d90bee97SLionel Sambuc static void
printsignals(void)114d90bee97SLionel Sambuc printsignals(void)
115d90bee97SLionel Sambuc {
116d90bee97SLionel Sambuc int n;
117d90bee97SLionel Sambuc
118d90bee97SLionel Sambuc out1str("EXIT ");
119d90bee97SLionel Sambuc
120d90bee97SLionel Sambuc for (n = 1; n < NSIG; n++) {
121d90bee97SLionel Sambuc out1fmt("%s", sys_signame[n]);
122d90bee97SLionel Sambuc if ((n == NSIG/2) || n == (NSIG - 1))
123d90bee97SLionel Sambuc out1str("\n");
124d90bee97SLionel Sambuc else
125d90bee97SLionel Sambuc out1c(' ');
126d90bee97SLionel Sambuc }
127d90bee97SLionel Sambuc }
128d90bee97SLionel Sambuc
129d90bee97SLionel Sambuc /*
130d90bee97SLionel Sambuc * The trap builtin.
131d90bee97SLionel Sambuc */
132d90bee97SLionel Sambuc
133d90bee97SLionel Sambuc int
trapcmd(int argc,char ** argv)134d90bee97SLionel Sambuc trapcmd(int argc, char **argv)
135d90bee97SLionel Sambuc {
136d90bee97SLionel Sambuc char *action;
137d90bee97SLionel Sambuc char **ap;
138d90bee97SLionel Sambuc int signo;
139d90bee97SLionel Sambuc
140d90bee97SLionel Sambuc if (argc <= 1) {
141d90bee97SLionel Sambuc for (signo = 0 ; signo <= NSIG ; signo++)
142d90bee97SLionel Sambuc if (trap[signo] != NULL) {
143d90bee97SLionel Sambuc out1fmt("trap -- ");
144d90bee97SLionel Sambuc print_quoted(trap[signo]);
145d90bee97SLionel Sambuc out1fmt(" %s\n",
146d90bee97SLionel Sambuc (signo) ? sys_signame[signo] : "EXIT");
147d90bee97SLionel Sambuc }
148d90bee97SLionel Sambuc return 0;
149d90bee97SLionel Sambuc }
150d90bee97SLionel Sambuc ap = argv + 1;
151d90bee97SLionel Sambuc
152d90bee97SLionel Sambuc action = NULL;
153d90bee97SLionel Sambuc
154d90bee97SLionel Sambuc if (strcmp(*ap, "--") == 0)
155d90bee97SLionel Sambuc if (*++ap == NULL)
156d90bee97SLionel Sambuc return 0;
157d90bee97SLionel Sambuc
158d90bee97SLionel Sambuc if (signame_to_signum(*ap) == -1) {
159d90bee97SLionel Sambuc if ((*ap)[0] == '-') {
160d90bee97SLionel Sambuc if ((*ap)[1] == '\0')
161d90bee97SLionel Sambuc ap++;
162d90bee97SLionel Sambuc else if ((*ap)[1] == 'l' && (*ap)[2] == '\0') {
163d90bee97SLionel Sambuc printsignals();
164d90bee97SLionel Sambuc return 0;
165d90bee97SLionel Sambuc }
166d90bee97SLionel Sambuc else
167d90bee97SLionel Sambuc error("bad option %s\n", *ap);
168d90bee97SLionel Sambuc }
169d90bee97SLionel Sambuc else
170d90bee97SLionel Sambuc action = *ap++;
171d90bee97SLionel Sambuc }
172d90bee97SLionel Sambuc
173d90bee97SLionel Sambuc while (*ap) {
174d90bee97SLionel Sambuc if (is_number(*ap))
175d90bee97SLionel Sambuc signo = number(*ap);
176d90bee97SLionel Sambuc else
177d90bee97SLionel Sambuc signo = signame_to_signum(*ap);
178d90bee97SLionel Sambuc
179d90bee97SLionel Sambuc if (signo < 0 || signo > NSIG)
180d90bee97SLionel Sambuc error("%s: bad trap", *ap);
181d90bee97SLionel Sambuc
182d90bee97SLionel Sambuc INTOFF;
183d90bee97SLionel Sambuc if (action)
184d90bee97SLionel Sambuc action = savestr(action);
185d90bee97SLionel Sambuc
186d90bee97SLionel Sambuc if (trap[signo])
187d90bee97SLionel Sambuc ckfree(trap[signo]);
188d90bee97SLionel Sambuc
189d90bee97SLionel Sambuc trap[signo] = action;
190d90bee97SLionel Sambuc
191d90bee97SLionel Sambuc if (signo != 0)
192d90bee97SLionel Sambuc setsignal(signo, 0);
193d90bee97SLionel Sambuc INTON;
194d90bee97SLionel Sambuc ap++;
195d90bee97SLionel Sambuc }
196d90bee97SLionel Sambuc return 0;
197d90bee97SLionel Sambuc }
198d90bee97SLionel Sambuc
199d90bee97SLionel Sambuc
200d90bee97SLionel Sambuc
201d90bee97SLionel Sambuc /*
202d90bee97SLionel Sambuc * Clear traps on a fork or vfork.
203d90bee97SLionel Sambuc * Takes one arg vfork, to tell it to not be destructive of
204d90bee97SLionel Sambuc * the parents variables.
205d90bee97SLionel Sambuc */
206d90bee97SLionel Sambuc
207d90bee97SLionel Sambuc void
clear_traps(int vforked)208d90bee97SLionel Sambuc clear_traps(int vforked)
209d90bee97SLionel Sambuc {
210d90bee97SLionel Sambuc char **tp;
211d90bee97SLionel Sambuc
212d90bee97SLionel Sambuc for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
213d90bee97SLionel Sambuc if (*tp && **tp) { /* trap not NULL or SIG_IGN */
214d90bee97SLionel Sambuc INTOFF;
215d90bee97SLionel Sambuc if (!vforked) {
216d90bee97SLionel Sambuc ckfree(*tp);
217d90bee97SLionel Sambuc *tp = NULL;
218d90bee97SLionel Sambuc }
219d90bee97SLionel Sambuc if (tp != &trap[0])
220d90bee97SLionel Sambuc setsignal(tp - trap, vforked);
221d90bee97SLionel Sambuc INTON;
222d90bee97SLionel Sambuc }
223d90bee97SLionel Sambuc }
224d90bee97SLionel Sambuc }
225d90bee97SLionel Sambuc
226d90bee97SLionel Sambuc
227d90bee97SLionel Sambuc
228d90bee97SLionel Sambuc /*
229d90bee97SLionel Sambuc * Set the signal handler for the specified signal. The routine figures
230d90bee97SLionel Sambuc * out what it should be set to.
231d90bee97SLionel Sambuc */
232d90bee97SLionel Sambuc
233d90bee97SLionel Sambuc sig_t
setsignal(int signo,int vforked)234d90bee97SLionel Sambuc setsignal(int signo, int vforked)
235d90bee97SLionel Sambuc {
236d90bee97SLionel Sambuc int action;
237d90bee97SLionel Sambuc sig_t sigact = SIG_DFL, sig;
238d90bee97SLionel Sambuc char *t, tsig;
239d90bee97SLionel Sambuc
240d90bee97SLionel Sambuc if ((t = trap[signo]) == NULL)
241d90bee97SLionel Sambuc action = S_DFL;
242d90bee97SLionel Sambuc else if (*t != '\0')
243d90bee97SLionel Sambuc action = S_CATCH;
244d90bee97SLionel Sambuc else
245d90bee97SLionel Sambuc action = S_IGN;
246d90bee97SLionel Sambuc if (rootshell && !vforked && action == S_DFL) {
247d90bee97SLionel Sambuc switch (signo) {
248d90bee97SLionel Sambuc case SIGINT:
249d90bee97SLionel Sambuc if (iflag || minusc || sflag == 0)
250d90bee97SLionel Sambuc action = S_CATCH;
251d90bee97SLionel Sambuc break;
252d90bee97SLionel Sambuc case SIGQUIT:
253d90bee97SLionel Sambuc #ifdef DEBUG
254d90bee97SLionel Sambuc if (debug)
255d90bee97SLionel Sambuc break;
256d90bee97SLionel Sambuc #endif
257d90bee97SLionel Sambuc /* FALLTHROUGH */
258d90bee97SLionel Sambuc case SIGTERM:
259d90bee97SLionel Sambuc if (iflag)
260d90bee97SLionel Sambuc action = S_IGN;
261d90bee97SLionel Sambuc break;
262d90bee97SLionel Sambuc #if JOBS
263d90bee97SLionel Sambuc case SIGTSTP:
264d90bee97SLionel Sambuc case SIGTTOU:
265d90bee97SLionel Sambuc if (mflag)
266d90bee97SLionel Sambuc action = S_IGN;
267d90bee97SLionel Sambuc break;
268d90bee97SLionel Sambuc #endif
269d90bee97SLionel Sambuc }
270d90bee97SLionel Sambuc }
271d90bee97SLionel Sambuc
272d90bee97SLionel Sambuc t = &sigmode[signo - 1];
273d90bee97SLionel Sambuc tsig = *t;
274d90bee97SLionel Sambuc if (tsig == 0) {
275d90bee97SLionel Sambuc /*
276d90bee97SLionel Sambuc * current setting unknown
277d90bee97SLionel Sambuc */
278d90bee97SLionel Sambuc if (!getsigaction(signo, &sigact)) {
279d90bee97SLionel Sambuc /*
280d90bee97SLionel Sambuc * Pretend it worked; maybe we should give a warning
281d90bee97SLionel Sambuc * here, but other shells don't. We don't alter
282d90bee97SLionel Sambuc * sigmode, so that we retry every time.
283d90bee97SLionel Sambuc */
284d90bee97SLionel Sambuc return 0;
285d90bee97SLionel Sambuc }
286d90bee97SLionel Sambuc if (sigact == SIG_IGN) {
287d90bee97SLionel Sambuc /*
288d90bee97SLionel Sambuc * POSIX 3.14.13 states that non-interactive shells
289d90bee97SLionel Sambuc * should ignore trap commands for signals that were
290d90bee97SLionel Sambuc * ignored upon entry, and leaves the behavior
291d90bee97SLionel Sambuc * unspecified for interactive shells. On interactive
292d90bee97SLionel Sambuc * shells, or if job control is on, and we have a job
293d90bee97SLionel Sambuc * control related signal, we allow the trap to work.
294d90bee97SLionel Sambuc *
295d90bee97SLionel Sambuc * This change allows us to be POSIX compliant, and
296d90bee97SLionel Sambuc * at the same time override the default behavior if
297d90bee97SLionel Sambuc * we need to by setting the interactive flag.
298d90bee97SLionel Sambuc */
299d90bee97SLionel Sambuc if ((mflag && (signo == SIGTSTP ||
300d90bee97SLionel Sambuc signo == SIGTTIN || signo == SIGTTOU)) || iflag) {
301d90bee97SLionel Sambuc tsig = S_IGN;
302d90bee97SLionel Sambuc } else
303d90bee97SLionel Sambuc tsig = S_HARD_IGN;
304d90bee97SLionel Sambuc } else {
305d90bee97SLionel Sambuc tsig = S_RESET; /* force to be set */
306d90bee97SLionel Sambuc }
307d90bee97SLionel Sambuc }
308d90bee97SLionel Sambuc if (tsig == S_HARD_IGN || tsig == action)
309d90bee97SLionel Sambuc return 0;
310d90bee97SLionel Sambuc switch (action) {
311d90bee97SLionel Sambuc case S_DFL: sigact = SIG_DFL; break;
312d90bee97SLionel Sambuc case S_CATCH: sigact = onsig; break;
313d90bee97SLionel Sambuc case S_IGN: sigact = SIG_IGN; break;
314d90bee97SLionel Sambuc }
315d90bee97SLionel Sambuc sig = signal(signo, sigact);
316d90bee97SLionel Sambuc if (sig != SIG_ERR) {
317d90bee97SLionel Sambuc sigset_t ss;
318d90bee97SLionel Sambuc if (!vforked)
319d90bee97SLionel Sambuc *t = action;
320d90bee97SLionel Sambuc if (action == S_CATCH)
321d90bee97SLionel Sambuc (void)siginterrupt(signo, 1);
322d90bee97SLionel Sambuc /*
323d90bee97SLionel Sambuc * If our parent accidentally blocked signals for
324d90bee97SLionel Sambuc * us make sure we unblock them
325d90bee97SLionel Sambuc */
326d90bee97SLionel Sambuc (void)sigemptyset(&ss);
327d90bee97SLionel Sambuc (void)sigaddset(&ss, signo);
328d90bee97SLionel Sambuc (void)sigprocmask(SIG_UNBLOCK, &ss, NULL);
329d90bee97SLionel Sambuc }
330d90bee97SLionel Sambuc return sig;
331d90bee97SLionel Sambuc }
332d90bee97SLionel Sambuc
333d90bee97SLionel Sambuc /*
334d90bee97SLionel Sambuc * Return the current setting for sig w/o changing it.
335d90bee97SLionel Sambuc */
336d90bee97SLionel Sambuc static int
getsigaction(int signo,sig_t * sigact)337d90bee97SLionel Sambuc getsigaction(int signo, sig_t *sigact)
338d90bee97SLionel Sambuc {
339d90bee97SLionel Sambuc struct sigaction sa;
340d90bee97SLionel Sambuc
341d90bee97SLionel Sambuc if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
342d90bee97SLionel Sambuc return 0;
343d90bee97SLionel Sambuc *sigact = (sig_t) sa.sa_handler;
344d90bee97SLionel Sambuc return 1;
345d90bee97SLionel Sambuc }
346d90bee97SLionel Sambuc
347d90bee97SLionel Sambuc /*
348d90bee97SLionel Sambuc * Ignore a signal.
349d90bee97SLionel Sambuc */
350d90bee97SLionel Sambuc
351d90bee97SLionel Sambuc void
ignoresig(int signo,int vforked)352d90bee97SLionel Sambuc ignoresig(int signo, int vforked)
353d90bee97SLionel Sambuc {
354d90bee97SLionel Sambuc if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
355d90bee97SLionel Sambuc signal(signo, SIG_IGN);
356d90bee97SLionel Sambuc }
357d90bee97SLionel Sambuc if (!vforked)
358d90bee97SLionel Sambuc sigmode[signo - 1] = S_HARD_IGN;
359d90bee97SLionel Sambuc }
360d90bee97SLionel Sambuc
361d90bee97SLionel Sambuc
362d90bee97SLionel Sambuc #ifdef mkinit
363d90bee97SLionel Sambuc INCLUDE <signal.h>
364d90bee97SLionel Sambuc INCLUDE "trap.h"
365d90bee97SLionel Sambuc
366d90bee97SLionel Sambuc SHELLPROC {
367d90bee97SLionel Sambuc char *sm;
368d90bee97SLionel Sambuc
369d90bee97SLionel Sambuc clear_traps(0);
370d90bee97SLionel Sambuc for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
371d90bee97SLionel Sambuc if (*sm == S_IGN)
372d90bee97SLionel Sambuc *sm = S_HARD_IGN;
373d90bee97SLionel Sambuc }
374d90bee97SLionel Sambuc }
375d90bee97SLionel Sambuc #endif
376d90bee97SLionel Sambuc
377d90bee97SLionel Sambuc
378d90bee97SLionel Sambuc
379d90bee97SLionel Sambuc /*
380d90bee97SLionel Sambuc * Signal handler.
381d90bee97SLionel Sambuc */
382d90bee97SLionel Sambuc
383d90bee97SLionel Sambuc void
onsig(int signo)384d90bee97SLionel Sambuc onsig(int signo)
385d90bee97SLionel Sambuc {
386d90bee97SLionel Sambuc signal(signo, onsig);
387d90bee97SLionel Sambuc if (signo == SIGINT && trap[SIGINT] == NULL) {
388d90bee97SLionel Sambuc onint();
389d90bee97SLionel Sambuc return;
390d90bee97SLionel Sambuc }
391d90bee97SLionel Sambuc gotsig[signo - 1] = 1;
392d90bee97SLionel Sambuc pendingsigs++;
393d90bee97SLionel Sambuc }
394d90bee97SLionel Sambuc
395d90bee97SLionel Sambuc
396d90bee97SLionel Sambuc
397d90bee97SLionel Sambuc /*
398d90bee97SLionel Sambuc * Called to execute a trap. Perhaps we should avoid entering new trap
399d90bee97SLionel Sambuc * handlers while we are executing a trap handler.
400d90bee97SLionel Sambuc */
401d90bee97SLionel Sambuc
402d90bee97SLionel Sambuc void
dotrap(void)403d90bee97SLionel Sambuc dotrap(void)
404d90bee97SLionel Sambuc {
405d90bee97SLionel Sambuc int i;
406d90bee97SLionel Sambuc int savestatus;
407d90bee97SLionel Sambuc
408d90bee97SLionel Sambuc for (;;) {
409d90bee97SLionel Sambuc for (i = 1 ; ; i++) {
410d90bee97SLionel Sambuc if (gotsig[i - 1])
411d90bee97SLionel Sambuc break;
412d90bee97SLionel Sambuc if (i >= NSIG)
413d90bee97SLionel Sambuc goto done;
414d90bee97SLionel Sambuc }
415d90bee97SLionel Sambuc gotsig[i - 1] = 0;
416d90bee97SLionel Sambuc savestatus=exitstatus;
417d90bee97SLionel Sambuc evalstring(trap[i], 0);
418d90bee97SLionel Sambuc exitstatus=savestatus;
419d90bee97SLionel Sambuc }
420d90bee97SLionel Sambuc done:
421d90bee97SLionel Sambuc pendingsigs = 0;
422d90bee97SLionel Sambuc }
423d90bee97SLionel Sambuc
424*0a6a1f1dSLionel Sambuc int
lastsig(void)425*0a6a1f1dSLionel Sambuc lastsig(void)
426*0a6a1f1dSLionel Sambuc {
427*0a6a1f1dSLionel Sambuc int i;
428d90bee97SLionel Sambuc
429*0a6a1f1dSLionel Sambuc for (i = NSIG; i > 0; i--)
430*0a6a1f1dSLionel Sambuc if (gotsig[i - 1])
431*0a6a1f1dSLionel Sambuc return i;
432*0a6a1f1dSLionel Sambuc return SIGINT; /* XXX */
433*0a6a1f1dSLionel Sambuc }
434d90bee97SLionel Sambuc
435d90bee97SLionel Sambuc /*
436d90bee97SLionel Sambuc * Controls whether the shell is interactive or not.
437d90bee97SLionel Sambuc */
438d90bee97SLionel Sambuc
439d90bee97SLionel Sambuc
440d90bee97SLionel Sambuc void
setinteractive(int on)441d90bee97SLionel Sambuc setinteractive(int on)
442d90bee97SLionel Sambuc {
443d90bee97SLionel Sambuc static int is_interactive;
444d90bee97SLionel Sambuc
445d90bee97SLionel Sambuc if (on == is_interactive)
446d90bee97SLionel Sambuc return;
447d90bee97SLionel Sambuc setsignal(SIGINT, 0);
448d90bee97SLionel Sambuc setsignal(SIGQUIT, 0);
449d90bee97SLionel Sambuc setsignal(SIGTERM, 0);
450d90bee97SLionel Sambuc is_interactive = on;
451d90bee97SLionel Sambuc }
452d90bee97SLionel Sambuc
453d90bee97SLionel Sambuc
454d90bee97SLionel Sambuc
455d90bee97SLionel Sambuc /*
456d90bee97SLionel Sambuc * Called to exit the shell.
457d90bee97SLionel Sambuc */
458d90bee97SLionel Sambuc
459d90bee97SLionel Sambuc void
exitshell(int status)460d90bee97SLionel Sambuc exitshell(int status)
461d90bee97SLionel Sambuc {
462d90bee97SLionel Sambuc struct jmploc loc1, loc2;
463d90bee97SLionel Sambuc char *p;
464d90bee97SLionel Sambuc
465d90bee97SLionel Sambuc TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
466d90bee97SLionel Sambuc if (setjmp(loc1.loc)) {
467d90bee97SLionel Sambuc goto l1;
468d90bee97SLionel Sambuc }
469d90bee97SLionel Sambuc if (setjmp(loc2.loc)) {
470d90bee97SLionel Sambuc goto l2;
471d90bee97SLionel Sambuc }
472d90bee97SLionel Sambuc handler = &loc1;
473d90bee97SLionel Sambuc if ((p = trap[0]) != NULL && *p != '\0') {
474d90bee97SLionel Sambuc trap[0] = NULL;
475d90bee97SLionel Sambuc evalstring(p, 0);
476d90bee97SLionel Sambuc }
477d90bee97SLionel Sambuc l1: handler = &loc2; /* probably unnecessary */
478d90bee97SLionel Sambuc flushall();
479d90bee97SLionel Sambuc #if JOBS
480d90bee97SLionel Sambuc setjobctl(0);
481d90bee97SLionel Sambuc #endif
482d90bee97SLionel Sambuc l2: _exit(status);
483d90bee97SLionel Sambuc /* NOTREACHED */
484d90bee97SLionel Sambuc }
485