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 3/3] Use 'posix_spawn_file_actions_addclosefrom_np' where available. Date: Thu, 30 Mar 2023 00:30:57 +0200 Message-ID: <20230329223057.28100-3-ludo@gnu.org> References: <87zg7vjimr.fsf@inria.fr> <20230329223057.28100-1-ludo@gnu.org> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26237"; 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:19 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 1pheL4-0006cf-Qm for guile-bugs@m.gmane-mx.org; Thu, 30 Mar 2023 00:32:18 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pheKs-0001p3-Tf; Wed, 29 Mar 2023 18:32:06 -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 1pheKq-0001l9-WE for bug-guile@gnu.org; Wed, 29 Mar 2023 18:32:05 -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 1pheKq-000822-Of for bug-guile@gnu.org; Wed, 29 Mar 2023 18:32:04 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pheKo-0006dj-I2; Wed, 29 Mar 2023 18:32:02 -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:02 +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.168012909425462 (code B ref 61095); Wed, 29 Mar 2023 22:32:02 +0000 Original-Received: (at 61095) by debbugs.gnu.org; 29 Mar 2023 22:31:34 +0000 Original-Received: from localhost ([127.0.0.1]:55154 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pheKL-0006cX-Kn for submit@debbugs.gnu.org; Wed, 29 Mar 2023 18:31:34 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:44744) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pheKJ-0006bx-19 for 61095@debbugs.gnu.org; Wed, 29 Mar 2023 18:31:31 -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 1pheKD-0007yR-Q1; Wed, 29 Mar 2023 18:31:25 -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=f+e3EZwIPoMN6TK98HeBx6eclBn838umUj+BHP93xys=; b=Rslhptbk+10gRfgSj6kf fF4Af7W49whJLxLtX63ZzspUN2Wpm8oGIIcaoeqYFTtGkwZKR7daMS8z3po1SzCNi42jx5zk8ogx4 Jn/YO46Yh30dCCP6GLxq7xATI6hAnHgzcIcJXMb+RCf1c88CHjiI8/vFPSX1rcJJnE0XEfwyou4BJ yUmFJeAoz3t7KsLdabcHmN0UcwVQHSjTT4uI1NFm/mz4+G9hYGrbnaubyxfva+QrUTb5X6mpXFJqr fRouo2tKYiT/twYmcsDDxPeZYsfB8DmDQUn62ztW3hbGr0vR4xlSa0yUq/hT+rA5trwhpt66fAljo jwxNXXTgXTf1jA==; 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 1pheKC-00015b-Qy; Wed, 29 Mar 2023 18:31:25 -0400 X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230329223057.28100-1-ludo@gnu.org> 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:10587 Archived-At: * configure.ac: Check for 'posix_spawn_file_actions_addclosefrom_np'. * libguile/posix.c (HAVE_ADDCLOSEFROM): New macro. (close_inherited_fds): Wrap in #ifdef HAVE_ADDCLOSEFROM. (do_spawn) [HAVE_ADDCLOSEFROM]: Use 'posix_spawn_file_actions_addclosefrom_np'. --- configure.ac | 4 +++- libguile/posix.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index d5ce1c4ac..4a93be979 100644 --- a/configure.ac +++ b/configure.ac @@ -515,6 +515,7 @@ AC_CHECK_HEADERS([crt_externs.h]) # sched_getaffinity, sched_setaffinity - GNU extensions (glibc) # sendfile - non-POSIX, found in glibc # pipe2 - non-POSIX, found in glibc (GNU/Linux and GNU/Hurd) +# posix_spawn_file_actions_addclosefrom_np - glibc >= 2.34 # AC_CHECK_FUNCS([DINFINITY DQNAN cexp chsize clog clog10 ctermid \ fesetround ftime ftruncate fchown fchownat fchmod fchdir readlinkat \ @@ -528,7 +529,8 @@ AC_CHECK_FUNCS([DINFINITY DQNAN cexp chsize clog clog10 ctermid \ index bcopy rindex truncate isblank _NSGetEnviron \ strcoll_l strtod_l strtol_l newlocale uselocale utimensat \ fstatat futimens openat \ - sched_getaffinity sched_setaffinity sendfile pipe2]) + sched_getaffinity sched_setaffinity sendfile pipe2 + posix_spawn_file_actions_addclosefrom_np]) # The newlib C library uses _NL_ prefixed locale langinfo constants. AC_CHECK_DECLS([_NL_NUMERIC_GROUPING], [], [], [[#include ]]) diff --git a/libguile/posix.c b/libguile/posix.c index b5830c43b..3adc743c4 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -1322,6 +1322,12 @@ SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0, #undef FUNC_NAME #endif /* HAVE_FORK */ +#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP +# define HAVE_ADDCLOSEFROM 1 +#endif + +#ifndef HAVE_ADDCLOSEFROM + static void close_inherited_fds (posix_spawn_file_actions_t *actions, int max_fd) { @@ -1346,6 +1352,8 @@ close_inherited_fds (posix_spawn_file_actions_t *actions, int max_fd) } } +#endif + static pid_t do_spawn (char *exec_file, char **exec_argv, char **exec_env, int in, int out, int err, int spawnp) @@ -1389,7 +1397,13 @@ do_spawn (char *exec_file, char **exec_argv, char **exec_env, posix_spawn_file_actions_adddup2 (&actions, fd_slot[1], 1); posix_spawn_file_actions_adddup2 (&actions, fd_slot[2], 2); +#ifdef HAVE_ADDCLOSEFROM + /* This function appears in glibc 2.34. It's both free from race + conditions and more efficient than the alternative. */ + posix_spawn_file_actions_addclosefrom_np (&actions, 3); +#else close_inherited_fds (&actions, max_fd); +#endif int res = -1; if (spawnp) -- 2.39.2