diff options
author | NIIBE Yutaka <[email protected]> | 2025-01-08 06:42:12 +0000 |
---|---|---|
committer | NIIBE Yutaka <[email protected]> | 2025-01-08 06:42:12 +0000 |
commit | e3e793302b67e37727caf11a868b5b91d7c13f8c (patch) | |
tree | c3dcccaec98f0322acce3f0838b2655ccac6a887 /src/spawn-posix.c | |
parent | New Windows API gpgrt_w32_reg_get_string. (diff) | |
download | libgpg-error-e3e793302b67e37727caf11a868b5b91d7c13f8c.tar.gz libgpg-error-e3e793302b67e37727caf11a868b5b91d7c13f8c.zip |
spawn: Use closefrom when available.
* configure.ac (AC_CHECK_FUNCS): Check closefrom.
* src/spawn-posix.c [HAVE_CLOSEFROM]: Use closefrom if possible.
--
Signed-off-by: NIIBE Yutaka <[email protected]>
Diffstat (limited to 'src/spawn-posix.c')
-rw-r--r-- | src/spawn-posix.c | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/src/spawn-posix.c b/src/spawn-posix.c index 0cdf0a6..3419cfc 100644 --- a/src/spawn-posix.c +++ b/src/spawn-posix.c @@ -84,7 +84,7 @@ struct gpgrt_process { }; - +#ifndef HAVE_CLOSEFROM /* Return the maximum number of currently allowed open file * descriptors. Only useful on POSIX systems but returns a value on * other systems too. */ @@ -175,7 +175,7 @@ get_max_fds (void) return max_fds; } - +#endif /* Close all file descriptors starting with descriptor FIRST. If * EXCEPT is not NULL, it is expected to be a list of file descriptors @@ -184,6 +184,49 @@ get_max_fds (void) void _gpgrt_close_all_fds (int first, const int *except) { +#ifdef HAVE_CLOSEFROM + int fd, i, except_start; + int fd_except_first, fd_except_last; + + if (except) + { + fd_except_first = except[0]; + fd_except_last = -1; + + /* Find the last entry in EXCEPT. */ + for (i = 0; except[i] != -1; i++) + ; + if (i != 0) + fd_except_last = except[--i]; + + if (fd_except_last != -1) + { + if (first < fd_except_last) + { + for (fd = first; fd < fd_except_first; fd++) + close (fd); + + except_start = 0; + for (; fd <= fd_except_last; fd++) + { + for (i = except_start; except[i] != -1; i++) + if (except[i] == fd) + { + except_start = i + 1; + break; + } + + if (except[i] == -1) + close (fd); + } + + first = fd; + } + } + } + + closefrom (first); +#else int max_fd = get_max_fds (); int fd, i, except_start; @@ -212,6 +255,7 @@ _gpgrt_close_all_fds (int first, const int *except) for (fd=first; fd < max_fd; fd++) close (fd); } +#endif _gpg_err_set_errno (0); } |