Skip to content

Commit 83bb240

Browse files
committed
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. Signed-off-by: Matthias Aßhauer <[email protected]>
1 parent ad0bbff commit 83bb240

7 files changed

+50
-3
lines changed

builtin/credential-cache--daemon.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix)
294294
argc = parse_options(argc, argv, prefix, options, usage, 0);
295295
socket_path = argv[0];
296296

297+
if (!have_unix_sockets())
298+
die(_("credential-cache--daemon unavailable; no unix socket support"));
297299
if (!socket_path)
298300
usage_with_options(usage, options);
299301

builtin/credential-cache.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ int cmd_credential_cache(int argc, const char **argv, const char *prefix)
149149
usage_with_options(usage, options);
150150
op = argv[0];
151151

152+
if (!have_unix_sockets())
153+
die(_("credential-cache unavailable; no unix socket support"));
154+
152155
if (!socket_path)
153156
socket_path = get_socket_path();
154157
if (!socket_path)

compat/mingw.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4215,3 +4215,22 @@ int file_attr_to_st_mode (DWORD attr, DWORD tag, const char *path)
42154215
fMode |= S_IWRITE;
42164216
return fMode;
42174217
}
4218+
4219+
int mingw_have_unix_sockets(void)
4220+
{
4221+
SC_HANDLE scm, srvc;
4222+
SERVICE_STATUS_PROCESS status;
4223+
DWORD bytes;
4224+
int ret = 0;
4225+
scm = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
4226+
if (scm) {
4227+
srvc = OpenServiceA(scm, "afunix", SERVICE_QUERY_STATUS);
4228+
if (srvc) {
4229+
if(QueryServiceStatusEx(srvc, SC_STATUS_PROCESS_INFO, (LPBYTE)&status, sizeof(SERVICE_STATUS_PROCESS), &bytes))
4230+
ret = status.dwCurrentState == SERVICE_RUNNING;
4231+
CloseServiceHandle(srvc);
4232+
}
4233+
CloseServiceHandle(scm);
4234+
}
4235+
return ret;
4236+
}

compat/mingw.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,9 @@ int err_win_to_posix(DWORD winerr);
729729
* Check current process is inside Windows Container.
730730
*/
731731
int is_inside_windows_container(void);
732+
733+
#ifndef NO_UNIX_SOCKETS
734+
int mingw_have_unix_sockets(void);
735+
#undef have_unix_sockets
736+
#define have_unix_sockets mingw_have_unix_sockets
737+
#endif

config.mak.uname

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ ifeq ($(uname_S),Windows)
447447
NO_POLL = YesPlease
448448
NO_SYMLINK_HEAD = YesPlease
449449
NO_IPV6 = YesPlease
450-
NO_UNIX_SOCKETS = YesPlease
451450
NO_SETENV = YesPlease
452451
NO_STRCASESTR = YesPlease
453452
NO_STRLCPY = YesPlease
@@ -652,7 +651,6 @@ ifeq ($(uname_S),MINGW)
652651
NO_LIBGEN_H = YesPlease
653652
NO_POLL = YesPlease
654653
NO_SYMLINK_HEAD = YesPlease
655-
NO_UNIX_SOCKETS = YesPlease
656654
NO_SETENV = YesPlease
657655
NO_STRCASESTR = YesPlease
658656
NO_STRLCPY = YesPlease

git-compat-util.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,18 @@ struct strbuf;
220220
#define GIT_WINDOWS_NATIVE
221221
#endif
222222

223+
#if defined(NO_UNIX_SOCKETS) || !defined(GIT_WINDOWS_NATIVE)
224+
static inline int _have_unix_sockets(void)
225+
{
226+
#if defined(NO_UNIX_SOCKETS)
227+
return 0;
228+
#else
229+
return 1;
230+
#endif
231+
}
232+
#define have_unix_sockets _have_unix_sockets
233+
#endif
234+
223235
#include <unistd.h>
224236
#include <stdio.h>
225237
#include <sys/stat.h>
@@ -1627,5 +1639,4 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
16271639
#define OFFSETOF_VAR(ptr, member) \
16281640
((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
16291641
#endif /* !__GNUC__ */
1630-
16311642
#endif

t/t0301-credential-cache.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ test -z "$NO_UNIX_SOCKETS" || {
88
skip_all='skipping credential-cache tests, unix sockets not available'
99
test_done
1010
}
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
1119

1220
uname_s=$(uname -s)
1321
case $uname_s in

0 commit comments

Comments
 (0)