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