Skip to contentSkip to conversation

tests/libqtest: Fix possible deadlock in qtest initialization

PATCH In review Marcel Apfelbaum 3 participants 4 messages Mar 11, 2014 – Mar 12, 2014

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 tests/libqtest: Fix possible deadlock in qtest initialization

'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.

---
 tests/libqtest.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
tests/libqtest.c +11 -2
index f587d36..c9e78aa 100644
@@ -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;
Signature · 1 line
1.8.3.1
Reviewed-by on v4 1/1
#1 lore raw
Quoted 11 lines · Marcel Apfelbaum

'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.

---
 tests/libqtest.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
#2 lore raw
Quoted 7 lines · Stefan Hajnoczi
Quoted 6 lines · Marcel Apfelbaum

'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.

Quoted 7 lines
Quoted 5 lines
---
 tests/libqtest.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Thanks for the help, Stefan!
Please be aware that until you take care of the
"abort during qtest_init" issue, qtest will crash
on such occasions.

Last thing, which maintainer should take this?
Thanks,
Marcel

#3 lore raw
Quoted 14 lines · Marcel Apfelbaum
Quoted 13 lines · Stefan Hajnoczi
Quoted 11 lines · Marcel Apfelbaum

'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.

---
 tests/libqtest.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
Quoted 8 lines
Quoted 1 line

Thanks for the help, Stefan!
Please be aware that until you take care of the
"abort during qtest_init" issue, qtest will crash
on such occasions.

Last thing, which maintainer should take this?

Andreas Färber is the most likely victim^H^H^H^H^H^Hmaintainer:

"I don't feel like the official qtest maintainer, but I care and am actively working on it. I would consider it best to have individual tests maintained by subsystem maintainers and only define a maintainer for the core libqtest.c/qtest.c code."

Although I'm interested in libqtest myself I don't have the bandwidth to review/merge all future patches in that area.

Stefan