Skip to content

Commit 2205530

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 79980f8 commit 2205530

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
@@ -200,13 +200,57 @@ static int testsuite(int argc, const char **argv)
200200
return !!ret;
201201
}
202202

203+
static int inherit_handle(const char *argv0)
204+
{
205+
struct child_process cp = CHILD_PROCESS_INIT;
206+
char path[PATH_MAX];
207+
int tmp;
208+
209+
/* First, open an inheritable handle */
210+
xsnprintf(path, sizeof(path), "out-XXXXXX");
211+
tmp = xmkstemp(path);
212+
213+
argv_array_pushl(&cp.args,
214+
"test-tool", argv0, "inherited-handle-child", NULL);
215+
cp.in = -1;
216+
cp.no_stdout = cp.no_stderr = 1;
217+
if (start_command(&cp) < 0)
218+
die("Could not start child process");
219+
220+
/* Then close it, and try to delete it. */
221+
close(tmp);
222+
if (unlink(path))
223+
die("Could not delete '%s'", path);
224+
225+
if (close(cp.in) < 0 || finish_command(&cp) < 0)
226+
die("Child did not finish");
227+
228+
return 0;
229+
}
230+
231+
static int inherit_handle_child(void)
232+
{
233+
struct strbuf buf = STRBUF_INIT;
234+
235+
if (strbuf_read(&buf, 0, 0) < 0)
236+
die("Could not read stdin");
237+
printf("Received %s\n", buf.buf);
238+
strbuf_release(&buf);
239+
240+
return 0;
241+
}
242+
203243
int cmd__run_command(int argc, const char **argv)
204244
{
205245
struct child_process proc = CHILD_PROCESS_INIT;
206246
int jobs;
207247

208248
if (argc > 1 && !strcmp(argv[1], "testsuite"))
209249
exit(testsuite(argc - 1, argv + 1));
250+
if (!strcmp(argv[1], "inherited-handle"))
251+
exit(inherit_handle(argv[0]));
252+
if (!strcmp(argv[1], "inherited-handle-child"))
253+
exit(inherit_handle_child());
210254

211255
if (argc < 3)
212256
return 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)