2000-05-22 14:18:46 +04:00
|
|
|
/* $NetBSD: trap.c,v 1.24 2000/05/22 10:18:47 elric Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-05-11 21:09:42 +04:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-05 01:01:48 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
1995-06-07 08:16:57 +04:00
|
|
|
static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
|
1995-03-21 12:01:59 +03:00
|
|
|
#else
|
2000-05-22 14:18:46 +04:00
|
|
|
__RCSID("$NetBSD: trap.c,v 1.24 2000/05/22 10:18:47 elric Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1995-05-12 01:28:33 +04:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "shell.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "nodes.h" /* for other headers */
|
|
|
|
#include "eval.h"
|
|
|
|
#include "jobs.h"
|
1995-05-12 01:28:33 +04:00
|
|
|
#include "show.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "options.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "trap.h"
|
|
|
|
#include "mystring.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sigmode records the current value of the signal handlers for the various
|
|
|
|
* modes. A value of zero means that the current handler is not known.
|
|
|
|
* S_HARD_IGN indicates that the signal was ignored on entry to the shell,
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define S_DFL 1 /* default signal handling (SIG_DFL) */
|
|
|
|
#define S_CATCH 2 /* signal is caught */
|
|
|
|
#define S_IGN 3 /* signal is ignored (SIG_IGN) */
|
|
|
|
#define S_HARD_IGN 4 /* signal is ignored permenantly */
|
1994-05-11 21:09:42 +04:00
|
|
|
#define S_RESET 5 /* temporary - to reset a hard ignored sig */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
extern char nullstr[1]; /* null string */
|
|
|
|
|
1994-05-13 03:55:26 +04:00
|
|
|
char *trap[NSIG+1]; /* trap handler commands */
|
|
|
|
MKINIT char sigmode[NSIG]; /* current value of signal */
|
|
|
|
char gotsig[NSIG]; /* indicates specified signal received */
|
1994-05-11 21:09:42 +04:00
|
|
|
int pendingsigs; /* indicates some signal received */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-06-07 08:16:57 +04:00
|
|
|
static int getsigaction __P((int, sig_t *));
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* The trap builtin.
|
|
|
|
*/
|
|
|
|
|
1994-12-05 22:07:32 +03:00
|
|
|
int
|
|
|
|
trapcmd(argc, argv)
|
|
|
|
int argc;
|
1996-10-16 19:45:03 +04:00
|
|
|
char **argv;
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
char *action;
|
|
|
|
char **ap;
|
|
|
|
int signo;
|
|
|
|
|
|
|
|
if (argc <= 1) {
|
1994-05-13 03:55:26 +04:00
|
|
|
for (signo = 0 ; signo <= NSIG ; signo++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (trap[signo] != NULL)
|
|
|
|
out1fmt("%d: %s\n", signo, trap[signo]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ap = argv + 1;
|
|
|
|
if (is_number(*ap))
|
|
|
|
action = NULL;
|
|
|
|
else
|
|
|
|
action = *ap++;
|
|
|
|
while (*ap) {
|
1994-05-13 03:55:26 +04:00
|
|
|
if ((signo = number(*ap)) < 0 || signo > NSIG)
|
1993-03-21 12:45:37 +03:00
|
|
|
error("%s: bad trap", *ap);
|
|
|
|
INTOFF;
|
|
|
|
if (action)
|
|
|
|
action = savestr(action);
|
|
|
|
if (trap[signo])
|
|
|
|
ckfree(trap[signo]);
|
|
|
|
trap[signo] = action;
|
|
|
|
if (signo != 0)
|
2000-05-22 14:18:46 +04:00
|
|
|
setsignal(signo);
|
1993-03-21 12:45:37 +03:00
|
|
|
INTON;
|
|
|
|
ap++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2000-05-22 14:18:46 +04:00
|
|
|
* Clear traps on a fork.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2000-05-22 14:18:46 +04:00
|
|
|
clear_traps() {
|
1993-03-21 12:45:37 +03:00
|
|
|
char **tp;
|
|
|
|
|
1994-05-13 03:55:26 +04:00
|
|
|
for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*tp && **tp) { /* trap not NULL or SIG_IGN */
|
|
|
|
INTOFF;
|
2000-05-22 14:18:46 +04:00
|
|
|
ckfree(*tp);
|
|
|
|
*tp = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (tp != &trap[0])
|
2000-05-22 14:18:46 +04:00
|
|
|
setsignal(tp - trap);
|
1993-03-21 12:45:37 +03:00
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the signal handler for the specified signal. The routine figures
|
|
|
|
* out what it should be set to.
|
|
|
|
*/
|
|
|
|
|
1994-12-23 16:24:39 +03:00
|
|
|
long
|
2000-05-22 14:18:46 +04:00
|
|
|
setsignal(signo)
|
1994-12-05 22:07:32 +03:00
|
|
|
int signo;
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int action;
|
1995-05-12 01:28:33 +04:00
|
|
|
sig_t sigact = SIG_DFL;
|
2000-05-22 14:18:46 +04:00
|
|
|
char *t;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if ((t = trap[signo]) == NULL)
|
|
|
|
action = S_DFL;
|
|
|
|
else if (*t != '\0')
|
|
|
|
action = S_CATCH;
|
|
|
|
else
|
|
|
|
action = S_IGN;
|
2000-05-22 14:18:46 +04:00
|
|
|
if (rootshell && action == S_DFL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (signo) {
|
|
|
|
case SIGINT:
|
1999-03-27 16:46:19 +03:00
|
|
|
if (iflag || minusc || sflag == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
action = S_CATCH;
|
|
|
|
break;
|
|
|
|
case SIGQUIT:
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
extern int debug;
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SIGTERM:
|
|
|
|
if (iflag)
|
|
|
|
action = S_IGN;
|
|
|
|
break;
|
|
|
|
#if JOBS
|
|
|
|
case SIGTSTP:
|
|
|
|
case SIGTTOU:
|
1994-05-11 21:09:42 +04:00
|
|
|
if (mflag)
|
1993-03-21 12:45:37 +03:00
|
|
|
action = S_IGN;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
1995-06-05 18:24:33 +04:00
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
t = &sigmode[signo - 1];
|
2000-05-22 14:18:46 +04:00
|
|
|
if (*t == 0) {
|
1996-10-16 19:45:03 +04:00
|
|
|
/*
|
|
|
|
* current setting unknown
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1995-06-07 08:16:57 +04:00
|
|
|
if (!getsigaction(signo, &sigact)) {
|
|
|
|
/*
|
|
|
|
* Pretend it worked; maybe we should give a warning
|
|
|
|
* here, but other shells don't. We don't alter
|
|
|
|
* sigmode, so that we retry every time.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (sigact == SIG_IGN) {
|
1996-10-16 19:45:03 +04:00
|
|
|
if (mflag && (signo == SIGTSTP ||
|
1994-05-11 21:09:42 +04:00
|
|
|
signo == SIGTTIN || signo == SIGTTOU)) {
|
2000-05-22 14:18:46 +04:00
|
|
|
*t = S_IGN; /* don't hard ignore these */
|
1994-05-11 21:09:42 +04:00
|
|
|
} else
|
2000-05-22 14:18:46 +04:00
|
|
|
*t = S_HARD_IGN;
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
2000-05-22 14:18:46 +04:00
|
|
|
*t = S_RESET; /* force to be set */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
2000-05-22 14:18:46 +04:00
|
|
|
if (*t == S_HARD_IGN || *t == action)
|
1993-03-21 12:45:37 +03:00
|
|
|
return 0;
|
|
|
|
switch (action) {
|
|
|
|
case S_DFL: sigact = SIG_DFL; break;
|
|
|
|
case S_CATCH: sigact = onsig; break;
|
|
|
|
case S_IGN: sigact = SIG_IGN; break;
|
|
|
|
}
|
2000-05-22 14:18:46 +04:00
|
|
|
*t = action;
|
1999-01-18 19:18:04 +03:00
|
|
|
siginterrupt(signo, 1);
|
1994-12-23 16:24:39 +03:00
|
|
|
return (long)signal(signo, sigact);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
/*
|
|
|
|
* Return the current setting for sig w/o changing it.
|
|
|
|
*/
|
1995-06-07 08:16:57 +04:00
|
|
|
static int
|
1996-10-16 19:45:03 +04:00
|
|
|
getsigaction(signo, sigact)
|
1994-12-05 22:07:32 +03:00
|
|
|
int signo;
|
1995-06-07 08:16:57 +04:00
|
|
|
sig_t *sigact;
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
|
1995-06-07 08:16:57 +04:00
|
|
|
return 0;
|
|
|
|
*sigact = (sig_t) sa.sa_handler;
|
|
|
|
return 1;
|
1994-05-11 21:09:42 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ignore a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2000-05-22 14:18:46 +04:00
|
|
|
ignoresig(signo)
|
1994-12-05 22:07:32 +03:00
|
|
|
int signo;
|
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
|
1993-03-21 12:45:37 +03:00
|
|
|
signal(signo, SIG_IGN);
|
|
|
|
}
|
2000-05-22 14:18:46 +04:00
|
|
|
sigmode[signo - 1] = S_HARD_IGN;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef mkinit
|
1994-05-13 03:55:26 +04:00
|
|
|
INCLUDE <signal.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
INCLUDE "trap.h"
|
|
|
|
|
|
|
|
SHELLPROC {
|
|
|
|
char *sm;
|
|
|
|
|
2000-05-22 14:18:46 +04:00
|
|
|
clear_traps();
|
1994-05-13 03:55:26 +04:00
|
|
|
for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*sm == S_IGN)
|
|
|
|
*sm = S_HARD_IGN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Signal handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
1996-10-16 19:45:03 +04:00
|
|
|
onsig(signo)
|
1994-12-05 22:07:32 +03:00
|
|
|
int signo;
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
signal(signo, onsig);
|
|
|
|
if (signo == SIGINT && trap[SIGINT] == NULL) {
|
|
|
|
onint();
|
|
|
|
return;
|
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
gotsig[signo - 1] = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
pendingsigs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to execute a trap. Perhaps we should avoid entering new trap
|
|
|
|
* handlers while we are executing a trap handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
dotrap() {
|
|
|
|
int i;
|
|
|
|
int savestatus;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
for (i = 1 ; ; i++) {
|
1994-05-11 21:09:42 +04:00
|
|
|
if (gotsig[i - 1])
|
1993-08-07 01:50:14 +04:00
|
|
|
break;
|
1994-05-13 03:55:26 +04:00
|
|
|
if (i >= NSIG)
|
1994-05-11 21:09:42 +04:00
|
|
|
goto done;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
gotsig[i - 1] = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
savestatus=exitstatus;
|
2000-01-28 02:39:38 +03:00
|
|
|
evalstring(trap[i], 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
exitstatus=savestatus;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
pendingsigs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Controls whether the shell is interactive or not.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1994-12-05 22:07:32 +03:00
|
|
|
setinteractive(on)
|
|
|
|
int on;
|
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
static int is_interactive;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (on == is_interactive)
|
|
|
|
return;
|
2000-05-22 14:18:46 +04:00
|
|
|
setsignal(SIGINT);
|
|
|
|
setsignal(SIGQUIT);
|
|
|
|
setsignal(SIGTERM);
|
1993-03-21 12:45:37 +03:00
|
|
|
is_interactive = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to exit the shell.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
1996-10-16 19:45:03 +04:00
|
|
|
exitshell(status)
|
1994-12-05 22:07:32 +03:00
|
|
|
int status;
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct jmploc loc1, loc2;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
|
1994-05-11 21:09:42 +04:00
|
|
|
if (setjmp(loc1.loc)) {
|
|
|
|
goto l1;
|
|
|
|
}
|
|
|
|
if (setjmp(loc2.loc)) {
|
|
|
|
goto l2;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
handler = &loc1;
|
|
|
|
if ((p = trap[0]) != NULL && *p != '\0') {
|
|
|
|
trap[0] = NULL;
|
2000-01-28 02:39:38 +03:00
|
|
|
evalstring(p, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
l1: handler = &loc2; /* probably unnecessary */
|
|
|
|
flushall();
|
|
|
|
#if JOBS
|
|
|
|
setjobctl(0);
|
|
|
|
#endif
|
|
|
|
l2: _exit(status);
|
1998-07-28 09:31:22 +04:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|