diff mbox

[2/2] qemu-ga: guest-shutdown: become synchronous

Message ID 1336763987-10920-3-git-send-email-lcapitulino@redhat.com
State New
Headers show

Commit Message

Luiz Capitulino May 11, 2012, 7:19 p.m. UTC
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 <lcapitulino@redhat.com>
---
 qga/commands-posix.c |   19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

Comments

Eric Blake May 11, 2012, 10:11 p.m. UTC | #1
On 05/11/2012 01:19 PM, Luiz Capitulino wrote:
> 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 <lcapitulino@redhat.com>
> ---
>  qga/commands-posix.c |   19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)
> 

>          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)) {

And another case where waitpid should be used in a loop.

Other than that, I'm in favor of this series.
diff mbox

Patch

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 {