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