From patchwork Thu Apr 19 17:36:55 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 153843 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 EAB4FB6FF0 for ; Fri, 20 Apr 2012 03:37:31 +1000 (EST) Received: from localhost ([::1]:56934 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SKvIP-0006YF-SA for incoming@patchwork.ozlabs.org; Thu, 19 Apr 2012 13:37:29 -0400 Received: from eggs.gnu.org ([208.118.235.92]:36407) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SKvHu-0005N7-Cw for qemu-devel@nongnu.org; Thu, 19 Apr 2012 13:37:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SKvHp-0007R1-IT for qemu-devel@nongnu.org; Thu, 19 Apr 2012 13:36:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36089) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SKvHp-0007Qh-AR for qemu-devel@nongnu.org; Thu, 19 Apr 2012 13:36:53 -0400 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 q3JHaouf014001 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 19 Apr 2012 13:36:50 -0400 Received: from localhost (ovpn-113-135.phx2.redhat.com [10.3.113.135]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q3JHanxi015229; Thu, 19 Apr 2012 13:36:49 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Thu, 19 Apr 2012 14:36:55 -0300 Message-Id: <1334857015-8726-3-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1334857015-8726-1-git-send-email-lcapitulino@redhat.com> References: <1334857015-8726-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: pbonzini@redhat.com, lersek@redhat.com, mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [RFC 2/2] qemu-ga: shutdown: fix zombies 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 automatic 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. An interesting change is that, the current code isn't able to report errors that happen in the child (eg. exec() failures), but this commit fixes that and child errors are now correctly reported. 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 1fcbfce..82dcc18 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -55,8 +55,9 @@ static void reopen_fd_to_null(int fd) 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) { @@ -71,8 +72,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); @@ -85,9 +86,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 {