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