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