qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [RFC 2/2] qemu-ga: shutdown: fix zombies


From: Luiz Capitulino
Subject: [Qemu-devel] [RFC 2/2] qemu-ga: shutdown: fix zombies
Date: Thu, 19 Apr 2012 14:36:55 -0300

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 <address@hidden>
---
 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 {
-- 
1.7.9.2.384.g4a92a




reply via email to

[Prev in Thread] Current Thread [Next in Thread]