From 699170f4c835d8ec8e310f58c34c6d158dd37d71 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Thu, 27 May 2010 16:08:00 +0800 Subject: [EVAL] Force fork if any trap is set, not just on EXIT In some cases the shell executes a subshell or an external command in the current process. This is not done if a trap on EXIT has been set, so that that trap can execute after the subshell or external command has finished. Extend that check to all traps. (A trap is "set" if a non-empty command string has been attached to it.) Improve encapsulation by exporting an accessor function for this and making the trap array static again. This is much like FreeBSD SVN r194127, enhanced to apply to subshells also (see FreeBSD SVN r194774). Example: dash -c '{ trap "echo moo" TERM; sleep 3; }& sleep 1; kill $!;wait' This should print "moo" after 3 seconds. Example: dash -c '{ trap "echo moo" TERM; (sleep 3) }& sleep 1; kill $!;wait' The same. Example: dash -c '{ trap "echo moo" TERM; sleep 3; :; }& sleep 1; kill $!;wait' This works correctly even without this patch. Signed-off-by: Jilles Tjoelker Signed-off-by: Herbert Xu --- src/trap.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/trap.c') diff --git a/src/trap.c b/src/trap.c index 3f93c46..ba32da6 100644 --- a/src/trap.c +++ b/src/trap.c @@ -69,7 +69,9 @@ /* trap handler commands */ -char *trap[NSIG]; +static char *trap[NSIG]; +/* number of non-null traps */ +int trapcnt; /* current value of signal */ char sigmode[NSIG - 1]; /* indicates specified signal received */ @@ -125,11 +127,17 @@ trapcmd(int argc, char **argv) if (action) { if (action[0] == '-' && action[1] == '\0') action = NULL; - else + else { + if (*action) + trapcnt++; action = savestr(action); + } } - if (trap[signo]) + if (trap[signo]) { + if (*trap[signo]) + trapcnt--; ckfree(trap[signo]); + } trap[signo] = action; if (signo != 0) setsignal(signo); @@ -150,16 +158,17 @@ clear_traps(void) { char **tp; + INTOFF; for (tp = trap ; tp < &trap[NSIG] ; tp++) { if (*tp && **tp) { /* trap not NULL or SIG_IGN */ - INTOFF; ckfree(*tp); *tp = NULL; if (tp != &trap[0]) setsignal(tp - trap); - INTON; } } + trapcnt = 0; + INTON; } -- cgit 1.4.1