From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Josselin Poiret via "Bug reports for GUILE, GNU's Ubiquitous Extension Language" Newsgroups: gmane.lisp.guile.bugs Subject: bug#52835: [PATCH 1/2] Fix child spawning closing standard fds prematurely Date: Mon, 27 Dec 2021 22:35:37 +0100 Message-ID: <299d40c7e9fc0c30c3aebfa06c69506b981bd1f8.1640638819.git.dev@jpoiret.xyz> References: Reply-To: Josselin Poiret Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9334"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 52835@debbugs.gnu.org To: Josselin Poiret Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Mon Dec 27 22:37:21 2021 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n1xgH-0002E1-7w for guile-bugs@m.gmane-mx.org; Mon, 27 Dec 2021 22:37:21 +0100 Original-Received: from localhost ([::1]:34172 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n1xgF-0002iW-5I for guile-bugs@m.gmane-mx.org; Mon, 27 Dec 2021 16:37:19 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:54994) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n1xfy-0002hr-Gx for bug-guile@gnu.org; Mon, 27 Dec 2021 16:37:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:32913) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n1xfy-00050b-7f for bug-guile@gnu.org; Mon, 27 Dec 2021 16:37:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n1xfy-0005wu-6H for bug-guile@gnu.org; Mon, 27 Dec 2021 16:37:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Josselin Poiret Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Mon, 27 Dec 2021 21:37:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 52835 X-GNU-PR-Package: guile X-GNU-PR-Keywords: patch Original-Received: via spool by 52835-submit@debbugs.gnu.org id=B52835.164064096922761 (code B ref 52835); Mon, 27 Dec 2021 21:37:02 +0000 Original-Received: (at 52835) by debbugs.gnu.org; 27 Dec 2021 21:36:09 +0000 Original-Received: from localhost ([127.0.0.1]:44450 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n1xf7-0005v3-6L for submit@debbugs.gnu.org; Mon, 27 Dec 2021 16:36:09 -0500 Original-Received: from jpoiret.xyz ([206.189.101.64]:42894) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n1xf3-0005us-BQ for 52835@debbugs.gnu.org; Mon, 27 Dec 2021 16:36:08 -0500 Original-Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id E3879184F5B; Mon, 27 Dec 2021 21:36:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1640640964; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ojPF/jO37wQEtTdoeOdj88gt6VBy/6zjvkMa4F5oTtk=; b=t+IJars5NA0A/M51qzJohD8TIqEJRm8a94YfIPGA3/wFrJ0otPQCyamodPLMGj6jhwM0lc 5qkgru+abIW1jmYUZc9ULnyREV6+8HyR/Un0IWJ6DaZVcGRI3tPXzfdxwtBKRHYOxYcX07 pC1aZ9HXpD3HmNY6K3zZvTD2Cm5ggiUBmfM3WBYXGd2Dfxg2ZUfcQtgQDFGgMhUMC54tm+ JuBlw9jNgMnm9kpwnCiQ43B0RUzgsgjI280to5zx+9gPi4WG2mhQ+9nGgy31rV3mXRHdso 8zsY/vABSuwd/VFYQmO1/Qezc3zuWuP/99lQtDj/aEFclcugN+6NPHdqb5XHyw== In-Reply-To: X-Spamd-Bar: / Authentication-Results: jpoiret.xyz; auth=pass smtp.auth=jpoiret@jpoiret.xyz smtp.mailfrom=dev@jpoiret.xyz X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.io gmane.lisp.guile.bugs:10229 Archived-At: * libguile/posix.c (start_child): Close standard file descriptors only after all of them have been dup2'd. --- libguile/posix.c | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/libguile/posix.c b/libguile/posix.c index 3ab12b99e..148ebeb3d 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -1357,27 +1357,25 @@ start_child (const char *exec_file, char **exec_argv, if (err == -1) err = open ("/dev/null", O_WRONLY); - if (in > 0) - { - if (out == 0) - out = renumber_file_descriptor (out, err); - if (err == 0) - err = renumber_file_descriptor (err, err); - do dup2 (in, 0); while (errno == EINTR); - close (in); - } - if (out > 1) - { - if (err == 1) - err = renumber_file_descriptor (err, err); - do dup2 (out, 1); while (errno == EINTR); - close (out); - } - if (err > 2) - { - do dup2 (err, 2); while (errno == EINTR); - close (err); - } + /* Dup each non-yet-dup2'd fd that's in the way to the next available fd, + so that we can safely dup2 to 0/1/2 without potentially overwriting + in/out/err. Note that dup2 doesn't do anything if its arguments are + equal. */ + if (out == 0) + do out = dup (out); while (errno == EINTR); + if (err == 0) + do err = dup (err); while (errno == EINTR); + do dup2 (in, 0); while (errno == EINTR); + + if (err == 1) + do err = dup (err); while (errno == EINTR); + do dup2 (out, 1); while (errno == EINTR); + + do dup2 (err, 2); while (errno == EINTR); + + if (in > 2) close (in); + if (out > 2) close (out); + if (err > 2) close (err); execvp (exec_file, exec_argv); -- 2.34.0