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