[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 04/07: Reduce redundant 'close' calls when forking on so
From: |
Ludovic Courtès |
Subject: |
[Guile-commits] 04/07: Reduce redundant 'close' calls when forking on some systems. |
Date: |
Fri, 23 Dec 2022 05:45:31 -0500 (EST) |
civodul pushed a commit to branch wip-posix-spawn
in repository guile.
commit 3c1df24849673e6245881342d40d44bde4fc22d0
Author: Andrew Whatson <whatson@gmail.com>
AuthorDate: Fri Dec 23 00:27:22 2022 +0100
Reduce redundant 'close' calls when forking on some systems.
Some systems provide "/proc/self/fd" which is a directory containing an
entry for each open file descriptor in the current process. We use this
to limit the number of close() calls needed to ensure file descriptors
aren't leaked to the child process when forking.
* libguile/posix.c (close_inherited_fds_slow):
(close_inherited_fds): New static helper functions.
(scm_spawn_process): Attempt to close inherited file descriptors
efficiently using 'close_inherited_fds', falling back to the brute-force
approach in 'close_inherited_fds_slow'.
---
libguile/posix.c | 39 +++++++++++++++++++++++++++++++++++++--
1 file changed, 37 insertions(+), 2 deletions(-)
diff --git a/libguile/posix.c b/libguile/posix.c
index f9c36d7ac..cee3e8e24 100644
--- a/libguile/posix.c
+++ b/libguile/posix.c
@@ -24,6 +24,7 @@
# include <config.h>
#endif
+#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@@ -1308,6 +1309,41 @@ SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
#undef FUNC_NAME
#endif /* HAVE_FORK */
+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);
+}
+
+static void
+close_inherited_fds (posix_spawn_file_actions_t *actions, int max_fd)
+{
+ DIR *dirp;
+ struct dirent *d;
+ int fd;
+
+ /* Try to use the platform-specific list of open file descriptors, so
+ we don't need to use the brute force approach. */
+ dirp = opendir ("/proc/self/fd");
+
+ if (dirp == NULL)
+ return close_inherited_fds_slow (actions, max_fd);
+
+ while ((d = readdir (dirp)) != NULL)
+ {
+ fd = atoi (d->d_name);
+
+ /* Skip "." and "..", garbage entries, stdin/stdout/stderr. */
+ if (fd <= 2)
+ continue;
+
+ posix_spawn_file_actions_addclose (actions, fd);
+ }
+
+ closedir (dirp);
+}
+
static int
do_spawn (char *exec_file, char **exec_argv, char **exec_env, int in, int out,
int err)
{
@@ -1350,8 +1386,7 @@ do_spawn (char *exec_file, char **exec_argv, char
**exec_env, int in, int out, i
posix_spawn_file_actions_adddup2 (&actions, fd_slot[1], 1);
posix_spawn_file_actions_adddup2 (&actions, fd_slot[2], 2);
- while (--max_fd > 2)
- posix_spawn_file_actions_addclose (&actions, max_fd);
+ close_inherited_fds (&actions, max_fd);
if (posix_spawnp (&pid, exec_file, &actions, attrp, exec_argv, environ) != 0)
return -1;
- [Guile-commits] branch wip-posix-spawn created (now 7073b5f67), Ludovic Courtès, 2022/12/23
- [Guile-commits] 04/07: Reduce redundant 'close' calls when forking on some systems.,
Ludovic Courtès <=
- [Guile-commits] 06/07: fixup! Make system* and piped-process internally use spawn., Ludovic Courtès, 2022/12/23
- [Guile-commits] 05/07: fixup! Make system* and piped-process internally use spawn., Ludovic Courtès, 2022/12/23
- [Guile-commits] 02/07: Add spawn*., Ludovic Courtès, 2022/12/23
- [Guile-commits] 07/07: fixup! Make system* and piped-process internally use spawn., Ludovic Courtès, 2022/12/23
- [Guile-commits] 01/07: Update gnulib to 0.1.5414-8204d and add posix_spawn, posix_spawnp., Ludovic Courtès, 2022/12/23
- [Guile-commits] 03/07: Make system* and piped-process internally use spawn., Ludovic Courtès, 2022/12/23