[glib: 1/2] gspawn: Handle ENOSYS from close_range()




commit 23f1a31923bb9e845dc54a34a686387c5b2a2a5f
Author: Philip Withnall <pwithnall endlessos org>
Date:   Thu Dec 3 14:30:29 2020 +0000

    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: 
https://github.com/python/cpython/blob/3529718925f40d14ed48d281d809187bc7314a14/Python/fileutils.c#L2227
    
    Spotted by Allison Karlitskaya in !1688.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 glib/gspawn.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
---
diff --git a/glib/gspawn.c b/glib/gspawn.c
index fa3cfe7c0..9b503cf7b 100644
--- a/glib/gspawn.c
+++ b/glib/gspawn.c
@@ -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 it’s 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
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]