ash: jobs - Do not block when waiting on SIGCHLD
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 18 Feb 2020 13:37:56 +0000 (14:37 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 18 Feb 2020 14:37:22 +0000 (15:37 +0100)
Upstream comment:

    Date: Mon, 7 May 2018 00:40:34 +0800
    jobs - Do not block when waiting on SIGCHLD

    Because of the nature of SIGCHLD, the process may have already been
    waited on and therefore we must be prepared for the case that wait
    may block.  So ensure that it doesn't by using WNOHANG.

    Furthermore, multiple jobs may have exited when gotsigchld is set.
    Therefore we need to wait until there are no zombies left.

    Lastly, waitforjob needs to be called with interrupts off and
    the original patch broke that.

    Fixes: 03876c0743a5 ("eval: Reap zombies after built-in...")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
While at it, removed INT_ON/OFF in waitforjob() - it must be called
from INT_OFF region anyway.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash.c

index 8047cf98f39d3efd42ea4fadfb3005723b4d0e71..2b13786943735bfe93f22ac425d4a33bf1dd7589 100644 (file)
@@ -5357,8 +5357,16 @@ waitforjob(struct job *jp)
 
        TRACE(("waitforjob(%%%d) called\n", jp ? jobno(jp) : 0));
 
-       INT_OFF;
-       while ((jp && jp->state == JOBRUNNING) || got_sigchld) {
+       if (!jp) {
+               int pid = got_sigchld;
+
+               while (pid > 0)
+                       pid = dowait(DOWAIT_NONBLOCK, NULL);
+
+               return exitstatus;
+       }
+
+       while (jp->state == JOBRUNNING) {
                /* In non-interactive shells, we _can_ get
                 * a keyboard signal here and be EINTRed,
                 * but we just loop back, waiting for command to complete.
@@ -5391,10 +5399,7 @@ waitforjob(struct job *jp)
                 */
                dowait(DOWAIT_BLOCK, jp);
        }
-       INT_ON;
 
-       if (!jp)
-               return exitstatus;
        st = getstatus(jp);
 #if JOBS
        if (jp->jobctl) {
@@ -10369,7 +10374,6 @@ evalcommand(union node *cmd, int flags)
                        jp = makejob(/*cmd,*/ 1);
                        if (forkshell(jp, cmd, FORK_FG) != 0) {
                                /* parent */
-                               INT_ON;
                                TRACE(("forked child exited with %d\n", status));
                                break;
                        }
@@ -10387,11 +10391,9 @@ evalcommand(union node *cmd, int flags)
                        if (cmd_is_exec && argc > 1)
                                listsetvar(varlist.list, VEXPORT);
                }
-               if (evalbltin(cmdentry.u.cmd, argc, argv, flags)) {
-                       if (exception_type == EXERROR && spclbltin <= 0) {
-                               FORCE_INT_ON;
-                               break;
-                       }
+               if (evalbltin(cmdentry.u.cmd, argc, argv, flags)
+                && !(exception_type == EXERROR && spclbltin <= 0)
+               ) {
  raise:
                        longjmp(exception_handler->loc, 1);
                }
@@ -10404,6 +10406,7 @@ evalcommand(union node *cmd, int flags)
        } /* switch */
 
        status = waitforjob(jp);
+       FORCE_INT_ON;
 
  out:
        if (cmd->ncmd.redirect)