1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "shell.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "nodes.h" /* for other headers */
|
|
|
|
#include "eval.h"
|
|
|
|
#include "jobs.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "show.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "options.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "trap.h"
|
|
|
|
#include "mystring.h"
|
2011-06-13 21:03:27 +00:00
|
|
|
#include "builtins.h"
|
2002-07-23 15:05:00 +00:00
|
|
|
#include "myhistedit.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) */
|
1999-05-08 10:22:15 +00:00
|
|
|
#define S_HARD_IGN 4 /* signal is ignored permanently */
|
1994-05-26 06:18:55 +00:00
|
|
|
#define S_RESET 5 /* temporary - to reset a hard ignored sig */
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
MKINIT char sigmode[NSIG]; /* current value of signal */
|
1998-08-24 10:20:37 +00:00
|
|
|
int pendingsigs; /* indicates some signal received */
|
1998-08-25 09:33:34 +00:00
|
|
|
int in_dotrap; /* do we execute in a trap handler? */
|
1998-09-08 13:16:52 +00:00
|
|
|
static char *volatile trap[NSIG]; /* trap handler commands */
|
2006-04-17 17:55:11 +00:00
|
|
|
static volatile sig_atomic_t gotsig[NSIG];
|
1998-08-24 10:20:37 +00:00
|
|
|
/* indicates specified signal received */
|
1996-12-24 23:59:53 +00:00
|
|
|
static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
|
2002-07-24 02:06:07 +00:00
|
|
|
volatile sig_atomic_t gotwinch;
|
2011-01-16 13:56:41 +00:00
|
|
|
static int last_trapsig;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2011-01-08 23:08:13 +00:00
|
|
|
static int exiting; /* exitshell() has been called */
|
|
|
|
static int exiting_exitstatus; /* value passed to exitshell() */
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int getsigaction(int, sig_t *);
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1996-12-24 23:59:53 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
1996-12-24 23:59:53 +00:00
|
|
|
* Map a string to a signal number.
|
2004-02-12 05:03:37 +00:00
|
|
|
*
|
|
|
|
* Note: the signal number may exceed NSIG.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2002-02-02 06:50:57 +00:00
|
|
|
sigstring_to_signum(char *sig)
|
1996-12-24 23:59:53 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (is_number(sig)) {
|
|
|
|
int signo;
|
|
|
|
|
|
|
|
signo = atoi(sig);
|
|
|
|
return ((signo >= 0 && signo < NSIG) ? signo : (-1));
|
2011-02-04 16:40:50 +00:00
|
|
|
} else if (strcasecmp(sig, "EXIT") == 0) {
|
1996-12-24 23:59:53 +00:00
|
|
|
return (0);
|
|
|
|
} else {
|
|
|
|
int n;
|
|
|
|
|
2011-02-04 16:40:50 +00:00
|
|
|
if (strncasecmp(sig, "SIG", 3) == 0)
|
1996-12-24 23:59:53 +00:00
|
|
|
sig += 3;
|
2004-01-28 19:01:10 +00:00
|
|
|
for (n = 1; n < sys_nsig; n++)
|
2004-02-12 05:05:56 +00:00
|
|
|
if (sys_signame[n] &&
|
|
|
|
strcasecmp(sys_signame[n], sig) == 0)
|
1996-12-24 23:59:53 +00:00
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-12-24 23:59:53 +00:00
|
|
|
/*
|
|
|
|
* Print a list of valid signal names.
|
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
printsignals(void)
|
1996-12-24 23:59:53 +00:00
|
|
|
{
|
2004-02-12 05:03:37 +00:00
|
|
|
int n, outlen;
|
1996-12-24 23:59:53 +00:00
|
|
|
|
2004-02-12 05:03:37 +00:00
|
|
|
outlen = 0;
|
2004-01-28 19:01:10 +00:00
|
|
|
for (n = 1; n < sys_nsig; n++) {
|
2004-02-12 05:03:37 +00:00
|
|
|
if (sys_signame[n]) {
|
|
|
|
out1fmt("%s", sys_signame[n]);
|
|
|
|
outlen += strlen(sys_signame[n]);
|
|
|
|
} else {
|
|
|
|
out1fmt("%d", n);
|
|
|
|
outlen += 3; /* good enough */
|
|
|
|
}
|
|
|
|
++outlen;
|
2011-01-14 21:30:27 +00:00
|
|
|
if (outlen > 71 || n == sys_nsig - 1) {
|
1996-12-24 23:59:53 +00:00
|
|
|
out1str("\n");
|
2004-02-12 05:03:37 +00:00
|
|
|
outlen = 0;
|
|
|
|
} else {
|
1996-12-24 23:59:53 +00:00
|
|
|
out1c(' ');
|
2004-02-12 05:03:37 +00:00
|
|
|
}
|
1996-12-24 23:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The trap builtin.
|
|
|
|
*/
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
trapcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char *action;
|
|
|
|
int signo;
|
2009-11-21 20:44:34 +00:00
|
|
|
int errors = 0;
|
2011-01-15 21:09:00 +00:00
|
|
|
int i;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2011-01-15 21:09:00 +00:00
|
|
|
while ((i = nextopt("l")) != '\0') {
|
|
|
|
switch (i) {
|
|
|
|
case 'l':
|
|
|
|
printsignals();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argv = argptr;
|
|
|
|
|
|
|
|
if (*argv == NULL) {
|
2004-01-28 19:01:10 +00:00
|
|
|
for (signo = 0 ; signo < sys_nsig ; signo++) {
|
2004-02-12 05:03:37 +00:00
|
|
|
if (signo < NSIG && trap[signo] != NULL) {
|
2005-12-08 20:08:36 +00:00
|
|
|
out1str("trap -- ");
|
|
|
|
out1qstr(trap[signo]);
|
2004-02-12 05:03:37 +00:00
|
|
|
if (signo == 0) {
|
2011-02-04 16:40:50 +00:00
|
|
|
out1str(" EXIT\n");
|
2004-02-12 05:03:37 +00:00
|
|
|
} else if (sys_signame[signo]) {
|
2005-12-08 20:08:36 +00:00
|
|
|
out1fmt(" %s\n", sys_signame[signo]);
|
2004-02-12 05:03:37 +00:00
|
|
|
} else {
|
2005-12-08 20:08:36 +00:00
|
|
|
out1fmt(" %d\n", signo);
|
2004-02-12 05:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1996-12-24 23:59:53 +00:00
|
|
|
action = NULL;
|
|
|
|
if (*argv && sigstring_to_signum(*argv) == -1) {
|
2011-01-15 21:09:00 +00:00
|
|
|
if (strcmp(*argv, "-") == 0)
|
1996-12-24 23:59:53 +00:00
|
|
|
argv++;
|
2011-01-15 21:09:00 +00:00
|
|
|
else {
|
|
|
|
action = *argv;
|
1996-12-24 23:59:53 +00:00
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
}
|
2012-01-14 21:54:12 +00:00
|
|
|
for (; *argv; argv++) {
|
2009-11-21 20:44:34 +00:00
|
|
|
if ((signo = sigstring_to_signum(*argv)) == -1) {
|
2010-12-21 20:47:06 +00:00
|
|
|
warning("bad signal %s", *argv);
|
2009-11-21 20:44:34 +00:00
|
|
|
errors = 1;
|
2012-01-14 21:54:12 +00:00
|
|
|
continue;
|
2009-11-21 20:44:34 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
|
|
|
if (action)
|
|
|
|
action = savestr(action);
|
|
|
|
if (trap[signo])
|
|
|
|
ckfree(trap[signo]);
|
|
|
|
trap[signo] = action;
|
|
|
|
if (signo != 0)
|
|
|
|
setsignal(signo);
|
|
|
|
INTON;
|
|
|
|
}
|
2009-11-21 20:44:34 +00:00
|
|
|
return errors;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear traps on a fork.
|
|
|
|
*/
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
clear_traps(void)
|
1996-12-24 23:59:53 +00:00
|
|
|
{
|
1998-09-08 13:16:52 +00:00
|
|
|
char *volatile *tp;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-12-24 23:59:53 +00:00
|
|
|
for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*tp && **tp) { /* trap not NULL or SIG_IGN */
|
|
|
|
INTOFF;
|
|
|
|
ckfree(*tp);
|
|
|
|
*tp = NULL;
|
|
|
|
if (tp != &trap[0])
|
|
|
|
setsignal(tp - trap);
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-13 21:10:41 +00:00
|
|
|
/*
|
|
|
|
* Check if we have any traps enabled.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
have_traps(void)
|
|
|
|
{
|
|
|
|
char *volatile *tp;
|
|
|
|
|
|
|
|
for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
|
|
|
|
if (*tp && **tp) /* trap not NULL or SIG_IGN */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Set the signal handler for the specified signal. The routine figures
|
|
|
|
* out what it should be set to.
|
|
|
|
*/
|
1997-11-10 11:32:24 +00:00
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
setsignal(int signo)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int action;
|
2009-11-11 23:13:24 +00:00
|
|
|
sig_t sigact = SIG_DFL;
|
|
|
|
struct sigaction sa;
|
1994-05-26 06:18:55 +00:00
|
|
|
char *t;
|
|
|
|
|
|
|
|
if ((t = trap[signo]) == NULL)
|
|
|
|
action = S_DFL;
|
|
|
|
else if (*t != '\0')
|
|
|
|
action = S_CATCH;
|
|
|
|
else
|
|
|
|
action = S_IGN;
|
1998-08-24 10:20:37 +00:00
|
|
|
if (action == S_DFL) {
|
1994-05-26 06:18:55 +00:00
|
|
|
switch (signo) {
|
|
|
|
case SIGINT:
|
1998-08-24 10:20:37 +00:00
|
|
|
action = S_CATCH;
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
|
|
|
case SIGQUIT:
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
extern int debug;
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
1998-08-25 08:49:47 +00:00
|
|
|
action = S_CATCH;
|
1998-08-24 10:20:37 +00:00
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
case SIGTERM:
|
1998-08-24 10:20:37 +00:00
|
|
|
if (rootshell && iflag)
|
1994-05-26 06:18:55 +00:00
|
|
|
action = S_IGN;
|
|
|
|
break;
|
|
|
|
#if JOBS
|
|
|
|
case SIGTSTP:
|
|
|
|
case SIGTTOU:
|
1998-08-24 10:20:37 +00:00
|
|
|
if (rootshell && mflag)
|
1994-05-26 06:18:55 +00:00
|
|
|
action = S_IGN;
|
|
|
|
break;
|
2002-07-23 15:05:00 +00:00
|
|
|
#endif
|
|
|
|
#ifndef NO_HISTORY
|
|
|
|
case SIGWINCH:
|
2002-07-24 02:06:07 +00:00
|
|
|
if (rootshell && iflag)
|
2002-07-23 15:05:00 +00:00
|
|
|
action = S_CATCH;
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1996-12-24 23:59:53 +00:00
|
|
|
t = &sigmode[signo];
|
1995-05-30 00:07:29 +00:00
|
|
|
if (*t == 0) {
|
|
|
|
/*
|
|
|
|
* current setting unknown
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
1996-09-01 10:22:36 +00: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.
|
|
|
|
*/
|
1997-11-10 11:32:24 +00:00
|
|
|
return;
|
1996-09-01 10:22:36 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
if (sigact == SIG_IGN) {
|
1995-05-30 00:07:29 +00:00
|
|
|
if (mflag && (signo == SIGTSTP ||
|
1994-05-26 06:18:55 +00:00
|
|
|
signo == SIGTTIN || signo == SIGTTOU)) {
|
|
|
|
*t = S_IGN; /* don't hard ignore these */
|
|
|
|
} else
|
|
|
|
*t = S_HARD_IGN;
|
|
|
|
} else {
|
|
|
|
*t = S_RESET; /* force to be set */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*t == S_HARD_IGN || *t == action)
|
1997-11-10 11:32:24 +00:00
|
|
|
return;
|
1994-05-26 06:18:55 +00:00
|
|
|
switch (action) {
|
|
|
|
case S_DFL: sigact = SIG_DFL; break;
|
|
|
|
case S_CATCH: sigact = onsig; break;
|
|
|
|
case S_IGN: sigact = SIG_IGN; break;
|
|
|
|
}
|
|
|
|
*t = action;
|
2009-11-11 23:13:24 +00:00
|
|
|
sa.sa_handler = sigact;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(signo, &sa, NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
1996-12-24 23:59:53 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Return the current setting for sig w/o changing it.
|
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2002-02-02 06:50:57 +00:00
|
|
|
getsigaction(int signo, sig_t *sigact)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
|
1996-09-01 10:22:36 +00:00
|
|
|
return 0;
|
|
|
|
*sigact = (sig_t) sa.sa_handler;
|
|
|
|
return 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
1996-12-24 23:59:53 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Ignore a signal.
|
|
|
|
*/
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
ignoresig(int signo)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1996-12-24 23:59:53 +00:00
|
|
|
|
|
|
|
if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
|
1994-05-26 06:18:55 +00:00
|
|
|
signal(signo, SIG_IGN);
|
|
|
|
}
|
1996-12-24 23:59:53 +00:00
|
|
|
sigmode[signo] = S_HARD_IGN;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Signal handler.
|
|
|
|
*/
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
onsig(int signo)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1997-11-10 11:32:24 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if (signo == SIGINT && trap[SIGINT] == NULL) {
|
|
|
|
onint();
|
|
|
|
return;
|
|
|
|
}
|
1998-09-08 13:16:52 +00:00
|
|
|
|
1996-12-24 23:59:53 +00:00
|
|
|
if (signo != SIGCHLD || !ignore_sigchld)
|
|
|
|
gotsig[signo] = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
pendingsigs++;
|
1998-09-08 13:16:52 +00:00
|
|
|
|
|
|
|
/* If we are currently in a wait builtin, prepare to break it */
|
|
|
|
if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
|
1998-08-24 10:20:37 +00:00
|
|
|
breakwaitcmd = 1;
|
2006-04-17 17:55:11 +00:00
|
|
|
/*
|
|
|
|
* If a trap is set, not ignored and not the null command, we need
|
1998-09-10 22:09:11 +00:00
|
|
|
* to make sure traps are executed even when a child blocks signals.
|
1998-09-08 13:16:52 +00:00
|
|
|
*/
|
1999-04-01 13:27:36 +00:00
|
|
|
if (Tflag &&
|
2006-04-17 17:55:11 +00:00
|
|
|
trap[signo] != NULL &&
|
Various small code cleanups resulting from a code reviewing
and linting procedure:
1. Remove useless sub-expression:
- if (*start || (!ifsspc && start > string && (nulonly || 1))) {
+ if (*start || (!ifsspc && start > string)) {
The sub-expression "(nulonly || 1)" always evaluates to true and
according to CVS logs seems to be just a left-over from some
debugging and introduced by accident. Removing the sub-expression
doesn't change semantics and a code inspection showed that the
variable "nulonly" is also not necessary here in any way (and the
expression would require fixing instead of removing).
2. Remove dead code:
- if (backslash && c == '\\') {
- if (read(STDIN_FILENO, &c, 1) != 1) {
- status = 1;
- break;
- }
- STPUTC(c, p);
- } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
+ if (ap[1] != NULL && strchr(ifs, c) != NULL) {
Inspection of the control and data flow showed that variable
"backslash" is always false (0) when the "if"-expression is
evaluated, hence the whole block is effectively dead code.
Additionally, the skipping of characters after a backslash is already
performed correctly a few lines above, so this code is also not
needed at all. According to the CVS logs and the ASH 0.2 sources,
this code existed in this way already since its early days.
3. Cleanup Style:
- ! trap[signo][0] == '\0' &&
+ ! (trap[signo][0] == '\0') &&
The expression wants to ensure the trap is not assigned the empty
string. But the "!" operator has higher precedence than "==", so the
comparison should be put into parenthesis to form the intended way of
expression. Nevertheless the code was effectively not really broken
as both particular NUL comparisons are semantically equal, of course.
But the parenthesized version is a lot more intuitive.
4. Remove shadowing variable declaration:
- char *q;
The declaration of symbol "q" hides another identical declaration of
"q" in the same context. As the other "q" is already reused multiple
times and also can be reused again without negative side-effects,
just remove the shadowing declaration.
5. Just small cosmetics:
- if (ifsset() != 0)
+ if (ifsset())
The ifsset() macro is already coded by returning the boolean result
of a comparison operator, so no need to compare this boolean result
again against a numerical value. This also aligns the macros usage to
the remaining existing code.
Reviewed by: stefanf@
2005-09-06 19:30:00 +00:00
|
|
|
! (trap[signo][0] == '\0') &&
|
1998-09-10 14:51:06 +00:00
|
|
|
! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
|
|
|
|
breakwaitcmd = 1;
|
2002-07-23 15:05:00 +00:00
|
|
|
|
|
|
|
#ifndef NO_HISTORY
|
|
|
|
if (signo == SIGWINCH)
|
2002-07-24 02:06:07 +00:00
|
|
|
gotwinch = 1;
|
2002-07-23 15:05:00 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to execute a trap. Perhaps we should avoid entering new trap
|
|
|
|
* handlers while we are executing a trap handler.
|
|
|
|
*/
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
dotrap(void)
|
1996-12-24 23:59:53 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int i;
|
2012-01-16 11:07:46 +00:00
|
|
|
int savestatus, prev_evalskip, prev_skipcount;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1998-08-24 10:20:37 +00:00
|
|
|
in_dotrap++;
|
1994-05-26 06:18:55 +00:00
|
|
|
for (;;) {
|
1996-12-24 23:59:53 +00:00
|
|
|
for (i = 1; i < NSIG; i++) {
|
|
|
|
if (gotsig[i]) {
|
|
|
|
gotsig[i] = 0;
|
|
|
|
if (trap[i]) {
|
|
|
|
/*
|
2004-02-12 05:05:56 +00:00
|
|
|
* Ignore SIGCHLD to avoid infinite
|
|
|
|
* recursion if the trap action does
|
|
|
|
* a fork.
|
1996-12-24 23:59:53 +00:00
|
|
|
*/
|
|
|
|
if (i == SIGCHLD)
|
|
|
|
ignore_sigchld++;
|
2012-01-16 11:07:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Backup current evalskip
|
|
|
|
* state and reset it before
|
|
|
|
* executing a trap, so that the
|
|
|
|
* trap is not disturbed by an
|
|
|
|
* ongoing break/continue/return
|
|
|
|
* statement.
|
|
|
|
*/
|
|
|
|
prev_evalskip = evalskip;
|
|
|
|
prev_skipcount = skipcount;
|
|
|
|
evalskip = 0;
|
|
|
|
|
2011-01-16 13:56:41 +00:00
|
|
|
last_trapsig = i;
|
1996-12-24 23:59:53 +00:00
|
|
|
savestatus = exitstatus;
|
2009-05-31 12:36:14 +00:00
|
|
|
evalstring(trap[i], 0);
|
1996-12-24 23:59:53 +00:00
|
|
|
exitstatus = savestatus;
|
2012-01-16 11:07:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If such a command was not
|
|
|
|
* already in progress, allow a
|
|
|
|
* break/continue/return in the
|
|
|
|
* trap action to have an effect
|
|
|
|
* outside of it.
|
|
|
|
*/
|
|
|
|
if (prev_evalskip != 0) {
|
|
|
|
evalskip = prev_evalskip;
|
|
|
|
skipcount = prev_skipcount;
|
|
|
|
}
|
|
|
|
|
1996-12-24 23:59:53 +00:00
|
|
|
if (i == SIGCHLD)
|
|
|
|
ignore_sigchld--;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
1996-12-24 23:59:53 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
1996-12-24 23:59:53 +00:00
|
|
|
if (i >= NSIG)
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
1998-08-24 10:20:37 +00:00
|
|
|
in_dotrap--;
|
1994-05-26 06:18:55 +00:00
|
|
|
pendingsigs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Controls whether the shell is interactive or not.
|
|
|
|
*/
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
setinteractive(int on)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1998-08-24 10:20:37 +00:00
|
|
|
static int is_interactive = -1;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (on == is_interactive)
|
|
|
|
return;
|
|
|
|
setsignal(SIGINT);
|
|
|
|
setsignal(SIGQUIT);
|
|
|
|
setsignal(SIGTERM);
|
2002-07-23 15:05:00 +00:00
|
|
|
#ifndef NO_HISTORY
|
|
|
|
setsignal(SIGWINCH);
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
is_interactive = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to exit the shell.
|
|
|
|
*/
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
exitshell(int status)
|
2011-01-08 23:08:13 +00:00
|
|
|
{
|
|
|
|
TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
|
|
|
|
exiting = 1;
|
|
|
|
exiting_exitstatus = status;
|
|
|
|
exitshell_savedstatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
exitshell_savedstatus(void)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct jmploc loc1, loc2;
|
|
|
|
char *p;
|
2011-01-16 13:56:41 +00:00
|
|
|
int sig = 0;
|
|
|
|
sigset_t sigs;
|
|
|
|
|
|
|
|
if (!exiting) {
|
|
|
|
if (in_dotrap && last_trapsig) {
|
|
|
|
sig = last_trapsig;
|
|
|
|
exiting_exitstatus = sig + 128;
|
|
|
|
} else
|
|
|
|
exiting_exitstatus = oexitstatus;
|
|
|
|
}
|
2011-01-08 23:08:13 +00:00
|
|
|
exitstatus = oexitstatus = exiting_exitstatus;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (setjmp(loc1.loc)) {
|
|
|
|
goto l1;
|
|
|
|
}
|
|
|
|
if (setjmp(loc2.loc)) {
|
|
|
|
goto l2;
|
|
|
|
}
|
|
|
|
handler = &loc1;
|
|
|
|
if ((p = trap[0]) != NULL && *p != '\0') {
|
2012-01-16 11:07:46 +00:00
|
|
|
/*
|
|
|
|
* Reset evalskip, or the trap on EXIT could be
|
|
|
|
* interrupted if the last command was a "return".
|
|
|
|
*/
|
|
|
|
evalskip = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
trap[0] = NULL;
|
2009-05-31 12:36:14 +00:00
|
|
|
evalstring(p, 0);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
l1: handler = &loc2; /* probably unnecessary */
|
|
|
|
flushall();
|
|
|
|
#if JOBS
|
|
|
|
setjobctl(0);
|
|
|
|
#endif
|
2011-01-16 13:56:41 +00:00
|
|
|
l2:
|
|
|
|
if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN &&
|
|
|
|
sig != SIGTTOU) {
|
|
|
|
signal(sig, SIG_DFL);
|
|
|
|
sigemptyset(&sigs);
|
|
|
|
sigaddset(&sigs, sig);
|
|
|
|
sigprocmask(SIG_UNBLOCK, &sigs, NULL);
|
|
|
|
kill(getpid(), sig);
|
|
|
|
/* If the default action is to ignore, fall back to _exit(). */
|
|
|
|
}
|
|
|
|
_exit(exiting_exitstatus);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|