2011-06-02 01:14:49 +08:00
|
|
|
/*
|
|
|
|
* QEMU Error Objects
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2. See
|
|
|
|
* the COPYING.LIB file in the top-level directory.
|
|
|
|
*/
|
2011-06-14 05:01:53 +08:00
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2012-12-18 01:19:43 +08:00
|
|
|
#include "qapi/error.h"
|
2014-03-22 07:42:28 +08:00
|
|
|
#include "qemu/error-report.h"
|
2011-06-02 01:14:49 +08:00
|
|
|
|
|
|
|
struct Error
|
|
|
|
{
|
|
|
|
char *msg;
|
2012-07-28 01:09:29 +08:00
|
|
|
ErrorClass err_class;
|
2011-06-02 01:14:49 +08:00
|
|
|
};
|
|
|
|
|
2014-01-02 10:46:59 +08:00
|
|
|
Error *error_abort;
|
|
|
|
|
2015-06-19 19:59:47 +08:00
|
|
|
static void error_setv(Error **errp, ErrorClass err_class,
|
|
|
|
const char *fmt, va_list ap)
|
2011-06-02 01:14:49 +08:00
|
|
|
{
|
|
|
|
Error *err;
|
2013-11-08 03:10:29 +08:00
|
|
|
int saved_errno = errno;
|
2011-06-02 01:14:49 +08:00
|
|
|
|
|
|
|
if (errp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-17 22:17:04 +08:00
|
|
|
assert(*errp == NULL);
|
2011-06-02 01:14:49 +08:00
|
|
|
|
2011-08-21 11:09:37 +08:00
|
|
|
err = g_malloc0(sizeof(*err));
|
2012-07-28 04:51:03 +08:00
|
|
|
err->msg = g_strdup_vprintf(fmt, ap);
|
2012-07-28 01:09:29 +08:00
|
|
|
err->err_class = err_class;
|
2011-06-02 01:14:49 +08:00
|
|
|
|
2014-01-02 10:46:59 +08:00
|
|
|
if (errp == &error_abort) {
|
2015-02-12 20:55:05 +08:00
|
|
|
error_report_err(err);
|
2014-01-02 10:46:59 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2011-06-02 01:14:49 +08:00
|
|
|
*errp = err;
|
2013-11-08 03:10:29 +08:00
|
|
|
|
|
|
|
errno = saved_errno;
|
2011-06-02 01:14:49 +08:00
|
|
|
}
|
|
|
|
|
2015-06-19 19:59:47 +08:00
|
|
|
void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
error_setv(errp, err_class, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2015-06-19 21:36:16 +08:00
|
|
|
void error_setg(Error **errp, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
error_setv(errp, ERROR_CLASS_GENERIC_ERROR, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2015-06-20 04:16:14 +08:00
|
|
|
void error_setg_errno(Error **errp, int os_errno, const char *fmt, ...)
|
2012-10-02 15:00:45 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
2015-06-19 19:59:47 +08:00
|
|
|
char *msg;
|
2013-11-08 03:10:29 +08:00
|
|
|
int saved_errno = errno;
|
2012-10-02 15:00:45 +08:00
|
|
|
|
|
|
|
if (errp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2015-06-20 04:16:14 +08:00
|
|
|
error_setv(errp, ERROR_CLASS_GENERIC_ERROR, fmt, ap);
|
2012-10-02 15:00:45 +08:00
|
|
|
va_end(ap);
|
|
|
|
|
2015-06-19 19:59:47 +08:00
|
|
|
if (os_errno != 0) {
|
|
|
|
msg = (*errp)->msg;
|
|
|
|
(*errp)->msg = g_strdup_printf("%s: %s", msg, strerror(os_errno));
|
|
|
|
g_free(msg);
|
2014-01-02 10:46:59 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 03:10:29 +08:00
|
|
|
errno = saved_errno;
|
2012-10-02 15:00:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-08 02:24:49 +08:00
|
|
|
void error_setg_file_open(Error **errp, int os_errno, const char *filename)
|
|
|
|
{
|
|
|
|
error_setg_errno(errp, os_errno, "Could not open '%s'", filename);
|
|
|
|
}
|
|
|
|
|
2013-08-07 23:40:11 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
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-20 02:44:54 +08:00
|
|
|
void error_setg_win32(Error **errp, int win32_err, const char *fmt, ...)
|
2013-08-07 23:40:11 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
2015-06-19 19:59:47 +08:00
|
|
|
char *msg1, *msg2;
|
2013-08-07 23:40:11 +08:00
|
|
|
|
|
|
|
if (errp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
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-20 02:44:54 +08:00
|
|
|
error_setv(errp, ERROR_CLASS_GENERIC_ERROR, fmt, ap);
|
2015-06-19 19:59:47 +08:00
|
|
|
va_end(ap);
|
|
|
|
|
2013-08-07 23:40:11 +08:00
|
|
|
if (win32_err != 0) {
|
2015-06-19 19:59:47 +08:00
|
|
|
msg1 = (*errp)->msg;
|
|
|
|
msg2 = g_win32_error_message(win32_err);
|
|
|
|
(*errp)->msg = g_strdup_printf("%s: %s (error: %x)", msg1, msg2,
|
|
|
|
(unsigned)win32_err);
|
2013-08-07 23:40:11 +08:00
|
|
|
g_free(msg2);
|
|
|
|
g_free(msg1);
|
2014-01-02 10:46:59 +08:00
|
|
|
}
|
2013-08-07 23:40:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-12-06 02:04:05 +08:00
|
|
|
Error *error_copy(const Error *err)
|
|
|
|
{
|
|
|
|
Error *err_new;
|
|
|
|
|
|
|
|
err_new = g_malloc0(sizeof(*err));
|
|
|
|
err_new->msg = g_strdup(err->msg);
|
2012-07-28 01:09:29 +08:00
|
|
|
err_new->err_class = err->err_class;
|
2011-12-06 02:04:05 +08:00
|
|
|
|
|
|
|
return err_new;
|
|
|
|
}
|
|
|
|
|
2012-08-02 03:29:38 +08:00
|
|
|
ErrorClass error_get_class(const Error *err)
|
|
|
|
{
|
|
|
|
return err->err_class;
|
|
|
|
}
|
|
|
|
|
2011-06-02 01:14:49 +08:00
|
|
|
const char *error_get_pretty(Error *err)
|
|
|
|
{
|
|
|
|
return err->msg;
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:27:19 +08:00
|
|
|
void error_report_err(Error *err)
|
|
|
|
{
|
|
|
|
error_report("%s", error_get_pretty(err));
|
|
|
|
error_free(err);
|
|
|
|
}
|
|
|
|
|
2011-06-02 01:14:49 +08:00
|
|
|
void error_free(Error *err)
|
|
|
|
{
|
|
|
|
if (err) {
|
2011-08-21 11:09:37 +08:00
|
|
|
g_free(err->msg);
|
|
|
|
g_free(err);
|
2011-06-02 01:14:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 19:26:32 +08:00
|
|
|
void error_propagate(Error **dst_errp, Error *local_err)
|
2011-06-02 01:14:49 +08:00
|
|
|
{
|
2014-05-02 19:26:32 +08:00
|
|
|
if (local_err && dst_errp == &error_abort) {
|
2015-02-12 20:55:05 +08:00
|
|
|
error_report_err(local_err);
|
2014-01-02 10:46:59 +08:00
|
|
|
abort();
|
2014-05-02 19:26:32 +08:00
|
|
|
} else if (dst_errp && !*dst_errp) {
|
|
|
|
*dst_errp = local_err;
|
2011-06-02 01:14:49 +08:00
|
|
|
} else if (local_err) {
|
|
|
|
error_free(local_err);
|
|
|
|
}
|
|
|
|
}
|