qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Richard Henderson" <richard.henderson@linaro.org>,
	"Daniel P. Berrangé" <berrange@redhat.com>
Subject: [PULL 4/8] tests/qtest: use prctl(PR_SET_PDEATHSIG) as fallback to kill QEMU
Date: Wed, 18 May 2022 11:04:34 +0200	[thread overview]
Message-ID: <20220518090438.158475-5-thuth@redhat.com> (raw)
In-Reply-To: <20220518090438.158475-1-thuth@redhat.com>

From: Daniel P. Berrangé <berrange@redhat.com>

Although we register a ABRT handler to kill off QEMU when g_assert()
triggers, we want an extra safety net. The QEMU process might be
non-functional and thus not have responded to SIGTERM. The test script
might also have crashed with SEGV, in which case the cleanup handlers
won't ever run.

Using the Linux specific prctl(PR_SET_PDEATHSIG) syscall, we
can ensure that QEMU gets sent SIGKILL as soon as the controlling
qtest exits, if nothing else has correctly told it to quit.

Note, technically the death signal is sent when the *thread* that
called fork() exits. IOW, if you are calling qtest_init() in one
thread, letting that thread exit, and then expecting to run
qtest_quit() in a different thread, things are not going to work
out. Fortunately that is not a scenario that exists in qtests,
as pairs of qtest_init and qtest_quit are always called from the
same thread.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Message-Id: <20220513154906.206715-3-berrange@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 tests/qtest/libqtest.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 4a4697c0d1..2e49618454 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -19,6 +19,9 @@
 #include <sys/socket.h>
 #include <sys/wait.h>
 #include <sys/un.h>
+#ifdef __linux__
+#include <sys/prctl.h>
+#endif /* __linux__ */
 
 #include "libqtest.h"
 #include "libqmp.h"
@@ -301,6 +304,20 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
     s->expected_status = 0;
     s->qemu_pid = fork();
     if (s->qemu_pid == 0) {
+#ifdef __linux__
+        /*
+         * Although we register a ABRT handler to kill off QEMU
+         * when g_assert() triggers, we want an extra safety
+         * net. The QEMU process might be non-functional and
+         * thus not have responded to SIGTERM. The test script
+         * might also have crashed with SEGV, in which case the
+         * cleanup handlers won't ever run.
+         *
+         * This PR_SET_PDEATHSIG setup will ensure any remaining
+         * QEMU will get terminated with SIGKILL in these cases.
+         */
+        prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
+#endif /* __linux__ */
         if (!g_setenv("QEMU_AUDIO_DRV", "none", true)) {
             exit(1);
         }
-- 
2.27.0



  parent reply	other threads:[~2022-05-18  9:24 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-18  9:04 [PULL 0/8] Misc patches (Gitlab-CI, qtest, Capstone, ...) Thomas Huth
2022-05-18  9:04 ` [PULL 1/8] gitlab-ci: Switch the container of the 'check-patch' & 'check-dco' jobs Thomas Huth
2022-05-18  9:04 ` [PULL 2/8] Remove Ubuntu 18.04 container support from the repository Thomas Huth
2022-05-18  9:04 ` [PULL 3/8] tests/qtest: fix registration of ABRT handler for QEMU cleanup Thomas Huth
2022-05-18  9:04 ` Thomas Huth [this message]
2022-05-18  9:04 ` [PULL 5/8] docs/about: Update the support statement for Windows Thomas Huth
2022-05-18  9:04 ` [PULL 6/8] tests/vm: Add capstone to the NetBSD and OpenBSD VMs Thomas Huth
2022-05-18  9:04 ` [PULL 7/8] capstone: Allow version 3.0.5 again Thomas Huth
2022-05-18  9:04 ` [PULL 8/8] capstone: Remove the capstone submodule Thomas Huth
2022-05-18 16:12 ` [PULL 0/8] Misc patches (Gitlab-CI, qtest, Capstone, ...) Richard Henderson
2022-05-19  6:29   ` Thomas Huth
2022-05-19 11:29     ` Stefan Hajnoczi
2022-05-19 11:43     ` Daniel P. Berrangé
2022-05-19 11:55     ` Peter Maydell
2022-05-20  6:22       ` Thomas Huth
2022-05-20 18:51         ` Richard Henderson

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=20220518090438.158475-5-thuth@redhat.com \
    --to=thuth@redhat.com \
    --cc=berrange@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /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).