From patchwork Wed Feb 19 14:50:44 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 321931 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 573092C00C7 for ; Thu, 20 Feb 2014 01:53:29 +1100 (EST) Received: from localhost ([::1]:59780 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WG8Wd-0000Kg-BA for incoming@patchwork.ozlabs.org; Wed, 19 Feb 2014 09:53:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48898) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WG8UN-00066Z-6H for qemu-devel@nongnu.org; Wed, 19 Feb 2014 09:51:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WG8UG-0003Ky-VO for qemu-devel@nongnu.org; Wed, 19 Feb 2014 09:51:07 -0500 Received: from mx1.redhat.com ([209.132.183.28]:11372) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WG8UG-0003Kt-NC for qemu-devel@nongnu.org; Wed, 19 Feb 2014 09:51:00 -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 s1JEovFr004028 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 19 Feb 2014 09:50:57 -0500 Received: from localhost (wlan-68-133.muc.redhat.com [10.32.68.133]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s1JEot2N032462; Wed, 19 Feb 2014 09:50:56 -0500 From: Stefan Hajnoczi To: Date: Wed, 19 Feb 2014 15:50:44 +0100 Message-Id: <1392821445-4587-3-git-send-email-stefanha@redhat.com> In-Reply-To: <1392821445-4587-1-git-send-email-stefanha@redhat.com> References: <1392821445-4587-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 , Anthony Liguori Subject: [Qemu-devel] [PULL 2/3] qtest: make QEMU our direct child process 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 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 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 Reviewed-by: Markus Armbruster --- 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); }