]> Git Repo - qemu.git/commitdiff
libqtest: Fix possible deadlock in qtest initialization
authorMarcel Apfelbaum <[email protected]>
Tue, 11 Mar 2014 13:00:34 +0000 (15:00 +0200)
committerAndreas Färber <[email protected]>
Thu, 13 Mar 2014 00:21:57 +0000 (01:21 +0100)
'socket_accept' waits for QEMU to init its unix socket.
If QEMU encounters an error during command line parsing,
it can exit before initializing the communication channel.

Using a timeout for sockets fixes the issue.

Reviewed-by: Eric Blake <[email protected]>
Signed-off-by: Marcel Apfelbaum <[email protected]>
Reviewed-by: Stefan Hajnoczi <[email protected]>
Signed-off-by: Andreas Färber <[email protected]>
tests/libqtest.c

index f587d36176ed388c69ab9ca48743f6b0f95c95c1..c9e78aa7410b4b2f63eba25a5cbb5887897a6e1c 100644 (file)
@@ -34,6 +34,7 @@
 #include "qapi/qmp/json-parser.h"
 
 #define MAX_IRQ 256
+#define SOCKET_TIMEOUT 5
 
 QTestState *global_qtest;
 
@@ -78,12 +79,16 @@ static int socket_accept(int sock)
     struct sockaddr_un addr;
     socklen_t addrlen;
     int ret;
+    struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
+                               .tv_usec = 0 };
+
+    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
+               sizeof(timeout));
 
     addrlen = sizeof(addr);
     do {
         ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
     } while (ret == -1 && errno == EINTR);
-    g_assert_no_errno(ret);
     close(sock);
 
     return ret;
@@ -147,12 +152,16 @@ QTestState *qtest_init(const char *extra_args)
     }
 
     s->fd = socket_accept(sock);
-    s->qmp_fd = socket_accept(qmpsock);
+    if (s->fd >= 0) {
+        s->qmp_fd = socket_accept(qmpsock);
+    }
     unlink(socket_path);
     unlink(qmp_socket_path);
     g_free(socket_path);
     g_free(qmp_socket_path);
 
+    g_assert(s->fd >= 0 && s->qmp_fd >= 0);
+
     s->rx = g_string_new("");
     for (i = 0; i < MAX_IRQ; i++) {
         s->irq_level[i] = false;
This page took 0.028311 seconds and 4 git commands to generate.