hush: rename last_waitpid_was_0 to we_have_children
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 26 May 2009 12:34:10 +0000 (14:34 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 26 May 2009 12:34:10 +0000 (14:34 +0200)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/hush.c

index fd839d0612d18515389cc0275107b64901d7c354..7360f0e4cdd993dcabe0771fa834b7c07511286f 100644 (file)
@@ -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
  * <ctrl-Z pressed>
@@ -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);
                        }