qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: Stefan Hajnoczi <stefanha@redhat.com>, qemu-devel@nongnu.org
Cc: "Bin Meng" <bin.meng@windriver.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [PULL 18/21] tests/qtest: libqtest: Introduce qtest_wait_qemu()
Date: Fri, 28 Oct 2022 15:23:01 +0200	[thread overview]
Message-ID: <20221028132304.829103-19-thuth@redhat.com> (raw)
In-Reply-To: <20221028132304.829103-1-thuth@redhat.com>

From: Bin Meng <bin.meng@windriver.com>

Introduce an API for qtest to wait for the QEMU process to terminate.

Suggested-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Bin Meng <bin.meng@windriver.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20221028045736.679903-7-bin.meng@windriver.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 tests/qtest/libqtest.h |  9 ++++++
 tests/qtest/libqtest.c | 63 +++++++++++++++++++++++++-----------------
 2 files changed, 47 insertions(+), 25 deletions(-)

diff --git a/tests/qtest/libqtest.h b/tests/qtest/libqtest.h
index 65c040e504..91a5f7edd9 100644
--- a/tests/qtest/libqtest.h
+++ b/tests/qtest/libqtest.h
@@ -75,6 +75,15 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args);
  */
 QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd);
 
+/**
+ * qtest_wait_qemu:
+ * @s: #QTestState instance to operate on.
+ *
+ * Wait for the QEMU process to terminate. It is safe to call this function
+ * multiple times.
+ */
+void qtest_wait_qemu(QTestState *s);
+
 /**
  * qtest_kill_qemu:
  * @s: #QTestState instance to operate on.
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index d12a604d78..e1e2d39a6e 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -156,37 +156,14 @@ void qtest_set_expected_status(QTestState *s, int status)
     s->expected_status = status;
 }
 
-void qtest_kill_qemu(QTestState *s)
+static void qtest_check_status(QTestState *s)
 {
-    pid_t pid = s->qemu_pid;
-#ifndef _WIN32
-    int wstatus;
-#else
-    DWORD ret;
-#endif
-
-    /* Skip wait if qtest_probe_child already reaped.  */
-    if (pid != -1) {
-#ifndef _WIN32
-        kill(pid, SIGTERM);
-        TFR(pid = waitpid(s->qemu_pid, &s->wstatus, 0));
-        assert(pid == s->qemu_pid);
-#else
-        TerminateProcess((HANDLE)pid, s->expected_status);
-        ret = WaitForSingleObject((HANDLE)pid, INFINITE);
-        assert(ret == WAIT_OBJECT_0);
-        GetExitCodeProcess((HANDLE)pid, &s->exit_code);
-        CloseHandle((HANDLE)pid);
-#endif
-        s->qemu_pid = -1;
-    }
-
     /*
      * Check whether qemu exited with expected exit status; anything else is
      * fishy and should be logged with as much detail as possible.
      */
 #ifndef _WIN32
-    wstatus = s->wstatus;
+    int wstatus = s->wstatus;
     if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != s->expected_status) {
         fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU "
                 "process but encountered exit status %d (expected %d)\n",
@@ -212,6 +189,42 @@ void qtest_kill_qemu(QTestState *s)
 #endif
 }
 
+void qtest_wait_qemu(QTestState *s)
+{
+#ifndef _WIN32
+    pid_t pid;
+
+    TFR(pid = waitpid(s->qemu_pid, &s->wstatus, 0));
+    assert(pid == s->qemu_pid);
+#else
+    DWORD ret;
+
+    ret = WaitForSingleObject((HANDLE)s->qemu_pid, INFINITE);
+    assert(ret == WAIT_OBJECT_0);
+    GetExitCodeProcess((HANDLE)s->qemu_pid, &s->exit_code);
+    CloseHandle((HANDLE)s->qemu_pid);
+#endif
+
+    qtest_check_status(s);
+}
+
+void qtest_kill_qemu(QTestState *s)
+{
+    /* Skip wait if qtest_probe_child() already reaped */
+    if (s->qemu_pid != -1) {
+#ifndef _WIN32
+        kill(s->qemu_pid, SIGTERM);
+#else
+        TerminateProcess((HANDLE)s->qemu_pid, s->expected_status);
+#endif
+        qtest_wait_qemu(s);
+        s->qemu_pid = -1;
+        return;
+    }
+
+    qtest_check_status(s);
+}
+
 static void kill_qemu_hook_func(void *s)
 {
     qtest_kill_qemu(s);
-- 
2.31.1



  parent reply	other threads:[~2022-10-28 13:26 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-28 13:22 [PULL 00/21] s390x and qtest patches Thomas Huth
2022-10-28 13:22 ` [PULL 01/21] s390x/pv: remove semicolon from macro definition Thomas Huth
2022-10-28 13:22 ` [PULL 02/21] s390x: step down as general arch maintainer Thomas Huth
2022-10-28 13:22 ` [PULL 03/21] s390x/tod-kvm: don't save/restore the TOD in PV guests Thomas Huth
2022-10-28 13:22 ` [PULL 04/21] tests/tcg/s390x: Test compiler flags only once, not every time Thomas Huth
2022-10-28 13:22 ` [PULL 05/21] target/s390x: Fix emulation of the VISTR instruction Thomas Huth
2022-10-28 13:22 ` [PULL 06/21] tests/tcg/s390x: Add a test for the vistr instruction Thomas Huth
2022-10-28 13:22 ` [PULL 07/21] MAINTAINERS: target/s390x/: add Ilya as reviewer Thomas Huth
2022-10-28 13:22 ` [PULL 08/21] tests/qtest/tpm: Clean up remainders of swtpm Thomas Huth
2022-10-28 13:22 ` [PULL 09/21] tests/qtest/cxl-test: Remove temporary directories after testing Thomas Huth
2022-10-28 13:22 ` [PULL 10/21] tests/qtest/libqos/e1000e: Use e1000_regs.h Thomas Huth
2022-10-28 13:22 ` [PULL 11/21] tests/vm: update openbsd to release 7.2 Thomas Huth
2022-10-28 13:22 ` [PULL 12/21] tests: Add sndio to the FreeBSD CI containers / VM Thomas Huth
2022-10-28 13:22 ` [PULL 13/21] accel/qtest: Support qtest accelerator for Windows Thomas Huth
2022-10-28 13:22 ` [PULL 14/21] tests/qtest: Use send/recv for socket communication Thomas Huth
2022-10-28 13:22 ` [PULL 15/21] tests/qtest: Support libqtest to build and run on Windows Thomas Huth
2022-10-28 13:22 ` [PULL 16/21] tests/qtest: device-plug-test: Reverse the usage of double/single quotes Thomas Huth
2022-10-28 13:23 ` [PULL 17/21] tests/qtest: Use EXIT_FAILURE instead of magic number Thomas Huth
2022-10-28 13:23 ` Thomas Huth [this message]
2022-10-28 13:23 ` [PULL 19/21] tests/qtest: migration-test: Make sure QEMU process "to" exited after migration is canceled Thomas Huth
2022-10-28 13:23 ` [PULL 20/21] tests/qtest: libqos: Do not build virtio-9p unconditionally Thomas Huth
2022-10-28 13:23 ` [PULL 21/21] tests/qtest: libqtest: Correct the timeout unit of blocking receive calls for win32 Thomas Huth
2022-10-31 10:27 ` [PULL 00/21] s390x and qtest patches Stefan Hajnoczi
2022-10-31 18:37 ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221028132304.829103-19-thuth@redhat.com \
    --to=thuth@redhat.com \
    --cc=bin.meng@windriver.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).