1
/* $NetBSD: trap.c,v 1.30 2003/08/26 18:13:25 jmmv Exp $ */
4
* Copyright (c) 1991, 1993
5
* The Regents of the University of California. All rights reserved.
7
* This code is derived from software contributed to Berkeley by
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. Neither the name of the University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
#include <sys/cdefs.h>
43
static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
45
__RCSID("$NetBSD: trap.c,v 1.30 2003/08/26 18:13:25 jmmv Exp $");
55
#include "nodes.h" /* for other headers */
68
typedef __sighandler_t sig_t;
71
#define sys_signame sys_siglist
75
* Sigmode records the current value of the signal handlers for the various
76
* modes. A value of zero means that the current handler is not known.
77
* S_HARD_IGN indicates that the signal was ignored on entry to the shell,
80
#define S_DFL 1 /* default signal handling (SIG_DFL) */
81
#define S_CATCH 2 /* signal is caught */
82
#define S_IGN 3 /* signal is ignored (SIG_IGN) */
83
#define S_HARD_IGN 4 /* signal is ignored permenantly */
84
#define S_RESET 5 /* temporary - to reset a hard ignored sig */
87
char *trap[NSIG+1]; /* trap handler commands */
88
MKINIT char sigmode[NSIG]; /* current value of signal */
89
char gotsig[NSIG]; /* indicates specified signal received */
90
int pendingsigs; /* indicates some signal received */
92
static int getsigaction(int, sig_t *);
95
* return the signal number described by `p' (as a number or a name)
96
* or -1 if it isn't one
100
signame_to_signum(const char *p)
107
if (strcasecmp(p, "exit") == 0 )
110
if (strncasecmp(p, "sig", 3) == 0)
113
for (i = 0; i < NSIG; ++i)
115
if (strcasecmp (p, strsignal(i)) == 0)
117
if (strcasecmp (p, sys_signame[i]) == 0)
124
* Print a list of valid signal names
133
for (n = 1; n < NSIG; n++) {
135
out1fmt("%s", strsignal(n));
137
out1fmt("%s", sys_signame[n]);
139
if ((n == NSIG/2) || n == (NSIG - 1))
151
trapcmd(int argc, char **argv)
158
for (signo = 0 ; signo <= NSIG ; signo++) {
159
if (trap[signo] != NULL)
161
out1fmt("trap -- '%s' %s\n", trap[signo],
162
(signo) ? strsignal(signo) : "EXIT");
164
out1fmt("trap -- '%s' %s\n", trap[signo],
165
(signo) ? sys_signame[signo] : "EXIT");
174
if (strcmp(*ap, "--") == 0)
178
if (signame_to_signum(*ap) == -1) {
179
if ((*ap)[0] == '-') {
180
if ((*ap)[1] == '\0')
182
else if ((*ap)[1] == 'l' && (*ap)[2] == '\0') {
187
error("bad option %s\n", *ap);
197
signo = signame_to_signum(*ap);
199
if (signo < 0 || signo > NSIG)
200
error("%s: bad trap", *ap);
204
action = savestr(action);
209
trap[signo] = action;
222
* Clear traps on a fork or vfork.
223
* Takes one arg vfork, to tell it to not be destructive of
224
* the parents variables.
228
clear_traps(int vforked)
232
for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
233
if (*tp && **tp) { /* trap not NULL or SIG_IGN */
240
setsignal(tp - trap, vforked);
249
* Set the signal handler for the specified signal. The routine figures
250
* out what it should be set to.
254
setsignal(int signo, int vforked)
257
sig_t sigact = SIG_DFL;
259
struct sigaction act, oact;
263
if ((t = trap[signo]) == NULL)
269
if (rootshell && !vforked && action == S_DFL) {
272
if (iflag || minusc || sflag == 0)
295
t = &sigmode[signo - 1];
299
* current setting unknown
301
if (!getsigaction(signo, &sigact)) {
303
* Pretend it worked; maybe we should give a warning
304
* here, but other shells don't. We don't alter
305
* sigmode, so that we retry every time.
309
if (sigact == SIG_IGN) {
310
if (mflag && (signo == SIGTSTP ||
311
signo == SIGTTIN || signo == SIGTTOU)) {
312
tsig = S_IGN; /* don't hard ignore these */
316
tsig = S_RESET; /* force to be set */
319
if (tsig == S_HARD_IGN || tsig == action)
322
case S_DFL: sigact = SIG_DFL; break;
323
case S_CATCH: sigact = onsig; break;
324
case S_IGN: sigact = SIG_IGN; break;
329
act.sa_handler = sigact;
330
sigemptyset(&act.sa_mask);
333
act.sa_flags |= SA_INTERRUPT;
335
if (sigaction(signo, &act, &oact) < 0)
336
return (long)SIG_ERR;
337
return (long)oact.sa_handler;
339
siginterrupt(signo, 1);
340
return (long)signal(signo, sigact);
345
* Return the current setting for sig w/o changing it.
348
getsigaction(int signo, sig_t *sigact)
352
if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
354
*sigact = (sig_t) sa.sa_handler;
363
ignoresig(int signo, int vforked)
365
if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
366
bsd_signal(signo, SIG_IGN);
369
sigmode[signo - 1] = S_HARD_IGN;
381
for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
393
* The __cdecl is to work around the fact that the Linux/i386 kernel prior
394
* to 2.6.9-rc2 didn't pass the proper arguments to regparm'd signal handlers.
399
bsd_signal(signo, onsig);
400
if (signo == SIGINT && trap[SIGINT] == NULL) {
404
gotsig[signo - 1] = 1;
411
* Called to execute a trap. Perhaps we should avoid entering new trap
412
* handlers while we are executing a trap handler.
422
for (i = 1 ; ; i++) {
429
savestatus=exitstatus;
430
evalstring(trap[i], 0);
431
exitstatus=savestatus;
440
* Controls whether the shell is interactive or not.
445
setinteractive(int on)
447
static int is_interactive;
449
if (on == is_interactive)
451
setsignal(SIGINT, 0);
452
setsignal(SIGQUIT, 0);
453
setsignal(SIGTERM, 0);
460
* Called to exit the shell.
464
exitshell(int status)
466
struct jmploc loc1, loc2;
469
TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
470
if (setjmp(loc1.loc)) {
473
if (setjmp(loc2.loc)) {
477
if ((p = trap[0]) != NULL && *p != '\0') {
481
l1: handler = &loc2; /* probably unnecessary */