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