summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2023-03-29 11:21:38 +0200
committerLudovic Courtès <ludo@gnu.org>2023-04-02 15:36:33 +0200
commit9cc85a4f52147fcdaa4c52a62bcc87bdb267d0a9 (patch)
tree2645356b8d91631d1b616f14754dea6250b5ca73
parent7d7067fe15a6dc82068cbafb11846a2de2896a83 (diff)
downloadguile-9cc85a4f52147fcdaa4c52a62bcc87bdb267d0a9.tar.gz
Use 'posix_spawn_file_actions_addclosefrom_np' where available.
* 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'.
-rw-r--r--configure.ac4
-rw-r--r--libguile/posix.c14
2 files changed, 17 insertions, 1 deletions
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 <langinfo.h>]])
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)