Skip to contentSkip to conversation

io/command: implement portable spawn

PATCH Superseded [email protected] 3 participants 13 messages Oct 6, 2022 Arm/Amlogic Meson SoC support

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover io/command: implement portable spawn

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

Hi,

This enables support of command spawning in the io/ subsystem on Windows.

v3:
- actually make it work :) (it seems I didn't test correctly earlier attempts)
- add a patch for readv_writev() and partial IOs
- add a patch to avoid some potential race in echo test
- drop usage of PIPE_NOWAIT which was broken

Marc-André Lureau (5):
osdep: make readv_writev() work with partial read/write tests/channel-helper: set blocking in main thread io/command: use glib GSpawn, instead of open-coding fork/exec io/command: implement support for win32
tests/unit: make test-io-channel-command work on win32

 include/io/channel-command.h         |   5 +-
 io/channel-command.c                 | 185 +++++++++++----------------
 tests/unit/io-channel-helpers.c      |   9 +-
 tests/unit/test-io-channel-command.c |  32 ++---
 util/osdep.c                         |  10 +-
 tests/unit/meson.build               |   2 +-
 6 files changed, 107 insertions(+), 136 deletions(-)
Signature · 1 line
2.37.3
#1 lore raw

PATCH v3 1/5 osdep: make readv_writev() work with partial read/write

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

With a pipe or other reasons, read/write may return less than the requested bytes. This happens with the test-io-channel-command test on Windows. glib spawn code uses a binary pipe of 4096 bytes, and the first read returns that much (although more are requested), for some unclear reason...

---
 util/osdep.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
util/osdep.c +8 -2
index 60fcbbaebe..b85715a743 100644
@@ -544,12 +544,17 @@ readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
 {
     unsigned i = 0;
     ssize_t ret = 0;
+    ssize_t off = 0;
     while (i < iov_cnt) {
         ssize_t r = do_write
-            ? write(fd, iov[i].iov_base, iov[i].iov_len)
-            : read(fd, iov[i].iov_base, iov[i].iov_len);
+            ? write(fd, iov[i].iov_base + off, iov[i].iov_len - off)
+            : read(fd, iov[i].iov_base + off, iov[i].iov_len - off);
         if (r > 0) {
             ret += r;
+            off += r;
+            if (off < iov[i].iov_len) {
+                continue;
+            }
         } else if (!r) {
             break;
         } else if (errno == EINTR) {
@@ -562,6 +567,7 @@ readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
             }
             break;
         }
+        off = 0;
         i++;
     }
     return ret;
Signature · 1 line
2.37.3
Reviewed-by on v3 1/5
#2 lore raw

Re: [PATCH v3 1/5] osdep: make readv_writev() work with partial read/write

Quoted 7 lines · On Thu, Oct 06, 2022 at 12:12:18PM +0400, [email protected] wrote:

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

With a pipe or other reasons, read/write may return less than the requested bytes. This happens with the test-io-channel-command test on Windows. glib spawn code uses a binary pipe of 4096 bytes, and the first read returns that much (although more are requested), for some unclear reason...

Wow, lucky this function isn't too widely used historically!

Quoted 5 lines
---
 util/osdep.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

util/osdep.c

Quoted 5 lines
util/osdep.c +0 -0
index 60fcbbaebe..b85715a743 100644

There's a comment just above here that says

helper function for iov_send_recv

which is not true, as iov_send_recv just calls send/recv instead, again in a loop with the exact same logic flaw. Care to fix the comment and also fix iov_send_recv in the same way ?

Quoted 31 lines
util/osdep.c +8 -2
@@ -544,12 +544,17 @@ readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
 {
     unsigned i = 0;
     ssize_t ret = 0;
+    ssize_t off = 0;
     while (i < iov_cnt) {
         ssize_t r = do_write
-            ? write(fd, iov[i].iov_base, iov[i].iov_len)
-            : read(fd, iov[i].iov_base, iov[i].iov_len);
+            ? write(fd, iov[i].iov_base + off, iov[i].iov_len - off)
+            : read(fd, iov[i].iov_base + off, iov[i].iov_len - off);
         if (r > 0) {
             ret += r;
+            off += r;
+            if (off < iov[i].iov_len) {
+                continue;
+            }
         } else if (!r) {
             break;
         } else if (errno == EINTR) {
@@ -562,6 +567,7 @@ readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
             }
             break;
         }
+        off = 0;
         i++;
     }
     return ret;
Signature · 1 line
2.37.3

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 :|
#3 lore raw

PATCH v3 2/5 tests/channel-helper: set blocking in main thread

In reply to [email protected]

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

The /io/channel/command/echo tests run the reader side and the writer side with the same underlying command channel. Setting the blocking mode of the fd/handles while the other end is already reading/writing may create issues (deadlock in win32 when earlier attempt of this series were using SetNamedPipeHandleState). Let's just do it before spawning the threads to avoid further concurrency issues.

---
 tests/unit/io-channel-helpers.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
tests/unit/io-channel-helpers.c +3 -6
index ff156ed3c4..c0799c21c2 100644
@@ -25,7 +25,6 @@
 struct QIOChannelTest {
     QIOChannel *src;
     QIOChannel *dst;
-    bool blocking;
     size_t len;
     size_t niov;
     char *input;
@@ -42,8 +41,6 @@ static gpointer test_io_thread_writer(gpointer opaque)
 {
     QIOChannelTest *data = opaque;
 
-    qio_channel_set_blocking(data->src, data->blocking, NULL);
-
     qio_channel_writev_all(data->src,
                            data->inputv,
                            data->niov,
@@ -58,8 +55,6 @@ static gpointer test_io_thread_reader(gpointer opaque)
 {
     QIOChannelTest *data = opaque;
 
-    qio_channel_set_blocking(data->dst, data->blocking, NULL);
-
     qio_channel_readv_all(data->dst,
                           data->outputv,
                           data->niov,
@@ -113,7 +108,9 @@ void qio_channel_test_run_threads(QIOChannelTest *test,
 
     test->src = src;
     test->dst = dst;
-    test->blocking = blocking;
+
+    qio_channel_set_blocking(test->dst, blocking, NULL);
+    qio_channel_set_blocking(test->src, blocking, NULL);
 
     reader = g_thread_new("reader",
                           test_io_thread_reader,
Signature · 1 line
2.37.3
Reviewed-by on v3 2/5
#4 lore raw

Re: [PATCH v3 2/5] tests/channel-helper: set blocking in main thread

Quoted 8 lines · On Thu, Oct 06, 2022 at 12:12:19PM +0400, [email protected] wrote:

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

The /io/channel/command/echo tests run the reader side and the writer side with the same underlying command channel. Setting the blocking mode of the fd/handles while the other end is already reading/writing may create issues (deadlock in win32 when earlier attempt of this series were using SetNamedPipeHandleState). Let's just do it before spawning the threads to avoid further concurrency issues.

Curious, makes me wonder why I did this in the threads in the first place...

Quoted 5 lines
---
 tests/unit/io-channel-helpers.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
Quoted 45 lines
tests/unit/io-channel-helpers.c +3 -6
index ff156ed3c4..c0799c21c2 100644
@@ -25,7 +25,6 @@
 struct QIOChannelTest {
     QIOChannel *src;
     QIOChannel *dst;
-    bool blocking;
     size_t len;
     size_t niov;
     char *input;
@@ -42,8 +41,6 @@ static gpointer test_io_thread_writer(gpointer opaque)
 {
     QIOChannelTest *data = opaque;
 
-    qio_channel_set_blocking(data->src, data->blocking, NULL);
-
     qio_channel_writev_all(data->src,
                            data->inputv,
                            data->niov,
@@ -58,8 +55,6 @@ static gpointer test_io_thread_reader(gpointer opaque)
 {
     QIOChannelTest *data = opaque;
 
-    qio_channel_set_blocking(data->dst, data->blocking, NULL);
-
     qio_channel_readv_all(data->dst,
                           data->outputv,
                           data->niov,
@@ -113,7 +108,9 @@ void qio_channel_test_run_threads(QIOChannelTest *test,
 
     test->src = src;
     test->dst = dst;
-    test->blocking = blocking;
+
+    qio_channel_set_blocking(test->dst, blocking, NULL);
+    qio_channel_set_blocking(test->src, blocking, NULL);
 
     reader = g_thread_new("reader",
                           test_io_thread_reader,
Signature · 1 line
2.37.3

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 :|
#5 lore raw

PATCH v3 3/5 io/command: use glib GSpawn, instead of open-coding fork/exec

In reply to [email protected]

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

Simplify qio_channel_command_new_spawn() with GSpawn API. This will allow to build for WIN32 in the following patches.

As pointed out by Daniel Berrangé: there is a change in semantics here too. The current code only touches stdin/stdout/stderr. Any other FDs which do NOT have O_CLOEXEC set will be inherited. With the new code, all FDs except stdin/out/err will be explicitly closed, because we don't set the flag G_SPAWN_LEAVE_DESCRIPTORS_OPEN. The only place we use QIOChannelCommand today is the migration exec: protocol, and that is only declared to use stdin/stdout.

---
 include/io/channel-command.h |   2 +-
 io/channel-command.c         | 105 ++++++-----------------------------
 2 files changed, 19 insertions(+), 88 deletions(-)
include/io/channel-command.h +1 -1
index 305ac1d280..8dc58273c0 100644
@@ -41,7 +41,7 @@ struct QIOChannelCommand {
     QIOChannel parent;
     int writefd;
     int readfd;
-    pid_t pid;
+    GPid pid;
 };
 
 
io/channel-command.c +18 -87

139 lines not on this page · raw message

Signature · 1 line
2.37.3
#6 lore raw

PATCH v3 4/5 io/command: implement support for win32

In reply to [email protected]

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

The initial implementation was changing the pipe state created by GLib to PIPE_NOWAIT, but it turns out it doesn't work (read/write returns an error). Since reading may return less than the requested amount, it seems to be non-blocking already. However, the IO operation may block until the FD is ready, I can't find good sources of information, to be safe we can just poll for readiness before.

Alternatively, we could setup the FDs ourself, and use UNIX sockets on Windows, which can be used in blocking/non-blocking mode. I haven't tried it, as I am not sure it is necessary.

---
 include/io/channel-command.h |  3 ++
 io/channel-command.c         | 80 ++++++++++++++++++++++++++----------
 2 files changed, 62 insertions(+), 21 deletions(-)
include/io/channel-command.h +3 -0
index 8dc58273c0..98934e6d9e 100644
@@ -42,6 +42,9 @@ struct QIOChannelCommand {
     int writefd;
     int readfd;
     GPid pid;
+#ifdef WIN32
+    bool blocking;
+#endif
 };
 
 
io/channel-command.c +59 -21

142 lines not on this page · raw message

Signature · 1 line
2.37.3
Reviewed-by on v3 4/5
#7 lore raw

Re: [PATCH v3 4/5] io/command: implement support for win32

Quoted 8 lines · On Thu, Oct 06, 2022 at 12:12:21PM +0400, [email protected] wrote:

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

The initial implementation was changing the pipe state created by GLib to PIPE_NOWAIT, but it turns out it doesn't work (read/write returns an error). Since reading may return less than the requested amount, it seems to be non-blocking already. However, the IO operation may block until the FD is ready, I can't find good sources of information, to be safe we can just poll for readiness before.

The downside is that we double the number of syscalls for each blocking I/O. Doesn't matter if we do actually block, but if there was data already pending, this is extra overhead.

But then this code never worked on Windows, so users will likely not have performance expectations at this time.

Quoted 10 lines

Alternatively, we could setup the FDs ourself, and use UNIX sockets on Windows, which can be used in blocking/non-blocking mode. I haven't tried it, as I am not sure it is necessary.

---
 include/io/channel-command.h |  3 ++
 io/channel-command.c         | 80 ++++++++++++++++++++++++++----------
 2 files changed, 62 insertions(+), 21 deletions(-)

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 :|
#8 lore raw

PATCH v3 5/5 tests/unit: make test-io-channel-command work on win32

In reply to [email protected]

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

This has been tested under msys2 & windows 11. I haven't tried to make it work with other environments yet, but that should be enough to validate the channel-command implementation anyway.

---
 tests/unit/test-io-channel-command.c | 32 ++++++++++++----------------
 tests/unit/meson.build               |  2 +-
 2 files changed, 15 insertions(+), 19 deletions(-)
tests/unit/test-io-channel-command.c +14 -18
index aa09c559cd..be98c3452a 100644
@@ -24,29 +24,27 @@
 #include "qapi/error.h"
 #include "qemu/module.h"
 
-#ifndef WIN32
+#define TEST_PATH "test-io-channel-command.fifo"
+
+#define SOCAT_SRC "PIPE:" TEST_PATH ",wronly"
+#define SOCAT_DST "PIPE:" TEST_PATH ",rdonly"
+
 static void test_io_channel_command_fifo(bool async)
 {
-#define TEST_FIFO "tests/test-io-channel-command.fifo"
     QIOChannel *src, *dst;
     QIOChannelTest *test;
-    const char *srcfifo = "PIPE:" TEST_FIFO ",wronly";
-    const char *dstfifo = "PIPE:" TEST_FIFO ",rdonly";
     const char *srcargv[] = {
-        "/bin/socat", "-", srcfifo, NULL,
+        g_getenv("SOCAT"), "-", SOCAT_SRC, NULL,
     };
     const char *dstargv[] = {
-        "/bin/socat", dstfifo, "-", NULL,
+        g_getenv("SOCAT"), SOCAT_DST, "-", NULL,
     };
 
-    unlink(TEST_FIFO);
-    if (access("/bin/socat", X_OK) < 0) {
+    unlink(TEST_PATH);
+    if (!g_file_test(g_getenv("SOCAT"), G_FILE_TEST_IS_EXECUTABLE)) {
         g_test_skip("socat is missing");
         return;
     }
-    if (mkfifo(TEST_FIFO, 0600) < 0) {
-        abort();
-    }
     src = QIO_CHANNEL(qio_channel_command_new_spawn(srcargv,
                                                     O_WRONLY,
                                                     &error_abort));
@@ -61,7 +59,7 @@ static void test_io_channel_command_fifo(bool async)
     object_unref(OBJECT(src));
     object_unref(OBJECT(dst));
 
-    unlink(TEST_FIFO);
+    unlink(TEST_PATH);
 }
 
 
@@ -81,11 +79,12 @@ static void test_io_channel_command_echo(bool async)
     QIOChannel *ioc;
     QIOChannelTest *test;
     const char *socatargv[] = {
-        "/bin/socat", "-", "-", NULL,
+        g_getenv("SOCAT"), "-", "-", NULL,
     };
 
-    if (access("/bin/socat", X_OK) < 0) {
-        return; /* Pretend success if socat is not present */
+    if (!g_file_test(g_getenv("SOCAT"), G_FILE_TEST_IS_EXECUTABLE)) {
+        g_test_skip("socat is missing");
+        return;
     }
 
     ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
@@ -108,7 +107,6 @@ static void test_io_channel_command_echo_sync(void)
 {
     test_io_channel_command_echo(false);
 }
-#endif
 
 int main(int argc, char **argv)
 {
@@ -116,7 +114,6 @@ int main(int argc, char **argv)
 
     g_test_init(&argc, &argv, NULL);
 
-#ifndef WIN32
     g_test_add_func("/io/channel/command/fifo/sync",
                     test_io_channel_command_fifo_sync);
     g_test_add_func("/io/channel/command/fifo/async",
@@ -125,7 +122,6 @@ int main(int argc, char **argv)
                     test_io_channel_command_echo_sync);
     g_test_add_func("/io/channel/command/echo/async",
                     test_io_channel_command_echo_async);
-#endif
 
     return g_test_run();
 }
tests/unit/meson.build +1 -1
index b497a41378..42e8218ac2 100644
@@ -1,4 +1,3 @@
-
 testblock = declare_dependency(dependencies: [block], sources: 'iothread.c')
 
 tests = {
@@ -164,6 +163,7 @@ endif
 test_env = environment()
 test_env.set('G_TEST_SRCDIR', meson.current_source_dir())
 test_env.set('G_TEST_BUILDDIR', meson.current_build_dir())
+test_env.set('SOCAT', find_program('socat').full_path())
 
 slow_tests = {
   'test-crypto-tlscredsx509': 45,
Signature · 1 line
2.37.3
#9 lore raw

Re: [PATCH v3 5/5] tests/unit: make test-io-channel-command work on win32

Quoted 26 lines · On Thu, Oct 06, 2022 at 12:12:22PM +0400, [email protected] wrote:

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

This has been tested under msys2 & windows 11. I haven't tried to make it work with other environments yet, but that should be enough to validate the channel-command implementation anyway.

---
 tests/unit/test-io-channel-command.c | 32 ++++++++++++----------------
 tests/unit/meson.build               |  2 +-
 2 files changed, 15 insertions(+), 19 deletions(-)
tests/unit/test-io-channel-command.c +5 -1
index aa09c559cd..be98c3452a 100644
@@ -24,29 +24,27 @@
 #include "qapi/error.h"
 #include "qemu/module.h"
 
-#ifndef WIN32
+#define TEST_PATH "test-io-channel-command.fifo"
+
+#define SOCAT_SRC "PIPE:" TEST_PATH ",wronly"
+#define SOCAT_DST "PIPE:" TEST_PATH ",rdonly"
+

tests/unit/test-io-channel-command.c line 37

Quoted 10 lines
tests/unit/test-io-channel-command.c +1 -4
 static void test_io_channel_command_fifo(bool async)
 {
-#define TEST_FIFO "tests/test-io-channel-command.fifo"
     QIOChannel *src, *dst;
     QIOChannelTest *test;
-    const char *srcfifo = "PIPE:" TEST_FIFO ",wronly";
-    const char *dstfifo = "PIPE:" TEST_FIFO ",rdonly";
     const char *srcargv[] = {
-        "/bin/socat", "-", srcfifo, NULL,
+        g_getenv("SOCAT"), "-", SOCAT_SRC, NULL,

Please don't rely on env variables, as it complicates the ability to invoke the test directly, without the meson harness. Either pass the path from meson at compile time in config-host.h, or make this code use an unqualified path, so it honours $PATH at runtime.

Quoted 73 lines
tests/unit/test-io-channel-command.c +8 -13
     };
     const char *dstargv[] = {
-        "/bin/socat", dstfifo, "-", NULL,
+        g_getenv("SOCAT"), SOCAT_DST, "-", NULL,
     };
 
-    unlink(TEST_FIFO);
-    if (access("/bin/socat", X_OK) < 0) {
+    unlink(TEST_PATH);
+    if (!g_file_test(g_getenv("SOCAT"), G_FILE_TEST_IS_EXECUTABLE)) {
         g_test_skip("socat is missing");
         return;
     }
-    if (mkfifo(TEST_FIFO, 0600) < 0) {
-        abort();
-    }
     src = QIO_CHANNEL(qio_channel_command_new_spawn(srcargv,
                                                     O_WRONLY,
                                                     &error_abort));
@@ -61,7 +59,7 @@ static void test_io_channel_command_fifo(bool async)
     object_unref(OBJECT(src));
     object_unref(OBJECT(dst));
 
-    unlink(TEST_FIFO);
+    unlink(TEST_PATH);
 }
 
 
@@ -81,11 +79,12 @@ static void test_io_channel_command_echo(bool async)
     QIOChannel *ioc;
     QIOChannelTest *test;
     const char *socatargv[] = {
-        "/bin/socat", "-", "-", NULL,
+        g_getenv("SOCAT"), "-", "-", NULL,
     };
 
-    if (access("/bin/socat", X_OK) < 0) {
-        return; /* Pretend success if socat is not present */
+    if (!g_file_test(g_getenv("SOCAT"), G_FILE_TEST_IS_EXECUTABLE)) {
+        g_test_skip("socat is missing");
+        return;
     }
 
     ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
@@ -108,7 +107,6 @@ static void test_io_channel_command_echo_sync(void)
 {
     test_io_channel_command_echo(false);
 }
-#endif
 
 int main(int argc, char **argv)
 {
@@ -116,7 +114,6 @@ int main(int argc, char **argv)
 
     g_test_init(&argc, &argv, NULL);
 
-#ifndef WIN32
     g_test_add_func("/io/channel/command/fifo/sync",
                     test_io_channel_command_fifo_sync);
     g_test_add_func("/io/channel/command/fifo/async",
@@ -125,7 +122,6 @@ int main(int argc, char **argv)
                     test_io_channel_command_echo_sync);
     g_test_add_func("/io/channel/command/echo/async",
                     test_io_channel_command_echo_async);
-#endif
 
     return g_test_run();
 }
tests/unit/meson.build +0 -0
index b497a41378..42e8218ac2 100644

tests/unit/meson.build line 3

Quoted 4 lines
tests/unit/meson.build +0 -1
@@ -1,4 +1,3 @@
-
 testblock = declare_dependency(dependencies: [block], sources: 'iothread.c')
 
 tests = {

Spurious line deletion.

Quoted 11 lines
tests/unit/meson.build +1 -0
@@ -164,6 +163,7 @@ endif
 test_env = environment()
 test_env.set('G_TEST_SRCDIR', meson.current_source_dir())
 test_env.set('G_TEST_BUILDDIR', meson.current_build_dir())
+test_env.set('SOCAT', find_program('socat').full_path())
 
 slow_tests = {
   'test-crypto-tlscredsx509': 45,
Signature · 1 line
2.37.3

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 :|
#10 lore raw

Re: [PATCH v3 5/5] tests/unit: make test-io-channel-command work on win32

Hi

Quoted 35 lines · Daniel P. Berrangé
Quoted 33 lines · On Thu, Oct 06, 2022 at 12:12:22PM +0400, [email protected] wrote:

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

This has been tested under msys2 & windows 11. I haven't tried to make it work with other environments yet, but that should be enough to validate the channel-command implementation anyway.

---
 tests/unit/test-io-channel-command.c | 32 ++++++++++++----------------
 tests/unit/meson.build               |  2 +-
 2 files changed, 15 insertions(+), 19 deletions(-)
tests/unit/test-io-channel-command.c +5 -4
index aa09c559cd..be98c3452a 100644
@@ -24,29 +24,27 @@
 #include "qapi/error.h"
 #include "qemu/module.h"
 
-#ifndef WIN32
+#define TEST_PATH "test-io-channel-command.fifo"
+
+#define SOCAT_SRC "PIPE:" TEST_PATH ",wronly"
+#define SOCAT_DST "PIPE:" TEST_PATH ",rdonly"
+
 static void test_io_channel_command_fifo(bool async)
 {
-#define TEST_FIFO "tests/test-io-channel-command.fifo"
     QIOChannel *src, *dst;
     QIOChannelTest *test;
-    const char *srcfifo = "PIPE:" TEST_FIFO ",wronly";
-    const char *dstfifo = "PIPE:" TEST_FIFO ",rdonly";
Quoted 8 lines

tests/unit/test-io-channel-command.c line 37

Quoted 3 lines
tests/unit/test-io-channel-command.c +1 -1
     const char *srcargv[] = {
-        "/bin/socat", "-", srcfifo, NULL,
+        g_getenv("SOCAT"), "-", SOCAT_SRC, NULL,

Please don't rely on env variables, as it complicates the ability to invoke the test directly, without the meson harness. Either pass the path from meson at compile time in config-host.h, or make this code use an unqualified path, so it honours $PATH at runtime.

I tried to pass it through config-host.h, but I dont see a way to escape the \ is the paths.

Unqualified path doesn't work under msys2.

Quoted 101 lines
Quoted 74 lines
tests/unit/test-io-channel-command.c +8 -13
     };
     const char *dstargv[] = {
-        "/bin/socat", dstfifo, "-", NULL,
+        g_getenv("SOCAT"), SOCAT_DST, "-", NULL,
     };
 
-    unlink(TEST_FIFO);
-    if (access("/bin/socat", X_OK) < 0) {
+    unlink(TEST_PATH);
+    if (!g_file_test(g_getenv("SOCAT"), G_FILE_TEST_IS_EXECUTABLE)) {
         g_test_skip("socat is missing");
         return;
     }
-    if (mkfifo(TEST_FIFO, 0600) < 0) {
-        abort();
-    }
     src = QIO_CHANNEL(qio_channel_command_new_spawn(srcargv,
                                                     O_WRONLY,
                                                     &error_abort));
@@ -61,7 +59,7 @@ static void test_io_channel_command_fifo(bool async)
     object_unref(OBJECT(src));
     object_unref(OBJECT(dst));
 
-    unlink(TEST_FIFO);
+    unlink(TEST_PATH);
 }
 
 
@@ -81,11 +79,12 @@ static void test_io_channel_command_echo(bool async)
     QIOChannel *ioc;
     QIOChannelTest *test;
     const char *socatargv[] = {
-        "/bin/socat", "-", "-", NULL,
+        g_getenv("SOCAT"), "-", "-", NULL,
     };
 
-    if (access("/bin/socat", X_OK) < 0) {
-        return; /* Pretend success if socat is not present */
+    if (!g_file_test(g_getenv("SOCAT"), G_FILE_TEST_IS_EXECUTABLE)) {
+        g_test_skip("socat is missing");
+        return;
     }
 
     ioc = QIO_CHANNEL(qio_channel_command_new_spawn(socatargv,
@@ -108,7 +107,6 @@ static void test_io_channel_command_echo_sync(void)
 {
     test_io_channel_command_echo(false);
 }
-#endif
 
 int main(int argc, char **argv)
 {
@@ -116,7 +114,6 @@ int main(int argc, char **argv)
 
     g_test_init(&argc, &argv, NULL);
 
-#ifndef WIN32
     g_test_add_func("/io/channel/command/fifo/sync",
                     test_io_channel_command_fifo_sync);
     g_test_add_func("/io/channel/command/fifo/async",
@@ -125,7 +122,6 @@ int main(int argc, char **argv)
                     test_io_channel_command_echo_sync);
     g_test_add_func("/io/channel/command/echo/async",
                     test_io_channel_command_echo_async);
-#endif
 
     return g_test_run();
 }
tests/unit/meson.build +0 -0
index b497a41378..42e8218ac2 100644

tests/unit/meson.build line 3

Quoted 4 lines
tests/unit/meson.build +0 -1
@@ -1,4 +1,3 @@
-
 testblock = declare_dependency(dependencies: [block], sources: 'iothread.c')
 
 tests = {

Spurious line deletion.

Quoted 11 lines
tests/unit/meson.build +1 -1
@@ -164,6 +163,7 @@ endif
 test_env = environment()
 test_env.set('G_TEST_SRCDIR', meson.current_source_dir())
 test_env.set('G_TEST_BUILDDIR', meson.current_build_dir())
+test_env.set('SOCAT', find_program('socat').full_path())
 
 slow_tests = {
   'test-crypto-tlscredsx509': 45,
--

2.37.3

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 :|
#11 lore raw

Re: [PATCH v3 5/5] tests/unit: make test-io-channel-command work on win32

Quoted 47 lines · Marc-André Lureau

Hi

Quoted 35 lines · Daniel P. Berrangé
Quoted 33 lines · On Thu, Oct 06, 2022 at 12:12:22PM +0400, [email protected] wrote:

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

This has been tested under msys2 & windows 11. I haven't tried to make it work with other environments yet, but that should be enough to validate the channel-command implementation anyway.

---
 tests/unit/test-io-channel-command.c | 32 ++++++++++++----------------
 tests/unit/meson.build               |  2 +-
 2 files changed, 15 insertions(+), 19 deletions(-)
tests/unit/test-io-channel-command.c +5 -4
index aa09c559cd..be98c3452a 100644
@@ -24,29 +24,27 @@
 #include "qapi/error.h"
 #include "qemu/module.h"
 
-#ifndef WIN32
+#define TEST_PATH "test-io-channel-command.fifo"
+
+#define SOCAT_SRC "PIPE:" TEST_PATH ",wronly"
+#define SOCAT_DST "PIPE:" TEST_PATH ",rdonly"
+
 static void test_io_channel_command_fifo(bool async)
 {
-#define TEST_FIFO "tests/test-io-channel-command.fifo"
     QIOChannel *src, *dst;
     QIOChannelTest *test;
-    const char *srcfifo = "PIPE:" TEST_FIFO ",wronly";
-    const char *dstfifo = "PIPE:" TEST_FIFO ",rdonly";
Quoted 8 lines

tests/unit/test-io-channel-command.c line 37

Quoted 3 lines
tests/unit/test-io-channel-command.c +1 -1
     const char *srcargv[] = {
-        "/bin/socat", "-", srcfifo, NULL,
+        g_getenv("SOCAT"), "-", SOCAT_SRC, NULL,

Please don't rely on env variables, as it complicates the ability to invoke the test directly, without the meson harness. Either pass the path from meson at compile time in config-host.h, or make this code use an unqualified path, so it honours $PATH at runtime.

Quoted 2 lines

I tried to pass it through config-host.h, but I dont see a way to escape the \ is the paths.

This must be possible, as we have lots of strings in config-host.h that are paths - eg many CONFIG_QEMU_xxxDIR variables

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 :|
#12 lore raw

Re: [PATCH v3 5/5] tests/unit: make test-io-channel-command work on win32

Hi

Quoted 53 lines · Daniel P. Berrangé
Quoted 3 lines · Marc-André Lureau

Hi

On Thu, Oct 6, 2022 at 12:42 PM Daniel P. Berrangé <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines

On Thu, Oct 06, 2022 at 12:12:22PM +0400, [email protected]

Quoted 3 lines · wrote:
Quoted 3 lines
Quoted 3 lines

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

This has been tested under msys2 & windows 11. I haven't tried to

make

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

it work with other environments yet, but that should be enough to validate the channel-command implementation anyway.

---
 tests/unit/test-io-channel-command.c | 32

++++++++++++----------------

Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
 tests/unit/meson.build               |  2 +-
 2 files changed, 15 insertions(+), 19 deletions(-)
(quoted diff) +0 -0

b/tests/unit/test-io-channel-command.c

Quoted 29 lines
Quoted 28 lines
Quoted 13 lines

index aa09c559cd..be98c3452a 100644

tests/unit/test-io-channel-command.c +5 -1
@@ -24,29 +24,27 @@
 #include "qapi/error.h"
 #include "qemu/module.h"
 
-#ifndef WIN32
+#define TEST_PATH "test-io-channel-command.fifo"
+
+#define SOCAT_SRC "PIPE:" TEST_PATH ",wronly"
+#define SOCAT_DST "PIPE:" TEST_PATH ",rdonly"
+

tests/unit/test-io-channel-command.c line 37

Quoted 10 lines
tests/unit/test-io-channel-command.c +1 -4
 static void test_io_channel_command_fifo(bool async)
 {
-#define TEST_FIFO "tests/test-io-channel-command.fifo"
     QIOChannel *src, *dst;
     QIOChannelTest *test;
-    const char *srcfifo = "PIPE:" TEST_FIFO ",wronly";
-    const char *dstfifo = "PIPE:" TEST_FIFO ",rdonly";
     const char *srcargv[] = {
-        "/bin/socat", "-", srcfifo, NULL,
+        g_getenv("SOCAT"), "-", SOCAT_SRC, NULL,

Please don't rely on env variables, as it complicates the ability to invoke the test directly, without the meson harness. Either pass the path from meson at compile time in config-host.h, or make this code use an unqualified path, so it honours $PATH at runtime.

Quoted 6 lines
Quoted 2 lines

I tried to pass it through config-host.h, but I dont see a way to escape the \ is the paths.

This must be possible, as we have lots of strings in config-host.h that are paths - eg many CONFIG_QEMU_xxxDIR variables

Those paths, generated by meson I believe, use /-dir separators. But find_program() returns \-seperated paths.. Maybe the solution is to replace \ with / ? (hopefully the path is not quoted already...)

There is a proposal for to_quoted():
https://github.com/mesonbuild/meson/issues/10417

I found another solution, using g_find_program_in_path(). See v3.

thanks

Signature · 1 line
Marc-André Lureau