Skip to contentSkip to conversation

Enable unix socket support on Windows

PATCH Superseded Bin Meng 3 participants 11 messages Jul 27, 2022 – Jul 28, 2022 Arm/Amlogic Meson SoC support

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

v2 cover Enable unix socket support on Windows

Support for the unix socket has existed both in BSD and Linux for the longest time, but not on Windows. Since Windows 10 build 17063 [1], the native support for the unix socket has came to Windows. Starting this build, two Win32 processes can use the AF_UNIX address family over Winsock API to communicate with each other.

Introduce a new build time config option CONFIG_AF_UNIX when the build host has such a capability, and a run-time check afunix_available() for Windows host in the QEMU sockets util codes.

[1] https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/

Changes in v2:
- move #include <afunix.h> to os-win32.h
- define WIN_BUILD_AF_UNIX only when CONFIG_WIN32
- drop #include <afunix.h> as it is now already included in osdep.h
- new patch: tests/unit: Update test-io-channel-socket.c for Windows

Bin Meng (6):
util/qemu-sockets: Replace the call to close a socket with
closesocket()
util/oslib-win32: Add a helper to get the Windows version qga/commands-win32: Use os_get_win_version() util/qemu-sockets: Enable unix socket support on Windows chardev/char-socket: Update AF_UNIX for Windows tests/unit: Update test-io-channel-socket.c for Windows

 meson.build                         |  6 ++++
 include/sysemu/os-win32.h           |  6 ++++
 chardev/char-socket.c               |  4 ++-
 qga/commands-win32.c                | 27 +---------------
 tests/unit/test-io-channel-socket.c | 16 ++++++++--
 util/oslib-win32.c                  | 15 +++++++++
 util/qemu-sockets.c                 | 49 +++++++++++++++++++++++------
 7 files changed, 85 insertions(+), 38 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/6 util/qemu-sockets: Replace the call to close a socket with closesocket()

From: Bin Meng <[email protected]>

close() is a *nix function. It works on any file descriptor, and sockets in *nix are an example of a file descriptor.

closesocket() is a Windows-specific function, which works only specifically with sockets. Sockets on Windows do not use *nix-style file descriptors, and socket() returns a handle to a kernel object instead, so it must be closed with closesocket().

In QEMU there is already a logic to handle such platform difference in os-posix.h and os-win32.h, that:

* closesocket maps to close on POSIX
* closesocket maps to a wrapper that calls the real closesocket() on Windows

Replace the call to close a socket with closesocket() instead.

---

(no changes since v1)

 util/qemu-sockets.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/qemu-sockets.c +2 -2
index 13b5b197f9..0e2298278f 100644
@@ -487,7 +487,7 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error **errp)
 
         if (ret < 0) {
             error_setg_errno(errp, errno, "Unable to set KEEPALIVE");
-            close(sock);
+            closesocket(sock);
             return -1;
         }
     }
@@ -1050,7 +1050,7 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
     return sock;
 
  err:
-    close(sock);
+    closesocket(sock);
     return -1;
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v2 1/6
#2 lore raw

Re: [PATCH v2 1/6] util/qemu-sockets: Replace the call to close a socket with closesocket()

Quoted 13 lines · Bin Meng

From: Bin Meng <[email protected]>

close() is a *nix function. It works on any file descriptor, and sockets in *nix are an example of a file descriptor.

closesocket() is a Windows-specific function, which works only specifically with sockets. Sockets on Windows do not use *nix-style file descriptors, and socket() returns a handle to a kernel object instead, so it must be closed with closesocket().

In QEMU there is already a logic to handle such platform difference in os-posix.h and os-win32.h, that:

Quoted 8 lines

* closesocket maps to close on POSIX
* closesocket maps to a wrapper that calls the real closesocket() on Windows

Replace the call to close a socket with closesocket() instead.

This is a fix, could go in 7.1. Daniel, do you take it?

Thanks

---

Quoted 35 lines

(no changes since v1)

 util/qemu-sockets.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/qemu-sockets.c +1 -1
index 13b5b197f9..0e2298278f 100644
@@ -487,7 +487,7 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error
 **errp)
 
         if (ret < 0) {
             error_setg_errno(errp, errno, "Unable to set KEEPALIVE");
-            close(sock);
+            closesocket(sock);
             return -1;
         }
     }
util/qemu-sockets.c +1 -2
@@ -1050,7 +1050,7 @@ static int unix_connect_saddr(UnixSocketAddress
 *saddr, Error **errp)
     return sock;
 
  err:
-    close(sock);
+    closesocket(sock);
     return -1;
 }
 
--

2.34.1

Signature · 1 line
Marc-André Lureau
author
#3 lore raw

PATCH v2 2/6 util/oslib-win32: Add a helper to get the Windows version

In reply to Bin Meng

From: Bin Meng <[email protected]>

This adds a helper to get the Windows version via the RtlGetVersion call, for QEMU codes to determine the Windows version at run-time.

---

(no changes since v1)

 include/sysemu/os-win32.h |  2 ++
 util/oslib-win32.c        | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
include/sysemu/os-win32.h +2 -0
index edc3b38a57..1e324026a4 100644
@@ -204,6 +204,8 @@ ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags);
 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
                            struct sockaddr *addr, socklen_t *addrlen);
 
+void os_get_win_version(RTL_OSVERSIONINFOEXW *info);
+
 #ifdef __cplusplus
 }
 #endif
util/oslib-win32.c +15 -0
index 5723d3eb4c..6d2387b9ff 100644
@@ -547,3 +547,18 @@ int qemu_msync(void *addr, size_t length, int fd)
      */
     return qemu_fdatasync(fd);
 }
+
+void os_get_win_version(RTL_OSVERSIONINFOEXW *info)
+{
+    typedef LONG (WINAPI *rtl_get_version_t)(PRTL_OSVERSIONINFOEXW);
+
+    /* RtlGetVersion is available starting with Windows 2000 */
+    HMODULE module = GetModuleHandle("ntdll");
+    PVOID fun = GetProcAddress(module, "RtlGetVersion");
+    rtl_get_version_t rtl_get_version = (rtl_get_version_t)fun;
+
+    info->dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
+    rtl_get_version(info);
+
+    return;
+}
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v2 2/6] util/oslib-win32: Add a helper to get the Windows version

Hi

Quoted 11 lines · Bin Meng

From: Bin Meng <[email protected]>

This adds a helper to get the Windows version via the RtlGetVersion call, for QEMU codes to determine the Windows version at run-time.

---

(no changes since v1)

It looks like you are introducing new code, but in fact it's almost identical as the ga_get_win_version() function. Please do the move in the same patch.

Quoted 46 lines
 include/sysemu/os-win32.h |  2 ++
 util/oslib-win32.c        | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
include/sysemu/os-win32.h +2 -0
index edc3b38a57..1e324026a4 100644
@@ -204,6 +204,8 @@ ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t
 len, int flags);
 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
                            struct sockaddr *addr, socklen_t *addrlen);
 
+void os_get_win_version(RTL_OSVERSIONINFOEXW *info);
+
 #ifdef __cplusplus
 }

#endif

util/oslib-win32.c +15 -1
index 5723d3eb4c..6d2387b9ff 100644
@@ -547,3 +547,18 @@ int qemu_msync(void *addr, size_t length, int fd)
      */
     return qemu_fdatasync(fd);
 }
+
+void os_get_win_version(RTL_OSVERSIONINFOEXW *info)
+{
+    typedef LONG (WINAPI *rtl_get_version_t)(PRTL_OSVERSIONINFOEXW);
+
+    /* RtlGetVersion is available starting with Windows 2000 */
+    HMODULE module = GetModuleHandle("ntdll");
+    PVOID fun = GetProcAddress(module, "RtlGetVersion");
+    rtl_get_version_t rtl_get_version = (rtl_get_version_t)fun;
+
+    info->dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
+    rtl_get_version(info);
+
+    return;
+}
--

2.34.1

Signature · 1 line
Marc-André Lureau
author
#5 lore raw

PATCH v2 3/6 qga/commands-win32: Use os_get_win_version()

In reply to Bin Meng

From: Bin Meng <[email protected]>

Drop its own ga_get_win_version() implementation, and use the one provided in oslib-win32 instead.

---

(no changes since v1)

 qga/commands-win32.c | 27 +--------------------------
 1 file changed, 1 insertion(+), 26 deletions(-)
qga/commands-win32.c +1 -26
index 7ed7664715..6186f2e1f2 100644
@@ -2178,26 +2178,6 @@ static ga_win_10_0_t const WIN_10_0_CLIENT_VERSION_MATRIX[3] = {
     {0, 0}
 };
 
-static void ga_get_win_version(RTL_OSVERSIONINFOEXW *info, Error **errp)
-{
-    typedef NTSTATUS(WINAPI *rtl_get_version_t)(
-        RTL_OSVERSIONINFOEXW *os_version_info_ex);
-
-    info->dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
-
-    HMODULE module = GetModuleHandle("ntdll");
-    PVOID fun = GetProcAddress(module, "RtlGetVersion");
-    if (fun == NULL) {
-        error_setg(errp, QERR_QGA_COMMAND_FAILED,
-            "Failed to get address of RtlGetVersion");
-        return;
-    }
-
-    rtl_get_version_t rtl_get_version = (rtl_get_version_t)fun;
-    rtl_get_version(info);
-    return;
-}
-
 static char *ga_get_win_name(OSVERSIONINFOEXW const *os_version, bool id)
 {
     DWORD major = os_version->dwMajorVersion;
@@ -2312,17 +2292,12 @@ static char *ga_get_current_arch(void)
 
 GuestOSInfo *qmp_guest_get_osinfo(Error **errp)
 {
-    Error *local_err = NULL;
     OSVERSIONINFOEXW os_version = {0};
     bool server;
     char *product_name;
     GuestOSInfo *info;
 
-    ga_get_win_version(&os_version, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
-        return NULL;
-    }
+    os_get_win_version(&os_version);
 
     server = os_version.wProductType != VER_NT_WORKSTATION;
     product_name = ga_get_win_product_name(errp);
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2 3/6] qga/commands-win32: Use os_get_win_version()

Hi

Quoted 7 lines · Bin Meng

From: Bin Meng <[email protected]>

Drop its own ga_get_win_version() implementation, and use the one provided in oslib-win32 instead.

Will be squashed with the previous patch, since the move should be done together.

Quoted 63 lines

---

(no changes since v1)

 qga/commands-win32.c | 27 +--------------------------
 1 file changed, 1 insertion(+), 26 deletions(-)
qga/commands-win32.c +0 -20
index 7ed7664715..6186f2e1f2 100644
@@ -2178,26 +2178,6 @@ static ga_win_10_0_t const
 WIN_10_0_CLIENT_VERSION_MATRIX[3] = {
     {0, 0}
 };
 
-static void ga_get_win_version(RTL_OSVERSIONINFOEXW *info, Error **errp)
-{
-    typedef NTSTATUS(WINAPI *rtl_get_version_t)(
-        RTL_OSVERSIONINFOEXW *os_version_info_ex);
-
-    info->dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
-
-    HMODULE module = GetModuleHandle("ntdll");
-    PVOID fun = GetProcAddress(module, "RtlGetVersion");
-    if (fun == NULL) {
-        error_setg(errp, QERR_QGA_COMMAND_FAILED,
-            "Failed to get address of RtlGetVersion");
-        return;
-    }
-
-    rtl_get_version_t rtl_get_version = (rtl_get_version_t)fun;
-    rtl_get_version(info);
-    return;
-}
-
 static char *ga_get_win_name(OSVERSIONINFOEXW const *os_version, bool id)
 {
     DWORD major = os_version->dwMajorVersion;
qga/commands-win32.c +1 -7
@@ -2312,17 +2292,12 @@ static char *ga_get_current_arch(void)
 
 GuestOSInfo *qmp_guest_get_osinfo(Error **errp)
 {
-    Error *local_err = NULL;
     OSVERSIONINFOEXW os_version = {0};
     bool server;
     char *product_name;
     GuestOSInfo *info;
 
-    ga_get_win_version(&os_version, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
-        return NULL;
-    }
+    os_get_win_version(&os_version);
 
     server = os_version.wProductType != VER_NT_WORKSTATION;
     product_name = ga_get_win_product_name(errp);
--

2.34.1

Signature · 1 line
Marc-André Lureau
author
#7 lore raw

PATCH v2 4/6 util/qemu-sockets: Enable unix socket support on Windows

In reply to Bin Meng

From: Bin Meng <[email protected]>

Support for the unix socket has existed both in BSD and Linux for the longest time, but not on Windows. Since Windows 10 build 17063 [1], the native support for the unix socket has came to Windows. Starting this build, two Win32 processes can use the AF_UNIX address family over Winsock API to communicate with each other.

Introduce a new build time config option CONFIG_AF_UNIX when the build host has such a capability, and a run-time check afunix_available() for Windows host in the QEMU sockets util codes.

[1] https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/

---

Changes in v2:
- move #include <afunix.h> to os-win32.h
- define WIN_BUILD_AF_UNIX only when CONFIG_WIN32

 meson.build               |  6 ++++++
 include/sysemu/os-win32.h |  4 ++++
 util/qemu-sockets.c       | 45 +++++++++++++++++++++++++++++++++------
 3 files changed, 48 insertions(+), 7 deletions(-)
meson.build +6 -0
index 75aaca8462..73e5de5957 100644
@@ -2327,6 +2327,12 @@ have_afalg = get_option('crypto_afalg') \
   '''), error_message: 'AF_ALG requested but could not be detected').allowed()
 config_host_data.set('CONFIG_AF_ALG', have_afalg)
 
+if targetos != 'windows'
+  config_host_data.set('CONFIG_AF_UNIX', true)
+else
+  config_host_data.set('CONFIG_AF_UNIX', cc.has_header('afunix.h'))
+endif
+
 config_host_data.set('CONFIG_AF_VSOCK', cc.has_header_symbol(
   'linux/vm_sockets.h', 'AF_VSOCK',
   prefix: '#include <sys/socket.h>',
include/sysemu/os-win32.h +4 -0
index 1e324026a4..62aac7c930 100644
@@ -30,6 +30,10 @@
 #include <windows.h>
 #include <ws2tcpip.h>
 
+#ifdef CONFIG_AF_UNIX
+# include <afunix.h>
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
util/qemu-sockets.c +38 -7

101 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v2 5/6 chardev/char-socket: Update AF_UNIX for Windows

In reply to Bin Meng

From: Bin Meng <[email protected]>

Now that AF_UNIX has come to Windows, update the existing logic in qemu_chr_compute_filename() and qmp_chardev_open_socket() for Windows.

---

Changes in v2:
- drop #include <afunix.h> as it is now already included in osdep.h

 chardev/char-socket.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/char-socket.c +3 -1
index dc4e218eeb..14a56b7b13 100644
@@ -557,7 +557,7 @@ static char *qemu_chr_compute_filename(SocketChardev *s)
     const char *left = "", *right = "";
 
     switch (ss->ss_family) {
-#ifndef _WIN32
+#ifdef CONFIG_AF_UNIX
     case AF_UNIX:
         return g_strdup_printf("unix:%s%s",
                                ((struct sockaddr_un *)(ss))->sun_path,
@@ -1372,10 +1372,12 @@ static void qmp_chardev_open_socket(Chardev *chr,
     }
 
     qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
+#ifndef _WIN32
     /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
     if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
         qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
     }
+#endif
 
     /*
      * In the chardev-change special-case, we shouldn't register a new yank
Signature · 1 line
2.34.1
Reviewed-by on v2 5/6
#9 lore raw

Re: [PATCH v2 5/6] chardev/char-socket: Update AF_UNIX for Windows

Hi

Quoted 7 lines · Bin Meng

From: Bin Meng <[email protected]>

Now that AF_UNIX has come to Windows, update the existing logic in qemu_chr_compute_filename() and qmp_chardev_open_socket() for Windows.

lgtm,

Quoted 37 lines

---

Changes in v2:
- drop #include <afunix.h> as it is now already included in osdep.h

 chardev/char-socket.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/char-socket.c +3 -2
index dc4e218eeb..14a56b7b13 100644
@@ -557,7 +557,7 @@ static char *qemu_chr_compute_filename(SocketChardev *s)
     const char *left = "", *right = "";
 
     switch (ss->ss_family) {
-#ifndef _WIN32
+#ifdef CONFIG_AF_UNIX
     case AF_UNIX:
         return g_strdup_printf("unix:%s%s",
                                ((struct sockaddr_un *)(ss))->sun_path,
@@ -1372,10 +1372,12 @@ static void qmp_chardev_open_socket(Chardev *chr,
     }
 
     qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
+#ifndef _WIN32
     /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
     if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
         qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
     }
+#endif
 
     /*
      * In the chardev-change special-case, we shouldn't register a new yank
--

2.34.1

author
#10 lore raw

PATCH v2 6/6 tests/unit: Update test-io-channel-socket.c for Windows

In reply to Bin Meng

From: Bin Meng <[email protected]>

Enable the following 3 test cases for Windows when AF_UNIX is available:

* test_io_channel_unix_sync
* test_io_channel_unix_async
* test_io_channel_unix_listen_cleanup

---

Changes in v2:
- new patch: tests/unit: Update test-io-channel-socket.c for Windows

 tests/unit/test-io-channel-socket.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
tests/unit/test-io-channel-socket.c +14 -2
index 6713886d02..ec5df32489 100644
@@ -179,10 +179,12 @@ static void test_io_channel(bool async,
         test_io_channel_setup_async(listen_addr, connect_addr,
                                     &srv, &src, &dst);
 
+#ifndef _WIN32
         g_assert(!passFD ||
                  qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
         g_assert(!passFD ||
                  qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
+#endif
         g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
         g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
 
@@ -206,10 +208,12 @@ static void test_io_channel(bool async,
         test_io_channel_setup_async(listen_addr, connect_addr,
                                     &srv, &src, &dst);
 
+#ifndef _WIN32
         g_assert(!passFD ||
                  qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
         g_assert(!passFD ||
                  qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
+#endif
         g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
         g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
 
@@ -236,10 +240,12 @@ static void test_io_channel(bool async,
         test_io_channel_setup_sync(listen_addr, connect_addr,
                                    &srv, &src, &dst);
 
+#ifndef _WIN32
         g_assert(!passFD ||
                  qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
         g_assert(!passFD ||
                  qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
+#endif
         g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
         g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
 
@@ -263,10 +269,12 @@ static void test_io_channel(bool async,
         test_io_channel_setup_sync(listen_addr, connect_addr,
                                    &srv, &src, &dst);
 
+#ifndef _WIN32
         g_assert(!passFD ||
                  qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
         g_assert(!passFD ||
                  qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
+#endif
         g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
         g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
 
@@ -367,7 +375,7 @@ static void test_io_channel_ipv6_async(void)
 }
 
 
-#ifndef _WIN32
+#ifdef CONFIG_AF_UNIX
 static void test_io_channel_unix(bool async)
 {
     SocketAddress *listen_addr = g_new0(SocketAddress, 1);
@@ -398,6 +406,7 @@ static void test_io_channel_unix_async(void)
     return test_io_channel_unix(true);
 }
 
+#ifndef _WIN32
 static void test_io_channel_unix_fd_pass(void)
 {
     SocketAddress *listen_addr = g_new0(SocketAddress, 1);
@@ -491,6 +500,7 @@ static void test_io_channel_unix_fd_pass(void)
     }
     g_free(fdrecv);
 }
+#endif /* _WIN32 */
 
 static void test_io_channel_unix_listen_cleanup(void)
 {
@@ -588,13 +598,15 @@ int main(int argc, char **argv)
                         test_io_channel_ipv6_async);
     }
 
-#ifndef _WIN32
+#ifdef CONFIG_AF_UNIX
     g_test_add_func("/io/channel/socket/unix-sync",
                     test_io_channel_unix_sync);
     g_test_add_func("/io/channel/socket/unix-async",
                     test_io_channel_unix_async);
+#ifndef _WIN32
     g_test_add_func("/io/channel/socket/unix-fd-pass",
                     test_io_channel_unix_fd_pass);
+#endif
     g_test_add_func("/io/channel/socket/unix-listen-cleanup",
                     test_io_channel_unix_listen_cleanup);
 #endif /* _WIN32 */
Signature · 1 line
2.34.1