Skip to content

Commit ac33519

Browse files
dschogitster
authored andcommitted
mingw: restrict file handle inheritance only on Windows 7 and later
Turns out that it don't work so well on Vista, see git-for-windows#1742 for details. According to https://devblogs.microsoft.com/oldnewthing/?p=8873, it *should* work on Windows Vista and later. But apparently there are issues on Windows Vista when pipes are involved. Given that Windows Vista is past its end of life (official support ended on April 11th, 2017), let's not spend *too* much time on this issue and just disable the file handle inheritance restriction on any Windows version earlier than Windows 7. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9a780a3 commit ac33519

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Documentation/config/core.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,12 @@ core.unsetenvvars::
559559
Defaults to `PERL5LIB` to account for the fact that Git for
560560
Windows insists on using its own Perl interpreter.
561561

562+
core.restrictinheritedhandles::
563+
Windows-only: override whether spawned processes inherit only standard
564+
file handles (`stdin`, `stdout` and `stderr`) or all handles. Can be
565+
`auto`, `true` or `false`. Defaults to `auto`, which means `true` on
566+
Windows 7 and later, and `false` on older Windows versions.
567+
562568
core.createObject::
563569
You can set this to 'link', in which case a hardlink followed by
564570
a delete of the source are used to make sure that object creation

compat/mingw.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ enum hide_dotfiles_type {
212212
HIDE_DOTFILES_DOTGITONLY
213213
};
214214

215+
static int core_restrict_inherited_handles = -1;
215216
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
216217
static char *unset_environment_variables;
217218

@@ -231,6 +232,15 @@ int mingw_core_config(const char *var, const char *value, void *cb)
231232
return 0;
232233
}
233234

235+
if (!strcmp(var, "core.restrictinheritedhandles")) {
236+
if (value && !strcasecmp(value, "auto"))
237+
core_restrict_inherited_handles = -1;
238+
else
239+
core_restrict_inherited_handles =
240+
git_config_bool(var, value);
241+
return 0;
242+
}
243+
234244
return 0;
235245
}
236246

@@ -1398,7 +1408,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
13981408
const char *dir,
13991409
int prepend_cmd, int fhin, int fhout, int fherr)
14001410
{
1401-
static int restrict_handle_inheritance = 1;
1411+
static int restrict_handle_inheritance = -1;
14021412
STARTUPINFOEXW si;
14031413
PROCESS_INFORMATION pi;
14041414
LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL;
@@ -1413,6 +1423,16 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14131423
const char *(*quote_arg)(const char *arg) =
14141424
is_msys2_sh(*argv) ? quote_arg_msys2 : quote_arg_msvc;
14151425

1426+
if (restrict_handle_inheritance < 0)
1427+
restrict_handle_inheritance = core_restrict_inherited_handles;
1428+
/*
1429+
* The following code to restrict which handles are inherited seems
1430+
* to work properly only on Windows 7 and later, so let's disable it
1431+
* on Windows Vista and 2008.
1432+
*/
1433+
if (restrict_handle_inheritance < 0)
1434+
restrict_handle_inheritance = GetVersion() >> 16 >= 7601;
1435+
14161436
do_unset_environment_variables();
14171437

14181438
/* Determine whether or not we are associated to a console */

0 commit comments

Comments
 (0)