From 258e93a155f88fd7385e4d1e26f287044ba973e9 Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Thu, 13 Jul 2006 18:32:11 +0200 Subject: [PATCH] daemon: if one of the standard fds is missing open it to /dev/null Signed-off-by: Matthias Lederhofer Signed-off-by: Junio C Hamano --- daemon.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/daemon.c b/daemon.c index a7636bc50c..01ccda33ea 100644 --- a/daemon.c +++ b/daemon.c @@ -662,6 +662,18 @@ static int service_loop(int socknum, int *socklist) } } +/* if any standard file descriptor is missing open it to /dev/null */ +static void sanitize_stdfds(void) +{ + int fd = open("/dev/null", O_RDWR, 0); + while (fd != -1 && fd < 2) + fd = dup(fd); + if (fd == -1) + die("open /dev/null or dup failed: %s", strerror(errno)); + if (fd > 2) + close(fd); +} + static int serve(int port) { int socknum, *socklist; @@ -773,5 +785,7 @@ int main(int argc, char **argv) return execute(peer); } + sanitize_stdfds(); + return serve(port); }