2011-06-01 21:14:49 +04: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 01:01:53 +04:00
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2012-12-17 21:19:43 +04:00
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qapi/qmp/qjson.h"
|
|
|
|
#include "qapi/qmp/qdict.h"
|
2012-07-27 21:09:29 +04:00
|
|
|
#include "qapi-types.h"
|
2012-12-17 21:19:43 +04:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2011-06-01 21:14:49 +04:00
|
|
|
|
|
|
|
struct Error
|
|
|
|
{
|
|
|
|
char *msg;
|
2012-07-27 21:09:29 +04:00
|
|
|
ErrorClass err_class;
|
2011-06-01 21:14:49 +04:00
|
|
|
};
|
|
|
|
|
2014-01-02 06:46:59 +04:00
|
|
|
Error *error_abort;
|
|
|
|
|
2012-07-27 21:09:29 +04:00
|
|
|
void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
|
2011-06-01 21:14:49 +04:00
|
|
|
{
|
|
|
|
Error *err;
|
|
|
|
va_list ap;
|
2013-11-07 23:10:29 +04:00
|
|
|
int saved_errno = errno;
|
2011-06-01 21:14:49 +04:00
|
|
|
|
|
|
|
if (errp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-17 18:17:04 +04:00
|
|
|
assert(*errp == NULL);
|
2011-06-01 21:14:49 +04:00
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
err = g_malloc0(sizeof(*err));
|
2011-06-01 21:14:49 +04:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2012-07-28 00:51:03 +04:00
|
|
|
err->msg = g_strdup_vprintf(fmt, ap);
|
2011-06-01 21:14:49 +04:00
|
|
|
va_end(ap);
|
2012-07-27 21:09:29 +04:00
|
|
|
err->err_class = err_class;
|
2011-06-01 21:14:49 +04:00
|
|
|
|
2014-01-02 06:46:59 +04:00
|
|
|
if (errp == &error_abort) {
|
2014-01-15 07:37:09 +04:00
|
|
|
fprintf(stderr, "%s\n", error_get_pretty(err));
|
2014-01-02 06:46:59 +04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2011-06-01 21:14:49 +04:00
|
|
|
*errp = err;
|
2013-11-07 23:10:29 +04:00
|
|
|
|
|
|
|
errno = saved_errno;
|
2011-06-01 21:14:49 +04:00
|
|
|
}
|
|
|
|
|
2012-10-02 11:00:45 +04:00
|
|
|
void error_set_errno(Error **errp, int os_errno, ErrorClass err_class,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
Error *err;
|
|
|
|
char *msg1;
|
|
|
|
va_list ap;
|
2013-11-07 23:10:29 +04:00
|
|
|
int saved_errno = errno;
|
2012-10-02 11:00:45 +04:00
|
|
|
|
|
|
|
if (errp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(*errp == NULL);
|
|
|
|
|
|
|
|
err = g_malloc0(sizeof(*err));
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
msg1 = g_strdup_vprintf(fmt, ap);
|
|
|
|
if (os_errno != 0) {
|
|
|
|
err->msg = g_strdup_printf("%s: %s", msg1, strerror(os_errno));
|
|
|
|
g_free(msg1);
|
|
|
|
} else {
|
|
|
|
err->msg = msg1;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
err->err_class = err_class;
|
|
|
|
|
2014-01-02 06:46:59 +04:00
|
|
|
if (errp == &error_abort) {
|
2014-01-15 07:37:09 +04:00
|
|
|
fprintf(stderr, "%s\n", error_get_pretty(err));
|
2014-01-02 06:46:59 +04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2012-10-02 11:00:45 +04:00
|
|
|
*errp = err;
|
2013-11-07 23:10:29 +04:00
|
|
|
|
|
|
|
errno = saved_errno;
|
2012-10-02 11:00:45 +04:00
|
|
|
}
|
|
|
|
|
2013-06-07 22:24:49 +04: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 19:40:11 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
Error *err;
|
|
|
|
char *msg1;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (errp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(*errp == NULL);
|
|
|
|
|
|
|
|
err = g_malloc0(sizeof(*err));
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
msg1 = g_strdup_vprintf(fmt, ap);
|
|
|
|
if (win32_err != 0) {
|
|
|
|
char *msg2 = g_win32_error_message(win32_err);
|
|
|
|
err->msg = g_strdup_printf("%s: %s (error: %x)", msg1, msg2,
|
|
|
|
(unsigned)win32_err);
|
|
|
|
g_free(msg2);
|
|
|
|
g_free(msg1);
|
|
|
|
} else {
|
|
|
|
err->msg = msg1;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
err->err_class = err_class;
|
|
|
|
|
2014-01-02 06:46:59 +04:00
|
|
|
if (errp == &error_abort) {
|
2014-01-15 07:37:09 +04:00
|
|
|
fprintf(stderr, "%s\n", error_get_pretty(err));
|
2014-01-02 06:46:59 +04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2013-08-07 19:40:11 +04:00
|
|
|
*errp = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-12-05 22:04:05 +04: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-27 21:09:29 +04:00
|
|
|
err_new->err_class = err->err_class;
|
2011-12-05 22:04:05 +04:00
|
|
|
|
|
|
|
return err_new;
|
|
|
|
}
|
|
|
|
|
2011-06-01 21:14:49 +04:00
|
|
|
bool error_is_set(Error **errp)
|
|
|
|
{
|
|
|
|
return (errp && *errp);
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:29:38 +04:00
|
|
|
ErrorClass error_get_class(const Error *err)
|
|
|
|
{
|
|
|
|
return err->err_class;
|
|
|
|
}
|
|
|
|
|
2011-06-01 21:14:49 +04:00
|
|
|
const char *error_get_pretty(Error *err)
|
|
|
|
{
|
|
|
|
return err->msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void error_free(Error *err)
|
|
|
|
{
|
|
|
|
if (err) {
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(err->msg);
|
|
|
|
g_free(err);
|
2011-06-01 21:14:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void error_propagate(Error **dst_err, Error *local_err)
|
|
|
|
{
|
2014-01-02 06:46:59 +04:00
|
|
|
if (local_err && dst_err == &error_abort) {
|
2014-01-15 07:37:09 +04:00
|
|
|
fprintf(stderr, "%s\n", error_get_pretty(local_err));
|
2014-01-02 06:46:59 +04:00
|
|
|
abort();
|
|
|
|
} else if (dst_err && !*dst_err) {
|
2011-06-01 21:14:49 +04:00
|
|
|
*dst_err = local_err;
|
|
|
|
} else if (local_err) {
|
|
|
|
error_free(local_err);
|
|
|
|
}
|
|
|
|
}
|