Win32: detect unix socket support at runtime

Windows 10 build 17063 introduced support for unix sockets to Windows. bb390b1 (git-compat-util: include declaration for unix sockets in windows, 2021-09-14) introduced a way to build git with unix socket support on Windows, but you still had to decide at build time which Windows version the compiled executable was supposed to run on. We can detect at runtime wether the operating system supports unix sockets and act accordingly for all supported Windows versions. This fixes https://github.com/git-for-windows/git/issues/3892 Signed-off-by: Matthias Aßhauer <mha1993@live.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthias Aßhauer committed Apr 3, 2024 at 15:42 UTC 2406bf5fc5fbaa042e05fc0001ba72beb888d60f
7 files changed +50 -2
builtin/credential-cache--daemon.c
+2
@@ -294,6 +294,8 @@ int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
294 argc = parse_options(argc, argv, prefix, options, usage, 0);
295 socket_path = argv[0];
296
297 + if (!have_unix_sockets())
298 + die(_("credential-cache--daemon unavailable; no unix socket support"));
299 if (!socket_path)
300 usage_with_options(usage, options);
301
builtin/credential-cache.c
+3
@@ -149,6 +149,9 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)
149 usage_with_options(usage, options);
150 op = argv[0];
151
152 + if (!have_unix_sockets())
153 + die(_("credential-cache unavailable; no unix socket support"));
154 +
155 if (!socket_path)
156 socket_path = get_socket_path();
157 if (!socket_path)
compat/mingw.c
+19
@@ -3158,3 +3158,22 @@ int uname(struct utsname *buf)
3158 "%u", (v >> 16) & 0x7fff);
3159 return 0;
3160 }
3161 +
3162 +int mingw_have_unix_sockets(void)
3163 +{
3164 + SC_HANDLE scm, srvc;
3165 + SERVICE_STATUS_PROCESS status;
3166 + DWORD bytes;
3167 + int ret = 0;
3168 + scm = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
3169 + if (scm) {
3170 + srvc = OpenServiceA(scm, "afunix", SERVICE_QUERY_STATUS);
3171 + if (srvc) {
3172 + if(QueryServiceStatusEx(srvc, SC_STATUS_PROCESS_INFO, (LPBYTE)&status, sizeof(SERVICE_STATUS_PROCESS), &bytes))
3173 + ret = status.dwCurrentState == SERVICE_RUNNING;
3174 + CloseServiceHandle(srvc);
3175 + }
3176 + CloseServiceHandle(scm);
3177 + }
3178 + return ret;
3179 +}
compat/mingw.h
+6
@@ -631,3 +631,9 @@ void open_in_gdb(void);
631 * Used by Pthread API implementation for Windows
632 */
633 int err_win_to_posix(DWORD winerr);
634 +
635 +#ifndef NO_UNIX_SOCKETS
636 +int mingw_have_unix_sockets(void);
637 +#undef have_unix_sockets
638 +#define have_unix_sockets mingw_have_unix_sockets
639 +#endif
config.mak.uname
-2
@@ -447,7 +447,6 @@ ifeq ($(uname_S),Windows)
447 NO_POLL = YesPlease
448 NO_SYMLINK_HEAD = YesPlease
449 NO_IPV6 = YesPlease
450 - NO_UNIX_SOCKETS = YesPlease
450 NO_SETENV = YesPlease
451 NO_STRCASESTR = YesPlease
452 NO_STRLCPY = YesPlease
@@ -661,7 +660,6 @@ ifeq ($(uname_S),MINGW)
660 NO_LIBGEN_H = YesPlease
661 NO_POLL = YesPlease
662 NO_SYMLINK_HEAD = YesPlease
664 - NO_UNIX_SOCKETS = YesPlease
663 NO_SETENV = YesPlease
664 NO_STRCASESTR = YesPlease
665 NO_STRLCPY = YesPlease
git-compat-util.h
+12
@@ -218,6 +218,18 @@ struct strbuf;
218 #define GIT_WINDOWS_NATIVE
219 #endif
220
221 +#if defined(NO_UNIX_SOCKETS) || !defined(GIT_WINDOWS_NATIVE)
222 +static inline int _have_unix_sockets(void)
223 +{
224 +#if defined(NO_UNIX_SOCKETS)
225 + return 0;
226 +#else
227 + return 1;
228 +#endif
229 +}
230 +#define have_unix_sockets _have_unix_sockets
231 +#endif
232 +
233 #include <unistd.h>
234 #include <stdio.h>
235 #include <sys/stat.h>
t/t0301-credential-cache.sh
+8
@@ -8,6 +8,14 @@ test -z "$NO_UNIX_SOCKETS" || {
8 skip_all='skipping credential-cache tests, unix sockets not available'
9 test_done
10 }
11 +if test_have_prereq MINGW
12 +then
13 + service_running=$(sc query afunix | grep "4 RUNNING")
14 + test -z "$service_running" || {
15 + skip_all='skipping credential-cache tests, unix sockets not available'
16 + test_done
17 + }
18 +fi
19
20 uname_s=$(uname -s)
21 case $uname_s in