aboutsummaryrefslogtreecommitdiff
path: root/compat/mingw.c
diff options
context:
space:
mode:
authorJohannes Sixt <johannes.sixt@telecom.at>2007-12-07 22:05:36 +0100
committerJohannes Sixt <johannes.sixt@telecom.at>2008-06-23 13:40:31 +0200
commit897bb8cb2c2ce6b73038bd8d4106fde079a09cf6 (patch)
tree80307ea3202fd6faa95d35873945860f23e415f1 /compat/mingw.c
parentf1a4dfb85a432ae338d162179eaac5d50154fbeb (diff)
downloadgit-897bb8cb2c2ce6b73038bd8d4106fde079a09cf6.tar.gz
git-897bb8cb2c2ce6b73038bd8d4106fde079a09cf6.tar.xz
Windows: A pipe() replacement whose ends are not inherited to children.
On Unix the idiom to use a pipe is as follows: pipe(fd); pid = fork(); if (!pid) { dup2(fd[1], 1); close(fd[1]); close(fd[0]); ... } close(fd[1]); i.e. the child process closes the both pipe ends after duplicating one to the file descriptors where they are needed. On Windows, which does not have fork(), we never have an opportunity to (1) duplicate a pipe end in the child, (2) close unused pipe ends. Instead, we must use this idiom: save1 = dup(1); pipe(fd); dup2(fd[1], 1); spawn(...); dup2(save1, 1); close(fd[1]); i.e. save away the descriptor at the destination slot, replace by the pipe end, spawn process, restore the saved file. But there is a problem: Notice that the child did not only inherit the dup2()ed descriptor, but also *both* original pipe ends. Although the one end that was dup()ed could be closed before the spawn(), we cannot close the other end - the child inherits it, no matter what. The solution is to generate non-inheritable pipes. At the first glance, this looks strange: The purpose of pipes is usually to be inherited to child processes. But notice that in the course of actions as outlined above, the pipe descriptor that we want to inherit to the child is dup2()ed, and as it so happens, Windows's dup2() creates inheritable duplicates. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Diffstat (limited to 'compat/mingw.c')
-rw-r--r--compat/mingw.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/compat/mingw.c b/compat/mingw.c
index 89b29c55e..31a9b9e25 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -54,6 +54,51 @@ int gettimeofday(struct timeval *tv, void *tz)
return 0;
}
+int pipe(int filedes[2])
+{
+ int fd;
+ HANDLE h[2], parent;
+
+ if (_pipe(filedes, 8192, 0) < 0)
+ return -1;
+
+ parent = GetCurrentProcess();
+
+ if (!DuplicateHandle (parent, (HANDLE)_get_osfhandle(filedes[0]),
+ parent, &h[0], 0, FALSE, DUPLICATE_SAME_ACCESS)) {
+ close(filedes[0]);
+ close(filedes[1]);
+ return -1;
+ }
+ if (!DuplicateHandle (parent, (HANDLE)_get_osfhandle(filedes[1]),
+ parent, &h[1], 0, FALSE, DUPLICATE_SAME_ACCESS)) {
+ close(filedes[0]);
+ close(filedes[1]);
+ CloseHandle(h[0]);
+ return -1;
+ }
+ fd = _open_osfhandle((int)h[0], O_NOINHERIT);
+ if (fd < 0) {
+ close(filedes[0]);
+ close(filedes[1]);
+ CloseHandle(h[0]);
+ CloseHandle(h[1]);
+ return -1;
+ }
+ close(filedes[0]);
+ filedes[0] = fd;
+ fd = _open_osfhandle((int)h[1], O_NOINHERIT);
+ if (fd < 0) {
+ close(filedes[0]);
+ close(filedes[1]);
+ CloseHandle(h[1]);
+ return -1;
+ }
+ close(filedes[1]);
+ filedes[1] = fd;
+ return 0;
+}
+
int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
{
return -1;