From patchwork Tue Mar 11 13:00:34 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcel Apfelbaum X-Patchwork-Id: 329092 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id AE5082C00A3 for ; Wed, 12 Mar 2014 00:07:25 +1100 (EST) Received: from localhost ([::1]:54711 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNMOw-0002F9-Qz for incoming@patchwork.ozlabs.org; Tue, 11 Mar 2014 09:07:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60916) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNMOc-0002Cj-6A for qemu-devel@nongnu.org; Tue, 11 Mar 2014 09:07:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WNMOW-0002Y4-66 for qemu-devel@nongnu.org; Tue, 11 Mar 2014 09:07:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:15954) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNMOV-0002Xp-UI for qemu-devel@nongnu.org; Tue, 11 Mar 2014 09:06:56 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s2BD6sob032021 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 11 Mar 2014 09:06:54 -0400 Received: from localhost.localdomain.com (vpn1-6-106.ams2.redhat.com [10.36.6.106]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s2BD0BB4023941; Tue, 11 Mar 2014 09:00:12 -0400 From: Marcel Apfelbaum To: qemu-devel@nongnu.org Date: Tue, 11 Mar 2014 15:00:34 +0200 Message-Id: <1394542834-31741-1-git-send-email-marcel.a@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, stefanha@redhat.com, armbru@redhat.com, aliguori@amazon.com, afaerber@suse.de Subject: [Qemu-devel] [PATCH V4] tests/libqtest: Fix possible deadlock in qtest initialization X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org '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 Signed-off-by: Marcel Apfelbaum Reviewed-by: Stefan Hajnoczi --- tests/libqtest.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/libqtest.c b/tests/libqtest.c index f587d36..c9e78aa 100644 --- a/tests/libqtest.c +++ b/tests/libqtest.c @@ -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;