]> git.ipfire.org Git - thirdparty/git.git/commitdiff
mingw: demonstrate that all file handles are inherited by child processes
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 22 Nov 2019 14:41:02 +0000 (14:41 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sat, 23 Nov 2019 02:17:01 +0000 (11:17 +0900)
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>
t/helper/test-run-command.c
t/t0061-run-command.sh

index ead6dc611ada93daa5e9de2429e62ba7576c74e4..40ec4dbb6e3292762a2ca8ab827129c3164a79db 100644 (file)
@@ -200,6 +200,46 @@ 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 */
+       xsnprintf(path, sizeof(path), "out-XXXXXX");
+       tmp = xmkstemp(path);
+
+       argv_array_pushl(&cp.args,
+                        "test-tool", 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__run_command(int argc, const char **argv)
 {
        struct child_process proc = CHILD_PROCESS_INIT;
@@ -207,6 +247,10 @@ int cmd__run_command(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;
index 17c9c0f3bb64782d86b9f15901791c4e05b9d122..473a3405efb5111eecd14ac4e8dba8b4195eb9dd 100755 (executable)
@@ -12,6 +12,10 @@ cat >hello-script <<-EOF
        cat hello-script
 EOF
 
+test_expect_failure MINGW 'subprocess inherits only std handles' '
+       test-tool run-command inherited-handle
+'
+
 test_expect_success 'start_command reports ENOENT (slash)' '
        test-tool run-command start-command-ENOENT ./does-not-exist 2>err &&
        test_i18ngrep "\./does-not-exist" err