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