summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2011-01-16 13:56:41 +0000
committerjilles <jilles@FreeBSD.org>2011-01-16 13:56:41 +0000
commit3967e15d578a2539a47faa095aceabb2aa7fee9b (patch)
tree610b187759dee5d5b104edeac20a38f14fa0728b /bin
parent5ca779f63c301bfc79e2b5c70e9149eebd39f8a7 (diff)
downloadFreeBSD-src-3967e15d578a2539a47faa095aceabb2aa7fee9b.zip
FreeBSD-src-3967e15d578a2539a47faa095aceabb2aa7fee9b.tar.gz
sh: If exit is used without args from a trap action, exit on the signal.
This is useful so that it is easier to exit on a signal than to reset the trap to default and resend the signal. It matches ksh93. POSIX says that 'exit' without args from a trap action uses the exit status from the last command before the trap, which is different from 'exit $?' and matches this if the previous command is assumed to have exited on the signal. If the signal is SIGSTOP, SIGTSTP, SIGTTIN or SIGTTOU, or if the default action for the signal is to ignore it, a normal _exit(2) is done with exit status 128+signal_number.
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/trap.c27
1 files changed, 23 insertions, 4 deletions
diff --git a/bin/sh/trap.c b/bin/sh/trap.c
index d2e5308..048c75c 100644
--- a/bin/sh/trap.c
+++ b/bin/sh/trap.c
@@ -79,6 +79,7 @@ static volatile sig_atomic_t gotsig[NSIG];
/* indicates specified signal received */
static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
volatile sig_atomic_t gotwinch;
+static int last_trapsig;
static int exiting; /* exitshell() has been called */
static int exiting_exitstatus; /* value passed to exitshell() */
@@ -441,6 +442,7 @@ dotrap(void)
*/
if (i == SIGCHLD)
ignore_sigchld++;
+ last_trapsig = i;
savestatus = exitstatus;
evalstring(trap[i], 0);
exitstatus = savestatus;
@@ -495,9 +497,16 @@ exitshell_savedstatus(void)
{
struct jmploc loc1, loc2;
char *p;
-
- if (!exiting)
- exiting_exitstatus = oexitstatus;
+ int sig = 0;
+ sigset_t sigs;
+
+ if (!exiting) {
+ if (in_dotrap && last_trapsig) {
+ sig = last_trapsig;
+ exiting_exitstatus = sig + 128;
+ } else
+ exiting_exitstatus = oexitstatus;
+ }
exitstatus = oexitstatus = exiting_exitstatus;
if (setjmp(loc1.loc)) {
goto l1;
@@ -515,5 +524,15 @@ l1: handler = &loc2; /* probably unnecessary */
#if JOBS
setjobctl(0);
#endif
-l2: _exit(exiting_exitstatus);
+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);
}
OpenPOWER on IntegriCloud