From 8b533f0b020393f4d65e402d2c81027c998e60b8 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 5 Feb 2018 21:31:34 +0100 Subject: [PATCH] fixup! mingw: spawned processes need to inherit only standard handles Let's make the code a little more robust: when `CreateProcess()` fails and we tried to restrict handle inheritance, and when that failure was *still* not anticipated by the current code, warn loudly but fall back to not restricting the handle inheritance. Use an environment variable to avoid warning more than once per Git operation, because it would get very annoying if a simple `git fetch` reported the same type of warning about six times. This should avoid blocking users if there are still bugs left, but still give us the benefit of proper bug reports. While at it, reformat the CreateProcess() call to conform to Git's conventions. Signed-off-by: Johannes Schindelin --- compat/mingw.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 081c39e29a82cd..89a516501e33b9 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1750,13 +1750,40 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen * handle inheritance. This is still better than failing to create * processes. */ - if (!ret && GetLastError() == ERROR_NO_SYSTEM_RESOURCES && - restrict_handle_inheritance && stdhandles_count) { + if (!ret && restrict_handle_inheritance && stdhandles_count) { + DWORD err = GetLastError(); + if (err != ERROR_NO_SYSTEM_RESOURCES && + !getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) { + struct strbuf buf = STRBUF_INIT; + DWORD fl; + int i; + + setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1); + + for (i = 0; i < stdhandles_count; i++) { + HANDLE h = stdhandles[i]; + strbuf_addf(&buf, "handle #%d: %p (type %lx, " + "handle info (%d) %lx\n", i, h, + GetFileType(h), + GetHandleInformation(h, &fl), + fl); + } + strbuf_addstr(&buf, "\nThis is a bug; please report it " + "at\nhttps://github.com/git-for-windows/" + "git/issues/new\n\n" + "To suppress this warning, please set " + "the environment variable\n\n" + "\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1" + "\n"); + warning("failed to restrict file handles (%ld)\n\n%s", + err, buf.buf); + strbuf_release(&buf); + } restrict_handle_inheritance = 0; flags &= ~EXTENDED_STARTUPINFO_PRESENT; ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, - TRUE, flags, wenvblk, dir ? wdir : NULL, - &si.StartupInfo, &pi); + TRUE, flags, wenvblk, dir ? wdir : NULL, + &si.StartupInfo, &pi); } if (!ret)