From e2df5f46d02d170d1d8c9dfdd186ef23504109dc Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 26 May 2009 14:34:10 +0200 Subject: hush: rename last_waitpid_was_0 to we_have_children Signed-off-by: Denys Vlasenko --- shell/hush.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/shell/hush.c b/shell/hush.c index fd839d061..7360f0e4c 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -495,7 +495,7 @@ struct globals { #if ENABLE_HUSH_FAST unsigned count_SIGCHLD; unsigned handled_SIGCHLD; - smallint last_waitpid_was_0; + smallint we_have_children; #endif /* which signals have non-DFL handler (even with no traps set)? */ unsigned non_DFL_mask; @@ -3292,22 +3292,30 @@ static int checkjobs(struct pipe* fg_pipe) debug_printf_jobs("checkjobs %p\n", fg_pipe); + attributes = WUNTRACED; + if (fg_pipe == NULL) + attributes |= WNOHANG; + errno = 0; #if ENABLE_HUSH_FAST if (G.handled_SIGCHLD == G.count_SIGCHLD) { -//bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d was 0?:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.last_waitpid_was_0); - /* avoid doing syscall, nothing there anyway */ - if (G.last_waitpid_was_0) +//bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", +//getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); + /* There was heither fork nor SIGCHLD since last waitpid */ + /* Avoid doing syscall, nothing there anyway */ + if (!G.we_have_children) { + errno = ECHILD; + return -1; + } + if (fg_pipe == NULL) { /* is WNOHANG set? */ + /* We have children, but they did not exit + * or stop yet (we saw no SIGCHLD) */ return 0; - errno = ECHILD; - return -1; + } + /* else: !WNOHANG, waitpid will block, can't short-circuit */ } #endif - attributes = WUNTRACED; - if (fg_pipe == NULL) - attributes |= WNOHANG; - /* Do we do this right? * bash-3.00# sleep 20 | false * @@ -3330,7 +3338,7 @@ static int checkjobs(struct pipe* fg_pipe) bb_perror_msg("waitpid"); #if ENABLE_HUSH_FAST else { /* Until next SIGCHLD, waitpid's are useless */ - G.last_waitpid_was_0 = (childpid == 0); + G.we_have_children = (childpid == 0); G.handled_SIGCHLD = i; //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); } -- cgit v1.2.3