Skip to contentSkip to conversation

vl.c: unify exit on error handling

PATCH In review Igor Mammedov 2 participants 4 messages Oct 20, 2014

lore
Share & tools
lore
#0 lore raw

v2 cover vl.c: unify exit on error handling

v2:
* s/exit_if_error/error_report_fatal/
* move error_report_fatal() into util/error.c

reduces amount of code needed for handling exit on error for new users, turning it from 5LOC to 1 for each case.

Igor Mammedov (2):
vl.c: use single local_err throughout main()
vl.c: reduce exit on error code duplication

 include/qemu/error-report.h |  2 ++
 util/error.c                | 24 ++++++++++++++++++++++++
 vl.c                        | 38 +++++++-------------------------------
 3 files changed, 33 insertions(+), 31 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH v2 1/2 vl.c: use single local_err throughout main()

---
 vl.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
vl.c +1 -3
index aee73e1..528c289 100644
@@ -2771,6 +2771,7 @@ int main(int argc, char **argv, char **envp)
     };
     const char *trace_events = NULL;
     const char *trace_file = NULL;
+    Error *local_err = NULL;
     const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
                                         1024 * 1024;
     ram_addr_t maxram_size = default_ram_size;
@@ -4072,7 +4073,6 @@ int main(int argc, char **argv, char **envp)
     configure_accelerator(current_machine);
 
     if (qtest_chrdev) {
-        Error *local_err = NULL;
         qtest_init(qtest_chrdev, qtest_log, &local_err);
         if (local_err) {
             error_report("%s", error_get_pretty(local_err));
@@ -4316,7 +4316,6 @@ int main(int argc, char **argv, char **envp)
 #ifdef CONFIG_VNC
     /* init remote displays */
     if (vnc_display) {
-        Error *local_err = NULL;
         vnc_display_init(ds);
         vnc_display_open(ds, vnc_display, &local_err);
         if (local_err != NULL) {
@@ -4371,7 +4370,6 @@ int main(int argc, char **argv, char **envp)
     }
 
     if (incoming) {
-        Error *local_err = NULL;
         qemu_start_incoming_migration(incoming, &local_err);
         if (local_err) {
             error_report("-incoming %s: %s", incoming,
Signature · 1 line
1.9.3
#2 lore raw

PATCH v2 2/2 vl.c: reduce exit on error code duplication

In reply to Igor Mammedov

use error_report_fatal() helper instead of a bunch of

    if (local_err) {
        error_report(foo);
        error_free(local_err);
        exit(1);
    }

code blocks

---
* s/exit_if_error/error_report_fatal/
* move error_report_fatal() into util/error.c

---
 include/qemu/error-report.h |  2 ++
 util/error.c                | 24 ++++++++++++++++++++++++
 vl.c                        | 34 ++++++----------------------------
 3 files changed, 32 insertions(+), 28 deletions(-)
include/qemu/error-report.h +2 -0
index 7ab2355..a80a1a7 100644
@@ -16,6 +16,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 #include "qemu/compiler.h"
+#include "qapi/error.h"
 
 typedef struct Location {
     /* all members are private to qemu-error.c */
@@ -40,6 +41,7 @@ void error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 void error_set_progname(const char *argv0);
 void error_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
 void error_report(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
+void error_report_fatal(Error *err, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
 const char *error_get_progname(void);
 extern bool enable_timestamp_msg;
 
util/error.c +24 -0
index 2ace0d8..61d6d21 100644
@@ -171,3 +171,27 @@ void error_propagate(Error **dst_errp, Error *local_err)
         error_free(local_err);
     }
 }
+
+void error_report_fatal(Error *err, const char *fmt, ...)
+{
+    va_list ap;
+
+    if (!err) {
+        return;
+    }
+
+    if (fmt) {
+        char *optional_msg = NULL;
+
+        va_start(ap, fmt);
+        optional_msg = g_strdup_vprintf(fmt, ap);
+        va_end(ap);
+        error_report("%s: %s", optional_msg, error_get_pretty(err));
+        g_free(optional_msg);
+    } else {
+        error_report("%s", error_get_pretty(err));
+    }
+
+    error_free(err);
+    exit(EXIT_FAILURE);
+}
vl.c +6 -28
index 528c289..4f03b96 100644
@@ -2264,11 +2264,7 @@ static int chardev_init_func(QemuOpts *opts, void *opaque)
     Error *local_err = NULL;
 
     qemu_chr_new_from_opts(opts, NULL, &local_err);
-    if (local_err) {
-        error_report("%s", error_get_pretty(local_err));
-        error_free(local_err);
-        return -1;
-    }
+    error_report_fatal(local_err, NULL);
     return 0;
 }
 
@@ -2674,12 +2670,7 @@ static int machine_set_property(const char *name, const char *value,
     string_input_visitor_cleanup(siv);
     g_free(qom_name);
 
-    if (local_err) {
-        qerror_report_err(local_err);
-        error_free(local_err);
-        return -1;
-    }
-
+    error_report_fatal(local_err, NULL);
     return 0;
 }
 
@@ -4074,11 +4065,7 @@ int main(int argc, char **argv, char **envp)
 
     if (qtest_chrdev) {
         qtest_init(qtest_chrdev, qtest_log, &local_err);
-        if (local_err) {
-            error_report("%s", error_get_pretty(local_err));
-            error_free(local_err);
-            exit(1);
-        }
+        error_report_fatal(local_err, NULL);
     }
 
     machine_opts = qemu_get_machine_opts();
@@ -4318,12 +4305,8 @@ int main(int argc, char **argv, char **envp)
     if (vnc_display) {
         vnc_display_init(ds);
         vnc_display_open(ds, vnc_display, &local_err);
-        if (local_err != NULL) {
-            error_report("Failed to start VNC server on `%s': %s",
-                         vnc_display, error_get_pretty(local_err));
-            error_free(local_err);
-            exit(1);
-        }
+        error_report_fatal(local_err, "Failed to start VNC server on '%s'",
+                           vnc_display);
 
         if (show_vnc_port) {
             printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
@@ -4371,12 +4354,7 @@ int main(int argc, char **argv, char **envp)
 
     if (incoming) {
         qemu_start_incoming_migration(incoming, &local_err);
-        if (local_err) {
-            error_report("-incoming %s: %s", incoming,
-                         error_get_pretty(local_err));
-            error_free(local_err);
-            exit(1);
-        }
+        error_report_fatal(local_err, "-incoming %s", incoming);
     } else if (autostart) {
         vm_start();
     }
Signature · 1 line
1.9.3
Reviewed-by on v2 2/2
#3 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] vl.c: reduce exit on error code duplication

Quoted 9 lines · Igor Mammedov

use error_report_fatal() helper instead of a bunch of

    if (local_err) {
        error_report(foo);
        error_free(local_err);
        exit(1);
    }

code blocks

Quoted 9 lines

---
* s/exit_if_error/error_report_fatal/
* move error_report_fatal() into util/error.c

---
 include/qemu/error-report.h |  2 ++
 util/error.c                | 24 ++++++++++++++++++++++++
 vl.c                        | 34 ++++++----------------------------
 3 files changed, 32 insertions(+), 28 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org