Statistics
| Branch: | Revision:

root / qerror.c @ 25b28f01

History | View | Annotate | Download (10.2 kB)

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 "qemu-common.h"
16
#include "qemu-error.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_BAD_BUS_FOR_DEVICE,
45
        .desc      = "Device '%(device)' can't go on a %(bad_bus_type) bus",
46
    },
47
    {
48
        .error_fmt = QERR_BUS_NOT_FOUND,
49
        .desc      = "Bus '%(bus)' not found",
50
    },
51
    {
52
        .error_fmt = QERR_BUS_NO_HOTPLUG,
53
        .desc      = "Bus '%(bus)' does not support hotplugging",
54
    },
55
    {
56
        .error_fmt = QERR_COMMAND_NOT_FOUND,
57
        .desc      = "The command %(name) has not been found",
58
    },
59
    {
60
        .error_fmt = QERR_DEVICE_ENCRYPTED,
61
        .desc      = "Device '%(device)' is encrypted",
62
    },
63
    {
64
        .error_fmt = QERR_DEVICE_INIT_FAILED,
65
        .desc      = "Device '%(device)' could not be initialized",
66
    },
67
    {
68
        .error_fmt = QERR_DEVICE_NOT_ENCRYPTED,
69
        .desc      = "Device '%(device)' is not encrypted",
70
    },
71
    {
72
        .error_fmt = QERR_DEVICE_LOCKED,
73
        .desc      = "Device '%(device)' is locked",
74
    },
75
    {
76
        .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
77
        .desc      = "Device '%(device)' has multiple child busses",
78
    },
79
    {
80
        .error_fmt = QERR_DEVICE_NOT_ACTIVE,
81
        .desc      = "Device '%(device)' has not been activated by the guest",
82
    },
83
    {
84
        .error_fmt = QERR_DEVICE_NOT_FOUND,
85
        .desc      = "Device '%(device)' not found",
86
    },
87
    {
88
        .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
89
        .desc      = "Device '%(device)' is not removable",
90
    },
91
    {
92
        .error_fmt = QERR_DEVICE_NO_BUS,
93
        .desc      = "Device '%(device)' has no child bus",
94
    },
95
    {
96
        .error_fmt = QERR_FD_NOT_FOUND,
97
        .desc      = "File descriptor named '%(name)' not found",
98
    },
99
    {
100
        .error_fmt = QERR_FD_NOT_SUPPLIED,
101
        .desc      = "No file descriptor supplied via SCM_RIGHTS",
102
    },
103
    {
104
        .error_fmt = QERR_INVALID_BLOCK_FORMAT,
105
        .desc      = "Invalid block format '%(name)'",
106
    },
107
    {
108
        .error_fmt = QERR_INVALID_PARAMETER,
109
        .desc      = "Invalid parameter '%(name)'",
110
    },
111
    {
112
        .error_fmt = QERR_INVALID_PARAMETER_TYPE,
113
        .desc      = "Invalid parameter type, expected: %(expected)",
114
    },
115
    {
116
        .error_fmt = QERR_INVALID_PASSWORD,
117
        .desc      = "Password incorrect",
118
    },
119
    {
120
        .error_fmt = QERR_JSON_PARSING,
121
        .desc      = "Invalid JSON syntax",
122
    },
123
    {
124
        .error_fmt = QERR_KVM_MISSING_CAP,
125
        .desc      = "Using KVM without %(capability), %(feature) unavailable",
126
    },
127
    {
128
        .error_fmt = QERR_MISSING_PARAMETER,
129
        .desc      = "Parameter '%(name)' is missing",
130
    },
131
    {
132
        .error_fmt = QERR_NO_BUS_FOR_DEVICE,
133
        .desc      = "No '%(bus)' bus found for device '%(device)'",
134
    },
135
    {
136
        .error_fmt = QERR_OPEN_FILE_FAILED,
137
        .desc      = "Could not open '%(filename)'",
138
    },
139
    {
140
        .error_fmt = QERR_PROPERTY_NOT_FOUND,
141
        .desc      = "Property '%(device).%(property)' not found",
142
    },
143
    {
144
        .error_fmt = QERR_PROPERTY_VALUE_BAD,
145
        .desc      = "Property '%(device).%(property)' doesn't take value '%(value)'",
146
    },
147
    {
148
        .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
149
        .desc      = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
150
    },
151
    {
152
        .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
153
        .desc      = "Property '%(device).%(property)' can't find value '%(value)'",
154
    },
155
    {
156
        .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
157
        .desc      = "Bad QMP input object",
158
    },
159
    {
160
        .error_fmt = QERR_SET_PASSWD_FAILED,
161
        .desc      = "Could not set password",
162
    },
163
    {
164
        .error_fmt = QERR_TOO_MANY_FILES,
165
        .desc      = "Too many open files",
166
    },
167
    {
168
        .error_fmt = QERR_UNDEFINED_ERROR,
169
        .desc      = "An undefined error has ocurred",
170
    },
171
    {
172
        .error_fmt = QERR_VNC_SERVER_FAILED,
173
        .desc      = "Could not start VNC server on %(target)",
174
    },
175
    {}
176
};
177

    
178
/**
179
 * qerror_new(): Create a new QError
180
 *
181
 * Return strong reference.
182
 */
183
QError *qerror_new(void)
184
{
185
    QError *qerr;
186

    
187
    qerr = qemu_mallocz(sizeof(*qerr));
188
    QOBJECT_INIT(qerr, &qerror_type);
189

    
190
    return qerr;
191
}
192

    
193
static void qerror_abort(const QError *qerr, const char *fmt, ...)
194
{
195
    va_list ap;
196

    
197
    fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
198
    fprintf(stderr, "qerror: -> ");
199

    
200
    va_start(ap, fmt);
201
    vfprintf(stderr, fmt, ap);
202
    va_end(ap);
203

    
204
    fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
205
    abort();
206
}
207

    
208
static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
209
{
210
    QObject *obj;
211

    
212
    obj = qobject_from_jsonv(fmt, va);
213
    if (!obj) {
214
        qerror_abort(qerr, "invalid format '%s'", fmt);
215
    }
216
    if (qobject_type(obj) != QTYPE_QDICT) {
217
        qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
218
    }
219

    
220
    qerr->error = qobject_to_qdict(obj);
221

    
222
    obj = qdict_get(qerr->error, "class");
223
    if (!obj) {
224
        qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
225
    }
226
    if (qobject_type(obj) != QTYPE_QSTRING) {
227
        qerror_abort(qerr, "'class' key value should be a QString");
228
    }
229
    
230
    obj = qdict_get(qerr->error, "data");
231
    if (!obj) {
232
        qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
233
    }
234
    if (qobject_type(obj) != QTYPE_QDICT) {
235
        qerror_abort(qerr, "'data' key value should be a QDICT");
236
    }
237
}
238

    
239
static void qerror_set_desc(QError *qerr, const char *fmt)
240
{
241
    int i;
242

    
243
    // FIXME: inefficient loop
244

    
245
    for (i = 0; qerror_table[i].error_fmt; i++) {
246
        if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
247
            qerr->entry = &qerror_table[i];
248
            return;
249
        }
250
    }
251

    
252
    qerror_abort(qerr, "error format '%s' not found", fmt);
253
}
254

    
255
/**
256
 * qerror_from_info(): Create a new QError from error information
257
 *
258
 * The information consists of:
259
 *
260
 * - file   the file name of where the error occurred
261
 * - linenr the line number of where the error occurred
262
 * - func   the function name of where the error occurred
263
 * - fmt    JSON printf-like dictionary, there must exist keys 'class' and
264
 *          'data'
265
 * - va     va_list of all arguments specified by fmt
266
 *
267
 * Return strong reference.
268
 */
269
QError *qerror_from_info(const char *file, int linenr, const char *func,
270
                         const char *fmt, va_list *va)
271
{
272
    QError *qerr;
273

    
274
    qerr = qerror_new();
275
    loc_save(&qerr->loc);
276
    qerr->linenr = linenr;
277
    qerr->file = file;
278
    qerr->func = func;
279

    
280
    if (!fmt) {
281
        qerror_abort(qerr, "QDict not specified");
282
    }
283

    
284
    qerror_set_data(qerr, fmt, va);
285
    qerror_set_desc(qerr, fmt);
286

    
287
    return qerr;
288
}
289

    
290
static void parse_error(const QError *qerror, int c)
291
{
292
    qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
293
}
294

    
295
static const char *append_field(QString *outstr, const QError *qerror,
296
                                const char *start)
297
{
298
    QObject *obj;
299
    QDict *qdict;
300
    QString *key_qs;
301
    const char *end, *key;
302

    
303
    if (*start != '%')
304
        parse_error(qerror, '%');
305
    start++;
306
    if (*start != '(')
307
        parse_error(qerror, '(');
308
    start++;
309

    
310
    end = strchr(start, ')');
311
    if (!end)
312
        parse_error(qerror, ')');
313

    
314
    key_qs = qstring_from_substr(start, 0, end - start - 1);
315
    key = qstring_get_str(key_qs);
316

    
317
    qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
318
    obj = qdict_get(qdict, key);
319
    if (!obj) {
320
        qerror_abort(qerror, "key '%s' not found in QDict", key);
321
    }
322

    
323
    switch (qobject_type(obj)) {
324
        case QTYPE_QSTRING:
325
            qstring_append(outstr, qdict_get_str(qdict, key));
326
            break;
327
        case QTYPE_QINT:
328
            qstring_append_int(outstr, qdict_get_int(qdict, key));
329
            break;
330
        default:
331
            qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
332
    }
333

    
334
    QDECREF(key_qs);
335
    return ++end;
336
}
337

    
338
/**
339
 * qerror_human(): Format QError data into human-readable string.
340
 *
341
 * Formats according to member 'desc' of the specified QError object.
342
 */
343
QString *qerror_human(const QError *qerror)
344
{
345
    const char *p;
346
    QString *qstring;
347

    
348
    assert(qerror->entry != NULL);
349

    
350
    qstring = qstring_new();
351

    
352
    for (p = qerror->entry->desc; *p != '\0';) {
353
        if (*p != '%') {
354
            qstring_append_chr(qstring, *p++);
355
        } else if (*(p + 1) == '%') {
356
            qstring_append_chr(qstring, '%');
357
            p += 2;
358
        } else {
359
            p = append_field(qstring, qerror, p);
360
        }
361
    }
362

    
363
    return qstring;
364
}
365

    
366
/**
367
 * qerror_print(): Print QError data
368
 *
369
 * This function will print the member 'desc' of the specified QError object,
370
 * it uses error_report() for this, so that the output is routed to the right
371
 * place (ie. stderr or Monitor's device).
372
 */
373
void qerror_print(QError *qerror)
374
{
375
    QString *qstring = qerror_human(qerror);
376
    loc_push_restore(&qerror->loc);
377
    error_report("%s", qstring_get_str(qstring));
378
    loc_pop(&qerror->loc);
379
    QDECREF(qstring);
380
}
381

    
382
/**
383
 * qobject_to_qerror(): Convert a QObject into a QError
384
 */
385
QError *qobject_to_qerror(const QObject *obj)
386
{
387
    if (qobject_type(obj) != QTYPE_QERROR) {
388
        return NULL;
389
    }
390

    
391
    return container_of(obj, QError, base);
392
}
393

    
394
/**
395
 * qerror_destroy_obj(): Free all memory allocated by a QError
396
 */
397
static void qerror_destroy_obj(QObject *obj)
398
{
399
    QError *qerr;
400

    
401
    assert(obj != NULL);
402
    qerr = qobject_to_qerror(obj);
403

    
404
    QDECREF(qerr->error);
405
    qemu_free(qerr);
406
}