[master] 61de2e3 Add a generic VFIL_null_fd() function for pointing a fd at /dev/null
Poul-Henning Kamp
phk at FreeBSD.org
Wed Jan 11 13:28:04 CET 2017
commit 61de2e37b0ab6834746552cd3188bb20b1db8c52
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Wed Jan 11 12:27:28 2017 +0000
Add a generic VFIL_null_fd() function for pointing a fd at /dev/null
diff --git a/bin/varnishd/mgt/mgt_child.c b/bin/varnishd/mgt/mgt_child.c
index 5f23a24..988643a 100644
--- a/bin/varnishd/mgt/mgt_child.c
+++ b/bin/varnishd/mgt/mgt_child.c
@@ -50,6 +50,7 @@
#include "vbm.h"
#include "vcli_serve.h"
#include "vev.h"
+#include "vfil.h"
#include "vlu.h"
#include "vtim.h"
@@ -299,7 +300,7 @@ mgt_launch_child(struct cli *cli)
unsigned u;
char *p;
struct vev *e;
- int i, j, k, cp[2];
+ int i, cp[2];
struct sigaction sa;
if (child_state != CH_STOPPED && child_state != CH_DIED)
@@ -345,8 +346,7 @@ mgt_launch_child(struct cli *cli)
if (pid == 0) {
/* Redirect stdin/out/err */
- AZ(close(STDIN_FILENO));
- assert(open("/dev/null", O_RDONLY) == STDIN_FILENO);
+ VFIL_null_fd(STDIN_FILENO);
assert(dup2(heritage.std_fd, STDOUT_FILENO) == STDOUT_FILENO);
assert(dup2(heritage.std_fd, STDERR_FILENO) == STDERR_FILENO);
@@ -366,13 +366,8 @@ mgt_launch_child(struct cli *cli)
for (i = STDERR_FILENO + 1; i < CLOSE_FD_UP_TO; i++) {
if (vbit_test(fd_map, i))
continue;
- if (close(i) == 0) {
- k = open("/dev/null", O_RDONLY);
- assert(k >= 0);
- j = dup2(k, i);
- assert(j == i);
- AZ(close(k));
- }
+ if (close(i) == 0)
+ VFIL_null_fd(i);
}
#ifdef HAVE_SETPROCTITLE
setproctitle("Varnish-Chld %s", heritage.name);
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 5d3126b..dce1dba 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -363,12 +363,9 @@ cli_stdin_close(void *priv)
(void)VPF_Remove(pfh);
exit(0);
} else {
- (void)close(0);
- (void)close(1);
- (void)close(2);
- AZ(open("/dev/null", O_RDONLY));
- assert(open("/dev/null", O_WRONLY) == 1);
- assert(open("/dev/null", O_WRONLY) == 2);
+ VFIL_null_fd(STDIN_FILENO);
+ VFIL_null_fd(STDOUT_FILENO);
+ VFIL_null_fd(STDERR_FILENO);
}
}
@@ -532,7 +529,6 @@ mgt_x_arg(const char *x_arg)
ARGV_ERR("Invalid -x argument\n");
}
-
/*--------------------------------------------------------------------*/
#define ERIC_MAGIC 0x2246988a /* Eric is not random */
@@ -541,7 +537,6 @@ static int
mgt_eric(void)
{
int eric_pipes[2];
- int fd;
unsigned u;
ssize_t sz;
@@ -555,11 +550,7 @@ mgt_eric(void)
AZ(close(eric_pipes[0]));
assert(setsid() > 1);
- fd = open("/dev/null", O_RDWR, 0);
- assert(fd > 0);
- assert(dup2(fd, STDIN_FILENO) == STDIN_FILENO);
- if (fd > STDIN_FILENO)
- AZ(close(fd));
+ VFIL_null_fd(STDIN_FILENO);
return (eric_pipes[1]);
default:
break;
@@ -577,28 +568,13 @@ mgt_eric(void)
static void
mgt_eric_im_done(int eric_fd, unsigned u)
{
- int fd;
-
- if (eric_fd < 0)
- return;
if (u == 0)
u = ERIC_MAGIC;
- fd = open("/dev/null", O_RDONLY);
- assert(fd >= 0);
- assert(dup2(fd, STDIN_FILENO) == STDIN_FILENO);
- AZ(close(fd));
-
- fd = open("/dev/null", O_WRONLY);
- assert(fd >= 0);
- assert(dup2(fd, STDOUT_FILENO) == STDOUT_FILENO);
- AZ(close(fd));
-
- fd = open("/dev/null", O_WRONLY);
- assert(fd >= 0);
- assert(dup2(fd, STDERR_FILENO) == STDERR_FILENO);
- AZ(close(fd));
+ VFIL_null_fd(STDIN_FILENO);
+ VFIL_null_fd(STDOUT_FILENO);
+ VFIL_null_fd(STDERR_FILENO);
assert(write(eric_fd, &u, sizeof u) == sizeof u);
AZ(close(eric_fd));
@@ -951,7 +927,11 @@ main(int argc, char * const *argv)
u = MGT_Run();
- mgt_eric_im_done(eric_fd, u);
+ if (eric_fd >= 0)
+ mgt_eric_im_done(eric_fd, u);
+
+ if (F_flag)
+ VFIL_null_fd(STDIN_FILENO);
o = vev_schedule(mgt_evb);
if (o != 0)
diff --git a/bin/varnishtest/vtc_main.c b/bin/varnishtest/vtc_main.c
index 1ca41f4..0a2c9ae 100644
--- a/bin/varnishtest/vtc_main.c
+++ b/bin/varnishtest/vtc_main.c
@@ -282,8 +282,7 @@ start_test(void)
jp->child = fork();
assert(jp->child >= 0);
if (jp->child == 0) {
- AZ(close(STDIN_FILENO));
- assert(open("/dev/null", O_RDONLY) == STDIN_FILENO);
+ VFIL_null_fd(STDIN_FILENO);
assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
VSUB_closefrom(STDERR_FILENO + 1);
diff --git a/include/vfil.h b/include/vfil.h
index 5d64dcb..48d6f62 100644
--- a/include/vfil.h
+++ b/include/vfil.h
@@ -31,6 +31,9 @@
struct vfil_path;
/* from libvarnish/vfil.c */
+
+void VFIL_null_fd(int);
+
int seed_random(void);
char *VFIL_readfile(const char *pfx, const char *fn, ssize_t *sz);
int VFIL_writefile(const char *pfx, const char *fn, const char *buf, size_t sz);
diff --git a/lib/libvarnish/vfil.c b/lib/libvarnish/vfil.c
index 489068e..ac39aff 100644
--- a/lib/libvarnish/vfil.c
+++ b/lib/libvarnish/vfil.c
@@ -62,6 +62,18 @@
#include "vfil.h"
#include "vqueue.h"
+void
+VFIL_null_fd(int target)
+{
+ int fd;
+
+ assert(target >= 0);
+ fd = open("/dev/null", O_RDWR);
+ assert(fd >= 0);
+ assert(dup2(fd, target) == target);
+ AZ(close(fd));
+}
+
static char *
vfil_readfd(int fd, ssize_t *sz)
{
diff --git a/lib/libvarnish/vsub.c b/lib/libvarnish/vsub.c
index 90b1c7c..87384c5 100644
--- a/lib/libvarnish/vsub.c
+++ b/lib/libvarnish/vsub.c
@@ -42,6 +42,7 @@
#include "vas.h"
#include "vdef.h"
+#include "vfil.h"
#include "vlu.h"
#include "vsb.h"
#include "vsub.h"
@@ -112,8 +113,7 @@ VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
return (1);
}
if (pid == 0) {
- AZ(close(STDIN_FILENO));
- assert(open("/dev/null", O_RDONLY) == STDIN_FILENO);
+ VFIL_null_fd(STDIN_FILENO);
assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
/* Close all other fds */
diff --git a/lib/libvarnishapi/daemon.c b/lib/libvarnishapi/daemon.c
index e68b3a1..aba6433 100644
--- a/lib/libvarnishapi/daemon.c
+++ b/lib/libvarnishapi/daemon.c
@@ -39,6 +39,7 @@
#include <unistd.h>
#include "compat/daemon.h"
+#include "vfil.h"
int
varnish_daemon(int nochdir, int noclose)
@@ -81,12 +82,10 @@ varnish_daemon(int nochdir, int noclose)
if (!nochdir)
(void)chdir("/");
- if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
- (void)dup2(fd, STDIN_FILENO);
- (void)dup2(fd, STDOUT_FILENO);
- (void)dup2(fd, STDERR_FILENO);
- if (fd > 2)
- (void)close(fd);
+ if (!noclose) {
+ VFIL_null_fd(STDIN_FILENO);
+ VFIL_null_fd(STDOUT_FILENO);
+ VFIL_null_fd(STDERR_FILENO);
}
return (0);
}
More information about the varnish-commit
mailing list