ivshmem: Simplify how we cope with short reads from server
[qemu/ar7.git] / util / error.c
blob47f93afe5f628092f100fae9df1bb0afd1ab60d6
1 /*
2 * QEMU Error Objects
4 * Copyright IBM, Corp. 2011
5 * Copyright (C) 2011-2015 Red Hat, Inc.
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Markus Armbruster <armbru@redhat.com>,
11 * This work is licensed under the terms of the GNU LGPL, version 2. See
12 * the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qemu/error-report.h"
19 struct Error
21 char *msg;
22 ErrorClass err_class;
23 const char *src, *func;
24 int line;
25 GString *hint;
28 Error *error_abort;
29 Error *error_fatal;
31 static void error_handle_fatal(Error **errp, Error *err)
33 if (errp == &error_abort) {
34 fprintf(stderr, "Unexpected error in %s() at %s:%d:\n",
35 err->func, err->src, err->line);
36 error_report_err(err);
37 abort();
39 if (errp == &error_fatal) {
40 error_report_err(err);
41 exit(1);
45 static void error_setv(Error **errp,
46 const char *src, int line, const char *func,
47 ErrorClass err_class, const char *fmt, va_list ap,
48 const char *suffix)
50 Error *err;
51 int saved_errno = errno;
53 if (errp == NULL) {
54 return;
56 assert(*errp == NULL);
58 err = g_malloc0(sizeof(*err));
59 err->msg = g_strdup_vprintf(fmt, ap);
60 if (suffix) {
61 char *msg = err->msg;
62 err->msg = g_strdup_printf("%s: %s", msg, suffix);
63 g_free(msg);
65 err->err_class = err_class;
66 err->src = src;
67 err->line = line;
68 err->func = func;
70 error_handle_fatal(errp, err);
71 *errp = err;
73 errno = saved_errno;
76 void error_set_internal(Error **errp,
77 const char *src, int line, const char *func,
78 ErrorClass err_class, const char *fmt, ...)
80 va_list ap;
82 va_start(ap, fmt);
83 error_setv(errp, src, line, func, err_class, fmt, ap, NULL);
84 va_end(ap);
87 void error_setg_internal(Error **errp,
88 const char *src, int line, const char *func,
89 const char *fmt, ...)
91 va_list ap;
93 va_start(ap, fmt);
94 error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap, NULL);
95 va_end(ap);
98 void error_setg_errno_internal(Error **errp,
99 const char *src, int line, const char *func,
100 int os_errno, const char *fmt, ...)
102 va_list ap;
103 int saved_errno = errno;
105 if (errp == NULL) {
106 return;
109 va_start(ap, fmt);
110 error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap,
111 os_errno != 0 ? strerror(os_errno) : NULL);
112 va_end(ap);
114 errno = saved_errno;
117 void error_setg_file_open_internal(Error **errp,
118 const char *src, int line, const char *func,
119 int os_errno, const char *filename)
121 error_setg_errno_internal(errp, src, line, func, os_errno,
122 "Could not open '%s'", filename);
125 void error_vprepend(Error **errp, const char *fmt, va_list ap)
127 GString *newmsg;
129 if (!errp) {
130 return;
133 newmsg = g_string_new(NULL);
134 g_string_vprintf(newmsg, fmt, ap);
135 g_string_append(newmsg, (*errp)->msg);
136 (*errp)->msg = g_string_free(newmsg, 0);
139 void error_prepend(Error **errp, const char *fmt, ...)
141 va_list ap;
143 va_start(ap, fmt);
144 error_vprepend(errp, fmt, ap);
145 va_end(ap);
148 void error_append_hint(Error **errp, const char *fmt, ...)
150 va_list ap;
151 int saved_errno = errno;
152 Error *err;
154 if (!errp) {
155 return;
157 err = *errp;
158 assert(err && errp != &error_abort && errp != &error_fatal);
160 if (!err->hint) {
161 err->hint = g_string_new(NULL);
163 va_start(ap, fmt);
164 g_string_append_vprintf(err->hint, fmt, ap);
165 va_end(ap);
167 errno = saved_errno;
170 #ifdef _WIN32
172 void error_setg_win32_internal(Error **errp,
173 const char *src, int line, const char *func,
174 int win32_err, const char *fmt, ...)
176 va_list ap;
177 char *suffix = NULL;
179 if (errp == NULL) {
180 return;
183 if (win32_err != 0) {
184 suffix = g_win32_error_message(win32_err);
187 va_start(ap, fmt);
188 error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR,
189 fmt, ap, suffix);
190 va_end(ap);
192 g_free(suffix);
195 #endif
197 Error *error_copy(const Error *err)
199 Error *err_new;
201 err_new = g_malloc0(sizeof(*err));
202 err_new->msg = g_strdup(err->msg);
203 err_new->err_class = err->err_class;
204 err_new->src = err->src;
205 err_new->line = err->line;
206 err_new->func = err->func;
207 if (err->hint) {
208 err_new->hint = g_string_new(err->hint->str);
211 return err_new;
214 ErrorClass error_get_class(const Error *err)
216 return err->err_class;
219 const char *error_get_pretty(Error *err)
221 return err->msg;
224 void error_report_err(Error *err)
226 error_report("%s", error_get_pretty(err));
227 if (err->hint) {
228 error_printf_unless_qmp("%s", err->hint->str);
230 error_free(err);
233 void error_reportf_err(Error *err, const char *fmt, ...)
235 va_list ap;
237 va_start(ap, fmt);
238 error_vprepend(&err, fmt, ap);
239 va_end(ap);
240 error_report_err(err);
243 void error_free(Error *err)
245 if (err) {
246 g_free(err->msg);
247 if (err->hint) {
248 g_string_free(err->hint, true);
250 g_free(err);
254 void error_free_or_abort(Error **errp)
256 assert(errp && *errp);
257 error_free(*errp);
258 *errp = NULL;
261 void error_propagate(Error **dst_errp, Error *local_err)
263 if (!local_err) {
264 return;
266 error_handle_fatal(dst_errp, local_err);
267 if (dst_errp && !*dst_errp) {
268 *dst_errp = local_err;
269 } else {
270 error_free(local_err);