Merge branch 'jk/t0008-sigpipe-fix'
[gitweb.git] / setup.c
diff --git a/setup.c b/setup.c
index 94c1e61bda747ed5c664bbbf8431234dc8276d29..88aab94f1595de5a52c8c99ae8b8c4b9322b2fcf 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -908,3 +908,15 @@ const char *resolve_gitdir(const char *suspect)
                return suspect;
        return read_gitfile(suspect);
 }
+
+/* if any standard file descriptor is missing open it to /dev/null */
+void sanitize_stdfds(void)
+{
+       int fd = open("/dev/null", O_RDWR, 0);
+       while (fd != -1 && fd < 2)
+               fd = dup(fd);
+       if (fd == -1)
+               die_errno("open /dev/null or dup failed");
+       if (fd > 2)
+               close(fd);
+}