Skip to content

Commit c454e93

Browse files
authored
gh-112970: Detect and use closefrom() when available (#112969)
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>
1 parent 0d2fe6b commit c454e93

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use :c:func:`!closefrom` on Linux where available (e.g. glibc-2.34), rather than only FreeBSD.

Python/fileutils.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2878,9 +2878,9 @@ _Py_GetLocaleconvNumeric(struct lconv *lc,
28782878
* non-opened fd in the middle.
28792879
* 2b. If fdwalk(3) isn't available, just do a plain close(2) loop.
28802880
*/
2881-
#ifdef __FreeBSD__
2881+
#ifdef HAVE_CLOSEFROM
28822882
# define USE_CLOSEFROM
2883-
#endif /* __FreeBSD__ */
2883+
#endif /* HAVE_CLOSEFROM */
28842884

28852885
#ifdef HAVE_FDWALK
28862886
# define USE_FDWALK
@@ -2922,7 +2922,7 @@ _Py_closerange(int first, int last)
29222922
#ifdef USE_CLOSEFROM
29232923
if (last >= sysconf(_SC_OPEN_MAX)) {
29242924
/* Any errors encountered while closing file descriptors are ignored */
2925-
closefrom(first);
2925+
(void)closefrom(first);
29262926
}
29272927
else
29282928
#endif /* USE_CLOSEFROM */

configure

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -4745,7 +4745,7 @@ fi
47454745

47464746
# checks for library functions
47474747
AC_CHECK_FUNCS([ \
4748-
accept4 alarm bind_textdomain_codeset chmod chown clock close_range confstr \
4748+
accept4 alarm bind_textdomain_codeset chmod chown clock closefrom close_range confstr \
47494749
copy_file_range ctermid dup dup3 execv explicit_bzero explicit_memset \
47504750
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
47514751
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \

pyconfig.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@
157157
/* Define to 1 if you have the `clock_settime' function. */
158158
#undef HAVE_CLOCK_SETTIME
159159

160+
/* Define to 1 if you have the `closefrom' function. */
161+
#undef HAVE_CLOSEFROM
162+
160163
/* Define to 1 if you have the `close_range' function. */
161164
#undef HAVE_CLOSE_RANGE
162165

0 commit comments

Comments
 (0)