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