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