[master] 9b80c2e Add VSUB_closefrom() to close all fd's higher than some number.

Poul-Henning Kamp phk at FreeBSD.org
Wed Jan 6 14:51:54 CET 2016


commit 9b80c2e10d4f05d5051de6b7314e725809e84e96
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed Jan 6 13:48:04 2016 +0000

    Add VSUB_closefrom() to close all fd's higher than some number.
    
    Check if system has closefrom(2) and use that where available

diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 25c7ce4..c54ac60 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -57,6 +57,7 @@
 #include "vpf.h"
 #include "vrnd.h"
 #include "vsha256.h"
+#include "vsub.h"
 #include "vtim.h"
 #include "waiter/mgt_waiter.h"
 
@@ -530,8 +531,7 @@ main(int argc, char * const *argv)
 	 * Start out by closing all unwanted file descriptors we might
 	 * have inherited from sloppy process control daemons.
 	 */
-	for (o = sysconf(_SC_OPEN_MAX); o > STDERR_FILENO; o--)
-		(void)close(o);
+	VSUB_closefrom(STDERR_FILENO + 1);
 
 	VRND_Seed();
 
diff --git a/bin/varnishd/mgt/mgt_vcl.c b/bin/varnishd/mgt/mgt_vcl.c
index 37359f2..dec0ec0 100644
--- a/bin/varnishd/mgt/mgt_vcl.c
+++ b/bin/varnishd/mgt/mgt_vcl.c
@@ -93,6 +93,7 @@ mgt_vcl_del(struct vclprog *vp)
 	VTAILQ_REMOVE(&vclhead, vp, list);
 	XXXAZ(unlink(vp->fname));
 	bprintf(dn, "vcl_%s", vp->name);
+	WRONG("TEST");
 	VJ_master(JAIL_MASTER_FILE);
 	(void)rmdir(dn);		// compiler droppings, eg gcov
 	VJ_master(JAIL_MASTER_LOW);
diff --git a/configure.ac b/configure.ac
index f89977b..da9f7ca 100644
--- a/configure.ac
+++ b/configure.ac
@@ -245,6 +245,7 @@ AC_CHECK_FUNCS([getdtablesize])
 AC_CHECK_FUNCS([nanosleep])
 AC_CHECK_FUNCS([setppriv])
 AC_CHECK_FUNCS([fallocate])
+AC_CHECK_FUNCS([closefrom])
 
 save_LIBS="${LIBS}"
 LIBS="${PTHREAD_LIBS}"
diff --git a/include/vsub.h b/include/vsub.h
index bc337a1..de62908 100644
--- a/include/vsub.h
+++ b/include/vsub.h
@@ -33,3 +33,5 @@ typedef void vsub_func_f(void*);
 
 unsigned VSUB_run(struct vsb *, vsub_func_f *, void *priv, const char *name,
     int maxlines);
+
+void VSUB_closefrom(int fd);
diff --git a/lib/libvarnish/vsub.c b/lib/libvarnish/vsub.c
index f7b6a07..c87d297 100644
--- a/lib/libvarnish/vsub.c
+++ b/lib/libvarnish/vsub.c
@@ -52,6 +52,18 @@ struct vsub_priv {
 	int		maxlines;
 };
 
+void
+VSUB_closefrom(int fd)
+{
+#ifdef HAVE_CLOSEFROM
+	closefrom(fd);
+#else
+	int i;= sysconf(_SC_OPEN_MAX);
+	for (i = sysconf(_SC_OPEN_MAX); i > STDERR_FILENO; i--)
+		(void)close(i);
+#endif
+}
+
 static int
 vsub_vlu(void *priv, const char *str)
 {
@@ -70,7 +82,7 @@ unsigned
 VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
     int maxlines)
 {
-	int rv, p[2], sfd, hfd, status;
+	int rv, p[2], status;
 	pid_t pid;
 	struct vlu *vlu;
 	struct vsub_priv sp;
@@ -100,9 +112,7 @@ VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
 		assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
 		assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
 		/* Close all other fds */
-		hfd = sysconf(_SC_OPEN_MAX);
-		for (sfd = STDERR_FILENO+1; sfd < hfd; sfd++)
-			(void)close(sfd);
+		VSUB_closefrom(STDERR_FILENO + 1);
 		func(priv);
 		/*
 		 * func should either exec or exit, so getting here should be



More information about the varnish-commit mailing list