X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=shell.c;h=e3393690dd3b79af80e6b95710583e744fd574d4;hb=fd631d5828ec6b89e86e0568e5e798787fed9bbd;hp=0f6a727a8c267cdeb3a93c30bc688bb64ecd8003;hpb=a1184d85e8752658f02746982822f43f32316803;p=git.git diff --git a/shell.c b/shell.c index 0f6a727a8..e3393690d 100644 --- a/shell.c +++ b/shell.c @@ -48,6 +48,19 @@ int main(int argc, char **argv) { char *prog; struct commands *cmd; + int devnull_fd; + + /* + * Always open file descriptors 0/1/2 to avoid clobbering files + * in die(). It also avoids not messing up when the pipes are + * dup'ed onto stdin/stdout/stderr in the child processes we spawn. + */ + devnull_fd = open("/dev/null", O_RDWR); + while (devnull_fd >= 0 && devnull_fd <= 2) + devnull_fd = dup(devnull_fd); + if (devnull_fd == -1) + die("opening /dev/null failed (%s)", strerror(errno)); + close (devnull_fd); /* * Special hack to pretend to be a CVS server