From d600862a04ad9573814f2ef12a6004e5eefccc3a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 26 Jan 2018 16:34:59 +0100 Subject: [PATCH] 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 --- t/helper/test-run-command.c | 45 +++++++++++++++++++++++++++++++++++++ t/t0061-run-command.sh | 4 ++++ 2 files changed, 49 insertions(+) diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c index 3170608517..6a8e6d1813 100644 --- a/t/helper/test-run-command.c +++ b/t/helper/test-run-command.c @@ -188,6 +188,45 @@ static int testsuite(int argc, const char **argv) return !!ret; } +static int inherit_handle(const char *argv0) +{ + struct child_process cp = CHILD_PROCESS_INIT; + char path[PATH_MAX]; + int tmp; + + /* First, open an inheritable handle */ + sprintf(path, "out-XXXXXX"); + tmp = xmkstemp(path); + + argv_array_pushl(&cp.args, argv0, "inherited-handle-child", NULL); + cp.in = -1; + cp.no_stdout = cp.no_stderr = 1; + if (start_command(&cp) < 0) + die("Could not start child process"); + + /* Then close it, and try to delete it. */ + close(tmp); + if (unlink(path)) + die("Could not delete '%s'", path); + + if (close(cp.in) < 0 || finish_command(&cp) < 0) + die("Child did not finish"); + + return 0; +} + +static int inherit_handle_child(void) +{ + struct strbuf buf = STRBUF_INIT; + + if (strbuf_read(&buf, 0, 0) < 0) + die("Could not read stdin"); + printf("Received %s\n", buf.buf); + strbuf_release(&buf); + + return 0; +} + int cmd_main(int argc, const char **argv) { struct child_process proc = CHILD_PROCESS_INIT; @@ -195,6 +234,12 @@ int cmd_main(int argc, const char **argv) if (argc > 1 && !strcmp(argv[1], "testsuite")) exit(testsuite(argc - 1, argv + 1)); + + if (!strcmp(argv[1], "inherited-handle")) + exit(inherit_handle(argv[0])); + if (!strcmp(argv[1], "inherited-handle-child")) + exit(inherit_handle_child()); + if (argc < 3) return 1; proc.argv = (const char **)argv + 2; diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh index c3087af0ea..5b66135f69 100755 --- a/t/t0061-run-command.sh +++ b/t/t0061-run-command.sh @@ -13,6 +13,10 @@ cat >hello-script <<-EOF EOF >empty +test_expect_failure MINGW 'subprocess inherits only std handles' ' + test-run-command inherited-handle +' + test_expect_success 'start_command reports ENOENT' ' test-run-command start-command-ENOENT ./does-not-exist '