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