Windows: simplify the pipe(2) implementation
Our implementation of pipe() must create non-inheritable handles for the reason that when a child process is started, there is no opportunity to close the unneeded pipe ends in the child (on POSIX this is done between fork() and exec()). Previously, we used the _pipe() function provided by Microsoft's C runtime (which creates inheritable handles) and then turned the handles into non-inheritable handles using the DuplicateHandle() API. Simplify the procedure by using the CreatePipe() API, which can create non-inheritable handles right from the beginning. Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Sixt committed
Jan 15, 2010 at 21:12 UTC
3e34d6657733430164ef67ab2f000fa3d10d51b5
1 file changed
+8
-29
compat/mingw.c
+8
-29
@@ -299,46 +299,25 @@ int gettimeofday(struct timeval *tv, void *tz)
299
300
int pipe(int filedes[2])
301
{
302
- int fd;
303
- HANDLE h[2], parent;
304
-
305
- if (_pipe(filedes, 8192, 0) < 0)
306
- return -1;
302
+ HANDLE h[2];
303
308
- parent = GetCurrentProcess();
309
-
310
- if (!DuplicateHandle (parent, (HANDLE)_get_osfhandle(filedes[0]),
311
- parent, &h[0], 0, FALSE, DUPLICATE_SAME_ACCESS)) {
312
- close(filedes[0]);
313
- close(filedes[1]);
314
- return -1;
315
- }
316
- if (!DuplicateHandle (parent, (HANDLE)_get_osfhandle(filedes[1]),
317
- parent, &h[1], 0, FALSE, DUPLICATE_SAME_ACCESS)) {
318
- close(filedes[0]);
319
- close(filedes[1]);
320
- CloseHandle(h[0]);
304
+ /* this creates non-inheritable handles */
305
+ if (!CreatePipe(&h[0], &h[1], NULL, 8192)) {
306
+ errno = err_win_to_posix(GetLastError());
307
return -1;
308
}
323
- fd = _open_osfhandle((int)h[0], O_NOINHERIT);
324
- if (fd < 0) {
325
- close(filedes[0]);
326
- close(filedes[1]);
309
+ filedes[0] = _open_osfhandle((int)h[0], O_NOINHERIT);
310
+ if (filedes[0] < 0) {
311
CloseHandle(h[0]);
312
CloseHandle(h[1]);
313
return -1;
314
}
331
- close(filedes[0]);
332
- filedes[0] = fd;
333
- fd = _open_osfhandle((int)h[1], O_NOINHERIT);
334
- if (fd < 0) {
315
+ filedes[1] = _open_osfhandle((int)h[1], O_NOINHERIT);
316
+ if (filedes[0] < 0) {
317
close(filedes[0]);
336
- close(filedes[1]);
318
CloseHandle(h[1]);
319
return -1;
320
}
340
- close(filedes[1]);
341
- filedes[1] = fd;
321
return 0;
322
}
323