@matheusbsbs / matheus-bagliano / commits / eea4a7f4b3
Fork 0

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. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Nov 22, 2019 at 14:41 UTC eea4a7f4b3620df1b0bd3c1eb1d27e6fd4cb2ff5
2 files changed +48
t/helper/test-run-command.c
+44
@@ -200,6 +200,46 @@ static int testsuite(int argc, const char **argv)
200 return !!ret;
201 }
202
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 +
243 int cmd__run_command(int argc, const char **argv)
244 {
245 struct child_process proc = CHILD_PROCESS_INIT;
@@ -207,6 +247,10 @@ int cmd__run_command(int argc, const char **argv)
247
248 if (argc > 1 && !strcmp(argv[1], "testsuite"))
249 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());
254
255 if (argc < 3)
256 return 1;
t/t0061-run-command.sh
+4
@@ -12,6 +12,10 @@ cat >hello-script <<-EOF
12 cat hello-script
13 EOF
14
15 +test_expect_failure MINGW 'subprocess inherits only std handles' '
16 + test-tool run-command inherited-handle
17 +'
18 +
19 test_expect_success 'start_command reports ENOENT (slash)' '
20 test-tool run-command start-command-ENOENT ./does-not-exist 2>err &&
21 test_i18ngrep "\./does-not-exist" err