{"thread":{"id":"61261","subject":"[PATCH] Win32: detect unix socket support at runtime","startedAt":"2024-04-03T15:42:06Z","lastAt":"2024-04-03T15:42:06Z","messageCount":1,"participants":["Matthias Aßhauer via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"492165","messageId":"pull.1708.git.1712158923106.gitgitgadget@gmail.com","threadId":"61261","inReplyTo":null,"subject":"[PATCH] Win32: detect unix socket support at runtime","fromName":"Matthias Aßhauer via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-04-03T15:42:02Z","receivedAt":"2024-04-03T15:42:06Z","isPatch":true,"sender":{"key":"mha1993@live.de","avatar":"https://avatars.githubusercontent.com/u/6178234?v=4"},"body":"From: =?UTF-8?q?Matthias=20A=C3=9Fhauer?= <mha1993@live.de>\n\nWindows 10 build 17063 introduced support for unix sockets to Windows.\nbb390b1 (git-compat-util: include declaration for unix sockets in\nwindows, 2021-09-14) introduced a way to build git with unix socket\nsupport on Windows, but you still had to decide at build time which\nWindows version the compiled executable was supposed to run on.\n\nWe can detect at runtime wether the operating system supports unix\nsockets and act accordingly for all supported Windows versions.\n\nThis fixes https://github.com/git-for-windows/git/issues/3892\n\nSigned-off-by: Matthias Aßhauer <mha1993@live.de>\n---\n    Win32: detect unix socket support at runtime\n    \n    Microsoft recommends checking for unix socket support on the command\n    line trough the sc command. [1][2]\n    \n    > Check whether your Windows build has support for unix socket by\n    > running “sc query afunix” from a Windows admin command prompt.\n    \n    That command queries wether the Service afunix exists and what it's\n    current status is. [2]\n    \n    Using OpenSCManagerA() [3], OpenServiceA() [4], QueryServiceStatusEx()\n    [5] and CloseServiceHandle() [6] we can query the same information\n    without spawning a new process and parsing the output.\n    \n    All the used APIs are available Windows XP/Server 2003. [3][4][5][6].\n    They're also available on Nano Server images [7] and should thus be\n    available in docker containers.\n    \n    A quick test with time git credential-cache exit shows a negligible\n    startup penalty of 2ms.\n    \n    I've decided against introducing a third behaviour (disabled at compile\n    time/dynamic detection/enabled at compile time), because the main\n    difference would be a more cryptic error message on unsupported systems\n    and the aforementioned ~2ms startup time difference.\n    \n    This conflicts slightly with the patch series at [8], but rebasing onto\n    v2 made little sense with a v3 seemongly in the making.\n    \n    [1] https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/\n    [2]\n    https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/sc-query\n    [3]\n    https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-openscmanagera\n    [4]\n    https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-openservicea\n    [5]\n    https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-queryservicestatusex\n    [6]\n    https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-closeservicehandle\n    [7]\n    https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/mt588480(v=vs.85)\n    [8]\n    https://lore.kernel.org/git/pull.1681.git.git.1708506863243.gitgitgadget@gmail.com/\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1708%2Frimrul%2Fwin32-unix-socket-runtime-check-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1708/rimrul/win32-unix-socket-runtime-check-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1708\n\n builtin/credential-cache--daemon.c |  2 ++\n builtin/credential-cache.c         |  3 +++\n compat/mingw.c                     | 19 +++++++++++++++++++\n compat/mingw.h                     |  6 ++++++\n config.mak.uname                   |  2 --\n git-compat-util.h                  | 12 ++++++++++++\n t/t0301-credential-cache.sh        |  8 ++++++++\n 7 files changed, 50 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/credential-cache--daemon.c b/builtin/credential-cache--daemon.c\nindex 3a6a750a8eb..17f929dede3 100644\n--- a/builtin/credential-cache--daemon.c\n+++ b/builtin/credential-cache--daemon.c\n@@ -294,6 +294,8 @@ int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)\n \targc = parse_options(argc, argv, prefix, options, usage, 0);\n \tsocket_path = argv[0];\n \n+\tif (!have_unix_sockets())\n+\t\tdie(_(\"credential-cache--daemon unavailable; no unix socket support\"));\n \tif (!socket_path)\n \t\tusage_with_options(usage, options);\n \ndiff --git a/builtin/credential-cache.c b/builtin/credential-cache.c\nindex bba96d4ffd6..bef120b5375 100644\n--- a/builtin/credential-cache.c\n+++ b/builtin/credential-cache.c\n@@ -149,6 +149,9 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)\n \t\tusage_with_options(usage, options);\n \top = argv[0];\n \n+\tif (!have_unix_sockets())\n+\t\tdie(_(\"credential-cache unavailable; no unix socket support\"));\n+\n \tif (!socket_path)\n \t\tsocket_path = get_socket_path();\n \tif (!socket_path)\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex 320fb99a90e..4876344b5b8 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -3158,3 +3158,22 @@ int uname(struct utsname *buf)\n \t\t  \"%u\", (v >> 16) & 0x7fff);\n \treturn 0;\n }\n+\n+int mingw_have_unix_sockets(void)\n+{\n+\tSC_HANDLE scm, srvc;\n+\tSERVICE_STATUS_PROCESS status;\n+\tDWORD bytes;\n+\tint ret = 0;\n+\tscm = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);\n+\tif (scm) {\n+\t\tsrvc = OpenServiceA(scm, \"afunix\", SERVICE_QUERY_STATUS);\n+\t\tif (srvc) {\n+\t\t\tif(QueryServiceStatusEx(srvc, SC_STATUS_PROCESS_INFO, (LPBYTE)&status, sizeof(SERVICE_STATUS_PROCESS), &bytes))\n+\t\t\t\tret = status.dwCurrentState == SERVICE_RUNNING;\n+\t\t\tCloseServiceHandle(srvc);\n+\t\t}\n+\t\tCloseServiceHandle(scm);\n+\t}\n+\treturn ret;\n+}\ndiff --git a/compat/mingw.h b/compat/mingw.h\nindex 6aec50e4124..27b61284f46 100644\n--- a/compat/mingw.h\n+++ b/compat/mingw.h\n@@ -631,3 +631,9 @@ void open_in_gdb(void);\n  * Used by Pthread API implementation for Windows\n  */\n int err_win_to_posix(DWORD winerr);\n+\n+#ifndef NO_UNIX_SOCKETS\n+int mingw_have_unix_sockets(void);\n+#undef have_unix_sockets\n+#define have_unix_sockets mingw_have_unix_sockets\n+#endif\ndiff --git a/config.mak.uname b/config.mak.uname\nindex d0dcca2ec55..fcf3e2d785a 100644\n--- a/config.mak.uname\n+++ b/config.mak.uname\n@@ -447,7 +447,6 @@ ifeq ($(uname_S),Windows)\n \tNO_POLL = YesPlease\n \tNO_SYMLINK_HEAD = YesPlease\n \tNO_IPV6 = YesPlease\n-\tNO_UNIX_SOCKETS = YesPlease\n \tNO_SETENV = YesPlease\n \tNO_STRCASESTR = YesPlease\n \tNO_STRLCPY = YesPlease\n@@ -661,7 +660,6 @@ ifeq ($(uname_S),MINGW)\n \tNO_LIBGEN_H = YesPlease\n \tNO_POLL = YesPlease\n \tNO_SYMLINK_HEAD = YesPlease\n-\tNO_UNIX_SOCKETS = YesPlease\n \tNO_SETENV = YesPlease\n \tNO_STRCASESTR = YesPlease\n \tNO_STRLCPY = YesPlease\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex 7c2a6538e5a..044f87454a2 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -218,6 +218,18 @@ struct strbuf;\n #define GIT_WINDOWS_NATIVE\n #endif\n \n+#if defined(NO_UNIX_SOCKETS) || !defined(GIT_WINDOWS_NATIVE)\n+static inline int _have_unix_sockets(void)\n+{\n+#if defined(NO_UNIX_SOCKETS)\n+\treturn 0;\n+#else\n+\treturn 1;\n+#endif\n+}\n+#define have_unix_sockets _have_unix_sockets\n+#endif\n+\n #include <unistd.h>\n #include <stdio.h>\n #include <sys/stat.h>\ndiff --git a/t/t0301-credential-cache.sh b/t/t0301-credential-cache.sh\nindex 8300faadea9..f2c146fa2a1 100755\n--- a/t/t0301-credential-cache.sh\n+++ b/t/t0301-credential-cache.sh\n@@ -8,6 +8,14 @@ test -z \"$NO_UNIX_SOCKETS\" || {\n \tskip_all='skipping credential-cache tests, unix sockets not available'\n \ttest_done\n }\n+if test_have_prereq MINGW\n+then\n+\tservice_running=$(sc query afunix | grep \"4  RUNNING\")\n+\ttest -z \"$service_running\" || {\n+\t\tskip_all='skipping credential-cache tests, unix sockets not available'\n+\t\ttest_done\n+\t}\n+fi\n \n uname_s=$(uname -s)\n case $uname_s in\n\nbase-commit: c2cbfbd2e28cbe27c194d62183b42f27a6a5bb87\n-- \ngitgitgadget\n"}]}