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