Win32: detect unix socket support at runtime
PATCH 1/1 Win32: detect unix socket support at runtime
From: =?UTF-8?q?Matthias=20A=C3=9Fhauer?= <[email protected]>
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
---
Win32: detect unix socket support at runtime
Microsoft recommends checking for unix socket support on the command line trough the sc command. [1][2]
> Check whether your Windows build has support for unix socket by > running “sc query afunix” from a Windows admin command prompt.
That command queries wether the Service afunix exists and what it's current status is. [2]
Using OpenSCManagerA() [3], OpenServiceA() [4], QueryServiceStatusEx()
[5] and CloseServiceHandle() [6] we can query the same information without spawning a new process and parsing the output.
All the used APIs are available Windows XP/Server 2003. [3][4][5][6]. They're also available on Nano Server images [7] and should thus be available in docker containers.
A quick test with time git credential-cache exit shows a negligible startup penalty of 2ms.
I've decided against introducing a third behaviour (disabled at compile time/dynamic detection/enabled at compile time), because the main difference would be a more cryptic error message on unsupported systems and the aforementioned ~2ms startup time difference.
This conflicts slightly with the patch series at [8], but rebasing onto v2 made little sense with a v3 seemongly in the making.
[1] https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/
[2]
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/sc-query
[3]
https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-openscmanagera
[4]
https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-openservicea
[5]
https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-queryservicestatusex
[6]
https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-closeservicehandle
[7]
https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/mt588480(v=vs.85)
[8]
https://lore.kernel.org/git/[email protected]/
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1708%2Frimrul%2Fwin32-unix-socket-runtime-check-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1708/rimrul/win32-unix-socket-runtime-check-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1708
builtin/credential-cache--daemon.c | 2 ++ builtin/credential-cache.c | 3 +++ compat/mingw.c | 19 +++++++++++++++++++ compat/mingw.h | 6 ++++++ config.mak.uname | 2 -- git-compat-util.h | 12 ++++++++++++ t/t0301-credential-cache.sh | 8 ++++++++ 7 files changed, 50 insertions(+), 2 deletions(-)
builtin/credential-cache--daemon.c +2 -0
@@ -294,6 +294,8 @@ int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix) argc =parse_options (argc, argv, prefix, options, usage,0 ); socket_path = argv[0 ]; +if (!have_unix_sockets ()) +die (_ ("credential-cache--daemon unavailable; no unix socket support" ));if (!socket_path)usage_with_options (usage, options);
builtin/credential-cache.c +3 -0
@@ -149,6 +149,9 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)usage_with_options (usage, options); op = argv[0 ]; +if (!have_unix_sockets ()) +die (_ ("credential-cache unavailable; no unix socket support" )); +if (!socket_path) socket_path =get_socket_path ();if (!socket_path)
compat/mingw.c +19 -0
@@ -3158,3 +3158,22 @@ int uname(struct utsname *buf) "%u", (v >> 16) & 0x7fff);return 0 ; } + +int mingw_have_unix_sockets (void ) +{ +SC_HANDLE scm, srvc; +SERVICE_STATUS_PROCESS status; +DWORD bytes; +int ret =0 ; + scm =OpenSCManagerA (NULL ,NULL ,SC_MANAGER_CONNECT ); +if (scm) { + srvc =OpenServiceA (scm,"afunix" ,SERVICE_QUERY_STATUS ); +if (srvc) { +if (QueryServiceStatusEx (srvc,SC_STATUS_PROCESS_INFO , (LPBYTE )&status,sizeof (SERVICE_STATUS_PROCESS ), &bytes)) + ret = status.dwCurrentState ==SERVICE_RUNNING ; +CloseServiceHandle (srvc); + } +CloseServiceHandle (scm); + } +return ret; +}
compat/mingw.h +6 -0
@@ -631,3 +631,9 @@ void open_in_gdb(void);* Used by Pthread API implementation for Windows */ int err_win_to_posix (DWORD winerr); + +#ifndef NO_UNIX_SOCKETS +int mingw_have_unix_sockets (void ); +#undef have_unix_sockets +#define have_unix_sockets mingw_have_unix_sockets +#endif
config.mak.uname +0 -2
@@ -447,7 +447,6 @@ ifeq ($(uname_S),Windows) NO_POLL = YesPlease NO_SYMLINK_HEAD = YesPlease NO_IPV6 = YesPlease - NO_UNIX_SOCKETS = YesPlease NO_SETENV = YesPlease NO_STRCASESTR = YesPlease NO_STRLCPY = YesPlease
@@ -661,7 +660,6 @@ ifeq ($(uname_S),MINGW) NO_LIBGEN_H = YesPlease NO_POLL = YesPlease NO_SYMLINK_HEAD = YesPlease - NO_UNIX_SOCKETS = YesPlease NO_SETENV = YesPlease NO_STRCASESTR = YesPlease NO_STRLCPY = YesPlease
git-compat-util.h +12 -0
@@ -218,6 +218,18 @@ struct strbuf;#define GIT_WINDOWS_NATIVE #endif +#if defined(NO_UNIX_SOCKETS ) || !defined(GIT_WINDOWS_NATIVE ) +static inline int _have_unix_sockets (void ) +{ +#if defined(NO_UNIX_SOCKETS ) +return 0 ; +#else +return 1 ; +#endif +} +#define have_unix_sockets _have_unix_sockets +#endif +#include <unistd.h> #include <stdio.h> #include <sys/stat.h>
t/t0301-credential-cache.sh +8 -0
@@ -8,6 +8,14 @@ test -z "$NO_UNIX_SOCKETS" || { skip_all='skipping credential-cache tests, unix sockets not available' test_done } +if test_have_prereq MINGW +then + service_running=$(sc query afunix | grep "4 RUNNING") + test -z "$service_running" || { + skip_all='skipping credential-cache tests, unix sockets not available' + test_done + } +fi uname_s=$(uname -s) case $uname_s in
base-commit: c2cbfbd2e28cbe27c194d62183b42f27a6a5bb87
Signature · 1 line
gitgitgadget