daemon: if one of the standard fds is missing open it to /dev/null
Signed-off-by: Matthias Lederhofer <matled@gmx.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Matthias Lederhofer committed
Jul 13, 2006 at 18:32 UTC
258e93a155f88fd7385e4d1e26f287044ba973e9
1 file changed
+14
daemon.c
+14
@@ -662,6 +662,18 @@ static int service_loop(int socknum, int *socklist)
662
}
663
}
664
665
+/* if any standard file descriptor is missing open it to /dev/null */
666
+static void sanitize_stdfds(void)
667
+{
668
+ int fd = open("/dev/null", O_RDWR, 0);
669
+ while (fd != -1 && fd < 2)
670
+ fd = dup(fd);
671
+ if (fd == -1)
672
+ die("open /dev/null or dup failed: %s", strerror(errno));
673
+ if (fd > 2)
674
+ close(fd);
675
+}
676
+
677
static int serve(int port)
678
{
679
int socknum, *socklist;
@@ -773,5 +785,7 @@ int main(int argc, char **argv)
785
return execute(peer);
786
}
787
788
+ sanitize_stdfds();
789
+
790
return serve(port);
791
}