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 0/2] Fix spawning a child not setting standard fds properly Date: Mon, 27 Dec 2021 22:25:56 +0100 Message-ID: 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="873"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Josselin Poiret To: 52835@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Mon Dec 27 22:27:24 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 1n1xWd-000Aat-G0 for guile-bugs@m.gmane-mx.org; Mon, 27 Dec 2021 22:27:23 +0100 Original-Received: from localhost ([::1]:56682 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n1xWc-0007An-8P for guile-bugs@m.gmane-mx.org; Mon, 27 Dec 2021 16:27:22 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:53194) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n1xWI-0007AV-IG for bug-guile@gnu.org; Mon, 27 Dec 2021 16:27:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:32896) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n1xWI-0003gK-8y for bug-guile@gnu.org; Mon, 27 Dec 2021 16:27:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n1xWI-0005er-0c for bug-guile@gnu.org; Mon, 27 Dec 2021 16:27: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:27:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 52835 X-GNU-PR-Package: guile X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-guile@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.164064039421703 (code B ref -1); Mon, 27 Dec 2021 21:27:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 27 Dec 2021 21:26:34 +0000 Original-Received: from localhost ([127.0.0.1]:44442 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n1xVq-0005dz-7B for submit@debbugs.gnu.org; Mon, 27 Dec 2021 16:26:34 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:58184) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n1xVn-0005dr-S7 for submit@debbugs.gnu.org; Mon, 27 Dec 2021 16:26:33 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:53124) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n1xVn-000757-NE for bug-guile@gnu.org; Mon, 27 Dec 2021 16:26:31 -0500 Original-Received: from jpoiret.xyz ([206.189.101.64]:40796) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n1xVm-0003dE-1I for bug-guile@gnu.org; Mon, 27 Dec 2021 16:26:31 -0500 Original-Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id 23772184F6A; Mon, 27 Dec 2021 21:26:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1640640386; 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; bh=tQV4z7gWBGvaEEPcCfEXb+ERJ0AgQfAUqWo1DahSqOU=; b=dQvyzSo6OBWgrZEi7Z0wsWSYiSQ/rY0rz97S7y29ohNVpnGr8r9dNrzJR+CwhWN5qv5afz popyTJ44PXJyvD/X7c1eGno6jlB0Te2wx5WXI01dopvNScdeCCORB/S+SLBqxw+uwLPTCg fUbdeDbIV1BqDPKLxs1fDcfeSXuzLmHVTEWUbAaH3iYgv+9mssk+LVyS6r93gdgKGMVpaw Y3hHxS7KnaURqD3JCZNsjHPT9fGeMH2A7SznmK4q5X3W1Gkno8cOGjyDqoeUomp6onZHhF usqesdtp+pXgVhApV4xV1aCcxvywUW5cv3Tx3jyXorqAZCiKiw17g85MKVvarw== X-Spamd-Bar: ++++ Authentication-Results: jpoiret.xyz; auth=pass smtp.auth=jpoiret@jpoiret.xyz smtp.mailfrom=dev@jpoiret.xyz Received-SPF: pass client-ip=206.189.101.64; envelope-from=dev@jpoiret.xyz; helo=jpoiret.xyz X-Spam_score_int: 4 X-Spam_score: 0.4 X-Spam_bar: / X-Spam_report: (0.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FROM_SUSPICIOUS_NTLD=0.498, FROM_SUSPICIOUS_NTLD_FP=0.001, PDS_OTHER_BAD_TLD=1.997, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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:10228 Archived-At: Hello, While working on the Guix installer (that needs to use system* quite a lot), I've noticed that output/error redirection doesn't behave as intended when combined with system*. Here's a test you can try at home: --8<---------------cut here---------------start------------->8--- (call-with-output-file "/tmp/test.log" (lambda (port) (with-output-to-port port (lambda () (with-error-to-port port (lambda () (system* "bash" "-c" "echo bong >&2"))))))) --8<---------------cut here---------------end--------------->8--- With current Guix, you will notice that /tmp/test.log is empty, instead of the expected "bong". Worse even, when testing with --8<---------------cut here---------------start------------->8--- (with-error-to-port (current-output-port) (lambda () (system* "bash" "-c" "echo $$; sleep 10"))) --8<---------------cut here---------------end--------------->8--- you can actually inspect `/proc//fd/` and see that the stderr fd, 2, is actually closed. This means that the next opened fd will take its place, to which writes to stderr may end up. The logic behind the stdin/out/err redirection for child processes lies in `start_child`, in libguile/posix.c, and doesn't take into account cases like: * in/out/err having common values, as the common fd will be closed before it has been dup2'd to all the std fds (which happens in the first example); * in/out/err having values between 0 and 2 which aren't their corresponding std fd number, as they will not be dup2'd to the stdin/out/err (which happens in the second example). The first patch addresses this by: * moving in/out/err closing logic after they've all been dup2'd; * removing the check that in/out/err are > the corresponding stdin/out/err; * replacing renumber_file_descriptor by simply dup, as the former closes fds that might be shared. The closing logic of the first point is enough here. The second patch removes renumber_file_descriptor, as it is no longer used. Best, Josselin Josselin Poiret (2): Fix child spawning closing standard fds prematurely Remove unused renumber_file_descriptor libguile/posix.c | 72 ++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 52 deletions(-) -- 2.34.0