4 * Copyright IBM, Corp. 2011
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 "qemu/error-report.h"
25 void error_set(Error
**errp
, ErrorClass err_class
, const char *fmt
, ...)
29 int saved_errno
= errno
;
34 assert(*errp
== NULL
);
36 err
= g_malloc0(sizeof(*err
));
39 err
->msg
= g_strdup_vprintf(fmt
, ap
);
41 err
->err_class
= err_class
;
43 if (errp
== &error_abort
) {
44 error_report_err(err
);
53 void error_set_errno(Error
**errp
, int os_errno
, ErrorClass err_class
,
59 int saved_errno
= errno
;
64 assert(*errp
== NULL
);
66 err
= g_malloc0(sizeof(*err
));
69 msg1
= g_strdup_vprintf(fmt
, ap
);
71 err
->msg
= g_strdup_printf("%s: %s", msg1
, strerror(os_errno
));
77 err
->err_class
= err_class
;
79 if (errp
== &error_abort
) {
80 error_report_err(err
);
89 void error_setg_file_open(Error
**errp
, int os_errno
, const char *filename
)
91 error_setg_errno(errp
, os_errno
, "Could not open '%s'", filename
);
96 void error_set_win32(Error
**errp
, int win32_err
, ErrorClass err_class
,
106 assert(*errp
== NULL
);
108 err
= g_malloc0(sizeof(*err
));
111 msg1
= g_strdup_vprintf(fmt
, ap
);
112 if (win32_err
!= 0) {
113 char *msg2
= g_win32_error_message(win32_err
);
114 err
->msg
= g_strdup_printf("%s: %s (error: %x)", msg1
, msg2
,
115 (unsigned)win32_err
);
122 err
->err_class
= err_class
;
124 if (errp
== &error_abort
) {
125 error_report_err(err
);
134 Error
*error_copy(const Error
*err
)
138 err_new
= g_malloc0(sizeof(*err
));
139 err_new
->msg
= g_strdup(err
->msg
);
140 err_new
->err_class
= err
->err_class
;
145 ErrorClass
error_get_class(const Error
*err
)
147 return err
->err_class
;
150 const char *error_get_pretty(Error
*err
)
155 void error_report_err(Error
*err
)
157 error_report("%s", error_get_pretty(err
));
161 void error_free(Error
*err
)
169 void error_propagate(Error
**dst_errp
, Error
*local_err
)
171 if (local_err
&& dst_errp
== &error_abort
) {
172 error_report_err(local_err
);
174 } else if (dst_errp
&& !*dst_errp
) {
175 *dst_errp
= local_err
;
176 } else if (local_err
) {
177 error_free(local_err
);