From bbb325494c97896a92d932dfa712b3abf6dad242 Mon Sep 17 00:00:00 2001 From: Sam James Date: Mon, 11 Dec 2023 16:05:06 +0000 Subject: [PATCH 1/4] gh-112970: Wire up closefrom for Linux 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 (which was wrong for other BSDs anyway). Signed-off-by: Sam James --- Python/fileutils.c | 4 ++-- configure | 6 ++++++ configure.ac | 2 +- pyconfig.h.in | 3 +++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Python/fileutils.c b/Python/fileutils.c index 9d12bc89c95436..46eb8a3730831a 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -2878,9 +2878,9 @@ _Py_GetLocaleconvNumeric(struct lconv *lc, * 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 diff --git a/configure b/configure index 5f880d6d8edd96..6f494409f6dbea 100755 --- a/configure +++ b/configure @@ -17223,6 +17223,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 diff --git a/configure.ac b/configure.ac index c07d7ce6bdc918..e6a5aff1db21b1 100644 --- a/configure.ac +++ b/configure.ac @@ -4743,7 +4743,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 \ diff --git a/pyconfig.h.in b/pyconfig.h.in index 2978fa2c17301f..9c429c03722383 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -157,6 +157,9 @@ /* 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 From c4bbfd0b4a49ef15acba660f1e551652d4567880 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 11 Dec 2023 16:13:18 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-12-11-16-13-15.gh-issue-112970.87jmKP.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-12-11-16-13-15.gh-issue-112970.87jmKP.rst 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 index 00000000000000..281247b22f0d27 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-12-11-16-13-15.gh-issue-112970.87jmKP.rst @@ -0,0 +1 @@ +Use closefrom on Linux where available (e.g. glibc-2.34), rather than only FreeBSD. From 4048d4bc104b9ea371acc0996a390099a28c7ee1 Mon Sep 17 00:00:00 2001 From: Sam James Date: Mon, 11 Dec 2023 17:22:36 +0000 Subject: [PATCH 3/4] Fix NEWS item Signed-off-by: Sam James --- .../next/Library/2023-12-11-16-13-15.gh-issue-112970.87jmKP.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 281247b22f0d27..58ca26af511383 100644 --- 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 @@ -1 +1 @@ -Use closefrom on Linux where available (e.g. glibc-2.34), rather than only FreeBSD. +Use :c:func:`!closefrom` on Linux where available (e.g. glibc-2.34), rather than only FreeBSD. From 226037fd0f2607cac89721aeeb64258533e59af0 Mon Sep 17 00:00:00 2001 From: Sam James Date: Mon, 11 Dec 2023 23:13:17 +0000 Subject: [PATCH 4/4] Explicitly ignore closefrom return value There are two variants of closefrom, one returns void, but another returns int, so let's cast the return value to void to explicitly discard it to avoid -Wunused-result warnings. Signed-off-by: Sam James --- Python/fileutils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/fileutils.c b/Python/fileutils.c index 46eb8a3730831a..882d3299575cf3 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -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 */