]>
Commit | Line | Data |
---|---|---|
9f9daf9a LC |
1 | /* |
2 | * QError: QEMU Error data-type. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <[email protected]> | |
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" | |
9f9daf9a LC |
14 | #include "qemu-common.h" |
15 | ||
16 | static void qerror_destroy_obj(QObject *obj); | |
17 | ||
18 | static const QType qerror_type = { | |
19 | .code = QTYPE_QERROR, | |
20 | .destroy = qerror_destroy_obj, | |
21 | }; | |
22 | ||
23 | /** | |
24 | * The 'desc' parameter is a printf-like string, the format of the format | |
25 | * string is: | |
26 | * | |
27 | * %(KEY) | |
28 | * | |
29 | * Where KEY is a QDict key, which has to be passed to qerror_from_info(). | |
30 | * | |
31 | * Example: | |
32 | * | |
33 | * "foo error on device: %(device) slot: %(slot_nr)" | |
34 | * | |
35 | * A single percent sign can be printed if followed by a second one, | |
36 | * for example: | |
37 | * | |
38 | * "running out of foo: %(foo)%%" | |
39 | */ | |
d05ac8fa | 40 | static const QErrorStringTable qerror_table[] = { |
56e9f563 MA |
41 | { |
42 | .error_fmt = QERR_BAD_BUS_FOR_DEVICE, | |
43 | .desc = "Device '%(device)' can't go on a %(bad_bus_type) bus", | |
44 | }, | |
7bbd8237 MA |
45 | { |
46 | .error_fmt = QERR_BUS_NOT_FOUND, | |
47 | .desc = "Bus '%(bus)' not found", | |
48 | }, | |
8eae73b5 MA |
49 | { |
50 | .error_fmt = QERR_BUS_NO_HOTPLUG, | |
51 | .desc = "Bus '%(bus)' does not support hotplugging", | |
52 | }, | |
4b9d4683 | 53 | { |
e16a1812 MA |
54 | .error_fmt = QERR_COMMAND_NOT_FOUND, |
55 | .desc = "The command %(name) has not been found", | |
4b9d4683 | 56 | }, |
0df37c41 LC |
57 | { |
58 | .error_fmt = QERR_DEVICE_ENCRYPTED, | |
fc5469d8 | 59 | .desc = "Device '%(device)' is encrypted", |
0df37c41 | 60 | }, |
4d9a1a15 MA |
61 | { |
62 | .error_fmt = QERR_DEVICE_INIT_FAILED, | |
63 | .desc = "Device '%(device)' could not be initialized", | |
64 | }, | |
25b28f01 SH |
65 | { |
66 | .error_fmt = QERR_DEVICE_NOT_ENCRYPTED, | |
67 | .desc = "Device '%(device)' is not encrypted", | |
68 | }, | |
b0868380 MA |
69 | { |
70 | .error_fmt = QERR_DEVICE_LOCKED, | |
fc5469d8 | 71 | .desc = "Device '%(device)' is locked", |
b0868380 | 72 | }, |
1ae78718 MA |
73 | { |
74 | .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES, | |
75 | .desc = "Device '%(device)' has multiple child busses", | |
76 | }, | |
e16a1812 MA |
77 | { |
78 | .error_fmt = QERR_DEVICE_NOT_ACTIVE, | |
fc5469d8 | 79 | .desc = "Device '%(device)' has not been activated by the guest", |
e16a1812 | 80 | }, |
357b6156 LC |
81 | { |
82 | .error_fmt = QERR_DEVICE_NOT_FOUND, | |
fc5469d8 | 83 | .desc = "Device '%(device)' not found", |
357b6156 | 84 | }, |
5cfe0264 MA |
85 | { |
86 | .error_fmt = QERR_DEVICE_NOT_REMOVABLE, | |
fc5469d8 | 87 | .desc = "Device '%(device)' is not removable", |
5cfe0264 | 88 | }, |
07574baf MA |
89 | { |
90 | .error_fmt = QERR_DEVICE_NO_BUS, | |
91 | .desc = "Device '%(device)' has no child bus", | |
92 | }, | |
c7c338c4 MA |
93 | { |
94 | .error_fmt = QERR_FD_NOT_FOUND, | |
fc5469d8 | 95 | .desc = "File descriptor named '%(name)' not found", |
c7c338c4 | 96 | }, |
41471a23 MA |
97 | { |
98 | .error_fmt = QERR_FD_NOT_SUPPLIED, | |
99 | .desc = "No file descriptor supplied via SCM_RIGHTS", | |
100 | }, | |
17901e75 MA |
101 | { |
102 | .error_fmt = QERR_INVALID_BLOCK_FORMAT, | |
fc5469d8 | 103 | .desc = "Invalid block format '%(name)'", |
17901e75 | 104 | }, |
7a046f5f MA |
105 | { |
106 | .error_fmt = QERR_INVALID_PARAMETER, | |
fc5469d8 | 107 | .desc = "Invalid parameter '%(name)'", |
7a046f5f | 108 | }, |
055f6122 | 109 | { |
e16a1812 MA |
110 | .error_fmt = QERR_INVALID_PARAMETER_TYPE, |
111 | .desc = "Invalid parameter type, expected: %(expected)", | |
055f6122 | 112 | }, |
4b9d4683 | 113 | { |
e16a1812 | 114 | .error_fmt = QERR_INVALID_PASSWORD, |
fc5469d8 | 115 | .desc = "Password incorrect", |
4b9d4683 | 116 | }, |
f6d855c5 | 117 | { |
e16a1812 MA |
118 | .error_fmt = QERR_JSON_PARSING, |
119 | .desc = "Invalid JSON syntax", | |
f6d855c5 | 120 | }, |
82a60711 LC |
121 | { |
122 | .error_fmt = QERR_KVM_MISSING_CAP, | |
123 | .desc = "Using KVM without %(capability), %(feature) unavailable", | |
124 | }, | |
4b9d4683 LC |
125 | { |
126 | .error_fmt = QERR_MISSING_PARAMETER, | |
fc5469d8 MA |
127 | .desc = "Parameter '%(name)' is missing", |
128 | }, | |
fab5767f MA |
129 | { |
130 | .error_fmt = QERR_NO_BUS_FOR_DEVICE, | |
131 | .desc = "No '%(bus)' bus found for device '%(device)'", | |
132 | }, | |
fc5469d8 MA |
133 | { |
134 | .error_fmt = QERR_OPEN_FILE_FAILED, | |
135 | .desc = "Could not open '%(filename)'", | |
4b9d4683 | 136 | }, |
c58a35f8 MA |
137 | { |
138 | .error_fmt = QERR_PROPERTY_NOT_FOUND, | |
139 | .desc = "Property '%(device).%(property)' not found", | |
140 | }, | |
06b4a703 MA |
141 | { |
142 | .error_fmt = QERR_PROPERTY_VALUE_BAD, | |
143 | .desc = "Property '%(device).%(property)' doesn't take value '%(value)'", | |
144 | }, | |
9c5eff95 MA |
145 | { |
146 | .error_fmt = QERR_PROPERTY_VALUE_IN_USE, | |
147 | .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use", | |
148 | }, | |
84745d68 MA |
149 | { |
150 | .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND, | |
151 | .desc = "Property '%(device).%(property)' can't find value '%(value)'", | |
152 | }, | |
4b9d4683 LC |
153 | { |
154 | .error_fmt = QERR_QMP_BAD_INPUT_OBJECT, | |
155 | .desc = "Bad QMP input object", | |
156 | }, | |
7a84cb23 MA |
157 | { |
158 | .error_fmt = QERR_SET_PASSWD_FAILED, | |
159 | .desc = "Could not set password", | |
160 | }, | |
a488be27 MA |
161 | { |
162 | .error_fmt = QERR_TOO_MANY_FILES, | |
163 | .desc = "Too many open files", | |
164 | }, | |
4b9d4683 | 165 | { |
e16a1812 MA |
166 | .error_fmt = QERR_UNDEFINED_ERROR, |
167 | .desc = "An undefined error has ocurred", | |
4b9d4683 | 168 | }, |
a6906e31 MA |
169 | { |
170 | .error_fmt = QERR_VNC_SERVER_FAILED, | |
171 | .desc = "Could not start VNC server on %(target)", | |
172 | }, | |
9f9daf9a LC |
173 | {} |
174 | }; | |
175 | ||
176 | /** | |
177 | * qerror_new(): Create a new QError | |
178 | * | |
179 | * Return strong reference. | |
180 | */ | |
181 | QError *qerror_new(void) | |
182 | { | |
183 | QError *qerr; | |
184 | ||
185 | qerr = qemu_mallocz(sizeof(*qerr)); | |
186 | QOBJECT_INIT(qerr, &qerror_type); | |
187 | ||
188 | return qerr; | |
189 | } | |
190 | ||
191 | static void qerror_abort(const QError *qerr, const char *fmt, ...) | |
192 | { | |
193 | va_list ap; | |
194 | ||
195 | fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func); | |
196 | fprintf(stderr, "qerror: -> "); | |
197 | ||
198 | va_start(ap, fmt); | |
199 | vfprintf(stderr, fmt, ap); | |
200 | va_end(ap); | |
201 | ||
202 | fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr); | |
203 | abort(); | |
204 | } | |
205 | ||
206 | static void qerror_set_data(QError *qerr, const char *fmt, va_list *va) | |
207 | { | |
208 | QObject *obj; | |
209 | ||
210 | obj = qobject_from_jsonv(fmt, va); | |
211 | if (!obj) { | |
212 | qerror_abort(qerr, "invalid format '%s'", fmt); | |
213 | } | |
214 | if (qobject_type(obj) != QTYPE_QDICT) { | |
215 | qerror_abort(qerr, "error format is not a QDict '%s'", fmt); | |
216 | } | |
217 | ||
218 | qerr->error = qobject_to_qdict(obj); | |
219 | ||
220 | obj = qdict_get(qerr->error, "class"); | |
221 | if (!obj) { | |
222 | qerror_abort(qerr, "missing 'class' key in '%s'", fmt); | |
223 | } | |
224 | if (qobject_type(obj) != QTYPE_QSTRING) { | |
225 | qerror_abort(qerr, "'class' key value should be a QString"); | |
226 | } | |
227 | ||
228 | obj = qdict_get(qerr->error, "data"); | |
229 | if (!obj) { | |
230 | qerror_abort(qerr, "missing 'data' key in '%s'", fmt); | |
231 | } | |
232 | if (qobject_type(obj) != QTYPE_QDICT) { | |
233 | qerror_abort(qerr, "'data' key value should be a QDICT"); | |
234 | } | |
235 | } | |
236 | ||
237 | static void qerror_set_desc(QError *qerr, const char *fmt) | |
238 | { | |
239 | int i; | |
240 | ||
241 | // FIXME: inefficient loop | |
242 | ||
243 | for (i = 0; qerror_table[i].error_fmt; i++) { | |
244 | if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { | |
245 | qerr->entry = &qerror_table[i]; | |
246 | return; | |
247 | } | |
248 | } | |
249 | ||
250 | qerror_abort(qerr, "error format '%s' not found", fmt); | |
251 | } | |
252 | ||
253 | /** | |
254 | * qerror_from_info(): Create a new QError from error information | |
255 | * | |
256 | * The information consists of: | |
257 | * | |
258 | * - file the file name of where the error occurred | |
259 | * - linenr the line number of where the error occurred | |
260 | * - func the function name of where the error occurred | |
261 | * - fmt JSON printf-like dictionary, there must exist keys 'class' and | |
262 | * 'data' | |
263 | * - va va_list of all arguments specified by fmt | |
264 | * | |
265 | * Return strong reference. | |
266 | */ | |
267 | QError *qerror_from_info(const char *file, int linenr, const char *func, | |
268 | const char *fmt, va_list *va) | |
269 | { | |
270 | QError *qerr; | |
271 | ||
272 | qerr = qerror_new(); | |
827b0813 | 273 | loc_save(&qerr->loc); |
9f9daf9a LC |
274 | qerr->linenr = linenr; |
275 | qerr->file = file; | |
276 | qerr->func = func; | |
277 | ||
278 | if (!fmt) { | |
279 | qerror_abort(qerr, "QDict not specified"); | |
280 | } | |
281 | ||
282 | qerror_set_data(qerr, fmt, va); | |
283 | qerror_set_desc(qerr, fmt); | |
284 | ||
285 | return qerr; | |
286 | } | |
287 | ||
288 | static void parse_error(const QError *qerror, int c) | |
289 | { | |
290 | qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc); | |
291 | } | |
292 | ||
293 | static const char *append_field(QString *outstr, const QError *qerror, | |
294 | const char *start) | |
295 | { | |
296 | QObject *obj; | |
297 | QDict *qdict; | |
298 | QString *key_qs; | |
299 | const char *end, *key; | |
300 | ||
301 | if (*start != '%') | |
302 | parse_error(qerror, '%'); | |
303 | start++; | |
304 | if (*start != '(') | |
305 | parse_error(qerror, '('); | |
306 | start++; | |
307 | ||
308 | end = strchr(start, ')'); | |
309 | if (!end) | |
310 | parse_error(qerror, ')'); | |
311 | ||
312 | key_qs = qstring_from_substr(start, 0, end - start - 1); | |
313 | key = qstring_get_str(key_qs); | |
314 | ||
315 | qdict = qobject_to_qdict(qdict_get(qerror->error, "data")); | |
316 | obj = qdict_get(qdict, key); | |
317 | if (!obj) { | |
318 | qerror_abort(qerror, "key '%s' not found in QDict", key); | |
319 | } | |
320 | ||
321 | switch (qobject_type(obj)) { | |
322 | case QTYPE_QSTRING: | |
323 | qstring_append(outstr, qdict_get_str(qdict, key)); | |
324 | break; | |
325 | case QTYPE_QINT: | |
326 | qstring_append_int(outstr, qdict_get_int(qdict, key)); | |
327 | break; | |
328 | default: | |
329 | qerror_abort(qerror, "invalid type '%c'", qobject_type(obj)); | |
330 | } | |
331 | ||
332 | QDECREF(key_qs); | |
333 | return ++end; | |
334 | } | |
335 | ||
336 | /** | |
77e595e7 | 337 | * qerror_human(): Format QError data into human-readable string. |
9f9daf9a | 338 | * |
77e595e7 | 339 | * Formats according to member 'desc' of the specified QError object. |
9f9daf9a | 340 | */ |
77e595e7 | 341 | QString *qerror_human(const QError *qerror) |
9f9daf9a LC |
342 | { |
343 | const char *p; | |
344 | QString *qstring; | |
345 | ||
346 | assert(qerror->entry != NULL); | |
347 | ||
348 | qstring = qstring_new(); | |
349 | ||
350 | for (p = qerror->entry->desc; *p != '\0';) { | |
351 | if (*p != '%') { | |
352 | qstring_append_chr(qstring, *p++); | |
353 | } else if (*(p + 1) == '%') { | |
354 | qstring_append_chr(qstring, '%'); | |
355 | p += 2; | |
356 | } else { | |
357 | p = append_field(qstring, qerror, p); | |
358 | } | |
359 | } | |
360 | ||
77e595e7 MA |
361 | return qstring; |
362 | } | |
363 | ||
364 | /** | |
365 | * qerror_print(): Print QError data | |
366 | * | |
367 | * This function will print the member 'desc' of the specified QError object, | |
1ecda02b | 368 | * it uses error_report() for this, so that the output is routed to the right |
77e595e7 MA |
369 | * place (ie. stderr or Monitor's device). |
370 | */ | |
827b0813 | 371 | void qerror_print(QError *qerror) |
77e595e7 MA |
372 | { |
373 | QString *qstring = qerror_human(qerror); | |
827b0813 | 374 | loc_push_restore(&qerror->loc); |
1ecda02b | 375 | error_report("%s", qstring_get_str(qstring)); |
827b0813 | 376 | loc_pop(&qerror->loc); |
9f9daf9a LC |
377 | QDECREF(qstring); |
378 | } | |
379 | ||
380 | /** | |
381 | * qobject_to_qerror(): Convert a QObject into a QError | |
382 | */ | |
383 | QError *qobject_to_qerror(const QObject *obj) | |
384 | { | |
385 | if (qobject_type(obj) != QTYPE_QERROR) { | |
386 | return NULL; | |
387 | } | |
388 | ||
389 | return container_of(obj, QError, base); | |
390 | } | |
391 | ||
392 | /** | |
393 | * qerror_destroy_obj(): Free all memory allocated by a QError | |
394 | */ | |
395 | static void qerror_destroy_obj(QObject *obj) | |
396 | { | |
397 | QError *qerr; | |
398 | ||
399 | assert(obj != NULL); | |
400 | qerr = qobject_to_qerror(obj); | |
401 | ||
402 | QDECREF(qerr->error); | |
403 | qemu_free(qerr); | |
404 | } |