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