From patchwork Fri May 11 19:19:47 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 158570 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id BCFF6B700B for ; Sat, 12 May 2012 05:20:18 +1000 (EST) Received: from localhost ([::1]:60389 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSvNw-0006Cs-J4 for incoming@patchwork.ozlabs.org; Fri, 11 May 2012 15:20:16 -0400 Received: from eggs.gnu.org ([208.118.235.92]:55577) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSvNe-0005m8-GB for qemu-devel@nongnu.org; Fri, 11 May 2012 15:20:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SSvNc-0007YG-Ae for qemu-devel@nongnu.org; Fri, 11 May 2012 15:19:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:1255) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSvNc-0007XD-1r for qemu-devel@nongnu.org; Fri, 11 May 2012 15:19:56 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q4BJJhcu004837 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 11 May 2012 15:19:44 -0400 Received: from localhost (ovpn-116-25.ams2.redhat.com [10.36.116.25]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q4BJJfjm008463; Fri, 11 May 2012 15:19:43 -0400 From: Luiz Capitulino To: mdroth@linux.vnet.ibm.com Date: Fri, 11 May 2012 16:19:47 -0300 Message-Id: <1336763987-10920-3-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1336763987-10920-1-git-send-email-lcapitulino@redhat.com> References: <1336763987-10920-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH 2/2] qemu-ga: guest-shutdown: become synchronous 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 Last commit dropped qemu-ga's SIGCHLD handler, used to automatically reap terminated children processes. This introduced a bug to qmp_guest_shutdown(): it will generate zombies. This problem probably doesn't matter in the success case, as the VM will shutdown anyway, but let's do the right thing and reap the created process. This ultimately means that guest-shutdown is now a synchronous command. An interesting side effect is that guest-shutdown is now able to report an error to the client if shutting down fails. Signed-off-by: Luiz Capitulino --- qga/commands-posix.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index a61f9e0..f1b19af 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -36,8 +36,9 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) { - int ret; const char *shutdown_flag; + int ret, status; + pid_t rpid, pid; slog("guest-shutdown called, mode: %s", mode); if (!has_mode || strcmp(mode, "powerdown") == 0) { @@ -52,8 +53,8 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) return; } - ret = fork(); - if (ret == 0) { + pid = fork(); + if (pid == 0) { /* child, start the shutdown */ setsid(); fclose(stdin); @@ -66,9 +67,17 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) slog("guest-shutdown failed: %s", strerror(errno)); } exit(!!ret); - } else if (ret < 0) { - error_set(err, QERR_UNDEFINED_ERROR); + } else if (pid < 0) { + goto exit_err; } + + rpid = waitpid(pid, &status, 0); + if (rpid == pid && WIFEXITED(status) && !WEXITSTATUS(status)) { + return; + } + +exit_err: + error_set(err, QERR_UNDEFINED_ERROR); } typedef struct GuestFileHandle {