]> git.proxmox.com Git - mirror_qemu.git/blame - qerror.c
monitor: convert do_closefd() to QError
[mirror_qemu.git] / qerror.c
CommitLineData
9f9daf9a
LC
1/*
2 * QError: QEMU Error data-type.
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 */
12#include "qjson.h"
13#include "qerror.h"
14#include "qstring.h"
15#include "sysemu.h"
16#include "qemu-common.h"
17
18static void qerror_destroy_obj(QObject *obj);
19
20static const QType qerror_type = {
21 .code = QTYPE_QERROR,
22 .destroy = qerror_destroy_obj,
23};
24
25/**
26 * The 'desc' parameter is a printf-like string, the format of the format
27 * string is:
28 *
29 * %(KEY)
30 *
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
32 *
33 * Example:
34 *
35 * "foo error on device: %(device) slot: %(slot_nr)"
36 *
37 * A single percent sign can be printed if followed by a second one,
38 * for example:
39 *
40 * "running out of foo: %(foo)%%"
41 */
d05ac8fa 42static const QErrorStringTable qerror_table[] = {
4b9d4683 43 {
e16a1812
MA
44 .error_fmt = QERR_COMMAND_NOT_FOUND,
45 .desc = "The command %(name) has not been found",
4b9d4683 46 },
0df37c41
LC
47 {
48 .error_fmt = QERR_DEVICE_ENCRYPTED,
49 .desc = "The %(device) is encrypted",
50 },
b0868380
MA
51 {
52 .error_fmt = QERR_DEVICE_LOCKED,
53 .desc = "Device %(device) is locked",
54 },
e16a1812
MA
55 {
56 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
57 .desc = "The %(device) device has not been activated by the guest",
58 },
357b6156
LC
59 {
60 .error_fmt = QERR_DEVICE_NOT_FOUND,
61 .desc = "The %(device) device has not been found",
62 },
5cfe0264
MA
63 {
64 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
65 .desc = "Device %(device) is not removable",
66 },
c7c338c4
MA
67 {
68 .error_fmt = QERR_FD_NOT_FOUND,
69 .desc = "Failed to find file descriptor named %(name)",
70 },
17901e75
MA
71 {
72 .error_fmt = QERR_INVALID_BLOCK_FORMAT,
73 .desc = "Invalid block format %(name)",
74 },
055f6122 75 {
e16a1812
MA
76 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
77 .desc = "Invalid parameter type, expected: %(expected)",
055f6122 78 },
4b9d4683 79 {
e16a1812
MA
80 .error_fmt = QERR_INVALID_PASSWORD,
81 .desc = "The entered password is invalid",
4b9d4683 82 },
f6d855c5 83 {
e16a1812
MA
84 .error_fmt = QERR_JSON_PARSING,
85 .desc = "Invalid JSON syntax",
f6d855c5 86 },
82a60711
LC
87 {
88 .error_fmt = QERR_KVM_MISSING_CAP,
89 .desc = "Using KVM without %(capability), %(feature) unavailable",
90 },
4b9d4683
LC
91 {
92 .error_fmt = QERR_MISSING_PARAMETER,
93 .desc = "Parameter %(name) is missing",
94 },
95 {
96 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
97 .desc = "Bad QMP input object",
98 },
7a84cb23
MA
99 {
100 .error_fmt = QERR_SET_PASSWD_FAILED,
101 .desc = "Could not set password",
102 },
4b9d4683 103 {
e16a1812
MA
104 .error_fmt = QERR_UNDEFINED_ERROR,
105 .desc = "An undefined error has ocurred",
4b9d4683 106 },
a6906e31
MA
107 {
108 .error_fmt = QERR_VNC_SERVER_FAILED,
109 .desc = "Could not start VNC server on %(target)",
110 },
9f9daf9a
LC
111 {}
112};
113
114/**
115 * qerror_new(): Create a new QError
116 *
117 * Return strong reference.
118 */
119QError *qerror_new(void)
120{
121 QError *qerr;
122
123 qerr = qemu_mallocz(sizeof(*qerr));
124 QOBJECT_INIT(qerr, &qerror_type);
125
126 return qerr;
127}
128
129static void qerror_abort(const QError *qerr, const char *fmt, ...)
130{
131 va_list ap;
132
133 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
134 fprintf(stderr, "qerror: -> ");
135
136 va_start(ap, fmt);
137 vfprintf(stderr, fmt, ap);
138 va_end(ap);
139
140 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
141 abort();
142}
143
144static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
145{
146 QObject *obj;
147
148 obj = qobject_from_jsonv(fmt, va);
149 if (!obj) {
150 qerror_abort(qerr, "invalid format '%s'", fmt);
151 }
152 if (qobject_type(obj) != QTYPE_QDICT) {
153 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
154 }
155
156 qerr->error = qobject_to_qdict(obj);
157
158 obj = qdict_get(qerr->error, "class");
159 if (!obj) {
160 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
161 }
162 if (qobject_type(obj) != QTYPE_QSTRING) {
163 qerror_abort(qerr, "'class' key value should be a QString");
164 }
165
166 obj = qdict_get(qerr->error, "data");
167 if (!obj) {
168 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
169 }
170 if (qobject_type(obj) != QTYPE_QDICT) {
171 qerror_abort(qerr, "'data' key value should be a QDICT");
172 }
173}
174
175static void qerror_set_desc(QError *qerr, const char *fmt)
176{
177 int i;
178
179 // FIXME: inefficient loop
180
181 for (i = 0; qerror_table[i].error_fmt; i++) {
182 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
183 qerr->entry = &qerror_table[i];
184 return;
185 }
186 }
187
188 qerror_abort(qerr, "error format '%s' not found", fmt);
189}
190
191/**
192 * qerror_from_info(): Create a new QError from error information
193 *
194 * The information consists of:
195 *
196 * - file the file name of where the error occurred
197 * - linenr the line number of where the error occurred
198 * - func the function name of where the error occurred
199 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
200 * 'data'
201 * - va va_list of all arguments specified by fmt
202 *
203 * Return strong reference.
204 */
205QError *qerror_from_info(const char *file, int linenr, const char *func,
206 const char *fmt, va_list *va)
207{
208 QError *qerr;
209
210 qerr = qerror_new();
211 qerr->linenr = linenr;
212 qerr->file = file;
213 qerr->func = func;
214
215 if (!fmt) {
216 qerror_abort(qerr, "QDict not specified");
217 }
218
219 qerror_set_data(qerr, fmt, va);
220 qerror_set_desc(qerr, fmt);
221
222 return qerr;
223}
224
225static void parse_error(const QError *qerror, int c)
226{
227 qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
228}
229
230static const char *append_field(QString *outstr, const QError *qerror,
231 const char *start)
232{
233 QObject *obj;
234 QDict *qdict;
235 QString *key_qs;
236 const char *end, *key;
237
238 if (*start != '%')
239 parse_error(qerror, '%');
240 start++;
241 if (*start != '(')
242 parse_error(qerror, '(');
243 start++;
244
245 end = strchr(start, ')');
246 if (!end)
247 parse_error(qerror, ')');
248
249 key_qs = qstring_from_substr(start, 0, end - start - 1);
250 key = qstring_get_str(key_qs);
251
252 qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
253 obj = qdict_get(qdict, key);
254 if (!obj) {
255 qerror_abort(qerror, "key '%s' not found in QDict", key);
256 }
257
258 switch (qobject_type(obj)) {
259 case QTYPE_QSTRING:
260 qstring_append(outstr, qdict_get_str(qdict, key));
261 break;
262 case QTYPE_QINT:
263 qstring_append_int(outstr, qdict_get_int(qdict, key));
264 break;
265 default:
266 qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
267 }
268
269 QDECREF(key_qs);
270 return ++end;
271}
272
273/**
274 * qerror_print(): Print QError data
275 *
276 * This function will print the member 'desc' of the specified QError object,
277 * it uses qemu_error() for this, so that the output is routed to the right
278 * place (ie. stderr or Monitor's device).
279 */
280void qerror_print(const QError *qerror)
281{
282 const char *p;
283 QString *qstring;
284
285 assert(qerror->entry != NULL);
286
287 qstring = qstring_new();
288
289 for (p = qerror->entry->desc; *p != '\0';) {
290 if (*p != '%') {
291 qstring_append_chr(qstring, *p++);
292 } else if (*(p + 1) == '%') {
293 qstring_append_chr(qstring, '%');
294 p += 2;
295 } else {
296 p = append_field(qstring, qerror, p);
297 }
298 }
299
300 qemu_error("%s\n", qstring_get_str(qstring));
301 QDECREF(qstring);
302}
303
304/**
305 * qobject_to_qerror(): Convert a QObject into a QError
306 */
307QError *qobject_to_qerror(const QObject *obj)
308{
309 if (qobject_type(obj) != QTYPE_QERROR) {
310 return NULL;
311 }
312
313 return container_of(obj, QError, base);
314}
315
316/**
317 * qerror_destroy_obj(): Free all memory allocated by a QError
318 */
319static void qerror_destroy_obj(QObject *obj)
320{
321 QError *qerr;
322
323 assert(obj != NULL);
324 qerr = qobject_to_qerror(obj);
325
326 QDECREF(qerr->error);
327 qemu_free(qerr);
328}