diff options
author | Johannes Sixt <j6t@kdbg.org> | 2010-01-15 21:12:18 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2010-01-16 16:43:53 -0800 |
commit | 75301f90159a505f3683a5eba10174928dc30fb1 (patch) | |
tree | 996c0fdd16348c4496b3a35fee1d249036d58a91 /compat/mingw.h | |
parent | 3e34d6657733430164ef67ab2f000fa3d10d51b5 (diff) | |
download | git-75301f90159a505f3683a5eba10174928dc30fb1.tar.gz |
Windows: avoid the "dup dance" when spawning a child process
When stdin, stdout, or stderr must be redirected for a child process that
on Windows is spawned using one of the spawn() functions of Microsoft's
C runtime, then there is no choice other than to
1. make a backup copy of fd 0,1,2 with dup
2. dup2 the redirection source fd into 0,1,2
3. spawn
4. dup2 the backup back into 0,1,2
5. close the backup copy and the redirection source
We used this idiom as well -- but we are not using the spawn() functions
anymore!
Instead, we have our own implementation. We had hardcoded that stdin,
stdout, and stderr of the child process were inherited from the parent's
fds 0, 1, and 2. But we can actually specify any fd.
With this patch, the fds to inherit are passed from start_command()'s
WIN32 section to our spawn implementation. This way, we can avoid the
backup copies of the fds.
The backup copies were a bug waiting to surface: The OS handles underlying
the dup()ed fds were inherited by the child process (but were not
associated with a file descriptor in the child). Consequently, the file or
pipe represented by the OS handle remained open even after the backup copy
was closed in the parent process until the child exited.
Since our implementation of pipe() creates non-inheritable OS handles, we
still dup() file descriptors in start_command() because dup() happens to
create inheritable duplicates. (A nice side effect is that the fd cleanup
in start_command is the same for Windows and Unix and remains unchanged.)
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'compat/mingw.h')
-rw-r--r-- | compat/mingw.h | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/compat/mingw.h b/compat/mingw.h index b3d299f5bc..a105dc9a27 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -220,7 +220,8 @@ int mingw_fstat(int fd, struct stat *buf); int mingw_utime(const char *file_name, const struct utimbuf *times); #define utime mingw_utime -pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env); +pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env, + int fhin, int fhout, int fherr); void mingw_execvp(const char *cmd, char *const *argv); #define execvp mingw_execvp |