From d73305289e47d7e29f4d05d0ea4f692488990380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20A=C3=9Fhauer?= Date: Wed, 10 Jan 2024 16:15:42 +0100 Subject: [PATCH] WIP: win32: detect unix socket support at runtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 know 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. Signed-off-by: Matthias Aßhauer --- builtin/credential-cache.c | 3 +++ compat/mingw.c | 19 +++++++++++++++++++ compat/mingw.h | 6 ++++++ config.mak.uname | 4 ++-- git-compat-util.h | 11 +++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/builtin/credential-cache.c b/builtin/credential-cache.c index 43b9d0e5b16ba5..8c28fd88a8fb18 100644 --- a/builtin/credential-cache.c +++ b/builtin/credential-cache.c @@ -151,6 +151,9 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix) usage_with_options(usage, options); op = argv[0]; + if (!can_use_unix_sockets()) + die(_("credential-cache unavailable; no unix socket support")); + if (!socket_path) socket_path = get_socket_path(); if (!socket_path) diff --git a/compat/mingw.c b/compat/mingw.c index 41831e88163ee2..d6a374bf6e5f3e 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -4157,3 +4157,22 @@ int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path) fMode |= S_IWRITE; return fMode; } + +int mingw_can_use_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; +} diff --git a/compat/mingw.h b/compat/mingw.h index df85b7d1b26f9b..bcb3cb85eda1fb 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -729,3 +729,9 @@ int err_win_to_posix(DWORD winerr); * Check current process is inside Windows Container. */ int is_inside_windows_container(void); + +#ifndef NO_UNIX_SOCKETS +int mingw_can_use_unix_sockets(void); +#undef can_use_unix_sockets +#define can_use_unix_sockets mingw_can_use_unix_sockets +#endif diff --git a/config.mak.uname b/config.mak.uname index 61ba443865a026..96aa807c94b3c8 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -434,7 +434,7 @@ ifeq ($(uname_S),Windows) NO_POLL = YesPlease NO_SYMLINK_HEAD = YesPlease NO_IPV6 = YesPlease - NO_UNIX_SOCKETS = YesPlease + #NO_UNIX_SOCKETS = YesPlease NO_SETENV = YesPlease NO_STRCASESTR = YesPlease NO_STRLCPY = YesPlease @@ -639,7 +639,7 @@ ifeq ($(uname_S),MINGW) NO_LIBGEN_H = YesPlease NO_POLL = YesPlease NO_SYMLINK_HEAD = YesPlease - NO_UNIX_SOCKETS = YesPlease + #NO_UNIX_SOCKETS = YesPlease NO_SETENV = YesPlease NO_STRCASESTR = YesPlease NO_STRLCPY = YesPlease diff --git a/git-compat-util.h b/git-compat-util.h index 43e3143ba2aa16..ca17d13d1f8054 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1617,4 +1617,15 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset) ((uintptr_t)&(ptr)->member - (uintptr_t)(ptr)) #endif /* !__GNUC__ */ +#ifndef GIT_WINDOWS_NATIVE +static inline int _can_use_unix_sockets(void) +{ +#if defined(NO_UNIX_SOCKETS) + return 0; +#else + return 1; +#endif +} +#define can_use_unix_sockets _can_use_unix_sockets +#endif #endif