Permalink
Browse files

Add a generic VFIL_null_fd() function for pointing a fd at /dev/null

  • Loading branch information...
1 parent 328c18e commit 61de2e37b0ab6834746552cd3188bb20b1db8c52 @bsdphk bsdphk committed Jan 11, 2017
@@ -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);
@@ -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)
@@ -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);
View
@@ -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);
View
@@ -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)
{
@@ -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 */
@@ -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);
}

0 comments on commit 61de2e3

Please sign in to comment.