1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00

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'.
This commit is contained in:
Ludovic Courtès 2023-03-29 11:21:38 +02:00
parent 7d7067fe15
commit 9cc85a4f52
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
2 changed files with 17 additions and 1 deletions

View file

@ -515,6 +515,7 @@ AC_CHECK_HEADERS([crt_externs.h])
# sched_getaffinity, sched_setaffinity - GNU extensions (glibc) # sched_getaffinity, sched_setaffinity - GNU extensions (glibc)
# sendfile - non-POSIX, found in glibc # sendfile - non-POSIX, found in glibc
# pipe2 - non-POSIX, found in glibc (GNU/Linux and GNU/Hurd) # 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 \ AC_CHECK_FUNCS([DINFINITY DQNAN cexp chsize clog clog10 ctermid \
fesetround ftime ftruncate fchown fchownat fchmod fchdir readlinkat \ 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 \ index bcopy rindex truncate isblank _NSGetEnviron \
strcoll_l strtod_l strtol_l newlocale uselocale utimensat \ strcoll_l strtod_l strtol_l newlocale uselocale utimensat \
fstatat futimens openat \ 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. # The newlib C library uses _NL_ prefixed locale langinfo constants.
AC_CHECK_DECLS([_NL_NUMERIC_GROUPING], [], [], [[#include <langinfo.h>]]) AC_CHECK_DECLS([_NL_NUMERIC_GROUPING], [], [], [[#include <langinfo.h>]])

View file

@ -1322,6 +1322,12 @@ SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
#undef FUNC_NAME #undef FUNC_NAME
#endif /* HAVE_FORK */ #endif /* HAVE_FORK */
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
# define HAVE_ADDCLOSEFROM 1
#endif
#ifndef HAVE_ADDCLOSEFROM
static void static void
close_inherited_fds (posix_spawn_file_actions_t *actions, int max_fd) 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 static pid_t
do_spawn (char *exec_file, char **exec_argv, char **exec_env, do_spawn (char *exec_file, char **exec_argv, char **exec_env,
int in, int out, int err, int spawnp) 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[1], 1);
posix_spawn_file_actions_adddup2 (&actions, fd_slot[2], 2); 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); close_inherited_fds (&actions, max_fd);
#endif
int res = -1; int res = -1;
if (spawnp) if (spawnp)