Skip to content

Commit 3e34d66

Browse files
j6tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 928500e commit 3e34d66

File tree

1 file changed

+8
-29
lines changed

1 file changed

+8
-29
lines changed

compat/mingw.c

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -299,46 +299,25 @@ int gettimeofday(struct timeval *tv, void *tz)
299299

300300
int pipe(int filedes[2])
301301
{
302-
int fd;
303-
HANDLE h[2], parent;
304-
305-
if (_pipe(filedes, 8192, 0) < 0)
306-
return -1;
302+
HANDLE h[2];
307303

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());
321307
return -1;
322308
}
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) {
327311
CloseHandle(h[0]);
328312
CloseHandle(h[1]);
329313
return -1;
330314
}
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) {
335317
close(filedes[0]);
336-
close(filedes[1]);
337318
CloseHandle(h[1]);
338319
return -1;
339320
}
340-
close(filedes[1]);
341-
filedes[1] = fd;
342321
return 0;
343322
}
344323

0 commit comments

Comments
 (0)