aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-09-23 03:25:52 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-09-23 03:25:52 +0200
commit844f9909264b7f46620e06b7116facba0a940667 (patch)
treec0a21470c20f3dcab9c61abbd2e2137172741f09 /shell
parent91836baf85ce6980a33c6ce38730928a4edc3c5a (diff)
downloadbusybox-844f9909264b7f46620e06b7116facba0a940667.tar.gz
ash: fix `trap`
function old new delta forkshell 738 810 +72 popstring 134 140 +6 parse_command 1460 1463 +3 evalvar 1373 1371 -2 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/1 up/down: 81/-2) Total: 79 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
-rw-r--r--shell/ash.c57
-rw-r--r--shell/ash_test/ash-signals/savetrap.right3
-rwxr-xr-xshell/ash_test/ash-signals/savetrap.tests6
-rw-r--r--shell/ash_test/ash-signals/signal1.right10
4 files changed, 68 insertions, 8 deletions
diff --git a/shell/ash.c b/shell/ash.c
index 4360770d4..68aa675e5 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -4553,7 +4553,52 @@ forkchild(struct job *jp, union node *n, int mode)
* Do we do it correctly? */
closescript();
- clear_traps();
+
+ if (mode == FORK_NOJOB /* is it `xxx` ? */
+ && n && n->type == NCMD /* is it single cmd? */
+ /* && n->ncmd.args->type == NARG - always true? */
+ && strcmp(n->ncmd.args->narg.text, "trap") == 0
+ && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */
+ /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */
+ ) {
+ TRACE(("Trap hack\n"));
+ /* Awful hack for `trap` or $(trap).
+ *
+ * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html
+ * contains an example where "trap" is executed in a subshell:
+ *
+ * save_traps=$(trap)
+ * ...
+ * eval "$save_traps"
+ *
+ * Standard does not say that "trap" in subshell shall print
+ * parent shell's traps. It only says that its output
+ * must have suitable form, but then, in the above example
+ * (which is not supposed to be normative), it implies that.
+ *
+ * bash (and probably other shell) does implement it
+ * (traps are reset to defaults, but "trap" still shows them),
+ * but as a result, "trap" logic is hopelessly messed up:
+ *
+ * # trap
+ * trap -- 'echo Ho' SIGWINCH <--- we have a handler
+ * # (trap) <--- trap is in subshell - no output (correct, traps are reset)
+ * # true | trap <--- trap is in subshell - no output (ditto)
+ * # echo `true | trap` <--- in subshell - output (but traps are reset!)
+ * trap -- 'echo Ho' SIGWINCH
+ * # echo `(trap)` <--- in subshell in subshell - output
+ * trap -- 'echo Ho' SIGWINCH
+ * # echo `true | (trap)` <--- in subshell in subshell in subshell - output!
+ * trap -- 'echo Ho' SIGWINCH
+ *
+ * The rules when to forget and when to not forget traps
+ * get really complex and nonsensical.
+ *
+ * Our solution: ONLY bare $(trap) or `trap` is special.
+ */
+ } else {
+ clear_traps();
+ }
#if JOBS
/* do job control only in root shell */
doing_jobctl = 0;
@@ -4597,8 +4642,14 @@ forkchild(struct job *jp, union node *n, int mode)
setsignal(SIGQUIT);
}
#if JOBS
- if (n && n->type == NCMD && strcmp(n->ncmd.args->narg.text, "jobs") == 0) {
+ if (n && n->type == NCMD
+ && strcmp(n->ncmd.args->narg.text, "jobs") == 0
+ ) {
TRACE(("Job hack\n"));
+ /* "jobs": we do not want to clear job list for it,
+ * instead we remove only _its_ own_ job from job list.
+ * This makes "jobs .... | cat" more useful.
+ */
freejob(curjob);
return;
}
@@ -12208,7 +12259,7 @@ trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
if (!*ap) {
for (signo = 0; signo < NSIG; signo++) {
if (trap[signo] != NULL) {
- out1fmt("trap -- %s %s\n",
+ out1fmt("trap -- %s SIG%s\n",
single_quote(trap[signo]),
get_signame(signo));
}
diff --git a/shell/ash_test/ash-signals/savetrap.right b/shell/ash_test/ash-signals/savetrap.right
new file mode 100644
index 000000000..2d33427aa
--- /dev/null
+++ b/shell/ash_test/ash-signals/savetrap.right
@@ -0,0 +1,3 @@
+trap -- 'echo WINCH!' SIGWINCH
+trap -- 'echo WINCH!' SIGWINCH
+Done
diff --git a/shell/ash_test/ash-signals/savetrap.tests b/shell/ash_test/ash-signals/savetrap.tests
new file mode 100755
index 000000000..6492e86a2
--- /dev/null
+++ b/shell/ash_test/ash-signals/savetrap.tests
@@ -0,0 +1,6 @@
+trap 'echo WINCH!' SIGWINCH
+v=` trap `
+echo $v
+v=`trap`
+echo $v
+echo Done
diff --git a/shell/ash_test/ash-signals/signal1.right b/shell/ash_test/ash-signals/signal1.right
index cf403ac62..beb0a988e 100644
--- a/shell/ash_test/ash-signals/signal1.right
+++ b/shell/ash_test/ash-signals/signal1.right
@@ -1,20 +1,20 @@
got signal
-trap -- 'echo got signal' USR1
+trap -- 'echo got signal' SIGUSR1
sent 1 signal
got signal
wait interrupted
-trap -- 'echo got signal' USR1
+trap -- 'echo got signal' SIGUSR1
sent 2 signal
got signal
wait interrupted
-trap -- 'echo got signal' USR1
+trap -- 'echo got signal' SIGUSR1
sent 3 signal
got signal
wait interrupted
-trap -- 'echo got signal' USR1
+trap -- 'echo got signal' SIGUSR1
sent 4 signal
got signal
wait interrupted
-trap -- 'echo got signal' USR1
+trap -- 'echo got signal' SIGUSR1
sent 5 signal
sleep completed