From 85e94b981ef0df32c3e5170499298369513bf864 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Thu, 18 Jan 2007 14:04:06 +0100 Subject: [PATCH] Do not leak pipe file handles into the child processes. Windows's _pipe() by default allocates inheritable pipes. However, when a spawn happens, we do not have a possiblility to close the unused pipe ends in the child process. This is a problem. Consider the following situation: The child process only reads from the pipe and the parent process uses only the writable end; the parent even closes the writable end. As it happens, the child at this time usually still waits for input in a read(). But since the child has inherited an open writable end, it does not get EOF and hangs ad infinitum. For this reason, pipe handles must not be inheritable. At the first glance, this is curious, since after all it is the purpose of pipes to be inherited by child processes. However, in all cases where this inheritance is needed for a file descriptor, it is dup2()'d to stdin or stdout anyway, and, lo and behold, Windows's dup2() creates inheritable duplicates. --- compat/mingw.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index 53b3f780a4..8ed621d004 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -86,7 +86,47 @@ int gettimeofday(struct timeval *tv, void *tz) } int pipe(int filedes[2]) { - return _pipe(filedes, 4096, 0); + int fd; + HANDLE h[2], parent; + + if (_pipe(filedes, 4096, 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(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(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)