summaryrefslogtreecommitdiff
path: root/common-main.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-07-01 06:06:02 (GMT)
committerJunio C Hamano <gitster@pobox.com>2016-07-01 22:09:10 (GMT)
commit57f5d52a942e8bbfa82e2741faf050de0d6b3eb3 (patch)
tree01e59edce0ed0b96a8bcc275f21bc04a1db4bb34 /common-main.c
parent650c449250d7279dcbfe2f7cc23624955d53d339 (diff)
downloadgit-57f5d52a942e8bbfa82e2741faf050de0d6b3eb3.zip
git-57f5d52a942e8bbfa82e2741faf050de0d6b3eb3.tar.gz
git-57f5d52a942e8bbfa82e2741faf050de0d6b3eb3.tar.bz2
common-main: call sanitize_stdfds()
This is setup that should be done in every program for safety, but we never got around to adding it everywhere (so builtins benefited from the call in git.c, but any external commands did not). Putting it in the common main() gives us this safety everywhere. Note that the case in daemon.c is a little funny. We wait until we know whether we want to daemonize, and then either: - call daemonize(), which will close stdio and reopen it to /dev/null under the hood - sanitize_stdfds(), to fix up any odd cases But that is way too late; the point of sanitizing is to give us reliable descriptors on 0/1/2, and we will already have executed code, possibly called die(), etc. The sanitizing should be the very first thing that happens. With this patch, git-daemon will sanitize first, and can remove the call in the non-daemonize case. It does mean that daemonize() may just end up closing the descriptors we opened, but that's not a big deal (it's not wrong to do so, nor is it really less optimal than the case where our parent process redirected us from /dev/null ahead of time). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'common-main.c')
-rw-r--r--common-main.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/common-main.c b/common-main.c
index 57c912a..353c6ea 100644
--- a/common-main.c
+++ b/common-main.c
@@ -1,4 +1,4 @@
-#include "git-compat-util.h"
+#include "cache.h"
#include "exec_cmd.h"
int main(int argc, char **av)
@@ -9,6 +9,13 @@ int main(int argc, char **av)
*/
const char **argv = (const char **)av;
+ /*
+ * Always open file descriptors 0/1/2 to avoid clobbering files
+ * in die(). It also avoids messing up when the pipes are dup'ed
+ * onto stdin/stdout/stderr in the child processes we spawn.
+ */
+ sanitize_stdfds();
+
argv[0] = git_extract_argv0_path(argv[0]);
return cmd_main(argc, argv);