git: ensure 0/1/2 are open in main()
[gitweb.git] / setup.c
diff --git a/setup.c b/setup.c
index 1b120175f2184b782f389d2fc99f3e37710f35d3..12a85d16138f1500d8e86c4c5f80273b615e657a 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -872,3 +872,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);
+}