From patchwork Mon Mar 26 15:55:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 10308161 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9303760212 for ; Mon, 26 Mar 2018 15:56:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8474929755 for ; Mon, 26 Mar 2018 15:55:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 792002975B; Mon, 26 Mar 2018 15:55:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BCA9929755 for ; Mon, 26 Mar 2018 15:55:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752449AbeCZPz6 (ORCPT ); Mon, 26 Mar 2018 11:55:58 -0400 Received: from orcrist.hmeau.com ([104.223.48.154]:35042 "EHLO deadmen.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752451AbeCZPz6 (ORCPT ); Mon, 26 Mar 2018 11:55:58 -0400 Received: from gondobar.mordor.me.apana.org.au ([192.168.128.4] helo=gondobar) by deadmen.hmeau.com with esmtp (Exim 4.84_2 #2 (Debian)) id 1f0UTR-0007tz-2s; Mon, 26 Mar 2018 23:55:53 +0800 Received: from herbert by gondobar with local (Exim 4.84_2) (envelope-from ) id 1f0UTO-0002YJ-OG; Mon, 26 Mar 2018 23:55:50 +0800 Date: Mon, 26 Mar 2018 23:55:50 +0800 From: Herbert Xu To: DASH Mailing List Cc: Denys Vlasenko Subject: eval: Reap zombies after built-in commands and functions Message-ID: <20180326155550.GA9768@gondor.apana.org.au> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently dash does not reap dead children after built-in commands or functions. This means that if you construct a loop consisting of solely built-in commands and functions, then zombies can hang around indefinitely. This patch fixes this by reaping when necessary after each built-in command and function. Reported-by: Denys Vlasenko Signed-off-by: Herbert Xu diff --git a/src/eval.c b/src/eval.c index 7498f9d..19e06ec 100644 --- a/src/eval.c +++ b/src/eval.c @@ -848,6 +848,8 @@ bail: goto out; } + jp = NULL; + /* Execute the command. */ switch (cmdentry.cmdtype) { default: @@ -856,7 +858,6 @@ bail: INTOFF; jp = makejob(cmd, 1); if (forkshell(jp, cmd, FORK_FG) != 0) { - status = waitforjob(jp); INTON; break; } @@ -875,22 +876,22 @@ bail: if (evalbltin(cmdentry.u.cmd, argc, argv, flags)) { if (exception == EXERROR && spclbltin <= 0) { FORCEINTON; - goto readstatus; + break; } raise: longjmp(handler->loc, 1); } - goto readstatus; + break; case CMDFUNCTION: poplocalvars(1); if (evalfun(cmdentry.u.func, argc, argv, flags)) goto raise; -readstatus: - status = exitstatus; break; } + status = waitforjob(jp); + out: if (cmd->ncmd.redirect) popredir(execcmd); diff --git a/src/jobs.c b/src/jobs.c index f0d34ab..1a97c54 100644 --- a/src/jobs.c +++ b/src/jobs.c @@ -53,6 +53,7 @@ #include #undef CEOF /* syntax.h redefines this */ #endif +#include "eval.h" #include "redir.h" #include "show.h" #include "main.h" @@ -973,10 +974,11 @@ waitforjob(struct job *jp) { int st; - TRACE(("waitforjob(%%%d) called\n", jobno(jp))); - while (jp->state == JOBRUNNING) { + TRACE(("waitforjob(%%%d) called\n", jp ? jobno(jp) : 0)); + while ((jp && jp->state == JOBRUNNING) || gotsigchld) dowait(DOWAIT_BLOCK, jp); - } + if (!jp) + return exitstatus; st = getstatus(jp); #if JOBS if (jp->jobctl) {