From patchwork Tue May 15 14:48:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 159382 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 0AC9FB6FAF for ; Wed, 16 May 2012 02:15:35 +1000 (EST) Received: from localhost ([::1]:35702 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUJ5a-0003li-Ln for incoming@patchwork.ozlabs.org; Tue, 15 May 2012 10:51:02 -0400 Received: from eggs.gnu.org ([208.118.235.92]:53076) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUJ4S-0001H9-7F for qemu-devel@nongnu.org; Tue, 15 May 2012 10:50:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SUJ4M-00031L-Ma for qemu-devel@nongnu.org; Tue, 15 May 2012 10:49:51 -0400 Received: from mail-qc0-f173.google.com ([209.85.216.173]:46477) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUJ4M-0002zK-GT for qemu-devel@nongnu.org; Tue, 15 May 2012 10:49:46 -0400 Received: by mail-qc0-f173.google.com with SMTP id c20so4836115qcs.4 for ; Tue, 15 May 2012 07:49:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=SvmEopudYAo7TbfCAMkK+ZmvFV5xIHgZN+o7GZVwbCI=; b=t309LemqTF4Xq2GqFXG5NCgeMYcaoODJxVkvpTj5PyLTykXGofHLOpWnxRPh804SGA 6R1/PH87APEJuE8Gyrz7eY2S0xnUSJzCxxr8vCClB6zTBToUxWyiXCIYvhySlSRX8FWA R07RXggSDeu0LudC1oYDeVeQMLfln6WRPX5luwPfSCHNrLHWMje0DxsXQ3BJc2cB85LE ioDNcuELX6orE0Xaqjnw/2j2Ubuc5K9nHpU364B2tPKjz7bEhDueri8cYmaDi7aFfxub pMfjo9jC3tla1dwgtNGFKt6NhvyZuzARnNplZpeuPYJd+WsfjC7T2IqgRbeEhPgaohi6 YDIw== Received: by 10.60.11.136 with SMTP id q8mr9632557oeb.58.1337093385635; Tue, 15 May 2012 07:49:45 -0700 (PDT) Received: from localhost.localdomain ([32.97.110.59]) by mx.google.com with ESMTPS id a6sm22978806obo.10.2012.05.15.07.49.43 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 15 May 2012 07:49:44 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Tue, 15 May 2012 09:48:56 -0500 Message-Id: <1337093339-10786-11-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1337093339-10786-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1337093339-10786-1-git-send-email-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.216.173 Cc: mprivozn@redhat.com, aliguori@us.ibm.com, eblake@redhat.com, jcody@redhat.com, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH 10/13] 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 From: Luiz Capitulino 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 Reviewed-by: Eric Blake Signed-off-by: Michael Roth --- qga/commands-posix.c | 21 ++++++++++++++++----- 1 files changed, 16 insertions(+), 5 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 76c8235..68da71f 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,19 @@ 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; } + + do { + rpid = waitpid(pid, &status, 0); + } while (rpid == -1 && errno == EINTR); + if (rpid == pid && WIFEXITED(status) && !WEXITSTATUS(status)) { + return; + } + +exit_err: + error_set(err, QERR_UNDEFINED_ERROR); } typedef struct GuestFileHandle {