[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v6 02/11] tests/qtest: Use send/recv for socket communication
From: |
Bin Meng |
Subject: |
[PATCH v6 02/11] tests/qtest: Use send/recv for socket communication |
Date: |
Fri, 28 Oct 2022 12:57:27 +0800 |
From: Xuzhou Cheng <xuzhou.cheng@windriver.com>
Socket communication in the libqtest and libqmp codes uses read()
and write() which work on any file descriptor on *nix, and sockets
in *nix are an example of a file descriptor.
However sockets on Windows do not use *nix-style file descriptors,
so read() and write() cannot be used on sockets on Windows.
Switch over to use send() and recv() instead which work on both
Windows and *nix.
Signed-off-by: Xuzhou Cheng <xuzhou.cheng@windriver.com>
Signed-off-by: Bin Meng <bin.meng@windriver.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
Changes in v6:
- include <qemu/sockets.h> in libqtest.c
- move documentation comments of qemu_send_full() from util/osdep.c
to qemu/sockets.h
Changes in v2:
- Introduce qemu_send_full() and use it
include/qemu/sockets.h | 13 +++++++++++++
tests/qtest/libqmp.c | 5 +++--
tests/qtest/libqtest.c | 5 +++--
util/osdep.c | 22 ++++++++++++++++++++++
4 files changed, 41 insertions(+), 4 deletions(-)
diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
index 036745e586..61648f3f3c 100644
--- a/include/qemu/sockets.h
+++ b/include/qemu/sockets.h
@@ -33,6 +33,19 @@ int qemu_socketpair(int domain, int type, int protocol, int
sv[2]);
#endif
int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
+/*
+ * A variant of send(2) which handles partial send.
+ *
+ * Return the number of bytes transferred over the socket.
+ * Set errno if fewer than `count' bytes are sent.
+ *
+ * This function don't work with non-blocking socket's.
+ * Any of the possibilities with non-blocking socket's is bad:
+ * - return a short write (then name is wrong)
+ * - busy wait adding (errno == EAGAIN) to the loop
+ */
+ssize_t qemu_send_full(int s, const void *buf, size_t count)
+ G_GNUC_WARN_UNUSED_RESULT;
int socket_set_cork(int fd, int v);
int socket_set_nodelay(int fd);
void qemu_socket_set_block(int fd);
diff --git a/tests/qtest/libqmp.c b/tests/qtest/libqmp.c
index ade26c15f0..2b08382e5d 100644
--- a/tests/qtest/libqmp.c
+++ b/tests/qtest/libqmp.c
@@ -23,6 +23,7 @@
#endif
#include "qemu/cutils.h"
+#include "qemu/sockets.h"
#include "qapi/error.h"
#include "qapi/qmp/json-parser.h"
#include "qapi/qmp/qjson.h"
@@ -36,7 +37,7 @@ typedef struct {
static void socket_send(int fd, const char *buf, size_t size)
{
- size_t res = qemu_write_full(fd, buf, size);
+ ssize_t res = qemu_send_full(fd, buf, size);
assert(res == size);
}
@@ -69,7 +70,7 @@ QDict *qmp_fd_receive(int fd)
ssize_t len;
char c;
- len = read(fd, &c, 1);
+ len = recv(fd, &c, 1, 0);
if (len == -1 && errno == EINTR) {
continue;
}
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index b23eb3edc3..b01846fd98 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -27,6 +27,7 @@
#include "libqmp.h"
#include "qemu/ctype.h"
#include "qemu/cutils.h"
+#include "qemu/sockets.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qlist.h"
@@ -428,7 +429,7 @@ void qtest_quit(QTestState *s)
static void socket_send(int fd, const char *buf, size_t size)
{
- size_t res = qemu_write_full(fd, buf, size);
+ ssize_t res = qemu_send_full(fd, buf, size);
assert(res == size);
}
@@ -460,7 +461,7 @@ static GString *qtest_client_socket_recv_line(QTestState *s)
ssize_t len;
char buffer[1024];
- len = read(s->fd, buffer, sizeof(buffer));
+ len = recv(s->fd, buffer, sizeof(buffer), 0);
if (len == -1 && errno == EINTR) {
continue;
}
diff --git a/util/osdep.c b/util/osdep.c
index 746d5f7d71..77c1a6c562 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -502,6 +502,28 @@ int qemu_accept(int s, struct sockaddr *addr, socklen_t
*addrlen)
return ret;
}
+ssize_t qemu_send_full(int s, const void *buf, size_t count)
+{
+ ssize_t ret = 0;
+ ssize_t total = 0;
+
+ while (count) {
+ ret = send(s, buf, count, 0);
+ if (ret < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ break;
+ }
+
+ count -= ret;
+ buf += ret;
+ total += ret;
+ }
+
+ return total;
+}
+
void qemu_set_hw_version(const char *version)
{
hw_version = version;
--
2.25.1
- [PATCH v6 00/11] tests/qtest: Enable running qtest on Windows, Bin Meng, 2022/10/28
- [PATCH v6 02/11] tests/qtest: Use send/recv for socket communication,
Bin Meng <=
- [PATCH v6 03/11] tests/qtest: Support libqtest to build and run on Windows, Bin Meng, 2022/10/28
- [PATCH v6 04/11] tests/qtest: device-plug-test: Reverse the usage of double/single quotes, Bin Meng, 2022/10/28
- [PATCH v6 01/11] accel/qtest: Support qtest accelerator for Windows, Bin Meng, 2022/10/28
- [PATCH v6 05/11] tests/qtest: Use EXIT_FAILURE instead of magic number, Bin Meng, 2022/10/28
- [PATCH v6 06/11] tests/qtest: libqtest: Introduce qtest_wait_qemu(), Bin Meng, 2022/10/28
- [PATCH v6 07/11] tests/qtest: migration-test: Make sure QEMU process "to" exited after migration is canceled, Bin Meng, 2022/10/28