Skip to content

Commit

Permalink
fixup! mingw: spawned processes need to inherit only standard handles
Browse files Browse the repository at this point in the history
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 <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Feb 7, 2018
1 parent 8a71352 commit 8b533f0
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions compat/mingw.c
Expand Up @@ -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)
Expand Down

0 comments on commit 8b533f0

Please sign in to comment.