summaryrefslogtreecommitdiff
path: root/setup.c
diff options
context:
space:
mode:
authorThomas Rast <trast@inf.ethz.ch>2013-07-16 11:27:36 +0200
committerJunio C Hamano <gitster@pobox.com>2013-07-17 12:50:34 -0700
commit1d999ddd1daa6da2779d21b293ea9b275780bff8 (patch)
tree4c179cf554418c4acf3249a64b021225a5ec6361 /setup.c
parent62e91efafd2030b7dc33ac6450b4e9a316e6ecc6 (diff)
downloadgit-1d999ddd1daa6da2779d21b293ea9b275780bff8.tar.gz
daemon/shell: refactor redirection of 0/1/2 from /dev/null
Both daemon.c and shell.c contain logic to open FDs 0/1/2 from /dev/null if they are not already open. Move the function in daemon.c to setup.c and use it in shell.c, too. While there, remove a 'not' that inverted the meaning of the comment. The point is indeed to *avoid* messing up. Signed-off-by: Thomas Rast <trast@inf.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'setup.c')
-rw-r--r--setup.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/setup.c b/setup.c
index 1b120175f2..12a85d1613 100644
--- 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);
+}