git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/4] mingw: demonstrate that all file handles are inherited by child processes

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Nov 22, 2019, 14:41 UTC
Message-ID
<96608fd2e82b7337dea4ea9fb7a2f3f1f7e6c854.1574433665.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.670.git.git.1574433665.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

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>
---
 t/helper/test-run-command.c | 44 +++++++++++++++++++++++++++++++++++++
 t/t0061-run-command.sh      |  4 ++++
 2 files changed, 48 insertions(+)
diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
index ead6dc611a..40ec4dbb6e 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;
diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh
index 17c9c0f3bb..473a3405ef 100755
--- a/t/t0061-run-command.sh
+++ b/t/t0061-run-command.sh
@@ -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
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 3 of 13 in “On Windows, limit which file handles are inherited by spawned child processes”
  1. 0/4 On Windows, limit which file handles are inherited by spawned child processesJohannes Schindelin via GitGitGadget, Nov 22, 2019
  2. 2/4 mingw: work around incorrect standard handlesJohannes Schindelin via GitGitGadget, Nov 22, 2019
  3. 1/4 mingw: demonstrate that all file handles are inherited by child processesJohannes Schindelin via GitGitGadget, Nov 22, 2019
  4. 4/4 mingw: restrict file handle inheritance only on Windows 7 and laterJohannes Schindelin via GitGitGadget, Nov 22, 2019
  5. 3/4 mingw: spawned processes need to inherit only standard handlesJohannes Schindelin via GitGitGadget, Nov 22, 2019
  6. Johannes SixtNov 28, 2019
  7. Johannes SchindelinNov 29, 2019
  8. Johannes SchindelinNov 29, 2019
  9. Johannes SixtNov 29, 2019
  10. Johannes SixtNov 29, 2019
  11. Johannes SchindelinNov 30, 2019
  12. Junio C HamanoNov 25, 2019
  13. Johannes SchindelinNov 25, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.