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