2011-06-01 12:14:49 -05:00
|
|
|
/*
|
|
|
|
* QEMU Error Objects
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
2015-06-19 18:29:24 +02:00
|
|
|
* Copyright (C) 2011-2015 Red Hat, Inc.
|
2011-06-01 12:14:49 -05:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
2015-06-19 18:29:24 +02:00
|
|
|
* Markus Armbruster <armbru@redhat.com>
|
2011-06-01 12:14:49 -05:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2. See
|
|
|
|
* the COPYING.LIB file in the top-level directory.
|
|
|
|
*/
|
2015-06-19 18:29:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Error reporting system loosely patterned after Glib's GError.
|
|
|
|
*
|
|
|
|
* Create an error:
|
|
|
|
* error_setg(&err, "situation normal, all fouled up");
|
|
|
|
*
|
|
|
|
* Report an error to stderr:
|
|
|
|
* error_report_err(err);
|
|
|
|
* This frees the error object.
|
|
|
|
*
|
|
|
|
* Report an error somewhere else:
|
|
|
|
* const char *msg = error_get_pretty(err);
|
|
|
|
* do with msg what needs to be done...
|
|
|
|
* error_free(err);
|
|
|
|
*
|
|
|
|
* Handle an error without reporting it (just for completeness):
|
|
|
|
* error_free(err);
|
|
|
|
*
|
|
|
|
* Pass an existing error to the caller:
|
|
|
|
* error_propagate(errp, err);
|
|
|
|
* where Error **errp is a parameter, by convention the last one.
|
|
|
|
*
|
|
|
|
* Create a new error and pass it to the caller:
|
|
|
|
* error_setg(errp, "situation normal, all fouled up");
|
|
|
|
*
|
|
|
|
* Call a function and receive an error from it:
|
|
|
|
* Error *err = NULL;
|
|
|
|
* foo(arg, &err);
|
|
|
|
* if (err) {
|
|
|
|
* handle the error...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Call a function ignoring errors:
|
|
|
|
* foo(arg, NULL);
|
|
|
|
*
|
|
|
|
* Call a function aborting on errors:
|
|
|
|
* foo(arg, &error_abort);
|
|
|
|
*
|
|
|
|
* Receive an error and pass it on to the caller:
|
|
|
|
* Error *err = NULL;
|
|
|
|
* foo(arg, &err);
|
|
|
|
* if (err) {
|
|
|
|
* handle the error...
|
|
|
|
* error_propagate(errp, err);
|
|
|
|
* }
|
|
|
|
* where Error **errp is a parameter, by convention the last one.
|
|
|
|
*
|
|
|
|
* Do *not* "optimize" this to
|
|
|
|
* foo(arg, errp);
|
|
|
|
* if (*errp) { // WRONG!
|
|
|
|
* handle the error...
|
|
|
|
* }
|
|
|
|
* because errp may be NULL!
|
|
|
|
*
|
|
|
|
* But when all you do with the error is pass it on, please use
|
|
|
|
* foo(arg, errp);
|
|
|
|
* for readability.
|
|
|
|
*/
|
|
|
|
|
2011-06-01 12:14:49 -05:00
|
|
|
#ifndef ERROR_H
|
|
|
|
#define ERROR_H
|
|
|
|
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/compiler.h"
|
2012-07-27 14:09:29 -03:00
|
|
|
#include "qapi-types.h"
|
2011-06-01 12:14:49 -05:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Opaque error object.
|
2011-06-01 12:14:49 -05:00
|
|
|
*/
|
|
|
|
typedef struct Error Error;
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Get @err's human-readable error message.
|
2011-06-01 12:14:49 -05:00
|
|
|
*/
|
2015-06-19 18:29:24 +02:00
|
|
|
const char *error_get_pretty(Error *err);
|
2011-06-01 12:14:49 -05:00
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Get @err's error class.
|
|
|
|
* Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
|
|
|
|
* strongly discouraged.
|
|
|
|
*/
|
|
|
|
ErrorClass error_get_class(const Error *err);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new error object and assign it to *@errp.
|
|
|
|
* If @errp is NULL, the error is ignored. Don't bother creating one
|
|
|
|
* then.
|
|
|
|
* If @errp is &error_abort, print a suitable message and abort().
|
|
|
|
* If @errp is anything else, *@errp must be NULL.
|
|
|
|
* The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
|
|
|
|
* human-readable error message is made from printf-style @fmt, ...
|
|
|
|
*/
|
|
|
|
void error_setg(Error **errp, const char *fmt, ...)
|
|
|
|
GCC_FMT_ATTR(2, 3);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just like error_setg(), with @os_error info added to the message.
|
|
|
|
* If @os_error is non-zero, ": " + strerror(os_error) is appended to
|
|
|
|
* the human-readable error message.
|
2012-10-02 09:00:45 +02:00
|
|
|
*/
|
2015-06-19 22:16:14 +02:00
|
|
|
void error_setg_errno(Error **errp, int os_error, const char *fmt, ...)
|
|
|
|
GCC_FMT_ATTR(3, 4);
|
2012-10-02 09:00:45 +02:00
|
|
|
|
2013-08-07 11:40:11 -04:00
|
|
|
#ifdef _WIN32
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Just like error_setg(), with @win32_error info added to the message.
|
|
|
|
* If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
|
|
|
|
* is appended to the human-readable error message.
|
2013-08-07 11:40:11 -04:00
|
|
|
*/
|
qga: Clean up unnecessarily dirty casts
qga_vss_fsfreeze() casts error_set_win32() from
void (*)(Error **, int, ErrorClass, const char *, ...)
to
void (*)(void **, int, int, const char *, ...)
The result is later called. Since the two types are not compatible,
the call is undefined behavior. It works in practice anyway.
However, there's no real need for trickery here. Clean it up as
follows:
* Declare struct Error, and fix the first parameter.
* Switch to error_setg_win32(). This gets rid of the troublesome
ErrorClass parameter. Requires converting error_setg_win32() from
macro to function, but that's trivially easy, because this is the
only user of error_set_win32().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-19 20:44:54 +02:00
|
|
|
void error_setg_win32(Error **errp, int win32_err, const char *fmt, ...)
|
|
|
|
GCC_FMT_ATTR(3, 4);
|
2013-08-07 11:40:11 -04:00
|
|
|
#endif
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Propagate error object (if any) from @local_err to @dst_errp.
|
|
|
|
* If @local_err is NULL, do nothing (because there's nothing to
|
|
|
|
* propagate).
|
|
|
|
* Else, if @dst_errp is NULL, errors are being ignored. Free the
|
|
|
|
* error object.
|
|
|
|
* Else, if @dst_errp is &error_abort, print a suitable message and
|
|
|
|
* abort().
|
|
|
|
* Else, if @dst_errp already contains an error, ignore this one: free
|
|
|
|
* the error object.
|
|
|
|
* Else, move the error object from @local_err to *@dst_errp.
|
|
|
|
* On return, @local_err is invalid.
|
2012-08-29 11:20:57 -03:00
|
|
|
*/
|
2015-06-19 18:29:24 +02:00
|
|
|
void error_propagate(Error **dst_errp, Error *local_err);
|
2012-08-29 11:20:57 -03:00
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Convenience function to report open() failure.
|
2013-06-07 14:24:49 -04:00
|
|
|
*/
|
|
|
|
void error_setg_file_open(Error **errp, int os_errno, const char *filename);
|
|
|
|
|
2012-08-01 16:29:38 -03:00
|
|
|
/*
|
2015-06-19 18:29:24 +02:00
|
|
|
* Return an exact copy of @err.
|
2011-12-05 16:04:05 -02:00
|
|
|
*/
|
|
|
|
Error *error_copy(const Error *err);
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Free @err.
|
|
|
|
* @err may be NULL.
|
2011-06-01 12:14:49 -05:00
|
|
|
*/
|
2015-06-19 18:29:24 +02:00
|
|
|
void error_free(Error *err);
|
2011-06-01 12:14:49 -05:00
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Convenience function to error_report() and free @err.
|
2015-02-06 15:27:19 +01:00
|
|
|
*/
|
|
|
|
void error_report_err(Error *);
|
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Just like error_setg(), except you get to specify the error class.
|
|
|
|
* Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
|
|
|
|
* strongly discouraged.
|
2011-06-01 12:14:49 -05:00
|
|
|
*/
|
2015-06-19 18:29:24 +02:00
|
|
|
void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
|
|
|
|
GCC_FMT_ATTR(3, 4);
|
2011-06-01 12:14:49 -05:00
|
|
|
|
2015-06-19 18:29:24 +02:00
|
|
|
/*
|
|
|
|
* Pass to error_setg() & friends to abort() on error.
|
2014-01-01 18:46:59 -08:00
|
|
|
*/
|
|
|
|
extern Error *error_abort;
|
|
|
|
|
2011-06-01 12:14:49 -05:00
|
|
|
#endif
|