Patchwork [04/10] qemu-ga: qmp_guest_shutdown(): improve error reporting

login
register
mail settings
Submitter Luiz Capitulino
Date Nov. 27, 2012, 1:01 p.m.
Message ID <1354021324-31561-5-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/202208/
State New
Headers show

Comments

Luiz Capitulino - Nov. 27, 2012, 1:01 p.m.
Most errors are QERR_UNDEFINED_ERROR. Also, adds ga_wait_child() as
a future commit will use it too.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 qga/commands-posix.c | 48 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 10 deletions(-)
Michael Roth - Nov. 28, 2012, 7:27 p.m.
On Tue, Nov 27, 2012 at 11:01:58AM -0200, Luiz Capitulino wrote:
> Most errors are QERR_UNDEFINED_ERROR. Also, adds ga_wait_child() as
> a future commit will use it too.
> 
> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>

Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>

> ---
>  qga/commands-posix.c | 48 ++++++++++++++++++++++++++++++++++++++----------
>  1 file changed, 38 insertions(+), 10 deletions(-)
> 
> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> index 92fc550..b3a3f26 100644
> --- a/qga/commands-posix.c
> +++ b/qga/commands-posix.c
> @@ -46,10 +46,29 @@ extern char **environ;
>  #endif
>  #endif
> 
> +static void ga_wait_child(pid_t pid, int *status, Error **err)
> +{
> +    pid_t rpid;
> +
> +    *status = 0;
> +
> +    do {
> +        rpid = waitpid(pid, status, 0);
> +    } while (rpid == -1 && errno == EINTR);
> +
> +    if (rpid == -1) {
> +        error_setg_errno(err, errno, "failed to wait for child (pid: %d)", pid);
> +        return;
> +    }
> +
> +    g_assert(rpid == pid);
> +}
> +
>  void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
>  {
>      const char *shutdown_flag;
> -    pid_t rpid, pid;
> +    Error *local_err = NULL;
> +    pid_t pid;
>      int status;
> 
>      slog("guest-shutdown called, mode: %s", mode);
> @@ -60,8 +79,8 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
>      } else if (strcmp(mode, "reboot") == 0) {
>          shutdown_flag = "-r";
>      } else {
> -        error_set(err, QERR_INVALID_PARAMETER_VALUE, "mode",
> -                  "halt|powerdown|reboot");
> +        error_setg(err,
> +                   "mode is invalid (valid values are: halt|powerdown|reboot");
>          return;
>      }
> 
> @@ -77,18 +96,27 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
>                 "hypervisor initiated shutdown", (char*)NULL, environ);
>          _exit(EXIT_FAILURE);
>      } else if (pid < 0) {
> -        goto exit_err;
> +        error_setg_errno(err, errno, "failed to create child process");
> +        return;
>      }
> 
> -    do {
> -        rpid = waitpid(pid, &status, 0);
> -    } while (rpid == -1 && errno == EINTR);
> -    if (rpid == pid && WIFEXITED(status) && !WEXITSTATUS(status)) {
> +    ga_wait_child(pid, &status, &local_err);
> +    if (error_is_set(&local_err)) {
> +        error_propagate(err, local_err);
>          return;
>      }
> 
> -exit_err:
> -    error_set(err, QERR_UNDEFINED_ERROR);
> +    if (!WIFEXITED(status)) {
> +        error_setg(err, "child process has terminated abnormally");
> +        return;
> +    }
> +
> +    if (WEXITSTATUS(status)) {
> +        error_setg(err, "child process has failed to shutdown");
> +        return;
> +    }
> +
> +    /* succeded */
>  }
> 
>  typedef struct GuestFileHandle {
> -- 
> 1.8.0
>

Patch

diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 92fc550..b3a3f26 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -46,10 +46,29 @@  extern char **environ;
 #endif
 #endif
 
+static void ga_wait_child(pid_t pid, int *status, Error **err)
+{
+    pid_t rpid;
+
+    *status = 0;
+
+    do {
+        rpid = waitpid(pid, status, 0);
+    } while (rpid == -1 && errno == EINTR);
+
+    if (rpid == -1) {
+        error_setg_errno(err, errno, "failed to wait for child (pid: %d)", pid);
+        return;
+    }
+
+    g_assert(rpid == pid);
+}
+
 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
 {
     const char *shutdown_flag;
-    pid_t rpid, pid;
+    Error *local_err = NULL;
+    pid_t pid;
     int status;
 
     slog("guest-shutdown called, mode: %s", mode);
@@ -60,8 +79,8 @@  void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
     } else if (strcmp(mode, "reboot") == 0) {
         shutdown_flag = "-r";
     } else {
-        error_set(err, QERR_INVALID_PARAMETER_VALUE, "mode",
-                  "halt|powerdown|reboot");
+        error_setg(err,
+                   "mode is invalid (valid values are: halt|powerdown|reboot");
         return;
     }
 
@@ -77,18 +96,27 @@  void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
                "hypervisor initiated shutdown", (char*)NULL, environ);
         _exit(EXIT_FAILURE);
     } else if (pid < 0) {
-        goto exit_err;
+        error_setg_errno(err, errno, "failed to create child process");
+        return;
     }
 
-    do {
-        rpid = waitpid(pid, &status, 0);
-    } while (rpid == -1 && errno == EINTR);
-    if (rpid == pid && WIFEXITED(status) && !WEXITSTATUS(status)) {
+    ga_wait_child(pid, &status, &local_err);
+    if (error_is_set(&local_err)) {
+        error_propagate(err, local_err);
         return;
     }
 
-exit_err:
-    error_set(err, QERR_UNDEFINED_ERROR);
+    if (!WIFEXITED(status)) {
+        error_setg(err, "child process has terminated abnormally");
+        return;
+    }
+
+    if (WEXITSTATUS(status)) {
+        error_setg(err, "child process has failed to shutdown");
+        return;
+    }
+
+    /* succeded */
 }
 
 typedef struct GuestFileHandle {