Skip to contentSkip to conversation

Various win32 fixes & new 'get-win32-socket' QMP command

PATCH Applied [email protected] 5 participants 23 messages Jan 3, 2023 – Jan 16, 2023

lore
Share & tools
lore
#0 lore raw

cover Various win32 fixes & new 'get-win32-socket' QMP command

From: Marc-André Lureau <[email protected]>

Hi,

The following series first fixes a few tests on win32. The second part focuses on 'add_client' support, by limiting its scope to sockets and adding win32 support. Finally, it enables vnc-display test on win32, to exercise the new code paths and demonstrate its usage.

'get-win32-socket' can be used to write more robusts code & tests using sockets on Windows, and will be used by a follow up series testing dbus display support.

Marc-André Lureau (10):
ccid-card-emulated: fix cast warning/error
tests: fix path separator, use g_build_filename()
tests: fix test-io-channel-command on win32 tests/docker: fix a win32 error due to portability tests/readconfig: spice doesn't support unix socket on windows yet
osdep: implement qemu_socketpair() for win32
qmp: 'add_client' actually expects sockets
qmp: add 'get-win32-socket'
libqtest: make qtest_qmp_add_client work on win32
qtest: enable vnc-display test on win32

 qapi/misc.json                       |  32 ++++++++
 include/qemu/sockets.h               |   2 -
 tests/qtest/libqtest.h               |   5 +-
 hw/usb/ccid-card-emulated.c          |   2 +-
 monitor/misc.c                       |  74 ++++++++++++++----
 monitor/qmp-cmds.c                   |  19 +++--
 tests/qtest/libqtest.c               |  16 +++-
 tests/qtest/readconfig-test.c        |   6 +-
 tests/qtest/vnc-display-test.c       |   5 --
 tests/unit/test-io-channel-command.c |   8 +-
 util/oslib-win32.c                   | 110 +++++++++++++++++++++++++++
 tests/docker/docker.py               |   6 +-
 12 files changed, 245 insertions(+), 40 deletions(-)
Signature · 1 line
2.39.0
#1 lore raw

PATCH 1/10 ccid-card-emulated: fix cast warning/error

From: Marc-André Lureau <[email protected]>

../hw/usb/ccid-card-emulated.c: In function 'handle_apdu_thread':
../hw/usb/ccid-card-emulated.c:251:24: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
251 | assert((unsigned long)event > 1000);

---
 hw/usb/ccid-card-emulated.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/ccid-card-emulated.c +1 -1
index ee41a81801..c328660075 100644
@@ -248,7 +248,7 @@ static void *handle_apdu_thread(void* arg)
         WITH_QEMU_LOCK_GUARD(&card->vreader_mutex) {
             while (!QSIMPLEQ_EMPTY(&card->guest_apdu_list)) {
                 event = QSIMPLEQ_FIRST(&card->guest_apdu_list);
-                assert((unsigned long)event > 1000);
+                assert(event != NULL);
                 QSIMPLEQ_REMOVE_HEAD(&card->guest_apdu_list, entry);
                 if (event->p.data.type != EMUL_GUEST_APDU) {
                     DPRINTF(card, 1, "unexpected message in handle_apdu_thread\n");
Signature · 1 line
2.39.0
Reviewed-by on v1 1/10
#2 lore raw

Re: [PATCH 01/10] ccid-card-emulated: fix cast warning/error

Quoted 16 lines · On 03/01/2023 12.08, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

../hw/usb/ccid-card-emulated.c: In function 'handle_apdu_thread':
../hw/usb/ccid-card-emulated.c:251:24: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
251 | assert((unsigned long)event > 1000);

---
  hw/usb/ccid-card-emulated.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/ccid-card-emulated.c +0 -0
index ee41a81801..c328660075 100644

hw/usb/ccid-card-emulated.c static void *handle_apdu_thread(void* arg) · line 254

Quoted 8 lines
hw/usb/ccid-card-emulated.c +1 -1
@@ -248,7 +248,7 @@ static void *handle_apdu_thread(void* arg)
          WITH_QEMU_LOCK_GUARD(&card->vreader_mutex) {
              while (!QSIMPLEQ_EMPTY(&card->guest_apdu_list)) {
                  event = QSIMPLEQ_FIRST(&card->guest_apdu_list);
-                assert((unsigned long)event > 1000);
+                assert(event != NULL);
                  QSIMPLEQ_REMOVE_HEAD(&card->guest_apdu_list, entry);
                  if (event->p.data.type != EMUL_GUEST_APDU) {
                      DPRINTF(card, 1, "unexpected message in handle_apdu_thread\n");
#3 lore raw

Re: [PATCH 01/10] ccid-card-emulated: fix cast warning/error

In reply to [email protected]

Le 03/01/2023 à 12:08, [email protected] a écrit :

Quoted 16 lines

From: Marc-André Lureau <[email protected]>

../hw/usb/ccid-card-emulated.c: In function 'handle_apdu_thread':
../hw/usb/ccid-card-emulated.c:251:24: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
251 | assert((unsigned long)event > 1000);

---
  hw/usb/ccid-card-emulated.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/ccid-card-emulated.c +0 -0
index ee41a81801..c328660075 100644

hw/usb/ccid-card-emulated.c static void *handle_apdu_thread(void* arg) · line 254

Quoted 8 lines
hw/usb/ccid-card-emulated.c +1 -1
@@ -248,7 +248,7 @@ static void *handle_apdu_thread(void* arg)
          WITH_QEMU_LOCK_GUARD(&card->vreader_mutex) {
              while (!QSIMPLEQ_EMPTY(&card->guest_apdu_list)) {
                  event = QSIMPLEQ_FIRST(&card->guest_apdu_list);
-                assert((unsigned long)event > 1000);
+                assert(event != NULL);
                  QSIMPLEQ_REMOVE_HEAD(&card->guest_apdu_list, entry);
                  if (event->p.data.type != EMUL_GUEST_APDU) {
                      DPRINTF(card, 1, "unexpected message in handle_apdu_thread\n");

Applied to my trivial-patches branch.

Thanks,
Laurent

#4 lore raw

PATCH 2/10 tests: fix path separator, use g_build_filename()

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

---
 tests/unit/test-io-channel-command.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/unit/test-io-channel-command.c +1 -1
index 19f72eab96..096224962c 100644
@@ -32,7 +32,7 @@ static char *socat = NULL;
 static void test_io_channel_command_fifo(bool async)
 {
     g_autofree gchar *tmpdir = g_dir_make_tmp("qemu-test-io-channel.XXXXXX", NULL);
-    g_autofree gchar *fifo = g_strdup_printf("%s/%s", tmpdir, TEST_FIFO);
+    g_autofree gchar *fifo = g_build_filename(tmpdir, TEST_FIFO, NULL);
     g_autofree gchar *srcargs = g_strdup_printf("%s - PIPE:%s,wronly", socat, fifo);
     g_autofree gchar *dstargs = g_strdup_printf("%s PIPE:%s,rdonly -", socat, fifo);
     g_auto(GStrv) srcargv = g_strsplit(srcargs, " ", -1);
Signature · 1 line
2.39.0
Reviewed-by on v1 2/10
#5 lore raw

Re: [PATCH 02/10] tests: fix path separator, use g_build_filename()

Quoted 12 lines · On 03/01/2023 12.08, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

---
  tests/unit/test-io-channel-command.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/unit/test-io-channel-command.c +0 -0
index 19f72eab96..096224962c 100644

tests/unit/test-io-channel-command.c static char *socat = NULL; · line 38

Quoted 8 lines
tests/unit/test-io-channel-command.c +1 -1
@@ -32,7 +32,7 @@ static char *socat = NULL;
  static void test_io_channel_command_fifo(bool async)
  {
      g_autofree gchar *tmpdir = g_dir_make_tmp("qemu-test-io-channel.XXXXXX", NULL);
-    g_autofree gchar *fifo = g_strdup_printf("%s/%s", tmpdir, TEST_FIFO);
+    g_autofree gchar *fifo = g_build_filename(tmpdir, TEST_FIFO, NULL);
      g_autofree gchar *srcargs = g_strdup_printf("%s - PIPE:%s,wronly", socat, fifo);
      g_autofree gchar *dstargs = g_strdup_printf("%s PIPE:%s,rdonly -", socat, fifo);
      g_auto(GStrv) srcargv = g_strsplit(srcargs, " ", -1);
#7 lore raw

PATCH 3/10 tests: fix test-io-channel-command on win32

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

socat "PIPE:"" on Windows are named pipes, not fifo path names.

Fixes commit 68406d10859 ("tests/unit: cleanups for test-io-channel-command")
---
 tests/unit/test-io-channel-command.c | 6 ++++++
 1 file changed, 6 insertions(+)
tests/unit/test-io-channel-command.c +6 -0
index 096224962c..e76ef2daaa 100644
@@ -31,8 +31,12 @@ static char *socat = NULL;
 
 static void test_io_channel_command_fifo(bool async)
 {
+#ifdef WIN32
+    const gchar *fifo = TEST_FIFO;
+#else
     g_autofree gchar *tmpdir = g_dir_make_tmp("qemu-test-io-channel.XXXXXX", NULL);
     g_autofree gchar *fifo = g_build_filename(tmpdir, TEST_FIFO, NULL);
+#endif
     g_autofree gchar *srcargs = g_strdup_printf("%s - PIPE:%s,wronly", socat, fifo);
     g_autofree gchar *dstargs = g_strdup_printf("%s PIPE:%s,rdonly -", socat, fifo);
     g_auto(GStrv) srcargv = g_strsplit(srcargs, " ", -1);
@@ -57,7 +61,9 @@ static void test_io_channel_command_fifo(bool async)
     object_unref(OBJECT(src));
     object_unref(OBJECT(dst));
 
+#ifndef WIN32
     g_rmdir(tmpdir);
+#endif
 }
 
 
Signature · 1 line
2.39.0
#8 lore raw

PATCH 4/10 tests/docker: fix a win32 error due to portability

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

docker.py is run during configure, and produces an error: No module named 'pwd'.

Use a more portable and recommended alternative to lookup the user "login name".

---
 tests/docker/docker.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/docker/docker.py +3 -3
index 3a1ed7cb18..688ef62989 100755
@@ -23,10 +23,10 @@
 import tempfile
 import re
 import signal
+import getpass
 from tarfile import TarFile, TarInfo
 from io import StringIO, BytesIO
 from shutil import copy, rmtree
-from pwd import getpwuid
 from datetime import datetime, timedelta
 
 
@@ -316,7 +316,7 @@ def build_image(self, tag, docker_dir, dockerfile,
 
         if user:
             uid = os.getuid()
-            uname = getpwuid(uid).pw_name
+            uname = getpass.getuser()
             tmp_df.write("\n")
             tmp_df.write("RUN id %s 2>/dev/null || useradd -u %d -U %s" %
                          (uname, uid, uname))
@@ -570,7 +570,7 @@ def run(self, args, argv):
 
         if args.user:
             uid = os.getuid()
-            uname = getpwuid(uid).pw_name
+            uname = getpass.getuser()
             df.write("\n")
             df.write("RUN id %s 2>/dev/null || useradd -u %d -U %s" %
                      (uname, uid, uname))
Signature · 1 line
2.39.0
Reviewed-by on v1 4/10
#9 lore raw

Re: [PATCH 04/10] tests/docker: fix a win32 error due to portability

Quoted 5 lines · On 3/1/23 12:08, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

docker.py is run during configure, and produces an error: No module named 'pwd'.

Quoted 7 lines

Use a more portable and recommended alternative to lookup the user "login name".

---
  tests/docker/docker.py | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
#10 lore raw

PATCH 5/10 tests/readconfig: spice doesn't support unix socket on windows yet

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

---
 tests/qtest/readconfig-test.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tests/qtest/readconfig-test.c +4 -2
index c7a9b0c7dd..9ef870643d 100644
@@ -109,8 +109,10 @@ static void test_spice(void)
     QTestState *qts;
     const char *cfgdata =
         "[spice]\n"
-        "disable-ticketing = \"on\"\n"
-        "unix = \"on\"\n";
+#ifndef WIN32
+        "unix = \"on\"\n"
+#endif
+        "disable-ticketing = \"on\"\n";
 
     qts = qtest_init_with_config(cfgdata);
     /* Test valid command */
Signature · 1 line
2.39.0
Reviewed-by on v1 5/10
#11 lore raw

Re: [PATCH 05/10] tests/readconfig: spice doesn't support unix socket on windows yet

Quoted 14 lines · On 03/01/2023 12.08, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

---
  tests/qtest/readconfig-test.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
tests/qtest/readconfig-test.c +0 -0
index c7a9b0c7dd..9ef870643d 100644
@@ -109,8 +109,10 @@ static void test_spice(void)
      QTestState *qts;
      const char *cfgdata =

tests/qtest/readconfig-test.c static void test_spice(void) · line 118

Quoted 10 lines
tests/qtest/readconfig-test.c +4 -2
          "[spice]\n"
-        "disable-ticketing = \"on\"\n"
-        "unix = \"on\"\n";
+#ifndef WIN32
+        "unix = \"on\"\n"
+#endif
+        "disable-ticketing = \"on\"\n";
 
      qts = qtest_init_with_config(cfgdata);
      /* Test valid command */
#12 lore raw

PATCH 6/10 osdep: implement qemu_socketpair() for win32

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Manually implement a socketpair() function, using UNIX sockets and simple peer credential checking.

QEMU doesn't make much use of socketpair, beside vhost-user which is not available for win32 at this point. However, I intend to use it for writing some new portable tests.

---
 include/qemu/sockets.h |   2 -
 util/oslib-win32.c     | 110 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+), 2 deletions(-)
include/qemu/sockets.h +0 -2
index 2b0698a7c9..d935fd80da 100644
@@ -15,7 +15,6 @@ int inet_aton(const char *cp, struct in_addr *ia);
 bool fd_is_socket(int fd);
 int qemu_socket(int domain, int type, int protocol);
 
-#ifndef WIN32
 /**
  * qemu_socketpair:
  * @domain: specifies a communication domain, such as PF_UNIX
@@ -30,7 +29,6 @@ int qemu_socket(int domain, int type, int protocol);
  * Return 0 on success.
  */
 int qemu_socketpair(int domain, int type, int protocol, int sv[2]);
-#endif
 
 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
 /*
util/oslib-win32.c +110 -0

116 lines not on this page · raw message

Signature · 1 line
2.39.0
#13 lore raw

PATCH 7/10 qmp: 'add_client' actually expects sockets

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Whether it is SPICE, VNC, D-Bus, or the socket chardev, they all actually expect a socket kind or will fail in different ways at runtime.

Throw an error early if the given 'add_client' fd is not a socket, and close it to avoid leaks.

This allows to replace the close() call with a more correct & portable closesocket() version.

(this will allow importing sockets on Windows with a specialized command in the following patch, while keeping the remaining monitor associated sockets/add_client code & usage untouched)

---
 monitor/qmp-cmds.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
monitor/qmp-cmds.c +13 -6
index 2932b3f3a5..c491eb262b 100644
@@ -20,6 +20,7 @@
 #include "sysemu/sysemu.h"
 #include "qemu/config-file.h"
 #include "qemu/uuid.h"
+#include "qemu/sockets.h"
 #include "chardev/char.h"
 #include "ui/qemu-spice.h"
 #include "ui/console.h"
@@ -248,16 +249,22 @@ void qmp_add_client(const char *protocol, const char *fdname,
         return;
     }
 
+    if (!fd_is_socket(fd)) {
+        error_setg(errp, "add_client expects a socket");
+        close(fd);
+        return;
+    }
+
     if (strcmp(protocol, "spice") == 0) {
         if (!qemu_using_spice(errp)) {
-            close(fd);
+            closesocket(fd);
             return;
         }
         skipauth = has_skipauth ? skipauth : false;
         tls = has_tls ? tls : false;
         if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
             error_setg(errp, "spice failed to add client");
-            close(fd);
+            closesocket(fd);
         }
         return;
 #ifdef CONFIG_VNC
@@ -269,11 +276,11 @@ void qmp_add_client(const char *protocol, const char *fdname,
 #ifdef CONFIG_DBUS_DISPLAY
     } else if (strcmp(protocol, "@dbus-display") == 0) {
         if (!qemu_using_dbus_display(errp)) {
-            close(fd);
+            closesocket(fd);
             return;
         }
         if (!qemu_dbus_display.add_client(fd, errp)) {
-            close(fd);
+            closesocket(fd);
             return;
         }
         return;
@@ -281,14 +288,14 @@ void qmp_add_client(const char *protocol, const char *fdname,
     } else if ((s = qemu_chr_find(protocol)) != NULL) {
         if (qemu_chr_add_client(s, fd) < 0) {
             error_setg(errp, "failed to add client");
-            close(fd);
+            closesocket(fd);
             return;
         }
         return;
     }
 
     error_setg(errp, "protocol '%s' is invalid", protocol);
-    close(fd);
+    closesocket(fd);
 }
 
 
Signature · 1 line
2.39.0
Reviewed-by on v1 7/10
#14 lore raw

Re: [PATCH 07/10] qmp: 'add_client' actually expects sockets

Quoted 11 lines · On 3/1/23 12:08, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Whether it is SPICE, VNC, D-Bus, or the socket chardev, they all actually expect a socket kind or will fail in different ways at runtime.

Throw an error early if the given 'add_client' fd is not a socket, and close it to avoid leaks.

This allows to replace the close() call with a more correct & portable closesocket() version.

Quoted 8 lines

(this will allow importing sockets on Windows with a specialized command in the following patch, while keeping the remaining monitor associated sockets/add_client code & usage untouched)

---
  monitor/qmp-cmds.c | 19 +++++++++++++------
  1 file changed, 13 insertions(+), 6 deletions(-)
#15 lore raw

PATCH 8/10 qmp: add 'get-win32-socket'

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

A process with enough capabilities can duplicate a socket to QEMU. Add a QMP command to import it and add it to the monitor fd list, so it can be later used by other commands.

Note that we actually store the SOCKET in the FD list, appropriate care must now be taken to use the correct socket functions (similar approach is taken by our io/ code and in glib, this is internal and shouldn't affect the QEMU/QMP users)

---
 qapi/misc.json | 32 ++++++++++++++++++++++
 monitor/misc.c | 74 ++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 91 insertions(+), 15 deletions(-)
qapi/misc.json +32 -0
index 27ef5a2b20..a19dd78fab 100644
@@ -272,6 +272,38 @@
 ##
 { 'command': 'getfd', 'data': {'fdname': 'str'} }
 
+##
+# @get-win32-socket:
+#
+# Add a socket that was duplicated to QEMU process with WSADuplicateSocketW()
+# via WSASocket() & WSAPROTOCOL_INFOW structure and assign it a name. A SOCKET
+# is considered as a kind of "file descriptor" by QMP clients, for historical
+# reasons and simplicity, although QEMU takes care to use socket functions
+# appropriately.
+#
+# @info: the WSAPROTOCOL_INFOW structure (encoded in base64)
+#
+# @fdname: file descriptor name
+#
+# Returns: Nothing on success
+#
+# Since: 8.0
+#
+# Notes: If @fdname already exists, the file descriptor assigned to
+#        it will be closed and replaced by the received file
+#        descriptor.
+#
+#        The 'closefd' command can be used to explicitly close the
+#        file descriptor when it is no longer needed.
+#
+# Example:
+#
+# -> { "execute": "get-win32-socket", "arguments": { "info": "abcd123..", fdname": "skclient" } }
+# <- { "return": {} }
+#
+##
+{ 'command': 'get-win32-socket', 'data': {'info': 'str', 'fdname': 'str'}, 'if': 'CONFIG_WIN32' }
+
 ##
 # @closefd:
 #
monitor/misc.c +59 -15

119 lines not on this page · raw message

Signature · 1 line
2.39.0
#16 lore raw

Re: [PATCH 08/10] qmp: add 'get-win32-socket'

Quoted 46 lines · On Tue, Jan 03, 2023 at 03:08:12PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

A process with enough capabilities can duplicate a socket to QEMU. Add a QMP command to import it and add it to the monitor fd list, so it can be later used by other commands.

Note that we actually store the SOCKET in the FD list, appropriate care must now be taken to use the correct socket functions (similar approach is taken by our io/ code and in glib, this is internal and shouldn't affect the QEMU/QMP users)

---
 qapi/misc.json | 32 ++++++++++++++++++++++
 monitor/misc.c | 74 ++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 91 insertions(+), 15 deletions(-)
qapi/misc.json +21 -0
index 27ef5a2b20..a19dd78fab 100644
@@ -272,6 +272,38 @@
 ##
 { 'command': 'getfd', 'data': {'fdname': 'str'} }
 
+##
+# @get-win32-socket:
+#
+# Add a socket that was duplicated to QEMU process with WSADuplicateSocketW()
+# via WSASocket() & WSAPROTOCOL_INFOW structure and assign it a name. A SOCKET
+# is considered as a kind of "file descriptor" by QMP clients, for historical
+# reasons and simplicity, although QEMU takes care to use socket functions
+# appropriately.
+#
+# @info: the WSAPROTOCOL_INFOW structure (encoded in base64)
+#
+# @fdname: file descriptor name
+#
+# Returns: Nothing on success
+#
+# Since: 8.0
+#
+# Notes: If @fdname already exists, the file descriptor assigned to
+#        it will be closed and replaced by the received file
+#        descriptor.
+#

qapi/misc.json line 305

Quoted 10 lines
qapi/misc.json +10 -0
+#        The 'closefd' command can be used to explicitly close the
+#        file descriptor when it is no longer needed.
+#
+# Example:
+#
+# -> { "execute": "get-win32-socket", "arguments": { "info": "abcd123..", fdname": "skclient" } }
+# <- { "return": {} }
+#
+##
+{ 'command': 'get-win32-socket', 'data': {'info': 'str', 'fdname': 'str'}, 'if': 'CONFIG_WIN32' }

IIUC, this is needed because 'getfd' doesn't work on Windows ?

Can't we just implement getfd on Windows, using this WSAPROTOCOL_INFOW structure passing, or are you anticipating that future Windows make get normal 'getfd' support ?

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#17 lore raw

Re: [PATCH 08/10] qmp: add 'get-win32-socket'

Hi

Quoted 65 lines · Daniel P. Berrangé
Quoted 22 lines · On Tue, Jan 03, 2023 at 03:08:12PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

A process with enough capabilities can duplicate a socket to QEMU. Add a QMP command to import it and add it to the monitor fd list, so it can be later used by other commands.

Note that we actually store the SOCKET in the FD list, appropriate care must now be taken to use the correct socket functions (similar approach is taken by our io/ code and in glib, this is internal and shouldn't affect the QEMU/QMP users)

---
 qapi/misc.json | 32 ++++++++++++++++++++++
 monitor/misc.c | 74 ++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 91 insertions(+), 15 deletions(-)
qapi/misc.json +0 -0
index 27ef5a2b20..a19dd78fab 100644

qapi/misc.json line 278

Quoted 7 lines
qapi/misc.json +4 -0
@@ -272,6 +272,38 @@
 ##
 { 'command': 'getfd', 'data': {'fdname': 'str'} }
 
+##
+# @get-win32-socket:
+#
+# Add a socket that was duplicated to QEMU process with

WSADuplicateSocketW()

qapi/misc.json line 279

Quoted 1 line
qapi/misc.json +1 -0
+# via WSASocket() & WSAPROTOCOL_INFOW structure and assign it a name. A

SOCKET

qapi/misc.json line 280

Quoted 1 line
qapi/misc.json +1 -0
+# is considered as a kind of "file descriptor" by QMP clients, for

historical

qapi/misc.json line 281

Quoted 1 line
qapi/misc.json +1 -0
+# reasons and simplicity, although QEMU takes care to use socket

functions

Quoted 10 lines
qapi/misc.json +10 -0
+# appropriately.
+#
+# @info: the WSAPROTOCOL_INFOW structure (encoded in base64)
+#
+# @fdname: file descriptor name
+#
+# Returns: Nothing on success
+#
+# Since: 8.0
+#

qapi/misc.json

Quoted 10 lines
qapi/misc.json +10 -0
+# Notes: If @fdname already exists, the file descriptor assigned to
+#        it will be closed and replaced by the received file
+#        descriptor.
+#
+#        The 'closefd' command can be used to explicitly close the
+#        file descriptor when it is no longer needed.
+#
+# Example:
+#
+# -> { "execute": "get-win32-socket", "arguments": { "info":

"abcd123..", fdname": "skclient" } }

qapi/misc.json

Quoted 4 lines
qapi/misc.json +4 -0
+# <- { "return": {} }
+#
+##
+{ 'command': 'get-win32-socket', 'data': {'info': 'str', 'fdname':

'str'}, 'if': 'CONFIG_WIN32' }

Quoted 6 lines

IIUC, this is needed because 'getfd' doesn't work on Windows ?

Can't we just implement getfd on Windows, using this WSAPROTOCOL_INFOW structure passing, or are you anticipating that future Windows make get normal 'getfd' support ?

Yes, I guess we could reuse 'getfd' somehow, and add an optional argument 'wsaprotocol-info' to import sockets. Other usages would fail atm, since win32 sockets don't have SCM rights. I have some hope that one day Windows will support it though, at least for some type of fds (sockets to start with).

Whether this is preferable to a new command explicitely designed for win32 socket, I am not sure.

Quoted 12 lines

With regards,
Daniel
--
|: https://berrange.com -o-
https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o-
https://fstop138.berrange.com :|
|: https://entangle-photo.org -o-
https://www.instagram.com/dberrange :|

#18 lore raw

PATCH 9/10 libqtest: make qtest_qmp_add_client work on win32

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Use the "get-win32-socket" function to pass an opened socket to QEMU, instead of using "getfd", which relies on socket ancillary FD message passing.

---
 tests/qtest/libqtest.h |  5 ++---
 tests/qtest/libqtest.c | 16 ++++++++++++++--
 2 files changed, 16 insertions(+), 5 deletions(-)
tests/qtest/libqtest.h +2 -3
index fcf1c3c3b3..8d7d450963 100644
@@ -758,17 +758,16 @@ void qtest_qmp_device_add_qdict(QTestState *qts, const char *drv,
 void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
                           const char *fmt, ...) G_GNUC_PRINTF(4, 5);
 
-#ifndef _WIN32
 /**
  * qtest_qmp_add_client:
  * @qts: QTestState instance to operate on
  * @protocol: the protocol to add to
  * @fd: the client file-descriptor
  *
- * Call QMP ``getfd`` followed by ``add_client`` with the given @fd.
+ * Call QMP ``getfd`` (on Windows ``get-win32-socket``) followed by
+ * ``add_client`` with the given @fd.
  */
 void qtest_qmp_add_client(QTestState *qts, const char *protocol, int fd);
-#endif /* _WIN32 */
 
 /**
  * qtest_qmp_device_del_send:
tests/qtest/libqtest.c +14 -2
index 2fbc3b88f3..63ec328e27 100644
@@ -1440,13 +1440,26 @@ void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
     qobject_unref(args);
 }
 
-#ifndef _WIN32
 void qtest_qmp_add_client(QTestState *qts, const char *protocol, int fd)
 {
     QDict *resp;
 
+#ifdef WIN32
+    WSAPROTOCOL_INFOW info;
+    g_autofree char *info64  = NULL;
+
+    assert(fd_is_socket(fd));
+    if (WSADuplicateSocketW(fd, GetProcessId((HANDLE)qts->qemu_pid), &info) == SOCKET_ERROR) {
+        g_autofree char *emsg = g_win32_error_message(WSAGetLastError());
+        g_error("WSADuplicateSocketW failed: %s", emsg);
+    }
+    info64 = g_base64_encode((guchar *)&info, sizeof(info));
+    resp = qtest_qmp(qts, "{'execute': 'get-win32-socket',"
+                     "'arguments': {'fdname': 'fdname', 'info': %s}}", info64);
+#else
     resp = qtest_qmp_fds(qts, &fd, 1, "{'execute': 'getfd',"
                          "'arguments': {'fdname': 'fdname'}}");
+#endif
     g_assert(resp);
     g_assert(!qdict_haskey(resp, "event")); /* We don't expect any events */
     g_assert(!qdict_haskey(resp, "error"));
@@ -1460,7 +1473,6 @@ void qtest_qmp_add_client(QTestState *qts, const char *protocol, int fd)
     g_assert(!qdict_haskey(resp, "error"));
     qobject_unref(resp);
 }
-#endif
 
 /*
  * Generic hot-unplugging test via the device_del QMP command.
Signature · 1 line
2.39.0
Acked-by on v1 9/10
#19 lore raw

Re: [PATCH 09/10] libqtest: make qtest_qmp_add_client work on win32

Quoted 11 lines · On 03/01/2023 12.08, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Use the "get-win32-socket" function to pass an opened socket to QEMU, instead of using "getfd", which relies on socket ancillary FD message passing.

---
  tests/qtest/libqtest.h |  5 ++---
  tests/qtest/libqtest.c | 16 ++++++++++++++--
  2 files changed, 16 insertions(+), 5 deletions(-)
#20 lore raw

PATCH 10/10 qtest: enable vnc-display test on win32

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Now that qtest_qmp_add_client() works on win32, we can enable the VNC test.

---
 tests/qtest/vnc-display-test.c | 5 -----
 1 file changed, 5 deletions(-)
tests/qtest/vnc-display-test.c +0 -5
index e2a9d682bb..2997edc6ec 100644
@@ -34,10 +34,6 @@ static void on_vnc_auth_failure(VncConnection *self,
 static bool
 test_setup(Test *test)
 {
-#ifdef WIN32
-    g_test_skip("Not supported on Windows yet");
-    return false;
-#else
     int pair[2];
 
     test->qts = qtest_init("-vnc none -name vnc-test");
@@ -56,7 +52,6 @@ test_setup(Test *test)
 
     test->loop = g_main_loop_new(NULL, FALSE);
     return true;
-#endif
 }
 
 static void
Signature · 1 line
2.39.0
Acked-by on v1 10/10
#21 lore raw

Re: [PATCH 10/10] qtest: enable vnc-display test on win32

Quoted 26 lines · On 03/01/2023 12.08, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Now that qtest_qmp_add_client() works on win32, we can enable the VNC test.

---
  tests/qtest/vnc-display-test.c | 5 -----
  1 file changed, 5 deletions(-)
tests/qtest/vnc-display-test.c +0 -4
index e2a9d682bb..2997edc6ec 100644
@@ -34,10 +34,6 @@ static void on_vnc_auth_failure(VncConnection *self,
  static bool
  test_setup(Test *test)
  {
-#ifdef WIN32
-    g_test_skip("Not supported on Windows yet");
-    return false;
-#else
      int pair[2];
 
      test->qts = qtest_init("-vnc none -name vnc-test");

tests/qtest/vnc-display-test.c test_setup(Test *test) · line 57

Quoted 7 lines
tests/qtest/vnc-display-test.c +0 -1
@@ -56,7 +52,6 @@ test_setup(Test *test)
 
      test->loop = g_main_loop_new(NULL, FALSE);
      return true;
-#endif
  }
 
  static void
#22 lore raw
In reply to [email protected]

Cc'ing more Windows specialists.

Quoted 23 lines · On 3/1/23 12:08, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Hi,

The following series first fixes a few tests on win32. The second part focuses on 'add_client' support, by limiting its scope to sockets and adding win32 support. Finally, it enables vnc-display test on win32, to exercise the new code paths and demonstrate its usage.

'get-win32-socket' can be used to write more robusts code & tests using sockets on Windows, and will be used by a follow up series testing dbus display support.

Marc-André Lureau (10):
ccid-card-emulated: fix cast warning/error
tests: fix path separator, use g_build_filename()
tests: fix test-io-channel-command on win32 tests/docker: fix a win32 error due to portability tests/readconfig: spice doesn't support unix socket on windows yet
osdep: implement qemu_socketpair() for win32
qmp: 'add_client' actually expects sockets
qmp: add 'get-win32-socket'
libqtest: make qtest_qmp_add_client work on win32
qtest: enable vnc-display test on win32