@@ -274,7 +274,6 @@ int mingw_core_config(const char *var, const char *value, void *cb)
274
274
}
275
275
276
276
static DWORD symlink_file_flags = 0 , symlink_directory_flags = 1 ;
277
- DECLARE_PROC_ADDR (kernel32 .dll , BOOLEAN , CreateSymbolicLinkW , LPCWSTR , LPCWSTR , DWORD );
278
277
279
278
enum phantom_symlink_result {
280
279
PHANTOM_SYMLINK_RETRY ,
@@ -1641,8 +1640,13 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1641
1640
const char * dir , const char * prepend_cmd ,
1642
1641
int fhin , int fhout , int fherr )
1643
1642
{
1644
- STARTUPINFOW si ;
1643
+ static int restrict_handle_inheritance = 1 ;
1644
+ STARTUPINFOEXW si ;
1645
1645
PROCESS_INFORMATION pi ;
1646
+ LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL ;
1647
+ HANDLE stdhandles [3 ];
1648
+ DWORD stdhandles_count = 0 ;
1649
+ SIZE_T size ;
1646
1650
struct strbuf args ;
1647
1651
wchar_t wcmd [MAX_PATH ], wdir [MAX_PATH ], * wargs , * wenvblk = NULL ;
1648
1652
unsigned flags = CREATE_UNICODE_ENVIRONMENT ;
@@ -1679,11 +1683,23 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1679
1683
CloseHandle (cons );
1680
1684
}
1681
1685
memset (& si , 0 , sizeof (si ));
1682
- si .cb = sizeof (si );
1683
- si .dwFlags = STARTF_USESTDHANDLES ;
1684
- si .hStdInput = winansi_get_osfhandle (fhin );
1685
- si .hStdOutput = winansi_get_osfhandle (fhout );
1686
- si .hStdError = winansi_get_osfhandle (fherr );
1686
+ si .StartupInfo .cb = sizeof (si );
1687
+ si .StartupInfo .hStdInput = winansi_get_osfhandle (fhin );
1688
+ si .StartupInfo .hStdOutput = winansi_get_osfhandle (fhout );
1689
+ si .StartupInfo .hStdError = winansi_get_osfhandle (fherr );
1690
+
1691
+ /* The list of handles cannot contain duplicates */
1692
+ if (si .StartupInfo .hStdInput != INVALID_HANDLE_VALUE )
1693
+ stdhandles [stdhandles_count ++ ] = si .StartupInfo .hStdInput ;
1694
+ if (si .StartupInfo .hStdOutput != INVALID_HANDLE_VALUE &&
1695
+ si .StartupInfo .hStdOutput != si .StartupInfo .hStdInput )
1696
+ stdhandles [stdhandles_count ++ ] = si .StartupInfo .hStdOutput ;
1697
+ if (si .StartupInfo .hStdError != INVALID_HANDLE_VALUE &&
1698
+ si .StartupInfo .hStdError != si .StartupInfo .hStdInput &&
1699
+ si .StartupInfo .hStdError != si .StartupInfo .hStdOutput )
1700
+ stdhandles [stdhandles_count ++ ] = si .StartupInfo .hStdError ;
1701
+ if (stdhandles_count )
1702
+ si .StartupInfo .dwFlags |= STARTF_USESTDHANDLES ;
1687
1703
1688
1704
/* executables and the current directory don't support long paths */
1689
1705
if (* argv && !strcmp (cmd , * argv ))
@@ -1742,16 +1758,97 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1742
1758
wenvblk = make_environment_block (deltaenv );
1743
1759
1744
1760
memset (& pi , 0 , sizeof (pi ));
1745
- ret = CreateProcessW (* wcmd ? wcmd : NULL , wargs , NULL , NULL , TRUE,
1746
- flags , wenvblk , dir ? wdir : NULL , & si , & pi );
1761
+ if (restrict_handle_inheritance && stdhandles_count &&
1762
+ (InitializeProcThreadAttributeList (NULL , 1 , 0 , & size ) ||
1763
+ GetLastError () == ERROR_INSUFFICIENT_BUFFER ) &&
1764
+ (attr_list = (LPPROC_THREAD_ATTRIBUTE_LIST )
1765
+ (HeapAlloc (GetProcessHeap (), 0 , size ))) &&
1766
+ InitializeProcThreadAttributeList (attr_list , 1 , 0 , & size ) &&
1767
+ UpdateProcThreadAttribute (attr_list , 0 ,
1768
+ PROC_THREAD_ATTRIBUTE_HANDLE_LIST ,
1769
+ stdhandles ,
1770
+ stdhandles_count * sizeof (HANDLE ),
1771
+ NULL , NULL )) {
1772
+ si .lpAttributeList = attr_list ;
1773
+ flags |= EXTENDED_STARTUPINFO_PRESENT ;
1774
+ }
1775
+
1776
+ ret = CreateProcessW (* wcmd ? wcmd : NULL , wargs , NULL , NULL ,
1777
+ stdhandles_count ? TRUE : FALSE,
1778
+ flags , wenvblk , dir ? wdir : NULL ,
1779
+ & si .StartupInfo , & pi );
1780
+
1781
+ /*
1782
+ * On Windows 2008 R2, it seems that specifying certain types of handles
1783
+ * (such as FILE_TYPE_CHAR or FILE_TYPE_PIPE) will always produce an
1784
+ * error. Rather than playing finicky and fragile games, let's just try
1785
+ * to detect this situation and simply try again without restricting any
1786
+ * handle inheritance. This is still better than failing to create
1787
+ * processes.
1788
+ */
1789
+ if (!ret && restrict_handle_inheritance && stdhandles_count ) {
1790
+ DWORD err = GetLastError ();
1791
+ struct strbuf buf = STRBUF_INIT ;
1792
+
1793
+ if (err != ERROR_NO_SYSTEM_RESOURCES &&
1794
+ /*
1795
+ * On Windows 7 and earlier, handles on pipes and character
1796
+ * devices are inherited automatically, and cannot be
1797
+ * specified in the thread handle list. Rather than trying
1798
+ * to catch each and every corner case (and running the
1799
+ * chance of *still* forgetting a few), let's just fall
1800
+ * back to creating the process without trying to limit the
1801
+ * handle inheritance.
1802
+ */
1803
+ !(err == ERROR_INVALID_PARAMETER &&
1804
+ GetVersion () >> 16 < 9200 ) &&
1805
+ !getenv ("SUPPRESS_HANDLE_INHERITANCE_WARNING" )) {
1806
+ DWORD fl = 0 ;
1807
+ int i ;
1808
+
1809
+ setenv ("SUPPRESS_HANDLE_INHERITANCE_WARNING" , "1" , 1 );
1810
+
1811
+ for (i = 0 ; i < stdhandles_count ; i ++ ) {
1812
+ HANDLE h = stdhandles [i ];
1813
+ strbuf_addf (& buf , "handle #%d: %p (type %lx, "
1814
+ "handle info (%d) %lx\n" , i , h ,
1815
+ GetFileType (h ),
1816
+ GetHandleInformation (h , & fl ),
1817
+ fl );
1818
+ }
1819
+ strbuf_addstr (& buf , "\nThis is a bug; please report it "
1820
+ "at\nhttps://github.com/git-for-windows/"
1821
+ "git/issues/new\n\n"
1822
+ "To suppress this warning, please set "
1823
+ "the environment variable\n\n"
1824
+ "\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1"
1825
+ "\n" );
1826
+ }
1827
+ restrict_handle_inheritance = 0 ;
1828
+ flags &= ~EXTENDED_STARTUPINFO_PRESENT ;
1829
+ ret = CreateProcessW (* wcmd ? wcmd : NULL , wargs , NULL , NULL ,
1830
+ TRUE, flags , wenvblk , dir ? wdir : NULL ,
1831
+ & si .StartupInfo , & pi );
1832
+ if (ret && buf .len ) {
1833
+ errno = err_win_to_posix (GetLastError ());
1834
+ warning ("failed to restrict file handles (%ld)\n\n%s" ,
1835
+ err , buf .buf );
1836
+ }
1837
+ strbuf_release (& buf );
1838
+ } else if (!ret )
1839
+ errno = err_win_to_posix (GetLastError ());
1840
+
1841
+ if (si .lpAttributeList )
1842
+ DeleteProcThreadAttributeList (si .lpAttributeList );
1843
+ if (attr_list )
1844
+ HeapFree (GetProcessHeap (), 0 , attr_list );
1747
1845
1748
1846
free (wenvblk );
1749
1847
free (wargs );
1750
1848
1751
- if (!ret ) {
1752
- errno = ENOENT ;
1849
+ if (!ret )
1753
1850
return -1 ;
1754
- }
1851
+
1755
1852
CloseHandle (pi .hThread );
1756
1853
1757
1854
/*
@@ -2722,7 +2819,7 @@ int symlink(const char *target, const char *link)
2722
2819
int len ;
2723
2820
2724
2821
/* fail if symlinks are disabled or API is not supported (WinXP) */
2725
- if (!has_symlinks || ! INIT_PROC_ADDR ( CreateSymbolicLinkW ) ) {
2822
+ if (!has_symlinks ) {
2726
2823
errno = ENOSYS ;
2727
2824
return -1 ;
2728
2825
}
0 commit comments