]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112970: Detect and use closefrom() when available (#112969)
authorSam James <sam@gentoo.org>
Tue, 12 Dec 2023 10:25:27 +0000 (10:25 +0000)
committerGitHub <noreply@github.com>
Tue, 12 Dec 2023 10:25:27 +0000 (11:25 +0100)
glibc-2.34 implements closefrom(3) using the same semantics as on BSD.
Check for closefrom() in configure and use the check result in
fileutils.c, rather than hardcoding a FreeBSD check.

Some implementations of closefrom() return an int. Explicitly discard
the return value by casting it to void, to avoid future compiler
warnings.

Signed-off-by: Sam James <sam@gentoo.org>
Misc/NEWS.d/next/Library/2023-12-11-16-13-15.gh-issue-112970.87jmKP.rst [new file with mode: 0644]
Python/fileutils.c
configure
configure.ac
pyconfig.h.in

diff --git a/Misc/NEWS.d/next/Library/2023-12-11-16-13-15.gh-issue-112970.87jmKP.rst b/Misc/NEWS.d/next/Library/2023-12-11-16-13-15.gh-issue-112970.87jmKP.rst
new file mode 100644 (file)
index 0000000..58ca26a
--- /dev/null
@@ -0,0 +1 @@
+Use :c:func:`!closefrom` on Linux where available (e.g. glibc-2.34), rather than only FreeBSD.
index 9d12bc89c95436a4ef8873109cb7357fa4c259f8..882d3299575cf3b44dd4e1c5f92f243a46b992d8 100644 (file)
@@ -2878,9 +2878,9 @@ done:
  *    non-opened fd in the middle.
  * 2b. If fdwalk(3) isn't available, just do a plain close(2) loop.
  */
-#ifdef __FreeBSD__
+#ifdef HAVE_CLOSEFROM
 #  define USE_CLOSEFROM
-#endif /* __FreeBSD__ */
+#endif /* HAVE_CLOSEFROM */
 
 #ifdef HAVE_FDWALK
 #  define USE_FDWALK
@@ -2922,7 +2922,7 @@ _Py_closerange(int first, int last)
 #ifdef USE_CLOSEFROM
     if (last >= sysconf(_SC_OPEN_MAX)) {
         /* Any errors encountered while closing file descriptors are ignored */
-        closefrom(first);
+        (void)closefrom(first);
     }
     else
 #endif /* USE_CLOSEFROM */
index c4486441041a70948d865647eaf93199d2c3693f..cad3bce0c7de87e97cf173e62a599f9f5fbbc8fa 100755 (executable)
--- a/configure
+++ b/configure
@@ -17225,6 +17225,12 @@ if test "x$ac_cv_func_clock" = xyes
 then :
   printf "%s\n" "#define HAVE_CLOCK 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "closefrom" "ac_cv_func_closefrom"
+if test "x$ac_cv_func_closefrom" = xyes
+then :
+  printf "%s\n" "#define HAVE_CLOSEFROM 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "close_range" "ac_cv_func_close_range"
 if test "x$ac_cv_func_close_range" = xyes
index a725309424a49ef0492461c820586ee2393d5f51..7dda0b3fff95beec67229516789e86be8bb813e4 100644 (file)
@@ -4745,7 +4745,7 @@ fi
 
 # checks for library functions
 AC_CHECK_FUNCS([ \
-  accept4 alarm bind_textdomain_codeset chmod chown clock close_range confstr \
+  accept4 alarm bind_textdomain_codeset chmod chown clock closefrom close_range confstr \
   copy_file_range ctermid dup dup3 execv explicit_bzero explicit_memset \
   faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
   fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
index 2978fa2c17301f0ef16c5d8542d398693f27e7a6..9c429c037223839f3f83e4b31d02a4ae6274acaf 100644 (file)
 /* Define to 1 if you have the `clock_settime' function. */
 #undef HAVE_CLOCK_SETTIME
 
+/* Define to 1 if you have the `closefrom' function. */
+#undef HAVE_CLOSEFROM
+
 /* Define to 1 if you have the `close_range' function. */
 #undef HAVE_CLOSE_RANGE