qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: thuth@redhat.com, lviver@redhat.com
Subject: [Qemu-devel] [PULL 01/57] chardev-socket: do not blindly reset handlers when switching GMainContext
Date: Thu,  7 Mar 2019 18:29:08 +0100	[thread overview]
Message-ID: <1551979804-6060-2-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1551979804-6060-1-git-send-email-pbonzini@redhat.com>

If the socket is connecting or connected, tcp_chr_update_read_handler will
be called but it should not set the NetListener's callbacks again.
Otherwise, tcp_chr_accept is invoked while the socket is in connected
state and you get an assertion failure.

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 chardev/char-socket.c |   2 +-
 tests/test-char.c     | 102 +++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 102 insertions(+), 2 deletions(-)

diff --git a/chardev/char-socket.c b/chardev/char-socket.c
index 4fcdd8a..6d287ba 100644
--- a/chardev/char-socket.c
+++ b/chardev/char-socket.c
@@ -632,7 +632,7 @@ static void tcp_chr_update_read_handler(Chardev *chr)
 {
     SocketChardev *s = SOCKET_CHARDEV(chr);
 
-    if (s->listener) {
+    if (s->listener && s->state == TCP_CHARDEV_STATE_DISCONNECTED) {
         /*
          * It's possible that chardev context is changed in
          * qemu_chr_be_update_read_handlers().  Reset it for QIO net
diff --git a/tests/test-char.c b/tests/test-char.c
index 63b4d32..de32838 100644
--- a/tests/test-char.c
+++ b/tests/test-char.c
@@ -1003,6 +1003,103 @@ static void char_socket_client_test(gconstpointer opaque)
     g_free(optstr);
 }
 
+static void
+count_closed_event(void *opaque, int event)
+{
+    int *count = opaque;
+    if (event == CHR_EVENT_CLOSED) {
+        (*count)++;
+    }
+}
+
+static void
+char_socket_discard_read(void *opaque, const uint8_t *buf, int size)
+{
+}
+
+static void char_socket_server_two_clients_test(gconstpointer opaque)
+{
+    SocketAddress *incoming_addr = (gpointer) opaque;
+    Chardev *chr;
+    CharBackend be = {0};
+    QObject *qaddr;
+    SocketAddress *addr;
+    Visitor *v;
+    char *optstr;
+    QemuOpts *opts;
+    QIOChannelSocket *ioc1, *ioc2;
+    int closed = 0;
+
+    g_setenv("QTEST_SILENT_ERRORS", "1", 1);
+    /*
+     * We rely on addr containing "nowait", otherwise
+     * qemu_chr_new() will block until a client connects. We
+     * can't spawn our client thread though, because until
+     * qemu_chr_new() returns we don't know what TCP port was
+     * allocated by the OS
+     */
+    optstr = char_socket_addr_to_opt_str(incoming_addr,
+                                         false,
+                                         NULL,
+                                         true);
+    opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
+                                   optstr, true);
+    g_assert_nonnull(opts);
+    chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
+    qemu_opts_del(opts);
+    g_assert_nonnull(chr);
+    g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
+
+    qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
+    g_assert_nonnull(qaddr);
+
+    v = qobject_input_visitor_new(qaddr);
+    visit_type_SocketAddress(v, "addr", &addr, &error_abort);
+    visit_free(v);
+    qobject_unref(qaddr);
+
+    qemu_chr_fe_init(&be, chr, &error_abort);
+
+    qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read,
+                             count_closed_event, NULL,
+                             &closed, NULL, true);
+
+    ioc1 = qio_channel_socket_new();
+    qio_channel_socket_connect_sync(ioc1, addr, &error_abort);
+    qemu_chr_wait_connected(chr, &error_abort);
+
+    /* switch the chardev to another context */
+    GMainContext *ctx = g_main_context_new();
+    qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read,
+                             count_closed_event, NULL,
+                             &closed, ctx, true);
+
+    /* Start a second connection while the first is still connected.
+     * It will be placed in the listen() backlog, and connect() will
+     * succeed immediately.
+     */
+    ioc2 = qio_channel_socket_new();
+    qio_channel_socket_connect_sync(ioc2, addr, &error_abort);
+
+    object_unref(OBJECT(ioc1));
+    /* The two connections should now be processed serially.  */
+    while (g_main_context_iteration(ctx, TRUE)) {
+        if (closed == 1 && ioc2) {
+            object_unref(OBJECT(ioc2));
+            ioc2 = NULL;
+        }
+        if (closed == 2) {
+            break;
+        }
+    }
+
+    qapi_free_SocketAddress(addr);
+    object_unparent(OBJECT(chr));
+    g_main_context_unref(ctx);
+    g_free(optstr);
+    g_unsetenv("QTEST_SILENT_ERRORS");
+}
+
 
 #ifdef HAVE_CHARDEV_SERIAL
 static void char_serial_test(void)
@@ -1342,12 +1439,15 @@ int main(int argc, char **argv)
 
     SOCKET_SERVER_TEST(tcp, &tcpaddr);
     SOCKET_CLIENT_TEST(tcp, &tcpaddr);
+    g_test_add_data_func("/char/socket/server/two-clients/tcp", &tcpaddr,
+                         char_socket_server_two_clients_test);
 #ifndef WIN32
     SOCKET_SERVER_TEST(unix, &unixaddr);
     SOCKET_CLIENT_TEST(unix, &unixaddr);
+    g_test_add_data_func("/char/socket/server/two-clients/unix", &unixaddr,
+                         char_socket_server_two_clients_test);
 #endif
 
-
     g_test_add_func("/char/udp", char_udp_test);
 #ifdef HAVE_CHARDEV_SERIAL
     g_test_add_func("/char/serial", char_serial_test);
-- 
1.8.3.1

  reply	other threads:[~2019-03-07 17:30 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-07 17:29 [Qemu-devel] [PULL 00/57] qgraph merge Paolo Bonzini
2019-03-07 17:29 ` Paolo Bonzini [this message]
2019-03-07 17:29 ` [Qemu-devel] [PULL 02/57] tests/libqos: introduce virtio_start_device Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 03/57] tests/libqos: rename qpci_init_pc and qpci_init_spapr functions Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 04/57] tests/libqos: embed allocators instead of malloc-ing them separately Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 05/57] tests: qgraph API for the qtest driver framework Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 06/57] tests/libqos: pci-pc driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 07/57] tests/libqos: x86_64/pc machine node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 08/57] tests/libqos: sdhci driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 09/57] tests/libqos: arm/raspi2 machine node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 10/57] tests/libqos: arm/smdkc210 " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 11/57] tests/libqos: arm/sabrelite " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 12/57] tests/libqos: arm/xilinx-zynq-a9 " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 13/57] tests/libqos: aarch64/xlnx-zcu102 " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 14/57] qos-test: sdhci test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 15/57] tests/qgraph: add generic PCI testcases Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 16/57] tests/libqos: pci-spapr driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 17/57] tests/qgraph: ppc64/pseries machine node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 18/57] tests/libqos: has_buggy_msi flag Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 19/57] tests/libqos: e1000e driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 20/57] qos-test: e1000e test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 21/57] tests/libqos: virtio-pci driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 22/57] tests/libqos: remove global_qtest from virtio endianness checks Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 23/57] tests/libqos: virtio-mmio driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 24/57] tests/libqos: arm/virt machine node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 25/57] tests/qgraph: add generic virtio testcases Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 26/57] tests/libqos: virtio-serial driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 27/57] qos-test: virtio-console and virtio-serial test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 28/57] tests/libqos: virtio-9p driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 29/57] qos-test: virtio-9p test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 30/57] tests/libqos: virtio-balloon driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 31/57] tests/qgraph: remove virtio-balloon-test Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 32/57] tests/libqos: virtio-rng driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 33/57] qos-test: virtio-rng test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 34/57] tests/libqos: virtio-blk driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 35/57] qos-test: virtio-blk test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 36/57] tests/libqos: virtio-net driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 37/57] qos-test: virtio-net test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 38/57] tests/libqos: support multiqueue for virtio-net Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 39/57] vhost-user-test: always use 256 MiB of guest memory Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 40/57] qos-test: vhost-user test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 41/57] tests/libqos: virtio-scsi driver and interface nodes Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 42/57] qos-test: virtio-scsi test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 43/57] tests/libqos: remove pre-qgraph QVirtioPCIDevice API Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 44/57] tests: move virtio entirely to qos-test Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 45/57] qos-test: ac97 test node Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 46/57] qos-test: tpci200 " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 47/57] qos-test: ipoctal232 " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 48/57] qos-test: ne2k_pci " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 49/57] qos-test: nvme " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 50/57] qos-test: pcnet " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 51/57] qos-test: spapr-phb " Paolo Bonzini
2019-03-07 17:29 ` [Qemu-devel] [PULL 52/57] qos-test: usb-hcd-ohci " Paolo Bonzini
2019-03-07 17:30 ` [Qemu-devel] [PULL 53/57] qos-test: vmxnet3 " Paolo Bonzini
2019-03-07 17:30 ` [Qemu-devel] [PULL 54/57] qos-test: es1370 " Paolo Bonzini
2019-03-07 17:30 ` [Qemu-devel] [PULL 55/57] qos-test: eepro100 " Paolo Bonzini
2019-03-07 17:30 ` [Qemu-devel] [PULL 56/57] qos-test: e1000 " Paolo Bonzini
2019-03-07 17:30 ` [Qemu-devel] [PULL 57/57] qos-test: megasas " Paolo Bonzini
2019-03-08 15:16 ` [Qemu-devel] [PULL 00/57] qgraph merge Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1551979804-6060-2-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=lviver@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).