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