]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - qerror.c
qerror: add check-qerror.sh to verify alphabetical order
[lisovros/qemu_apohw.git] / qerror.c
1 /*
2  * QError Module
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 scripts/check-qerror.sh to check.
44  */
45 static const QErrorStringTable qerror_table[] = {
46     {
47         .error_fmt = QERR_BAD_BUS_FOR_DEVICE,
48         .desc      = "Device '%(device)' can't go on a %(bad_bus_type) bus",
49     },
50     {
51         .error_fmt = QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
52         .desc      = "Block format '%(format)' used by device '%(name)' does not support feature '%(feature)'",
53     },
54     {
55         .error_fmt = QERR_BUS_NOT_FOUND,
56         .desc      = "Bus '%(bus)' not found",
57     },
58     {
59         .error_fmt = QERR_BUS_NO_HOTPLUG,
60         .desc      = "Bus '%(bus)' does not support hotplugging",
61     },
62     {
63         .error_fmt = QERR_COMMAND_NOT_FOUND,
64         .desc      = "The command %(name) has not been found",
65     },
66     {
67         .error_fmt = QERR_COMMAND_DISABLED,
68         .desc      = "The command %(name) has been disabled for this instance",
69     },
70     {
71         .error_fmt = QERR_DEVICE_ENCRYPTED,
72         .desc      = "Device '%(device)' is encrypted",
73     },
74     {
75         .error_fmt = QERR_DEVICE_INIT_FAILED,
76         .desc      = "Device '%(device)' could not be initialized",
77     },
78     {
79         .error_fmt = QERR_DEVICE_IN_USE,
80         .desc      = "Device '%(device)' is in use",
81     },
82     {
83         .error_fmt = QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
84         .desc      = "Migration is disabled when using feature '%(feature)' in device '%(device)'",
85     },
86     {
87         .error_fmt = QERR_DEVICE_LOCKED,
88         .desc      = "Device '%(device)' is locked",
89     },
90     {
91         .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
92         .desc      = "Device '%(device)' has multiple child busses",
93     },
94     {
95         .error_fmt = QERR_DEVICE_NOT_ACTIVE,
96         .desc      = "Device '%(device)' has not been activated",
97     },
98     {
99         .error_fmt = QERR_DEVICE_NOT_ENCRYPTED,
100         .desc      = "Device '%(device)' is not encrypted",
101     },
102     {
103         .error_fmt = QERR_DEVICE_NOT_FOUND,
104         .desc      = "Device '%(device)' not found",
105     },
106     {
107         .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
108         .desc      = "Device '%(device)' is not removable",
109     },
110     {
111         .error_fmt = QERR_DEVICE_NO_BUS,
112         .desc      = "Device '%(device)' has no child bus",
113     },
114     {
115         .error_fmt = QERR_DEVICE_NO_HOTPLUG,
116         .desc      = "Device '%(device)' does not support hotplugging",
117     },
118     {
119         .error_fmt = QERR_DUPLICATE_ID,
120         .desc      = "Duplicate ID '%(id)' for %(object)",
121     },
122     {
123         .error_fmt = QERR_FD_NOT_FOUND,
124         .desc      = "File descriptor named '%(name)' not found",
125     },
126     {
127         .error_fmt = QERR_FD_NOT_SUPPLIED,
128         .desc      = "No file descriptor supplied via SCM_RIGHTS",
129     },
130     {
131         .error_fmt = QERR_FEATURE_DISABLED,
132         .desc      = "The feature '%(name)' is not enabled",
133     },
134     {
135         .error_fmt = QERR_INVALID_BLOCK_FORMAT,
136         .desc      = "Invalid block format '%(name)'",
137     },
138     {
139         .error_fmt = QERR_INVALID_PARAMETER,
140         .desc      = "Invalid parameter '%(name)'",
141     },
142     {
143         .error_fmt = QERR_INVALID_PARAMETER_TYPE,
144         .desc      = "Invalid parameter type, expected: %(expected)",
145     },
146     {
147         .error_fmt = QERR_INVALID_PARAMETER_VALUE,
148         .desc      = "Parameter '%(name)' expects %(expected)",
149     },
150     {
151         .error_fmt = QERR_INVALID_PASSWORD,
152         .desc      = "Password incorrect",
153     },
154     {
155         .error_fmt = QERR_IO_ERROR,
156         .desc      = "An IO error has occurred",
157     },
158     {
159         .error_fmt = QERR_JSON_PARSING,
160         .desc      = "Invalid JSON syntax",
161     },
162     {
163         .error_fmt = QERR_JSON_PARSE_ERROR,
164         .desc      = "JSON parse error, %(message)",
165
166     },
167     {
168         .error_fmt = QERR_KVM_MISSING_CAP,
169         .desc      = "Using KVM without %(capability), %(feature) unavailable",
170     },
171     {
172         .error_fmt = QERR_MIGRATION_EXPECTED,
173         .desc      = "An incoming migration is expected before this command can be executed",
174     },
175     {
176         .error_fmt = QERR_MISSING_PARAMETER,
177         .desc      = "Parameter '%(name)' is missing",
178     },
179     {
180         .error_fmt = QERR_NO_BUS_FOR_DEVICE,
181         .desc      = "No '%(bus)' bus found for device '%(device)'",
182     },
183     {
184         .error_fmt = QERR_OPEN_FILE_FAILED,
185         .desc      = "Could not open '%(filename)'",
186     },
187     {
188         .error_fmt = QERR_PERMISSION_DENIED,
189         .desc      = "Insufficient permission to perform this operation",
190     },
191     {
192         .error_fmt = QERR_PROPERTY_NOT_FOUND,
193         .desc      = "Property '%(device).%(property)' not found",
194     },
195     {
196         .error_fmt = QERR_PROPERTY_VALUE_BAD,
197         .desc      = "Property '%(device).%(property)' doesn't take value '%(value)'",
198     },
199     {
200         .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
201         .desc      = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
202     },
203     {
204         .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
205         .desc      = "Property '%(device).%(property)' can't find value '%(value)'",
206     },
207     {
208         .error_fmt = QERR_PROPERTY_VALUE_OUT_OF_RANGE,
209         .desc      = "Property '%(device).%(property)' doesn't take "
210                      "value %(value) (minimum: %(min), maximum: %(max)'",
211     },
212     {
213         .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
214         .desc      = "Expected '%(expected)' in QMP input",
215     },
216     {
217         .error_fmt = QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
218         .desc      = "QMP input object member '%(member)' expects '%(expected)'",
219     },
220     {
221         .error_fmt = QERR_QMP_EXTRA_MEMBER,
222         .desc      = "QMP input object member '%(member)' is unexpected",
223     },
224     {
225         .error_fmt = QERR_RESET_REQUIRED,
226         .desc      = "Resetting the Virtual Machine is required",
227     },
228     {
229         .error_fmt = QERR_SET_PASSWD_FAILED,
230         .desc      = "Could not set password",
231     },
232     {
233         .error_fmt = QERR_ADD_CLIENT_FAILED,
234         .desc      = "Could not add client",
235     },
236     {
237         .error_fmt = QERR_TOO_MANY_FILES,
238         .desc      = "Too many open files",
239     },
240     {
241         .error_fmt = QERR_UNDEFINED_ERROR,
242         .desc      = "An undefined error has occurred",
243     },
244     {
245         .error_fmt = QERR_UNSUPPORTED,
246         .desc      = "this feature or command is not currently supported",
247     },
248     {
249         .error_fmt = QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
250         .desc      = "'%(device)' uses a %(format) feature which is not "
251                      "supported by this qemu version: %(feature)",
252     },
253     {
254         .error_fmt = QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
255         .desc      = "Migration is disabled when VirtFS export path '%(path)' "
256                      "is mounted in the guest using mount_tag '%(tag)'",
257     },
258     {
259         .error_fmt = QERR_VNC_SERVER_FAILED,
260         .desc      = "Could not start VNC server on %(target)",
261     },
262     {
263         .error_fmt = QERR_QGA_LOGGING_FAILED,
264         .desc      = "Guest agent failed to log non-optional log statement",
265     },
266     {
267         .error_fmt = QERR_QGA_COMMAND_FAILED,
268         .desc      = "Guest agent command failed, error was '%(message)'",
269     },
270     {
271         .error_fmt = QERR_INVALID_PARAMETER_COMBINATION,
272         .desc      = "Invalid parameter combination",
273     },
274     {}
275 };
276
277 /**
278  * qerror_new(): Create a new QError
279  *
280  * Return strong reference.
281  */
282 QError *qerror_new(void)
283 {
284     QError *qerr;
285
286     qerr = g_malloc0(sizeof(*qerr));
287     QOBJECT_INIT(qerr, &qerror_type);
288
289     return qerr;
290 }
291
292 static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError *qerr,
293                                             const char *fmt, ...)
294 {
295     va_list ap;
296
297     fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
298     fprintf(stderr, "qerror: -> ");
299
300     va_start(ap, fmt);
301     vfprintf(stderr, fmt, ap);
302     va_end(ap);
303
304     fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
305     abort();
306 }
307
308 static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
309                                                const char *fmt, va_list *va)
310 {
311     QObject *obj;
312
313     obj = qobject_from_jsonv(fmt, va);
314     if (!obj) {
315         qerror_abort(qerr, "invalid format '%s'", fmt);
316     }
317     if (qobject_type(obj) != QTYPE_QDICT) {
318         qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
319     }
320
321     qerr->error = qobject_to_qdict(obj);
322
323     obj = qdict_get(qerr->error, "class");
324     if (!obj) {
325         qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
326     }
327     if (qobject_type(obj) != QTYPE_QSTRING) {
328         qerror_abort(qerr, "'class' key value should be a QString");
329     }
330     
331     obj = qdict_get(qerr->error, "data");
332     if (!obj) {
333         qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
334     }
335     if (qobject_type(obj) != QTYPE_QDICT) {
336         qerror_abort(qerr, "'data' key value should be a QDICT");
337     }
338 }
339
340 static void qerror_set_desc(QError *qerr, const char *fmt)
341 {
342     int i;
343
344     // FIXME: inefficient loop
345
346     for (i = 0; qerror_table[i].error_fmt; i++) {
347         if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
348             qerr->entry = &qerror_table[i];
349             return;
350         }
351     }
352
353     qerror_abort(qerr, "error format '%s' not found", fmt);
354 }
355
356 /**
357  * qerror_from_info(): Create a new QError from error information
358  *
359  * The information consists of:
360  *
361  * - file   the file name of where the error occurred
362  * - linenr the line number of where the error occurred
363  * - func   the function name of where the error occurred
364  * - fmt    JSON printf-like dictionary, there must exist keys 'class' and
365  *          'data'
366  * - va     va_list of all arguments specified by fmt
367  *
368  * Return strong reference.
369  */
370 QError *qerror_from_info(const char *file, int linenr, const char *func,
371                          const char *fmt, va_list *va)
372 {
373     QError *qerr;
374
375     qerr = qerror_new();
376     loc_save(&qerr->loc);
377     qerr->linenr = linenr;
378     qerr->file = file;
379     qerr->func = func;
380
381     if (!fmt) {
382         qerror_abort(qerr, "QDict not specified");
383     }
384
385     qerror_set_data(qerr, fmt, va);
386     qerror_set_desc(qerr, fmt);
387
388     return qerr;
389 }
390
391 static void parse_error(const QErrorStringTable *entry, int c)
392 {
393     fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
394     abort();
395 }
396
397 static const char *append_field(QDict *error, QString *outstr,
398                                 const QErrorStringTable *entry,
399                                 const char *start)
400 {
401     QObject *obj;
402     QDict *qdict;
403     QString *key_qs;
404     const char *end, *key;
405
406     if (*start != '%')
407         parse_error(entry, '%');
408     start++;
409     if (*start != '(')
410         parse_error(entry, '(');
411     start++;
412
413     end = strchr(start, ')');
414     if (!end)
415         parse_error(entry, ')');
416
417     key_qs = qstring_from_substr(start, 0, end - start - 1);
418     key = qstring_get_str(key_qs);
419
420     qdict = qobject_to_qdict(qdict_get(error, "data"));
421     obj = qdict_get(qdict, key);
422     if (!obj) {
423         abort();
424     }
425
426     switch (qobject_type(obj)) {
427         case QTYPE_QSTRING:
428             qstring_append(outstr, qdict_get_str(qdict, key));
429             break;
430         case QTYPE_QINT:
431             qstring_append_int(outstr, qdict_get_int(qdict, key));
432             break;
433         default:
434             abort();
435     }
436
437     QDECREF(key_qs);
438     return ++end;
439 }
440
441 static QString *qerror_format_desc(QDict *error,
442                                    const QErrorStringTable *entry)
443 {
444     QString *qstring;
445     const char *p;
446
447     assert(entry != NULL);
448
449     qstring = qstring_new();
450
451     for (p = entry->desc; *p != '\0';) {
452         if (*p != '%') {
453             qstring_append_chr(qstring, *p++);
454         } else if (*(p + 1) == '%') {
455             qstring_append_chr(qstring, '%');
456             p += 2;
457         } else {
458             p = append_field(error, qstring, entry, p);
459         }
460     }
461
462     return qstring;
463 }
464
465 QString *qerror_format(const char *fmt, QDict *error)
466 {
467     const QErrorStringTable *entry = NULL;
468     int i;
469
470     for (i = 0; qerror_table[i].error_fmt; i++) {
471         if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
472             entry = &qerror_table[i];
473             break;
474         }
475     }
476
477     return qerror_format_desc(error, entry);
478 }
479
480 /**
481  * qerror_human(): Format QError data into human-readable string.
482  */
483 QString *qerror_human(const QError *qerror)
484 {
485     return qerror_format_desc(qerror->error, qerror->entry);
486 }
487
488 /**
489  * qerror_print(): Print QError data
490  *
491  * This function will print the member 'desc' of the specified QError object,
492  * it uses error_report() for this, so that the output is routed to the right
493  * place (ie. stderr or Monitor's device).
494  */
495 void qerror_print(QError *qerror)
496 {
497     QString *qstring = qerror_human(qerror);
498     loc_push_restore(&qerror->loc);
499     error_report("%s", qstring_get_str(qstring));
500     loc_pop(&qerror->loc);
501     QDECREF(qstring);
502 }
503
504 void qerror_report_internal(const char *file, int linenr, const char *func,
505                             const char *fmt, ...)
506 {
507     va_list va;
508     QError *qerror;
509
510     va_start(va, fmt);
511     qerror = qerror_from_info(file, linenr, func, fmt, &va);
512     va_end(va);
513
514     if (monitor_cur_is_qmp()) {
515         monitor_set_error(cur_mon, qerror);
516     } else {
517         qerror_print(qerror);
518         QDECREF(qerror);
519     }
520 }
521
522 /* Evil... */
523 struct Error
524 {
525     QDict *obj;
526     const char *fmt;
527     char *msg;
528 };
529
530 void qerror_report_err(Error *err)
531 {
532     QError *qerr;
533     int i;
534
535     qerr = qerror_new();
536     loc_save(&qerr->loc);
537     QINCREF(err->obj);
538     qerr->error = err->obj;
539
540     for (i = 0; qerror_table[i].error_fmt; i++) {
541         if (strcmp(qerror_table[i].error_fmt, err->fmt) == 0) {
542             qerr->entry = &qerror_table[i];
543             break;
544         }
545     }
546
547     if (monitor_cur_is_qmp()) {
548         monitor_set_error(cur_mon, qerr);
549     } else {
550         qerror_print(qerr);
551         QDECREF(qerr);
552     }
553 }
554
555 /**
556  * qobject_to_qerror(): Convert a QObject into a QError
557  */
558 QError *qobject_to_qerror(const QObject *obj)
559 {
560     if (qobject_type(obj) != QTYPE_QERROR) {
561         return NULL;
562     }
563
564     return container_of(obj, QError, base);
565 }
566
567 /**
568  * qerror_destroy_obj(): Free all memory allocated by a QError
569  */
570 static void qerror_destroy_obj(QObject *obj)
571 {
572     QError *qerr;
573
574     assert(obj != NULL);
575     qerr = qobject_to_qerror(obj);
576
577     QDECREF(qerr->error);
578     g_free(qerr);
579 }