From 5d24ee70bcbcf578614193526bcd5ed30a8eb16c Mon Sep 17 00:00:00 2001 From: Peter Crosthwaite Date: Wed, 1 Jan 2014 18:46:59 -0800 Subject: error: Add error_abort Add a special Error * that can be passed to error handling APIs to signal that any errors are fatal and should abort QEMU. There are two advantages to this: - allows for brevity when wishing to assert success of Error ** accepting APIs. No need for this pattern: Error * local_err = NULL; api_call(foo, bar, &local_err); assert_no_error(local_err); This also removes the need for _nofail variants of APIs with asserting call sites now reduced to 1LOC. - SIGABRT happens from within the offending API. When a fatal error occurs in an API call (when the caller is asserting sucess) failure often means the API itself is broken. With the abort happening in the API call now, the stack frames into the call are available at debug time. In the assert_no_error scheme the abort happens after the fact. The exact semantic is that when an error is raised, if the argument Error ** matches &error_abort, then the abort occurs immediately. The error messaged is reported. For error_propagate, if the destination error is &error_abort, then the abort happens at propagation time. Signed-off-by: Peter Crosthwaite Reviewed-by: Markus Armbruster Signed-off-by: Luiz Capitulino --- util/error.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/error.c b/util/error.c index 3ee362a7f5..f11f1d57a0 100644 --- a/util/error.c +++ b/util/error.c @@ -23,6 +23,8 @@ struct Error ErrorClass err_class; }; +Error *error_abort; + void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...) { Error *err; @@ -41,6 +43,11 @@ void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...) va_end(ap); err->err_class = err_class; + if (errp == &error_abort) { + error_report("%s", error_get_pretty(err)); + abort(); + } + *errp = err; errno = saved_errno; @@ -72,6 +79,11 @@ void error_set_errno(Error **errp, int os_errno, ErrorClass err_class, va_end(ap); err->err_class = err_class; + if (errp == &error_abort) { + error_report("%s", error_get_pretty(err)); + abort(); + } + *errp = err; errno = saved_errno; @@ -112,6 +124,11 @@ void error_set_win32(Error **errp, int win32_err, ErrorClass err_class, va_end(ap); err->err_class = err_class; + if (errp == &error_abort) { + error_report("%s", error_get_pretty(err)); + abort(); + } + *errp = err; } @@ -153,7 +170,10 @@ void error_free(Error *err) void error_propagate(Error **dst_err, Error *local_err) { - if (dst_err && !*dst_err) { + if (local_err && dst_err == &error_abort) { + error_report("%s", error_get_pretty(local_err)); + abort(); + } else if (dst_err && !*dst_err) { *dst_err = local_err; } else if (local_err) { error_free(local_err); -- cgit v1.2.1 From 87ea75d5e135c0527c6a9dbac4317913409f28c7 Mon Sep 17 00:00:00 2001 From: Peter Crosthwaite Date: Wed, 1 Jan 2014 18:49:17 -0800 Subject: qemu-option: Remove qemu_opts_create_nofail This is a boiler-plate _nofail variant of qemu_opts_create. Remove and use error_abort in call sites. null/0 arguments needs to be added for the id and fail_if_exists fields in affected callsites due to argument inconsistency between the normal and no_fail variants. Signed-off-by: Peter Crosthwaite Reviewed-by: Markus Armbruster Signed-off-by: Luiz Capitulino --- util/qemu-config.c | 2 +- util/qemu-option.c | 9 --------- util/qemu-sockets.c | 18 +++++++++--------- 3 files changed, 10 insertions(+), 19 deletions(-) (limited to 'util') diff --git a/util/qemu-config.c b/util/qemu-config.c index 04da942a25..7973659518 100644 --- a/util/qemu-config.c +++ b/util/qemu-config.c @@ -311,7 +311,7 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) error_free(local_err); goto out; } - opts = qemu_opts_create_nofail(list); + opts = qemu_opts_create(list, NULL, 0, &error_abort); continue; } if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) { diff --git a/util/qemu-option.c b/util/qemu-option.c index efcb5dcfcb..668e5d919f 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -791,15 +791,6 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, return opts; } -QemuOpts *qemu_opts_create_nofail(QemuOptsList *list) -{ - QemuOpts *opts; - Error *errp = NULL; - opts = qemu_opts_create(list, NULL, 0, &errp); - assert_no_error(errp); - return opts; -} - void qemu_opts_reset(QemuOptsList *list) { QemuOpts *opts, *next_opts; diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 6b97dc11f9..8818d7c0de 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -578,7 +578,7 @@ int inet_listen(const char *str, char *ostr, int olen, addr = inet_parse(str, errp); if (addr != NULL) { - opts = qemu_opts_create_nofail(&socket_optslist); + opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); inet_addr_to_opts(opts, addr); qapi_free_InetSocketAddress(addr); sock = inet_listen_opts(opts, port_offset, errp); @@ -617,7 +617,7 @@ int inet_connect(const char *str, Error **errp) addr = inet_parse(str, errp); if (addr != NULL) { - opts = qemu_opts_create_nofail(&socket_optslist); + opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); inet_addr_to_opts(opts, addr); qapi_free_InetSocketAddress(addr); sock = inet_connect_opts(opts, errp, NULL, NULL); @@ -651,7 +651,7 @@ int inet_nonblocking_connect(const char *str, addr = inet_parse(str, errp); if (addr != NULL) { - opts = qemu_opts_create_nofail(&socket_optslist); + opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); inet_addr_to_opts(opts, addr); qapi_free_InetSocketAddress(addr); sock = inet_connect_opts(opts, errp, callback, opaque); @@ -794,7 +794,7 @@ int unix_listen(const char *str, char *ostr, int olen, Error **errp) char *path, *optstr; int sock, len; - opts = qemu_opts_create_nofail(&socket_optslist); + opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); optstr = strchr(str, ','); if (optstr) { @@ -822,7 +822,7 @@ int unix_connect(const char *path, Error **errp) QemuOpts *opts; int sock; - opts = qemu_opts_create_nofail(&socket_optslist); + opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); qemu_opt_set(opts, "path", path); sock = unix_connect_opts(opts, errp, NULL, NULL); qemu_opts_del(opts); @@ -839,7 +839,7 @@ int unix_nonblocking_connect(const char *path, g_assert(callback != NULL); - opts = qemu_opts_create_nofail(&socket_optslist); + opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); qemu_opt_set(opts, "path", path); sock = unix_connect_opts(opts, errp, callback, opaque); qemu_opts_del(opts); @@ -889,7 +889,7 @@ int socket_connect(SocketAddress *addr, Error **errp, QemuOpts *opts; int fd; - opts = qemu_opts_create_nofail(&socket_optslist); + opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); switch (addr->kind) { case SOCKET_ADDRESS_KIND_INET: inet_addr_to_opts(opts, addr->inet); @@ -921,7 +921,7 @@ int socket_listen(SocketAddress *addr, Error **errp) QemuOpts *opts; int fd; - opts = qemu_opts_create_nofail(&socket_optslist); + opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); switch (addr->kind) { case SOCKET_ADDRESS_KIND_INET: inet_addr_to_opts(opts, addr->inet); @@ -949,7 +949,7 @@ int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp) QemuOpts *opts; int fd; - opts = qemu_opts_create_nofail(&socket_optslist); + opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); switch (remote->kind) { case SOCKET_ADDRESS_KIND_INET: qemu_opt_set(opts, "host", remote->inet->host); -- cgit v1.2.1