From patchwork Mon Feb 17 15:44:58 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 321045 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 A47602C00C8 for ; Tue, 18 Feb 2014 02:47:34 +1100 (EST) Received: from localhost ([::1]:41885 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WFQPr-0005ee-IY for incoming@patchwork.ozlabs.org; Mon, 17 Feb 2014 10:47:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33864) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WFQNk-0003Dk-Ps for qemu-devel@nongnu.org; Mon, 17 Feb 2014 10:45:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WFQNe-0000wA-Nz for qemu-devel@nongnu.org; Mon, 17 Feb 2014 10:45:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:27618) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WFQNe-0000vw-F6 for qemu-devel@nongnu.org; Mon, 17 Feb 2014 10:45:14 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s1HFjB4s015834 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 17 Feb 2014 10:45:11 -0500 Received: from localhost (ovpn-112-17.ams2.redhat.com [10.36.112.17]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s1HFj8hd011496; Mon, 17 Feb 2014 10:45:09 -0500 From: Stefan Hajnoczi To: Date: Mon, 17 Feb 2014 16:44:58 +0100 Message-Id: <1392651898-16749-4-git-send-email-stefanha@redhat.com> In-Reply-To: <1392651898-16749-1-git-send-email-stefanha@redhat.com> References: <1392651898-16749-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Peter Maydell , Andreas Faerber , Anthony Liguori Subject: [Qemu-devel] [PATCH 3/3] qtest: kill QEMU process on g_assert() failure 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 The QEMU process stays running if the test case fails. This patch fixes the leak by installing a SIGABRT signal handler which invokes qtest_end(). In order to make that work for assertion failures during qtest_init(), we need to initialize QTestState fields including file descriptors and pids carefully. qtest_quit() is then safe to call even during qtest_init(). Signed-off-by: Stefan Hajnoczi --- tests/libqtest.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/tests/libqtest.c b/tests/libqtest.c index 8b2b2d7..09a0481 100644 --- a/tests/libqtest.c +++ b/tests/libqtest.c @@ -44,6 +44,7 @@ struct QTestState bool irq_level[MAX_IRQ]; GString *rx; pid_t qemu_pid; /* our child QEMU process */ + struct sigaction sigact_old; /* restored on exit */ }; #define g_assert_no_errno(ret) do { \ @@ -88,6 +89,11 @@ static int socket_accept(int sock) return ret; } +static void sigabrt_handler(int signo) +{ + qtest_end(); +} + QTestState *qtest_init(const char *extra_args) { QTestState *s; @@ -96,11 +102,22 @@ QTestState *qtest_init(const char *extra_args) gchar *qmp_socket_path; gchar *command; const char *qemu_binary; + struct sigaction sigact; qemu_binary = getenv("QTEST_QEMU_BINARY"); g_assert(qemu_binary != NULL); - s = g_malloc(sizeof(*s)); + s = g_malloc0(sizeof(*s)); + s->fd = -1; + s->qmp_fd = -1; + s->qemu_pid = -1; + + /* Catch SIGABRT to clean up on g_assert() failure */ + sigact = (struct sigaction){ + .sa_handler = sigabrt_handler, + .sa_flags = SA_RESETHAND, + }; + sigaction(SIGABRT, &sigact, &s->sigact_old); socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid()); qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid()); @@ -150,13 +167,19 @@ void qtest_quit(QTestState *s) { int status; + sigaction(SIGABRT, &s->sigact_old, NULL); + if (s->qemu_pid != -1) { kill(s->qemu_pid, SIGTERM); waitpid(s->qemu_pid, &status, 0); } - close(s->fd); - close(s->qmp_fd); + if (s->fd != -1) { + close(s->fd); + } + if (s->qmp_fd != -1) { + close(s->qmp_fd); + } g_string_free(s->rx, true); g_free(s); }