gspawn: Handle ENOSYS from close_range()

It’s possible that GLib will eventually be compiled against a version of
libc which supports `close_range()` (hence `HAVE_CLOSE_RANGE` will be
defined), but then run against an older kernel which doesn’t support it.
In this case, we want to fall back to `fdwalk()`, which should work on
such systems.

This is what cpython does: 3529718925/Python/fileutils.c (L2227)

Spotted by Allison Karlitskaya in !1688.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2020-12-03 14:30:29 +00:00
parent 9a519c8bf0
commit 23f1a31923

View File

@ -1330,12 +1330,17 @@ safe_closefrom (int lowfd)
* simple wrapper of the fcntl command.
*/
(void) fcntl (lowfd, F_CLOSEM);
#elif defined(HAVE_CLOSE_RANGE)
#else
#if defined(HAVE_CLOSE_RANGE)
/* close_range() is available in Linux since kernel 5.9, and on FreeBSD at
* around the same time. It was designed for use in async-signal-safe
* situations: https://bugs.python.org/issue38061 */
(void) close_range (lowfd, G_MAXUINT);
#else
* situations: https://bugs.python.org/issue38061
*
* Handle ENOSYS in case its supported in libc but not the kernel; if so,
* fall back to safe_fdwalk(). */
if (close_range (lowfd, G_MAXUINT) != 0 && errno == ENOSYS)
#endif /* HAVE_CLOSE_RANGE */
(void) safe_fdwalk (close_func, GINT_TO_POINTER (lowfd));
#endif
}