Skip to contentSkip to conversation

Enable unix socket support on Windows

PATCH In review Bin Meng 3 participants 9 messages Aug 2, 2022 – Sep 1, 2022 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

v4 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 come to Windows. Starting this build, two Win32 processes can use the AF_UNIX address family over Winsock API to communicate with each other.

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

Changes in v4:
- instead of introducing CONFIG_AF_UNIX, add fallback afunix.h header in os-win32.h, and compile the AF_UNIX stuff for all Windows hosts
- drop CONFIG_AF_UNIX
- introduce a new helper socket_check_afunix_support() to runtime-check the availability of AF_UNIX socket, and skip those appropriately

Changes in v3:
- drop the run-time check afunix_available()

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 (4):
util/qemu-sockets: Replace the call to close a socket with
closesocket()
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                         |  3 +++
 include/sysemu/os-win32.h           | 17 +++++++++++++
 tests/unit/socket-helpers.h         |  9 +++++++
 chardev/char-socket.c               |  4 ++--
 tests/unit/socket-helpers.c         | 16 +++++++++++++
 tests/unit/test-io-channel-socket.c | 37 ++++++++++++++++++-----------
 util/qemu-sockets.c                 | 29 ++--------------------
 7 files changed, 72 insertions(+), 43 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v4 1/4 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
author
#2 lore raw

PATCH v4 2/4 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 come to Windows. Starting this build, two Win32 processes can use the AF_UNIX address family over Winsock API to communicate with each other.

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

---

Changes in v4:
- instead of introducing CONFIG_AF_UNIX, add fallback afunix.h header in os-win32.h, and compile the AF_UNIX stuff for all Windows hosts

Changes in v3:
- drop the run-time check afunix_available()

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

 meson.build               |  3 +++
 include/sysemu/os-win32.h | 17 +++++++++++++++++
 util/qemu-sockets.c       | 25 -------------------------
 3 files changed, 20 insertions(+), 25 deletions(-)
meson.build +3 -0
index 294e9a8f32..6749223f1a 100644
@@ -1890,6 +1890,9 @@ config_host_data.set('HAVE_PTY_H', cc.has_header('pty.h'))
 config_host_data.set('HAVE_SYS_DISK_H', cc.has_header('sys/disk.h'))
 config_host_data.set('HAVE_SYS_IOCCOM_H', cc.has_header('sys/ioccom.h'))
 config_host_data.set('HAVE_SYS_KCOV_H', cc.has_header('sys/kcov.h'))
+if targetos == 'windows'
+  config_host_data.set('HAVE_AFUNIX_H', cc.has_header('afunix.h'))
+endif
 
 # has_function
 config_host_data.set('CONFIG_ACCEPT4', cc.has_function('accept4'))
include/sysemu/os-win32.h +17 -0
index edc3b38a57..5b38c7bd04 100644
@@ -30,6 +30,23 @@
 #include <windows.h>
 #include <ws2tcpip.h>
 
+#ifdef HAVE_AFUNIX_H
+#include <afunix.h>
+#else
+/*
+ * Fallback definitions of things we need in afunix.h, if not available from
+ * the used Windows SDK or MinGW headers.
+ */
+#define UNIX_PATH_MAX 108
+
+typedef struct sockaddr_un {
+    ADDRESS_FAMILY sun_family;
+    char sun_path[UNIX_PATH_MAX];
+} SOCKADDR_UN, *PSOCKADDR_UN;
+
+#define SIO_AF_UNIX_GETPEERPID _WSAIOR(IOC_VENDOR, 256)
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
util/qemu-sockets.c +0 -25
index 0e2298278f..83f4bd6fd2 100644
@@ -880,8 +880,6 @@ static int vsock_parse(VsockSocketAddress *addr, const char *str,
 }
 #endif /* CONFIG_AF_VSOCK */
 
-#ifndef _WIN32
-
 static bool saddr_is_abstract(UnixSocketAddress *saddr)
 {
 #ifdef CONFIG_LINUX
@@ -1054,25 +1052,6 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
     return -1;
 }
 
-#else
-
-static int unix_listen_saddr(UnixSocketAddress *saddr,
-                             int num,
-                             Error **errp)
-{
-    error_setg(errp, "unix sockets are not available on windows");
-    errno = ENOTSUP;
-    return -1;
-}
-
-static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
-{
-    error_setg(errp, "unix sockets are not available on windows");
-    errno = ENOTSUP;
-    return -1;
-}
-#endif
-
 /* compatibility wrapper */
 int unix_listen(const char *str, Error **errp)
 {
@@ -1335,7 +1314,6 @@ socket_sockaddr_to_address_inet(struct sockaddr_storage *sa,
 }
 
 
-#ifndef WIN32
 static SocketAddress *
 socket_sockaddr_to_address_unix(struct sockaddr_storage *sa,
                                 socklen_t salen,
@@ -1362,7 +1340,6 @@ socket_sockaddr_to_address_unix(struct sockaddr_storage *sa,
     addr->u.q_unix.path = g_strndup(su->sun_path, salen);
     return addr;
 }
-#endif /* WIN32 */
 
 #ifdef CONFIG_AF_VSOCK
 static SocketAddress *
@@ -1394,10 +1371,8 @@ socket_sockaddr_to_address(struct sockaddr_storage *sa,
     case AF_INET6:
         return socket_sockaddr_to_address_inet(sa, salen, errp);
 
-#ifndef WIN32
     case AF_UNIX:
         return socket_sockaddr_to_address_unix(sa, salen, errp);
-#endif /* WIN32 */
 
 #ifdef CONFIG_AF_VSOCK
     case AF_VSOCK:
Signature · 1 line
2.34.1
Reviewed-by on v4 2/4
#3 lore raw

Re: [PATCH v4 2/4] util/qemu-sockets: Enable unix socket support on Windows

Quoted 8 lines · 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 come to Windows. Starting this build, two Win32 processes can use the AF_UNIX address family over Winsock API to communicate with each other.

Quoted 5 lines

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

Quoted 138 lines

---

Changes in v4:
- instead of introducing CONFIG_AF_UNIX, add fallback afunix.h header in os-win32.h, and compile the AF_UNIX stuff for all Windows hosts

Changes in v3:
- drop the run-time check afunix_available()

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

 meson.build               |  3 +++
 include/sysemu/os-win32.h | 17 +++++++++++++++++
 util/qemu-sockets.c       | 25 -------------------------
 3 files changed, 20 insertions(+), 25 deletions(-)
meson.build +3 -0
index 294e9a8f32..6749223f1a 100644
@@ -1890,6 +1890,9 @@ config_host_data.set('HAVE_PTY_H',
 cc.has_header('pty.h'))
 config_host_data.set('HAVE_SYS_DISK_H', cc.has_header('sys/disk.h'))
 config_host_data.set('HAVE_SYS_IOCCOM_H', cc.has_header('sys/ioccom.h'))
 config_host_data.set('HAVE_SYS_KCOV_H', cc.has_header('sys/kcov.h'))
+if targetos == 'windows'
+  config_host_data.set('HAVE_AFUNIX_H', cc.has_header('afunix.h'))
+endif
 
 # has_function

config_host_data.set('CONFIG_ACCEPT4', cc.has_function('accept4'))

include/sysemu/os-win32.h +5 -0
index edc3b38a57..5b38c7bd04 100644
@@ -30,6 +30,23 @@
 #include <windows.h>
 #include <ws2tcpip.h>
 
+#ifdef HAVE_AFUNIX_H
+#include <afunix.h>
+#else
+/*
+ * Fallback definitions of things we need in afunix.h, if not available

from
+ * the used Windows SDK or MinGW headers.
+ */

include/sysemu/os-win32.h +10 -0
+#define UNIX_PATH_MAX 108
+
+typedef struct sockaddr_un {
+    ADDRESS_FAMILY sun_family;
+    char sun_path[UNIX_PATH_MAX];
+} SOCKADDR_UN, *PSOCKADDR_UN;
+
+#define SIO_AF_UNIX_GETPEERPID _WSAIOR(IOC_VENDOR, 256)
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
util/qemu-sockets.c +0 -2
index 0e2298278f..83f4bd6fd2 100644
@@ -880,8 +880,6 @@ static int vsock_parse(VsockSocketAddress *addr, const
 char *str,
 }
 #endif /* CONFIG_AF_VSOCK */
 
-#ifndef _WIN32
-
 static bool saddr_is_abstract(UnixSocketAddress *saddr)
 {

#ifdef CONFIG_LINUX

util/qemu-sockets.c +0 -19
@@ -1054,25 +1052,6 @@ static int unix_connect_saddr(UnixSocketAddress
 *saddr, Error **errp)
     return -1;
 }
 
-#else
-
-static int unix_listen_saddr(UnixSocketAddress *saddr,
-                             int num,
-                             Error **errp)
-{
-    error_setg(errp, "unix sockets are not available on windows");
-    errno = ENOTSUP;
-    return -1;
-}
-
-static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)
-{
-    error_setg(errp, "unix sockets are not available on windows");
-    errno = ENOTSUP;
-    return -1;
-}
-#endif
-
 /* compatibility wrapper */
 int unix_listen(const char *str, Error **errp)

{

util/qemu-sockets.c +0 -1
@@ -1335,7 +1314,6 @@ socket_sockaddr_to_address_inet(struct
 sockaddr_storage *sa,
 }
 
 
-#ifndef WIN32
 static SocketAddress *
 socket_sockaddr_to_address_unix(struct sockaddr_storage *sa,

socklen_t salen,

util/qemu-sockets.c +0 -1
@@ -1362,7 +1340,6 @@ socket_sockaddr_to_address_unix(struct
 sockaddr_storage *sa,
     addr->u.q_unix.path = g_strndup(su->sun_path, salen);
     return addr;
 }
-#endif /* WIN32 */
 
 #ifdef CONFIG_AF_VSOCK

static SocketAddress *

util/qemu-sockets.c +0 -2
@@ -1394,10 +1371,8 @@ socket_sockaddr_to_address(struct sockaddr_storage
 *sa,
     case AF_INET6:
         return socket_sockaddr_to_address_inet(sa, salen, errp);
 
-#ifndef WIN32
     case AF_UNIX:
         return socket_sockaddr_to_address_unix(sa, salen, errp);
-#endif /* WIN32 */
 
 #ifdef CONFIG_AF_VSOCK

case AF_VSOCK:
--
2.34.1

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

PATCH v4 3/4 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 v4:
- drop CONFIG_AF_UNIX

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

 chardev/char-socket.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
chardev/char-socket.c +2 -2
index dc4e218eeb..879564aa8a 100644
@@ -557,12 +557,10 @@ static char *qemu_chr_compute_filename(SocketChardev *s)
     const char *left = "", *right = "";
 
     switch (ss->ss_family) {
-#ifndef _WIN32
     case AF_UNIX:
         return g_strdup_printf("unix:%s%s",
                                ((struct sockaddr_un *)(ss))->sun_path,
                                s->is_listen ? ",server=on" : "");
-#endif
     case AF_INET6:
         left  = "[";
         right = "]";
@@ -1372,10 +1370,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
author
#5 lore raw

PATCH v4 4/4 tests/unit: Update test-io-channel-socket.c for Windows

In reply to Bin Meng

From: Bin Meng <[email protected]>

Change to dynamically include the test cases by checking AF_UNIX availability using a new helper socket_check_afunix_support(). With such changes testing on a Windows host can be covered as well.

---

Changes in v4:
- introduce a new helper socket_check_afunix_support() to runtime-check the availability of AF_UNIX socket, and skip those appropriately

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

 tests/unit/socket-helpers.h         |  9 +++++++
 tests/unit/socket-helpers.c         | 16 +++++++++++++
 tests/unit/test-io-channel-socket.c | 37 ++++++++++++++++++-----------
 3 files changed, 48 insertions(+), 14 deletions(-)
tests/unit/socket-helpers.h +9 -0
index 512a004811..ed8477ceb3 100644
@@ -32,4 +32,13 @@
  */
 int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6);
 
+/*
+ * @has_afunix: set to true on return if unix socket support is available
+ *
+ * Check whether unix domain socket support is available for use.
+ * On success, @has_afunix will be set to indicate whether AF_UNIX protocol
+ * is available.
+ */
+void socket_check_afunix_support(bool *has_afunix);
+
 #endif
tests/unit/socket-helpers.c +16 -0
index 5af4de513b..eecadf3a3c 100644
@@ -154,3 +154,19 @@ int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6)
 
     return 0;
 }
+
+void socket_check_afunix_support(bool *has_afunix)
+{
+    int fd;
+
+    fd = socket(PF_UNIX, SOCK_STREAM, 0);
+    closesocket(fd);
+
+#ifdef _WIN32
+    *has_afunix = (fd != (int)INVALID_SOCKET);
+#else
+    *has_afunix = (fd >= 0);
+#endif
+
+    return;
+}
tests/unit/test-io-channel-socket.c +23 -14
index 6713886d02..b36a5d972a 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,6 @@ static void test_io_channel_ipv6_async(void)
 }
 
 
-#ifndef _WIN32
 static void test_io_channel_unix(bool async)
 {
     SocketAddress *listen_addr = g_new0(SocketAddress, 1);
@@ -398,6 +405,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 +499,7 @@ static void test_io_channel_unix_fd_pass(void)
     }
     g_free(fdrecv);
 }
+#endif /* _WIN32 */
 
 static void test_io_channel_unix_listen_cleanup(void)
 {
@@ -522,9 +531,6 @@ static void test_io_channel_unix_listen_cleanup(void)
     unlink(TEST_SOCKET);
 }
 
-#endif /* _WIN32 */
-
-
 static void test_io_channel_ipv4_fd(void)
 {
     QIOChannel *ioc;
@@ -555,7 +561,7 @@ static void test_io_channel_ipv4_fd(void)
 
 int main(int argc, char **argv)
 {
-    bool has_ipv4, has_ipv6;
+    bool has_ipv4, has_ipv6, has_afunix;
 
     module_call_init(MODULE_INIT_QOM);
     qemu_init_main_loop(&error_abort);
@@ -588,16 +594,19 @@ int main(int argc, char **argv)
                         test_io_channel_ipv6_async);
     }
 
+    socket_check_afunix_support(&has_afunix);
+    if (has_afunix) {
+        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-sync",
-                    test_io_channel_unix_sync);
-    g_test_add_func("/io/channel/socket/unix-async",
-                    test_io_channel_unix_async);
-    g_test_add_func("/io/channel/socket/unix-fd-pass",
-                    test_io_channel_unix_fd_pass);
-    g_test_add_func("/io/channel/socket/unix-listen-cleanup",
-                    test_io_channel_unix_listen_cleanup);
-#endif /* _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);
+    }
 
 end:
     return g_test_run();
Signature · 1 line
2.34.1
Reviewed-by on v4 4/4
#6 lore raw

Re: [PATCH v4 4/4] tests/unit: Update test-io-channel-socket.c for Windows

Hi

Quoted 46 lines · Bin Meng

From: Bin Meng <[email protected]>

Change to dynamically include the test cases by checking AF_UNIX availability using a new helper socket_check_afunix_support(). With such changes testing on a Windows host can be covered as well.

---

Changes in v4:
- introduce a new helper socket_check_afunix_support() to runtime-check the availability of AF_UNIX socket, and skip those appropriately

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

 tests/unit/socket-helpers.h         |  9 +++++++
 tests/unit/socket-helpers.c         | 16 +++++++++++++
 tests/unit/test-io-channel-socket.c | 37 ++++++++++++++++++-----------
 3 files changed, 48 insertions(+), 14 deletions(-)
tests/unit/socket-helpers.h +5 -0
index 512a004811..ed8477ceb3 100644
@@ -32,4 +32,13 @@
  */
 int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6);
 
+/*
+ * @has_afunix: set to true on return if unix socket support is available
+ *
+ * Check whether unix domain socket support is available for use.
+ * On success, @has_afunix will be set to indicate whether AF_UNIX

protocol
+ * is available.
+ */

tests/unit/socket-helpers.h +2 -0
+void socket_check_afunix_support(bool *has_afunix);
+
 #endif
tests/unit/socket-helpers.c +0 -0
index 5af4de513b..eecadf3a3c 100644

tests/unit/socket-helpers.c int socket_check_protocol_support(bool *has_ipv4, · line 158

Quoted 6 lines
tests/unit/socket-helpers.c +2 -0
@@ -154,3 +154,19 @@ int socket_check_protocol_support(bool *has_ipv4,
 bool *has_ipv6)
 
     return 0;
 }
+
+void socket_check_afunix_support(bool *has_afunix)

Why not return TRUE for success? Ah, for consistency with socket_check_protocol_support (). Maybe you should extend that function instead then?

Quoted 131 lines
tests/unit/socket-helpers.c +14 -0
+{
+    int fd;
+
+    fd = socket(PF_UNIX, SOCK_STREAM, 0);
+    closesocket(fd);
+
+#ifdef _WIN32
+    *has_afunix = (fd != (int)INVALID_SOCKET);
+#else
+    *has_afunix = (fd >= 0);
+#endif
+
+    return;
+}
tests/unit/test-io-channel-socket.c +2 -0
index 6713886d02..b36a5d972a 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));

tests/unit/test-io-channel-socket.c +2 -0
@@ -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));

tests/unit/test-io-channel-socket.c +2 -0
@@ -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));

tests/unit/test-io-channel-socket.c +2 -0
@@ -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));

tests/unit/test-io-channel-socket.c +3 -5
@@ -367,7 +375,6 @@ static void test_io_channel_ipv6_async(void)
 }
 
 
-#ifndef _WIN32
 static void test_io_channel_unix(bool async)
 {
     SocketAddress *listen_addr = g_new0(SocketAddress, 1);
@@ -398,6 +405,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 +499,7 @@ static void test_io_channel_unix_fd_pass(void)
     }
     g_free(fdrecv);
 }
+#endif /* _WIN32 */
 
 static void test_io_channel_unix_listen_cleanup(void)
 {
@@ -522,9 +531,6 @@ static void test_io_channel_unix_listen_cleanup(void)
     unlink(TEST_SOCKET);
 }
 
-#endif /* _WIN32 */
-
-
 static void test_io_channel_ipv4_fd(void)
 {
     QIOChannel *ioc;
@@ -555,7 +561,7 @@ static void test_io_channel_ipv4_fd(void)
 
 int main(int argc, char **argv)
 {
-    bool has_ipv4, has_ipv6;
+    bool has_ipv4, has_ipv6, has_afunix;
 
     module_call_init(MODULE_INIT_QOM);
     qemu_init_main_loop(&error_abort);

tests/unit/test-io-channel-socket.c int main(int argc, char **argv) · line 603

Quoted 10 lines
tests/unit/test-io-channel-socket.c +6 -0
@@ -588,16 +594,19 @@ int main(int argc, char **argv)
                         test_io_channel_ipv6_async);
     }
 
+    socket_check_afunix_support(&has_afunix);
+    if (has_afunix) {
+        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);
 

(I would also have g_test_skip() inside the tests, but that's pre-existing too)

#ifndef _WIN32

Quoted 19 lines
tests/unit/test-io-channel-socket.c +6 -10
-    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);
-    g_test_add_func("/io/channel/socket/unix-fd-pass",
-                    test_io_channel_unix_fd_pass);
-    g_test_add_func("/io/channel/socket/unix-listen-cleanup",
-                    test_io_channel_unix_listen_cleanup);
-#endif /* _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);
+    }
 
 end:
     return g_test_run();
--
Quoted 4 lines

2.34.1

So, lgtm

Signature · 1 line
Marc-André Lureau
author
#7 lore raw
In reply to Bin Meng

Hi,

Quoted 7 lines · Bin Meng

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 come to Windows. Starting this build, two Win32 processes can use the AF_UNIX address family over Winsock API to communicate with each other.

Quoted 9 lines

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

Changes in v4:
- instead of introducing CONFIG_AF_UNIX, add fallback afunix.h header in os-win32.h, and compile the AF_UNIX stuff for all Windows hosts
- drop CONFIG_AF_UNIX
- introduce a new helper socket_check_afunix_support() to runtime-check the availability of AF_UNIX socket, and skip those appropriately

All patches in this series have been reviewed. Would you please queue this? Thanks!

Regards,
Bin

#8 lore raw

Hi

Quoted 21 lines · Bin Meng

Hi,

Quoted 7 lines · Bin Meng

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 come to Windows. Starting this build, two Win32 processes can use the AF_UNIX address family over Winsock API to communicate with each other.

Quoted 9 lines

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

Changes in v4:
- instead of introducing CONFIG_AF_UNIX, add fallback afunix.h header in os-win32.h, and compile the AF_UNIX stuff for all Windows hosts
- drop CONFIG_AF_UNIX
- introduce a new helper socket_check_afunix_support() to runtime-check the availability of AF_UNIX socket, and skip those appropriately

Quoted 2 lines

All patches in this series have been reviewed. Would you please queue this? Thanks!

Yes, I was going to do it. Thanks