summaryrefslogtreecommitdiff
path: root/t/helper/test-run-command.c
diff options
context:
space:
mode:
authorJohannes Schindelin <johannes.schindelin@gmx.de>2019-11-22 14:41:02 (GMT)
committerJunio C Hamano <gitster@pobox.com>2019-11-23 02:17:01 (GMT)
commiteea4a7f4b3620df1b0bd3c1eb1d27e6fd4cb2ff5 (patch)
tree85e86a74f553f519877a0bd1565007fc344a3812 /t/helper/test-run-command.c
parentd9f6f3b6195a0ca35642561e530798ad1469bd41 (diff)
downloadgit-eea4a7f4b3620df1b0bd3c1eb1d27e6fd4cb2ff5.zip
git-eea4a7f4b3620df1b0bd3c1eb1d27e6fd4cb2ff5.tar.gz
git-eea4a7f4b3620df1b0bd3c1eb1d27e6fd4cb2ff5.tar.bz2
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>
Diffstat (limited to 't/helper/test-run-command.c')
-rw-r--r--t/helper/test-run-command.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
index ead6dc6..40ec4db 100644
--- a/t/helper/test-run-command.c
+++ b/t/helper/test-run-command.c
@@ -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;