Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / util / error.c
blobf11f1d57a0ae0674aace7726d45a5ea807d67a2b
1 /*
2 * QEMU Error Objects
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2. See
10 * the COPYING.LIB file in the top-level directory.
13 #include "qemu-common.h"
14 #include "qapi/error.h"
15 #include "qapi/qmp/qjson.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi-types.h"
18 #include "qapi/qmp/qerror.h"
20 struct Error
22 char *msg;
23 ErrorClass err_class;
26 Error *error_abort;
28 void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
30 Error *err;
31 va_list ap;
32 int saved_errno = errno;
34 if (errp == NULL) {
35 return;
37 assert(*errp == NULL);
39 err = g_malloc0(sizeof(*err));
41 va_start(ap, fmt);
42 err->msg = g_strdup_vprintf(fmt, ap);
43 va_end(ap);
44 err->err_class = err_class;
46 if (errp == &error_abort) {
47 error_report("%s", error_get_pretty(err));
48 abort();
51 *errp = err;
53 errno = saved_errno;
56 void error_set_errno(Error **errp, int os_errno, ErrorClass err_class,
57 const char *fmt, ...)
59 Error *err;
60 char *msg1;
61 va_list ap;
62 int saved_errno = errno;
64 if (errp == NULL) {
65 return;
67 assert(*errp == NULL);
69 err = g_malloc0(sizeof(*err));
71 va_start(ap, fmt);
72 msg1 = g_strdup_vprintf(fmt, ap);
73 if (os_errno != 0) {
74 err->msg = g_strdup_printf("%s: %s", msg1, strerror(os_errno));
75 g_free(msg1);
76 } else {
77 err->msg = msg1;
79 va_end(ap);
80 err->err_class = err_class;
82 if (errp == &error_abort) {
83 error_report("%s", error_get_pretty(err));
84 abort();
87 *errp = err;
89 errno = saved_errno;
92 void error_setg_file_open(Error **errp, int os_errno, const char *filename)
94 error_setg_errno(errp, os_errno, "Could not open '%s'", filename);
97 #ifdef _WIN32
99 void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
100 const char *fmt, ...)
102 Error *err;
103 char *msg1;
104 va_list ap;
106 if (errp == NULL) {
107 return;
109 assert(*errp == NULL);
111 err = g_malloc0(sizeof(*err));
113 va_start(ap, fmt);
114 msg1 = g_strdup_vprintf(fmt, ap);
115 if (win32_err != 0) {
116 char *msg2 = g_win32_error_message(win32_err);
117 err->msg = g_strdup_printf("%s: %s (error: %x)", msg1, msg2,
118 (unsigned)win32_err);
119 g_free(msg2);
120 g_free(msg1);
121 } else {
122 err->msg = msg1;
124 va_end(ap);
125 err->err_class = err_class;
127 if (errp == &error_abort) {
128 error_report("%s", error_get_pretty(err));
129 abort();
132 *errp = err;
135 #endif
137 Error *error_copy(const Error *err)
139 Error *err_new;
141 err_new = g_malloc0(sizeof(*err));
142 err_new->msg = g_strdup(err->msg);
143 err_new->err_class = err->err_class;
145 return err_new;
148 bool error_is_set(Error **errp)
150 return (errp && *errp);
153 ErrorClass error_get_class(const Error *err)
155 return err->err_class;
158 const char *error_get_pretty(Error *err)
160 return err->msg;
163 void error_free(Error *err)
165 if (err) {
166 g_free(err->msg);
167 g_free(err);
171 void error_propagate(Error **dst_err, Error *local_err)
173 if (local_err && dst_err == &error_abort) {
174 error_report("%s", error_get_pretty(local_err));
175 abort();
176 } else if (dst_err && !*dst_err) {
177 *dst_err = local_err;
178 } else if (local_err) {
179 error_free(local_err);