From f613f9573f044220453069ee81ed8706c6e18225 Mon Sep 17 00:00:00 2001 From: Herbert Xu Date: Sun, 19 Jan 2020 18:21:59 +0800 Subject: redir: Clear saved redirections in subshell When we enter a subshell we need to drop the saved redirections as otherwise a subsequent unwindredir could produce incorrect results. This patch does this by simply clearing redirlist. While we could actually free the memory underneath for subshells it isn't really worth the trouble for now. In order to ensure that this is done in every place where we enter a subshell, this patch adds a new mkinit hook called forkreset. The calls closescript, clear_traps and reset_handler are also added to the forkreset hook. This fixes a bug where the first two functions weren't called if we enter a subshell without forking. Reported-by: Harald van Dijk Signed-off-by: Herbert Xu --- src/trap.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) (limited to 'src/trap.c') diff --git a/src/trap.c b/src/trap.c index 58a7c60..82e7ece 100644 --- a/src/trap.c +++ b/src/trap.c @@ -66,7 +66,7 @@ /* trap handler commands */ -static char *trap[NSIG]; +MKINIT char *trap[NSIG]; /* number of non-null traps */ int trapcnt; /* current value of signal */ @@ -83,11 +83,29 @@ extern char *signal_names[]; static int decode_signum(const char *); #ifdef mkinit +INCLUDE "memalloc.h" INCLUDE "trap.h" + INIT { sigmode[SIGCHLD - 1] = S_DFL; setsignal(SIGCHLD); } + +FORKRESET { + char **tp; + + INTOFF; + for (tp = trap ; tp < &trap[NSIG] ; tp++) { + if (*tp && **tp) { /* trap not NULL or SIG_IGN */ + ckfree(*tp); + *tp = NULL; + if (tp != &trap[0]) + setsignal(tp - trap); + } + } + trapcnt = 0; + INTON; +} #endif /* @@ -150,30 +168,6 @@ trapcmd(int argc, char **argv) -/* - * Clear traps on a fork. - */ - -void -clear_traps(void) -{ - char **tp; - - INTOFF; - for (tp = trap ; tp < &trap[NSIG] ; tp++) { - if (*tp && **tp) { /* trap not NULL or SIG_IGN */ - ckfree(*tp); - *tp = NULL; - if (tp != &trap[0]) - setsignal(tp - trap); - } - } - trapcnt = 0; - INTON; -} - - - /* * Set the signal handler for the specified signal. The routine figures * out what it should be set to. -- cgit 1.4.1