These non-standard (former is Linux and FreeBSD, latter Solaris and
*BSD) functions are useful to avoid leaking unintended file descriptors
(i.e. those opened without O_CLOEXEC by negligence rather than
intentionally) to children on exec*(). Without them, one either has to
blindly call close() on integers up to _SC_OPEN_MAX (which can be very
large), or look up open file descriptors from /proc/self/fd (which
cannot be done safely after fork, since opendir() is not
async-signal-safe, not to mention this approach is Linux-specific).

The functions will be used in the following commit.
---
 compat/Makefile.local     |  4 ++++
 compat/closefrom.c        | 25 +++++++++++++++++++++++++
 compat/have_close_range.c |  8 ++++++++
 compat/have_closefrom.c   |  8 ++++++++
 configure                 | 32 ++++++++++++++++++++++++++++++++
 5 files changed, 77 insertions(+)
 create mode 100644 compat/closefrom.c
 create mode 100644 compat/have_close_range.c
 create mode 100644 compat/have_closefrom.c

diff --git a/compat/Makefile.local b/compat/Makefile.local
index c58ca746..70448be9 100644
--- a/compat/Makefile.local
+++ b/compat/Makefile.local
@@ -5,6 +5,10 @@ extra_cflags += -I$(srcdir)/$(dir)
 
 notmuch_compat_srcs :=
 
+ifeq ($(HAVE_CLOSE_RANGE)$(HAVE_CLOSEFROM),00)
+notmuch_compat_srcs += $(dir)/closefrom.c
+endif
+
 ifneq ($(HAVE_GETLINE),1)
 notmuch_compat_srcs += $(dir)/getline.c $(dir)/getdelim.c
 endif
diff --git a/compat/closefrom.c b/compat/closefrom.c
new file mode 100644
index 00000000..6e9cc12d
--- /dev/null
+++ b/compat/closefrom.c
@@ -0,0 +1,25 @@
+/* closefrom.c --- Implementation of replacement closefrom function.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 3, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.  */
+
+#include <unistd.h>
+
+void
+closefrom (int lowfd)
+{
+    for (int i = lowfd; i < 1024; i++)
+       close (i);
+}
diff --git a/compat/have_close_range.c b/compat/have_close_range.c
new file mode 100644
index 00000000..4a1a947f
--- /dev/null
+++ b/compat/have_close_range.c
@@ -0,0 +1,8 @@
+#define _GNU_SOURCE
+#include <unistd.h>
+
+int
+main ()
+{
+    return close_range (0, ~0U, 0);
+}
diff --git a/compat/have_closefrom.c b/compat/have_closefrom.c
new file mode 100644
index 00000000..438dd7b1
--- /dev/null
+++ b/compat/have_closefrom.c
@@ -0,0 +1,8 @@
+#include <unistd.h>
+
+int
+main ()
+{
+    closefrom (0);
+    return 0;
+}
diff --git a/configure b/configure
index 7afd08c7..ae1939d9 100755
--- a/configure
+++ b/configure
@@ -1184,6 +1184,28 @@ else
 fi
 rm -f compat/have_timegm
 
+printf "Checking for close_range... "
+have_close_range="0"
+have_closefrom="0"
+if ${CC} -o compat/have_close_range "$srcdir"/compat/have_close_range.c > 
/dev/null 2>&1
+then
+    printf "Yes.\n"
+    have_close_range="1"
+else
+    printf "No (will try closefrom).\n"
+
+    printf "Checking for closefrom... "
+    if ${CC} -o compat/have_closefrom "$srcdir"/compat/have_closefrom.c > 
/dev/null 2>&1
+    then
+        printf "Yes.\n"
+        have_closefrom="1"
+    else
+        printf "No (will use our own instead).\n"
+    fi
+    rm -f compat/have_closefrom
+fi
+rm -f compat/have_close_range
+
 cat <<EOF > _time_t.c
 #include <time.h>
 #include <assert.h>
@@ -1473,6 +1495,14 @@ HAVE_STRSEP = ${have_strsep}
 # build its own version)
 HAVE_TIMEGM = ${have_timegm}
 
+# Whether the close_range function is available (if not, then notmuch will
+# use closefrom)
+HAVE_CLOSE_RANGE = ${have_close_range}
+
+# Whether the closefrom function is available (if not, then notmuch will
+# use its own version)
+HAVE_CLOSEFROM = ${have_closefrom}
+
 # Whether struct dirent has d_type (if not, then notmuch will use stat)
 HAVE_D_TYPE = ${have_d_type}
 
@@ -1558,6 +1588,8 @@ COMMON_CONFIGURE_CFLAGS = \\
        -DHAVE_STRCASESTR=\$(HAVE_STRCASESTR)                   \\
        -DHAVE_STRSEP=\$(HAVE_STRSEP)                           \\
        -DHAVE_TIMEGM=\$(HAVE_TIMEGM)                           \\
+       -DHAVE_CLOSE_RANGE=\$(HAVE_CLOSE_RANGE)                 \\
+       -DHAVE_CLOSEFROM=\$(HAVE_CLOSEFROM)                     \\
        -DHAVE_D_TYPE=\$(HAVE_D_TYPE)                           \\
        -DSTD_GETPWUID=\$(STD_GETPWUID)                         \\
        -DSTD_ASCTIME=\$(STD_ASCTIME)                           \\
-- 
2.47.3

_______________________________________________
notmuch mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to