error: New error_fatal

Similar to error_abort, but doesn't report where the error was
created, and terminates the process with exit(1) rather than abort().

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <1441983105-26376-2-git-send-email-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
This commit is contained in:
Markus Armbruster 2015-09-11 16:51:42 +02:00
parent 4f966768ac
commit a29a37b994
2 changed files with 32 additions and 13 deletions

View File

@ -50,6 +50,9 @@
* Call a function aborting on errors: * Call a function aborting on errors:
* foo(arg, &error_abort); * foo(arg, &error_abort);
* *
* Call a function treating errors as fatal:
* foo(arg, &error_fatal);
*
* Receive an error and pass it on to the caller: * Receive an error and pass it on to the caller:
* Error *err = NULL; * Error *err = NULL;
* foo(arg, &err); * foo(arg, &err);
@ -100,6 +103,7 @@ ErrorClass error_get_class(const Error *err);
* If @errp is NULL, the error is ignored. Don't bother creating one * If @errp is NULL, the error is ignored. Don't bother creating one
* then. * then.
* If @errp is &error_abort, print a suitable message and abort(). * If @errp is &error_abort, print a suitable message and abort().
* If @errp is &error_fatal, print a suitable message and exit(1).
* If @errp is anything else, *@errp must be NULL. * If @errp is anything else, *@errp must be NULL.
* The new error's class is ERROR_CLASS_GENERIC_ERROR, and its * The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
* human-readable error message is made from printf-style @fmt, ... * human-readable error message is made from printf-style @fmt, ...
@ -148,6 +152,8 @@ void error_setg_win32_internal(Error **errp,
* error object. * error object.
* Else, if @dst_errp is &error_abort, print a suitable message and * Else, if @dst_errp is &error_abort, print a suitable message and
* abort(). * abort().
* Else, if @dst_errp is &error_fatal, print a suitable message and
* exit(1).
* Else, if @dst_errp already contains an error, ignore this one: free * Else, if @dst_errp already contains an error, ignore this one: free
* the error object. * the error object.
* Else, move the error object from @local_err to *@dst_errp. * Else, move the error object from @local_err to *@dst_errp.
@ -206,4 +212,9 @@ void error_set_internal(Error **errp,
*/ */
extern Error *error_abort; extern Error *error_abort;
/*
* Pass to error_setg() & friends to exit(1) on error.
*/
extern Error *error_fatal;
#endif #endif

View File

@ -2,9 +2,11 @@
* QEMU Error Objects * QEMU Error Objects
* *
* Copyright IBM, Corp. 2011 * Copyright IBM, Corp. 2011
* Copyright (C) 2011-2015 Red Hat, Inc.
* *
* Authors: * Authors:
* Anthony Liguori <aliguori@us.ibm.com> * Anthony Liguori <aliguori@us.ibm.com>
* Markus Armbruster <armbru@redhat.com>,
* *
* This work is licensed under the terms of the GNU LGPL, version 2. See * This work is licensed under the terms of the GNU LGPL, version 2. See
* the COPYING.LIB file in the top-level directory. * the COPYING.LIB file in the top-level directory.
@ -24,13 +26,20 @@ struct Error
}; };
Error *error_abort; Error *error_abort;
Error *error_fatal;
static void error_do_abort(Error *err) static void error_handle_fatal(Error **errp, Error *err)
{ {
if (errp == &error_abort) {
fprintf(stderr, "Unexpected error in %s() at %s:%d:\n", fprintf(stderr, "Unexpected error in %s() at %s:%d:\n",
err->func, err->src, err->line); err->func, err->src, err->line);
error_report_err(err); error_report_err(err);
abort(); abort();
}
if (errp == &error_fatal) {
error_report_err(err);
exit(1);
}
} }
static void error_setv(Error **errp, static void error_setv(Error **errp,
@ -52,10 +61,7 @@ static void error_setv(Error **errp,
err->line = line; err->line = line;
err->func = func; err->func = func;
if (errp == &error_abort) { error_handle_fatal(errp, err);
error_do_abort(err);
}
*errp = err; *errp = err;
errno = saved_errno; errno = saved_errno;
@ -216,11 +222,13 @@ void error_free(Error *err)
void error_propagate(Error **dst_errp, Error *local_err) void error_propagate(Error **dst_errp, Error *local_err)
{ {
if (local_err && dst_errp == &error_abort) { if (!local_err) {
error_do_abort(local_err); return;
} else if (dst_errp && !*dst_errp) { }
error_handle_fatal(dst_errp, local_err);
if (dst_errp && !*dst_errp) {
*dst_errp = local_err; *dst_errp = local_err;
} else if (local_err) { } else {
error_free(local_err); error_free(local_err);
} }
} }