]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
sysdeps: Use close_range() if available
authorSimon McVittie <smcv@collabora.com>
Tue, 1 Mar 2022 19:33:06 +0000 (19:33 +0000)
committerSimon McVittie <smcv@collabora.com>
Thu, 21 Apr 2022 10:02:14 +0000 (11:02 +0100)
The version with no flags set, which is a slight generalization of
closefrom(), is available on recent Linux and FreeBSD.

The version with CLOSE_RANGE_CLOEXEC is Linux-specific.

Signed-off-by: Simon McVittie <smcv@collabora.com>
cmake/ConfigureChecks.cmake
cmake/config.h.cmake
configure.ac
dbus/dbus-sysdeps-unix.c

index 78b0fa952d8ea98c7a4a0f8f29383eebee272e4c..f2ca64c7102b7826860ecf26ac40a4fd63298515 100644 (file)
@@ -53,6 +53,7 @@ check_symbol_exists(setenv       "stdlib.h"         HAVE_SETENV)             #
 check_symbol_exists(unsetenv     "stdlib.h"         HAVE_UNSETENV)           #  dbus-sysdeps.c
 check_symbol_exists(clearenv     "stdlib.h"         HAVE_CLEARENV)           #  dbus-sysdeps.c
 check_symbol_exists(closefrom    "unistd.h"         HAVE_CLOSEFROM)          #  dbus-sysdeps-unix.c
+check_symbol_exists(close_range  "unistd.h"         HAVE_CLOSE_RANGE)        #  dbus-sysdeps-unix.c
 check_symbol_exists(writev       "sys/uio.h"        HAVE_WRITEV)             #  dbus-sysdeps.c, dbus-sysdeps-win.c
 check_symbol_exists(setrlimit    "sys/resource.h"   HAVE_SETRLIMIT)          #  dbus-sysdeps.c, dbus-sysdeps-win.c, test/test-segfault.c
 check_symbol_exists(socketpair   "sys/socket.h"     HAVE_SOCKETPAIR)         #  dbus-sysdeps.c
index 48c5db43e6f7216971d85c9cef9faebac1e82024..74299d1ea5406ab9eb962f3da93adc9b0c270d20 100644 (file)
 /* Define to 1 if you have closefrom */
 #cmakedefine   HAVE_CLOSEFROM 1
 
+/* Define to 1 if you have close_range */
+#cmakedefine   HAVE_CLOSE_RANGE 1
+
 /* Define to 1 if you have writev */
 #cmakedefine   HAVE_WRITEV 1
 
index dbaf14b77b3b447c996a7b6cd92e96f1e78523fa..afe21813fdcbca2824e16ae3474f5173b248fb8e 100644 (file)
@@ -382,6 +382,7 @@ AS_IF([test x$enable_code_coverage = xyes],[
 AC_CHECK_FUNCS_ONCE([
 accept4
 clearenv
+close_range
 closefrom
 fpathconf
 getgrouplist
index 9640e79f10520923720a3d9dd463faebf8540b17..c3dd0b83e410869a5ac23a1af09bdb74cb29b058 100644 (file)
@@ -37,6 +37,7 @@
 #include "dbus-credentials.h"
 #include "dbus-nonce.h"
 
+#include <limits.h>
 #include <sys/types.h>
 #include <stdlib.h>
 #include <string.h>
@@ -4796,6 +4797,11 @@ act_on_fds_3_and_up (void (*func) (int fd))
 void
 _dbus_close_all (void)
 {
+#ifdef HAVE_CLOSE_RANGE
+  if (close_range (3, INT_MAX, 0) == 0)
+    return;
+#endif
+
   /* Some library implementations of closefrom() are not async-signal-safe,
    * and we call _dbus_close_all() after forking, so we only do this on
    * operating systems where we know that closefrom() is a system call */
@@ -4818,6 +4824,11 @@ _dbus_close_all (void)
 void
 _dbus_fd_set_all_close_on_exec (void)
 {
+#if defined(HAVE_CLOSE_RANGE) && defined(CLOSE_RANGE_CLOEXEC)
+  if (close_range (3, INT_MAX, CLOSE_RANGE_CLOEXEC) == 0)
+    return;
+#endif
+
   act_on_fds_3_and_up (_dbus_fd_set_close_on_exec);
 }