diff options
author | Jilles Tjoelker <jilles@stack.nl> | 2010-05-27 16:08:00 +0800 |
---|---|---|
committer | maximilian attems <max@stro.at> | 2011-06-03 18:44:12 +0200 |
commit | 405d07c99529e043366ce429e0289002c529033d (patch) | |
tree | 530607e96486ddba77bce4f62103b4b27f03267b | |
parent | 69b47734e1b758315cddac7e2e37c72b3b59f178 (diff) | |
download | klibc-405d07c99529e043366ce429e0289002c529033d.tar.gz |
[klibc] [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 <jilles@stack.nl>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
[ trivial trap.h merge fixup -maks ]
Signed-off-by: maximilian attems <max@stro.at>
-rw-r--r-- | usr/dash/eval.c | 4 | ||||
-rw-r--r-- | usr/dash/trap.c | 19 | ||||
-rw-r--r-- | usr/dash/trap.h | 7 |
3 files changed, 22 insertions, 8 deletions
diff --git a/usr/dash/eval.c b/usr/dash/eval.c index 3cd2c5f9d90f7..13c845a3337ef 100644 --- a/usr/dash/eval.c +++ b/usr/dash/eval.c @@ -449,7 +449,7 @@ evalsubshell(union node *n, int flags) int status; expredir(n->nredir.redirect); - if (!backgnd && flags & EV_EXIT && !trap[0]) + if (!backgnd && flags & EV_EXIT && !have_traps()) goto nofork; INTOFF; jp = makejob(n, 1); @@ -836,7 +836,7 @@ bail: switch (cmdentry.cmdtype) { default: /* Fork off a child process if necessary. */ - if (!(flags & EV_EXIT) || trap[0]) { + if (!(flags & EV_EXIT) || have_traps()) { INTOFF; jp = makejob(cmd, 1); if (forkshell(jp, cmd, FORK_FG) != 0) { diff --git a/usr/dash/trap.c b/usr/dash/trap.c index 18c3cd125c18b..6116cffe5426b 100644 --- a/usr/dash/trap.c +++ b/usr/dash/trap.c @@ -70,7 +70,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 */ @@ -124,11 +126,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); @@ -149,16 +157,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; } diff --git a/usr/dash/trap.h b/usr/dash/trap.h index e696f91ddd943..a3b190ae12e0c 100644 --- a/usr/dash/trap.h +++ b/usr/dash/trap.h @@ -36,7 +36,7 @@ #include <signal.h> -extern char *trap[]; +extern int trapcnt; extern char sigmode[]; extern volatile sig_atomic_t pendingsigs; @@ -50,3 +50,8 @@ void setinteractive(int); void exitshell(void) __attribute__((__noreturn__)); int decode_signal(const char *, int); const char *signal_name(int); + +static inline int have_traps(void) +{ + return trapcnt; +} |