Skip to content

Commit cf70f8d

Browse files
committed
mingw: demonstrate that all file handles are inherited by child processes
When spawning child processes, we really should be careful which file handles we let them inherit. This is doubly important on Windows, where we cannot rename, delete, or modify files if there is still a file handle open. Sadly, we have to guard this test inside #ifdef WIN32: we need to use the value of the HANDLE directly, and that concept does not exist on Linux/Unix. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 440ab39 commit cf70f8d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

t/helper/test-run-command.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,57 @@ static int quote_echo(int argc, const char **argv)
328328
return 0;
329329
}
330330

331+
static int inherit_handle(const char *argv0)
332+
{
333+
struct child_process cp = CHILD_PROCESS_INIT;
334+
char path[PATH_MAX];
335+
int tmp;
336+
337+
/* First, open an inheritable handle */
338+
xsnprintf(path, sizeof(path), "out-XXXXXX");
339+
tmp = xmkstemp(path);
340+
341+
argv_array_pushl(&cp.args,
342+
"test-tool", argv0, "inherited-handle-child", NULL);
343+
cp.in = -1;
344+
cp.no_stdout = cp.no_stderr = 1;
345+
if (start_command(&cp) < 0)
346+
die("Could not start child process");
347+
348+
/* Then close it, and try to delete it. */
349+
close(tmp);
350+
if (unlink(path))
351+
die("Could not delete '%s'", path);
352+
353+
if (close(cp.in) < 0 || finish_command(&cp) < 0)
354+
die("Child did not finish");
355+
356+
return 0;
357+
}
358+
359+
static int inherit_handle_child(void)
360+
{
361+
struct strbuf buf = STRBUF_INIT;
362+
363+
if (strbuf_read(&buf, 0, 0) < 0)
364+
die("Could not read stdin");
365+
printf("Received %s\n", buf.buf);
366+
strbuf_release(&buf);
367+
368+
return 0;
369+
}
370+
331371
int cmd__run_command(int argc, const char **argv)
332372
{
333373
struct child_process proc = CHILD_PROCESS_INIT;
334374
int jobs;
335375

336376
if (argc > 1 && !strcmp(argv[1], "testsuite"))
337377
exit(testsuite(argc - 1, argv + 1));
378+
if (!strcmp(argv[1], "inherited-handle"))
379+
exit(inherit_handle(argv[0]));
380+
if (!strcmp(argv[1], "inherited-handle-child"))
381+
exit(inherit_handle_child());
338382

339383
if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
340384
return !!quote_stress_test(argc - 1, argv + 1);

t/t0061-run-command.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ cat >hello-script <<-EOF
1212
cat hello-script
1313
EOF
1414

15+
test_expect_failure MINGW 'subprocess inherits only std handles' '
16+
test-tool run-command inherited-handle
17+
'
18+
1519
test_expect_success 'start_command reports ENOENT (slash)' '
1620
test-tool run-command start-command-ENOENT ./does-not-exist 2>err &&
1721
test_i18ngrep "\./does-not-exist" err

0 commit comments

Comments
 (0)