mirror of
https://github.com/git-for-windows/git.git
synced 2026-05-02 08:03:45 -05:00
Merge branch 'inherit-only-stdhandles'
When spawning child processes, we do want them to inherit the standard handles so that we can talk to them. We do *not* want them to inherit any other handle, as that would hold a lock to the respective files (preventing them from being renamed, modified or deleted), and the child process would not know how to access that handle anyway. Happily, there is an API to make that happen. It is supported in Windows Vista and later, which is exactly what we promise to support in Git for Windows for the time being. This also means that we lift, at long last, the target Windows version from Windows XP to Windows Vista. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
@@ -274,7 +274,6 @@ int mingw_core_config(const char *var, const char *value, void *cb)
|
||||
}
|
||||
|
||||
static DWORD symlink_file_flags = 0, symlink_directory_flags = 1;
|
||||
DECLARE_PROC_ADDR(kernel32.dll, BOOLEAN, CreateSymbolicLinkW, LPCWSTR, LPCWSTR, DWORD);
|
||||
|
||||
enum phantom_symlink_result {
|
||||
PHANTOM_SYMLINK_RETRY,
|
||||
@@ -1595,8 +1594,12 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
|
||||
int fhin, int fhout, int fherr)
|
||||
{
|
||||
static int atexit_handler_initialized;
|
||||
STARTUPINFOW si;
|
||||
STARTUPINFOEXW si;
|
||||
PROCESS_INFORMATION pi;
|
||||
LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL;
|
||||
HANDLE stdhandles[3];
|
||||
DWORD stdhandles_count = 0;
|
||||
SIZE_T size;
|
||||
struct strbuf args;
|
||||
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
|
||||
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
|
||||
@@ -1644,11 +1647,19 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
|
||||
CloseHandle(cons);
|
||||
}
|
||||
memset(&si, 0, sizeof(si));
|
||||
si.cb = sizeof(si);
|
||||
si.dwFlags = STARTF_USESTDHANDLES;
|
||||
si.hStdInput = winansi_get_osfhandle(fhin);
|
||||
si.hStdOutput = winansi_get_osfhandle(fhout);
|
||||
si.hStdError = winansi_get_osfhandle(fherr);
|
||||
si.StartupInfo.cb = sizeof(si);
|
||||
si.StartupInfo.hStdInput = winansi_get_osfhandle(fhin);
|
||||
si.StartupInfo.hStdOutput = winansi_get_osfhandle(fhout);
|
||||
si.StartupInfo.hStdError = winansi_get_osfhandle(fherr);
|
||||
|
||||
if (si.StartupInfo.hStdInput != INVALID_HANDLE_VALUE)
|
||||
stdhandles[stdhandles_count++] = si.StartupInfo.hStdInput;
|
||||
if (si.StartupInfo.hStdOutput != INVALID_HANDLE_VALUE)
|
||||
stdhandles[stdhandles_count++] = si.StartupInfo.hStdOutput;
|
||||
if (si.StartupInfo.hStdError != INVALID_HANDLE_VALUE)
|
||||
stdhandles[stdhandles_count++] = si.StartupInfo.hStdError;
|
||||
if (stdhandles_count)
|
||||
si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
|
||||
|
||||
/* executables and the current directory don't support long paths */
|
||||
if (*argv && !strcmp(cmd, *argv))
|
||||
@@ -1707,8 +1718,30 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
|
||||
wenvblk = make_environment_block(deltaenv);
|
||||
|
||||
memset(&pi, 0, sizeof(pi));
|
||||
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE,
|
||||
flags, wenvblk, dir ? wdir : NULL, &si, &pi);
|
||||
if (stdhandles_count &&
|
||||
(InitializeProcThreadAttributeList(NULL, 1, 0, &size) ||
|
||||
GetLastError() == ERROR_INSUFFICIENT_BUFFER) &&
|
||||
(attr_list = (LPPROC_THREAD_ATTRIBUTE_LIST)
|
||||
(HeapAlloc(GetProcessHeap(), 0, size))) &&
|
||||
InitializeProcThreadAttributeList(attr_list, 1, 0, &size) &&
|
||||
UpdateProcThreadAttribute(attr_list, 0,
|
||||
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
|
||||
stdhandles,
|
||||
stdhandles_count * sizeof(HANDLE),
|
||||
NULL, NULL)) {
|
||||
si.lpAttributeList = attr_list;
|
||||
flags |= EXTENDED_STARTUPINFO_PRESENT;
|
||||
}
|
||||
|
||||
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
|
||||
stdhandles_count ? TRUE : FALSE,
|
||||
flags, wenvblk, dir ? wdir : NULL,
|
||||
&si.StartupInfo, &pi);
|
||||
|
||||
if (si.lpAttributeList)
|
||||
DeleteProcThreadAttributeList(si.lpAttributeList);
|
||||
if (attr_list)
|
||||
HeapFree(GetProcessHeap(), 0, attr_list);
|
||||
|
||||
free(wenvblk);
|
||||
free(wargs);
|
||||
@@ -2687,7 +2720,7 @@ int symlink(const char *target, const char *link)
|
||||
int len;
|
||||
|
||||
/* fail if symlinks are disabled or API is not supported (WinXP) */
|
||||
if (!has_symlinks || !INIT_PROC_ADDR(CreateSymbolicLinkW)) {
|
||||
if (!has_symlinks) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,21 @@
|
||||
#ifndef _GL_POLL_H
|
||||
#define _GL_POLL_H
|
||||
|
||||
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
|
||||
/* Vista has its own, socket-only poll() */
|
||||
#undef POLLIN
|
||||
#undef POLLPRI
|
||||
#undef POLLOUT
|
||||
#undef POLLERR
|
||||
#undef POLLHUP
|
||||
#undef POLLNVAL
|
||||
#undef POLLRDNORM
|
||||
#undef POLLRDBAND
|
||||
#undef POLLWRNORM
|
||||
#undef POLLWRBAND
|
||||
#define pollfd compat_pollfd
|
||||
#endif
|
||||
|
||||
/* fake a poll(2) environment */
|
||||
#define POLLIN 0x0001 /* any readable data available */
|
||||
#define POLLPRI 0x0002 /* OOB/Urgent readable data */
|
||||
|
||||
@@ -399,8 +399,6 @@ ifeq ($(uname_S),Windows)
|
||||
NO_GETTEXT = YesPlease
|
||||
NO_PYTHON = YesPlease
|
||||
ETAGS_TARGET = ETAGS
|
||||
NO_INET_PTON = YesPlease
|
||||
NO_INET_NTOP = YesPlease
|
||||
NO_POSIX_GOODIES = UnfortunatelyYes
|
||||
NATIVE_CRLF = YesPlease
|
||||
DEFAULT_HELP_FORMAT = html
|
||||
@@ -568,8 +566,6 @@ ifneq (,$(findstring MINGW,$(uname_S)))
|
||||
NO_REGEX = YesPlease
|
||||
NO_PYTHON = YesPlease
|
||||
ETAGS_TARGET = ETAGS
|
||||
NO_INET_PTON = YesPlease
|
||||
NO_INET_NTOP = YesPlease
|
||||
NO_POSIX_GOODIES = UnfortunatelyYes
|
||||
DEFAULT_HELP_FORMAT = html
|
||||
COMPAT_CFLAGS += -DNOGDI -Icompat -Icompat/win32
|
||||
|
||||
@@ -172,8 +172,8 @@
|
||||
#define _SGI_SOURCE 1
|
||||
|
||||
#if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */
|
||||
# if defined (_MSC_VER) && !defined(_WIN32_WINNT)
|
||||
# define _WIN32_WINNT 0x0502
|
||||
# if !defined(_WIN32_WINNT)
|
||||
# define _WIN32_WINNT 0x0600
|
||||
# endif
|
||||
#define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */
|
||||
#include <winsock2.h>
|
||||
|
||||
@@ -188,6 +188,45 @@ 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 */
|
||||
sprintf(path, "out-XXXXXX");
|
||||
tmp = xmkstemp(path);
|
||||
|
||||
argv_array_pushl(&cp.args, 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_main(int argc, const char **argv)
|
||||
{
|
||||
struct child_process proc = CHILD_PROCESS_INIT;
|
||||
@@ -195,6 +234,12 @@ int cmd_main(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;
|
||||
proc.argv = (const char **)argv + 2;
|
||||
|
||||
@@ -13,6 +13,10 @@ cat >hello-script <<-EOF
|
||||
EOF
|
||||
>empty
|
||||
|
||||
test_expect_success MINGW 'subprocess inherits only std handles' '
|
||||
test-run-command inherited-handle
|
||||
'
|
||||
|
||||
test_expect_success 'start_command reports ENOENT' '
|
||||
test-run-command start-command-ENOENT ./does-not-exist
|
||||
'
|
||||
|
||||
Reference in New Issue
Block a user