From patchwork Mon Dec 17 16:21:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Thomas Huth X-Patchwork-Id: 154021 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2664522ljp; Mon, 17 Dec 2018 08:32:49 -0800 (PST) X-Google-Smtp-Source: AFSGD/W3UtYV9UsUH6Ya1yJKTtgjwohSqWeQVWdXH8P7mKt/WIy4/QhAs/Biv5JLbJ0PDLf612v8 X-Received: by 2002:ac8:4884:: with SMTP id i4mr13777701qtq.219.1545064369618; Mon, 17 Dec 2018 08:32:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545064369; cv=none; d=google.com; s=arc-20160816; b=ybzaxZc35/lv5GUJnlLe7s01kxFwtKqtGBh+lxhQpX834w1x2BiXVYfNcdVWI2Bfro ZUD8BcQXAos/46pbXQLOwSXxjIqZJyehGaaAMtNdDsZkd4w/z4EDr9miS9njFRrM3kr7 PDjlFe1i9AoBANOSVTB038BUi1/jB7LEg9giZZ/I2kIeRo6DFCNW75qxM8a2uOX87Fcx NfK+lL/u3CW1nLFRpKki3HKozZyB04F35SvH8ku5EbUZcCu/vIu+KHyZ+HTgxrCBpJqM y7qptq4cwYZhrqc8d38oux9DEpAX5WKuDfZKpY7J6qWRaa9bjKvidL9WNh/NFrtEj+Ui HVaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from; bh=xl8/B+XwlfINCifMdCDDrImwDsajzgAagymL7wr2yX0=; b=0dZBZ3r1f9th6DoLzoYUSaaAvDFt2oGF05TaJIir4vqpD30TMeUvD6b0fjr0Q5oq8l GgD4ctg86ExxdjZm7XpXs2QiTcbnIBZh/7snCqzSen0mY0Ghk1eYbwsVyZK7EvuFn1HO nRKw1FJyZ3cpPBJvAyTEy/nSl16rOrZWUpBai2KbDe5+8hTgdlDHaMKXx30gx7amgS5R 6qvBK1ZtiR27/bmOjJ9qEMrpn4j84IE9bqiTUKUx9iCAx7F+uTmOBf1cUnLIKLpZjrnb kUtCzX2ou/2T/bdd0vicnOLMreL0Oy6JkBHQbIhuBicBzXew+0nmq5bgsttDHB7Goewj CJ+g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id q2si1947645qve.72.2018.12.17.08.32.49 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 17 Dec 2018 08:32:49 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from localhost ([::1]:47753 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gYvp3-0001Lg-3k for patch@linaro.org; Mon, 17 Dec 2018 11:32:49 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51127) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gYveb-0000fn-QX for qemu-devel@nongnu.org; Mon, 17 Dec 2018 11:22:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gYveX-0000VH-GA for qemu-devel@nongnu.org; Mon, 17 Dec 2018 11:22:01 -0500 Received: from mx1.redhat.com ([209.132.183.28]:38084) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gYveX-0000UU-6N for qemu-devel@nongnu.org; Mon, 17 Dec 2018 11:21:57 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 47698325B2; Mon, 17 Dec 2018 16:21:56 +0000 (UTC) Received: from thuth.com (dhcp-200-199.str.redhat.com [10.33.200.199]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7759F1001942; Mon, 17 Dec 2018 16:21:47 +0000 (UTC) From: Thomas Huth To: qemu-devel@nongnu.org Date: Mon, 17 Dec 2018 17:21:28 +0100 Message-Id: <1545063690-10116-12-git-send-email-thuth@redhat.com> In-Reply-To: <1545063690-10116-1-git-send-email-thuth@redhat.com> References: <1545063690-10116-1-git-send-email-thuth@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 17 Dec 2018 16:21:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 11/13] tests: Exit boot-serial-test loop if child dies X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Richard Henderson There's no point in waiting 5 full minutes when there will be no more output. Compute timeout based on elapsed wall clock time instead of N * delays, as the delay is a minimum sleep time. Cc: Thomas Huth Cc: Laurent Vivier Cc: Paolo Bonzini Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Wainer dos Santos Moschetta [thuth: Replaced global_qtest with local qts variable] Signed-off-by: Thomas Huth --- tests/boot-serial-test.c | 20 ++++++++++---- tests/libqtest.c | 72 ++++++++++++++++++++++++++++++------------------ tests/libqtest.h | 8 ++++++ 3 files changed, 68 insertions(+), 32 deletions(-) -- 1.8.3.1 diff --git a/tests/boot-serial-test.c b/tests/boot-serial-test.c index 366fe51..58a48f3 100644 --- a/tests/boot-serial-test.c +++ b/tests/boot-serial-test.c @@ -128,13 +128,14 @@ static testdef_t tests[] = { { NULL } }; -static bool check_guest_output(const testdef_t *test, int fd) +static bool check_guest_output(QTestState *qts, const testdef_t *test, int fd) { - int i, nbr = 0, pos = 0, ccnt; + int nbr = 0, pos = 0, ccnt; + time_t now, start = time(NULL); char ch; - /* Poll serial output... Wait at most 360 seconds */ - for (i = 0; i < 36000; ++i) { + /* Poll serial output... */ + while (1) { ccnt = 0; while (ccnt++ < 512 && (nbr = read(fd, &ch, 1)) == 1) { if (ch == test->expect[pos]) { @@ -148,6 +149,15 @@ static bool check_guest_output(const testdef_t *test, int fd) } } g_assert(nbr >= 0); + /* Wait only if the child is still alive. */ + if (!qtest_probe_child(qts)) { + break; + } + /* Wait at most 360 seconds. */ + now = time(NULL); + if (now - start >= 360) { + break; + } g_usleep(10000); } @@ -199,7 +209,7 @@ static void test_machine(const void *data) unlink(codetmp); } - if (!check_guest_output(test, ser_fd)) { + if (!check_guest_output(qts, test, ser_fd)) { g_error("Failed to find expected string. Please check '%s'", serialtmp); } diff --git a/tests/libqtest.c b/tests/libqtest.c index 43be078..1d75d3c 100644 --- a/tests/libqtest.c +++ b/tests/libqtest.c @@ -39,10 +39,11 @@ struct QTestState { int fd; int qmp_fd; - bool irq_level[MAX_IRQ]; - GString *rx; pid_t qemu_pid; /* our child QEMU process */ + int wstatus; bool big_endian; + bool irq_level[MAX_IRQ]; + GString *rx; }; static GHookList abrt_hooks; @@ -96,36 +97,52 @@ static int socket_accept(int sock) return ret; } -static void kill_qemu(QTestState *s) +bool qtest_probe_child(QTestState *s) { - if (s->qemu_pid != -1) { - int wstatus = 0; - pid_t pid; + pid_t pid = s->qemu_pid; - kill(s->qemu_pid, SIGTERM); - TFR(pid = waitpid(s->qemu_pid, &wstatus, 0)); + if (pid != -1) { + pid = waitpid(pid, &s->wstatus, WNOHANG); + if (pid == 0) { + return true; + } + s->qemu_pid = -1; + } + return false; +} +static void kill_qemu(QTestState *s) +{ + pid_t pid = s->qemu_pid; + int wstatus; + + /* Skip wait if qtest_probe_child already reaped. */ + if (pid != -1) { + kill(pid, SIGTERM); + TFR(pid = waitpid(s->qemu_pid, &s->wstatus, 0)); assert(pid == s->qemu_pid); - /* - * We expect qemu to exit with status 0; anything else is - * fishy and should be logged with as much detail as possible. - */ - if (wstatus) { - if (WIFEXITED(wstatus)) { - fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU " - "process but encountered exit status %d\n", - __FILE__, __LINE__, WEXITSTATUS(wstatus)); - } else if (WIFSIGNALED(wstatus)) { - int sig = WTERMSIG(wstatus); - const char *signame = strsignal(sig) ?: "unknown ???"; - const char *dump = WCOREDUMP(wstatus) ? " (core dumped)" : ""; - - fprintf(stderr, "%s:%d: kill_qemu() detected QEMU death " - "from signal %d (%s)%s\n", - __FILE__, __LINE__, sig, signame, dump); - } - abort(); + } + + /* + * We expect qemu to exit with status 0; anything else is + * fishy and should be logged with as much detail as possible. + */ + wstatus = s->wstatus; + if (wstatus) { + if (WIFEXITED(wstatus)) { + fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU " + "process but encountered exit status %d\n", + __FILE__, __LINE__, WEXITSTATUS(wstatus)); + } else if (WIFSIGNALED(wstatus)) { + int sig = WTERMSIG(wstatus); + const char *signame = strsignal(sig) ?: "unknown ???"; + const char *dump = WCOREDUMP(wstatus) ? " (core dumped)" : ""; + + fprintf(stderr, "%s:%d: kill_qemu() detected QEMU death " + "from signal %d (%s)%s\n", + __FILE__, __LINE__, sig, signame, dump); } + abort(); } } @@ -228,6 +245,7 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args) g_test_message("starting QEMU: %s", command); + s->wstatus = 0; s->qemu_pid = fork(); if (s->qemu_pid == 0) { setenv("QEMU_AUDIO_DRV", "none", true); diff --git a/tests/libqtest.h b/tests/libqtest.h index 96a6c01..9758c51 100644 --- a/tests/libqtest.h +++ b/tests/libqtest.h @@ -1011,4 +1011,12 @@ bool qmp_rsp_is_err(QDict *rsp); */ void qmp_assert_error_class(QDict *rsp, const char *class); +/** + * qtest_probe_child: + * @s: QTestState instance to operate on. + * + * Returns: true if the child is still alive. + */ +bool qtest_probe_child(QTestState *s); + #endif