qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Andreas Faerber <afaerber@suse.de>,
	Anthony Liguori <aliguori@amazon.com>
Subject: [Qemu-devel] [PATCH 2/3] qtest: make QEMU our direct child process
Date: Mon, 17 Feb 2014 16:44:57 +0100	[thread overview]
Message-ID: <1392651898-16749-3-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1392651898-16749-1-git-send-email-stefanha@redhat.com>

qtest_init() cannot use exec*p() to launch QEMU since the exec*p()
functions take an argument array while qtest_init() takes char
*extra_args.  Therefore we execute /bin/sh -c <command-line> and let the
shell parse the argument string.

This left /bin/sh as our child process and our child's child was QEMU.
We still want QEMU's pid so the -pidfile option was used to let QEMU
report its pid.

The pidfile needs to be unlinked when the test case exits or fails.  In
other words, the pidfile creates a new problem for us!

Simplify all this using the shell 'exec' command.  It allows us to
replace the /bin/sh process with QEMU.  Then we no longer need to use
-pidfile because we already know our fork child's pid.

Note: Yes, it seems silly to exec /bin/sh when we could just exec QEMU
directly.  But remember qtest_init() takes a single char *extra_args
command-line fragment instead of a real argv[] array, so we need
/bin/sh's argument parsing behavior.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 tests/libqtest.c | 34 +++++-----------------------------
 1 file changed, 5 insertions(+), 29 deletions(-)

diff --git a/tests/libqtest.c b/tests/libqtest.c
index 2876ce4..8b2b2d7 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -43,7 +43,7 @@ struct QTestState
     int qmp_fd;
     bool irq_level[MAX_IRQ];
     GString *rx;
-    pid_t qemu_pid;  /* QEMU process spawned by our child */
+    pid_t qemu_pid;  /* our child QEMU process */
 };
 
 #define g_assert_no_errno(ret) do { \
@@ -88,32 +88,14 @@ static int socket_accept(int sock)
     return ret;
 }
 
-static pid_t read_pid_file(const char *pid_file)
-{
-    FILE *f;
-    char buffer[1024];
-    pid_t pid = -1;
-
-    f = fopen(pid_file, "r");
-    if (f) {
-        if (fgets(buffer, sizeof(buffer), f)) {
-            pid = atoi(buffer);
-        }
-        fclose(f);
-    }
-    return pid;
-}
-
 QTestState *qtest_init(const char *extra_args)
 {
     QTestState *s;
     int sock, qmpsock, i;
     gchar *socket_path;
     gchar *qmp_socket_path;
-    gchar *pid_file;
     gchar *command;
     const char *qemu_binary;
-    pid_t pid;
 
     qemu_binary = getenv("QTEST_QEMU_BINARY");
     g_assert(qemu_binary != NULL);
@@ -122,22 +104,20 @@ QTestState *qtest_init(const char *extra_args)
 
     socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
     qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
-    pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());
 
     sock = init_socket(socket_path);
     qmpsock = init_socket(qmp_socket_path);
 
-    pid = fork();
-    if (pid == 0) {
-        command = g_strdup_printf("%s "
+    s->qemu_pid = fork();
+    if (s->qemu_pid == 0) {
+        command = g_strdup_printf("exec %s "
                                   "-qtest unix:%s,nowait "
                                   "-qtest-log /dev/null "
                                   "-qmp unix:%s,nowait "
-                                  "-pidfile %s "
                                   "-machine accel=qtest "
                                   "-display none "
                                   "%s", qemu_binary, socket_path,
-                                  qmp_socket_path, pid_file,
+                                  qmp_socket_path,
                                   extra_args ?: "");
         execlp("/bin/sh", "sh", "-c", command, NULL);
         exit(1);
@@ -159,10 +139,6 @@ QTestState *qtest_init(const char *extra_args)
     qtest_qmp_discard_response(s, "");
     qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
 
-    s->qemu_pid = read_pid_file(pid_file);
-    unlink(pid_file);
-    g_free(pid_file);
-
     if (getenv("QTEST_STOP")) {
         kill(s->qemu_pid, SIGSTOP);
     }
-- 
1.8.5.3

  parent reply	other threads:[~2014-02-17 15:45 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-17 15:44 [Qemu-devel] [PATCH 0/3] qtest: avoid pidfile and QEMU process leaks Stefan Hajnoczi
2014-02-17 15:44 ` [Qemu-devel] [PATCH 1/3] qtest: drop unused child_pid field Stefan Hajnoczi
2014-02-17 15:44 ` Stefan Hajnoczi [this message]
2014-02-17 16:44   ` [Qemu-devel] [PATCH 2/3] qtest: make QEMU our direct child process Markus Armbruster
2014-02-18  9:00     ` Stefan Hajnoczi
2014-02-18  9:53       ` Markus Armbruster
2014-02-17 15:44 ` [Qemu-devel] [PATCH 3/3] qtest: kill QEMU process on g_assert() failure Stefan Hajnoczi
2014-02-17 16:16   ` Paolo Bonzini
2014-02-17 17:00     ` Markus Armbruster
2014-02-18  9:05       ` Stefan Hajnoczi
2014-02-18 10:05         ` Markus Armbruster
2014-02-18 10:23           ` Paolo Bonzini
2014-02-18 10:43             ` Markus Armbruster
2014-02-18 14:38               ` Stefan Hajnoczi
2014-02-18 10:07         ` Paolo Bonzini
2014-02-18 10:17           ` Daniel P. Berrange
2014-02-18 10:23             ` Paolo Bonzini
2014-02-17 16:49   ` Markus Armbruster
2014-02-17 16:56     ` Paolo Bonzini
2014-02-18  9:17     ` Stefan Hajnoczi
2014-02-18  9:55       ` Markus Armbruster
2014-02-18 14:44         ` Stefan Hajnoczi
2014-02-18 14:56       ` Peter Maydell
2014-02-18 10:02     ` Markus Armbruster
2014-02-18 14:38       ` Stefan Hajnoczi
2014-02-18 14:52         ` Markus Armbruster

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=1392651898-16749-3-git-send-email-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=afaerber@suse.de \
    --cc=aliguori@amazon.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.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).