Skip to content

Commit 57f5d52

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 650c449 commit 57f5d52

File tree

4 files changed

+9
-17
lines changed

4 files changed

+9
-17
lines changed

common-main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "git-compat-util.h"
1+
#include "cache.h"
22
#include "exec_cmd.h"
33

44
int main(int argc, char **av)
@@ -9,6 +9,13 @@ int main(int argc, char **av)
99
*/
1010
const char **argv = (const char **)av;
1111

12+
/*
13+
* Always open file descriptors 0/1/2 to avoid clobbering files
14+
* in die(). It also avoids messing up when the pipes are dup'ed
15+
* onto stdin/stdout/stderr in the child processes we spawn.
16+
*/
17+
sanitize_stdfds();
18+
1219
argv[0] = git_extract_argv0_path(argv[0]);
1320

1421
return cmd_main(argc, argv);

daemon.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,7 @@ int cmd_main(int argc, const char **argv)
13641364
if (detach) {
13651365
if (daemonize())
13661366
die("--detach not supported on this platform");
1367-
} else
1368-
sanitize_stdfds();
1367+
}
13691368

13701369
if (pid_file)
13711370
write_file(pid_file, "%"PRIuMAX, (uintmax_t) getpid());

git.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,6 @@ int cmd_main(int argc, const char **argv)
639639
if (!cmd)
640640
cmd = "git-help";
641641

642-
/*
643-
* Always open file descriptors 0/1/2 to avoid clobbering files
644-
* in die(). It also avoids messing up when the pipes are dup'ed
645-
* onto stdin/stdout/stderr in the child processes we spawn.
646-
*/
647-
sanitize_stdfds();
648-
649642
restore_sigpipe_to_default();
650643

651644
git_setup_gettext();

shell.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,6 @@ int cmd_main(int argc, const char **argv)
147147

148148
git_setup_gettext();
149149

150-
/*
151-
* Always open file descriptors 0/1/2 to avoid clobbering files
152-
* in die(). It also avoids messing up when the pipes are dup'ed
153-
* onto stdin/stdout/stderr in the child processes we spawn.
154-
*/
155-
sanitize_stdfds();
156-
157150
/*
158151
* Special hack to pretend to be a CVS server
159152
*/

0 commit comments

Comments
 (0)