From 21ad54b694c676ba608166be137b677bbb747eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= Date: Tue, 28 Mar 2023 23:35:35 +0200 Subject: 'spawn' closes only open file descriptors on non-GNU/Linux systems. 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. * NEWS: Update. --- libguile/posix.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'libguile') 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 -- cgit v1.2.1