From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:55052) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1STwhu-0003h0-Ik for qemu-devel@nongnu.org; Mon, 14 May 2012 10:57:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1STwho-00077F-Qd for qemu-devel@nongnu.org; Mon, 14 May 2012 10:57:06 -0400 Received: from mail-ob0-f173.google.com ([209.85.214.173]:51644) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1STwho-00076I-JJ for qemu-devel@nongnu.org; Mon, 14 May 2012 10:57:00 -0400 Received: by mail-ob0-f173.google.com with SMTP id wd20so9283583obb.4 for ; Mon, 14 May 2012 07:56:59 -0700 (PDT) Sender: fluxion From: Michael Roth Date: Mon, 14 May 2012 09:56:32 -0500 Message-Id: <1337007392-30304-3-git-send-email-mdroth@linux.vnet.ibm.com> In-Reply-To: <1337007392-30304-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1336763987-10920-1-git-send-email-lcapitulino@redhat.com> <1337007392-30304-1-git-send-email-mdroth@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 2/2] qemu-ga: guest-shutdown: become synchronous List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: eblake@redhat.com, lcapitulino@redhat.com 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 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 cb97646..9a59276 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 { -- 1.7.4.1