From c3f4d68f0dae9844a80fec65b50670c1a0f53802 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Fri, 30 Jul 2010 12:16:24 +0200 Subject: [PATCH] pluto: Do not close all file descriptors on startup, just redirect stdin, stdout and stderr to /dev/null. Otherwise the pipe used to synchronize pluto->events with the main thread would be closed. --- src/pluto/plutomain.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/pluto/plutomain.c b/src/pluto/plutomain.c index 585cdae47..c172e8f3a 100644 --- a/src/pluto/plutomain.c +++ b/src/pluto/plutomain.c @@ -626,27 +626,19 @@ int main(int argc, char **argv) fflush(stdout); } - /* Close everything but ctl_fd and (if needed) stderr. - * There is some danger that a library that we don't know - * about is using some fd that we don't know about. - * I guess we'll soon find out. + /* Redirect stdin, stdout and stderr to /dev/null */ { - int i; - - for (i = getdtablesize() - 1; i >= 0; i--) /* Bad hack */ - { - if ((!log_to_stderr || i != 2) && i != ctl_fd) - close(i); - } - - /* make sure that stdin, stdout, stderr are reserved */ - if (open("/dev/null", O_RDONLY) != 0) + int fd; + if ((fd = open("/dev/null", O_RDWR)) == -1) abort(); - if (dup2(0, 1) != 1) + if (dup2(fd, 0) != 0) abort(); - if (!log_to_stderr && dup2(0, 2) != 2) + if (dup2(fd, 1) != 1) abort(); + if (!log_to_stderr && dup2(fd, 2) != 2) + abort(); + close(fd); } init_constants();