From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Newsgroups: gmane.lisp.guile.bugs Subject: bug#61095: [PATCH 1/3] 'spawn' closes only open file descriptors on non-GNU/Linux systems. Date: Thu, 30 Mar 2023 00:30:55 +0200 Message-ID: <20230329223057.28100-1-ludo@gnu.org> References: <87zg7vjimr.fsf@inria.fr> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28056"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= , Josselin Poiret , Omar Polo , Andrew Whatson To: 61095@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Thu Mar 30 00:32:35 2023 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 1pheLL-0006z1-9M for guile-bugs@m.gmane-mx.org; Thu, 30 Mar 2023 00:32:35 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pheKq-0001ju-CP; Wed, 29 Mar 2023 18:32:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pheKo-0001ii-GJ for bug-guile@gnu.org; Wed, 29 Mar 2023 18:32:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pheKo-00081m-6A for bug-guile@gnu.org; Wed, 29 Mar 2023 18:32:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pheKn-0006dT-Lw; Wed, 29 Mar 2023 18:32:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: dev@jpoiret.xyz, op@omarpolo.com, whatson@tailcall.au, bug-guile@gnu.org Resent-Date: Wed, 29 Mar 2023 22:32:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61095 X-GNU-PR-Package: guile X-Debbugs-Original-Xcc: Josselin Poiret , Omar Polo , Andrew Whatson Original-Received: via spool by 61095-submit@debbugs.gnu.org id=B61095.168012908825431 (code B ref 61095); Wed, 29 Mar 2023 22:32:01 +0000 Original-Received: (at 61095) by debbugs.gnu.org; 29 Mar 2023 22:31:28 +0000 Original-Received: from localhost ([127.0.0.1]:55146 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pheKF-0006c7-V3 for submit@debbugs.gnu.org; Wed, 29 Mar 2023 18:31:28 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:44730) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pheKE-0006br-Fw for 61095@debbugs.gnu.org; Wed, 29 Mar 2023 18:31:26 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pheK8-0007x9-FK; Wed, 29 Mar 2023 18:31:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=yjuh/vN49lyFeD/2ScYl7MpNxHxFOQ+r2zSHLKmzB3A=; b=jRtSa9OOgJh/5t6s2U2h kk87MrIyABqJjce+r0GqJ29Kidj9d80MauUBuoj/f+uQMSARohNdV7WS9oNbFu3HUe8hqj7eeOX2g szvF2ybaDJ2qLxrwQADCgOCi5sucqKVB9rb+DtkkCbTTYw6Qugp/GHQoGSs/qaz1pgzeHba0lcF7T n8pPOqLrGC36SMpMfBlqnGF5r98qpeR5Tb5SdSMuckMBVvmTPxvvgf6wIrcKnGsqEOzT6HSACiNS1 YKb/+z1cGCeuYP2kDkQGMCG1oTD8AkOW9xP60om4SHeUKhckifxrjDz3zQhA8F0tk3fKtcIaZMf22 eEFmrT6+dReE3A==; Original-Received: from vpn-0-27.aquilenet.fr ([2a0c:e300:4:27::] helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pheK8-00015b-1K; Wed, 29 Mar 2023 18:31:20 -0400 X-Mailer: git-send-email 2.39.2 In-Reply-To: <87zg7vjimr.fsf@inria.fr> 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-bounces+guile-bugs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.bugs:10588 Archived-At: Fixes . Reported by Omar Polo . * libguile/posix.c (close_inherited_fds_slow): On systems other than GNU/Linux, call 'addclose' only when 'fcntl' succeeds on MAX_FD. --- libguile/posix.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/libguile/posix.c b/libguile/posix.c index 3a8be94e4..68e9bfade 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -1326,7 +1326,24 @@ static void close_inherited_fds_slow (posix_spawn_file_actions_t *actions, int max_fd) { while (--max_fd > 2) - posix_spawn_file_actions_addclose (actions, max_fd); + { + /* Adding a 'close' action for a file descriptor that is not open + causes 'posix_spawn' to fail on GNU/Hurd and on OpenBSD, but + not on GNU/Linux: . Hence this + strategy: + + - On GNU/Linux, close every FD, since that's the only + race-free way to make sure the child doesn't inherit one. + - On other systems, only close FDs currently open in the + parent; it works, but it's racy (XXX). + + The only reliable option is 'addclosefrom'. */ +#if ! (defined __GLIBC__ && defined __linux__) + int flags = fcntl (max_fd, F_GETFD, NULL); + if (flags >= 0) +#endif + posix_spawn_file_actions_addclose (actions, max_fd); + } } static void base-commit: e334e59589c3cbfc68d3f7d0d739000e0876b36d -- 2.39.2