Statistics
| Branch: | Revision:

root / qerror.c @ fab5767f

History | View | Annotate | Download (10.1 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_LOCKED,
69
        .desc      = "Device '%(device)' is locked",
70
    },
71
    {
72
        .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
73
        .desc      = "Device '%(device)' has multiple child busses",
74
    },
75
    {
76
        .error_fmt = QERR_DEVICE_NOT_ACTIVE,
77
        .desc      = "Device '%(device)' has not been activated by the guest",
78
    },
79
    {
80
        .error_fmt = QERR_DEVICE_NOT_FOUND,
81
        .desc      = "Device '%(device)' not found",
82
    },
83
    {
84
        .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
85
        .desc      = "Device '%(device)' is not removable",
86
    },
87
    {
88
        .error_fmt = QERR_DEVICE_NO_BUS,
89
        .desc      = "Device '%(device)' has no child bus",
90
    },
91
    {
92
        .error_fmt = QERR_FD_NOT_FOUND,
93
        .desc      = "File descriptor named '%(name)' not found",
94
    },
95
    {
96
        .error_fmt = QERR_FD_NOT_SUPPLIED,
97
        .desc      = "No file descriptor supplied via SCM_RIGHTS",
98
    },
99
    {
100
        .error_fmt = QERR_INVALID_BLOCK_FORMAT,
101
        .desc      = "Invalid block format '%(name)'",
102
    },
103
    {
104
        .error_fmt = QERR_INVALID_PARAMETER,
105
        .desc      = "Invalid parameter '%(name)'",
106
    },
107
    {
108
        .error_fmt = QERR_INVALID_PARAMETER_TYPE,
109
        .desc      = "Invalid parameter type, expected: %(expected)",
110
    },
111
    {
112
        .error_fmt = QERR_INVALID_PASSWORD,
113
        .desc      = "Password incorrect",
114
    },
115
    {
116
        .error_fmt = QERR_JSON_PARSING,
117
        .desc      = "Invalid JSON syntax",
118
    },
119
    {
120
        .error_fmt = QERR_KVM_MISSING_CAP,
121
        .desc      = "Using KVM without %(capability), %(feature) unavailable",
122
    },
123
    {
124
        .error_fmt = QERR_MISSING_PARAMETER,
125
        .desc      = "Parameter '%(name)' is missing",
126
    },
127
    {
128
        .error_fmt = QERR_NO_BUS_FOR_DEVICE,
129
        .desc      = "No '%(bus)' bus found for device '%(device)'",
130
    },
131
    {
132
        .error_fmt = QERR_OPEN_FILE_FAILED,
133
        .desc      = "Could not open '%(filename)'",
134
    },
135
    {
136
        .error_fmt = QERR_PROPERTY_NOT_FOUND,
137
        .desc      = "Property '%(device).%(property)' not found",
138
    },
139
    {
140
        .error_fmt = QERR_PROPERTY_VALUE_BAD,
141
        .desc      = "Property '%(device).%(property)' doesn't take value '%(value)'",
142
    },
143
    {
144
        .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
145
        .desc      = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
146
    },
147
    {
148
        .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
149
        .desc      = "Property '%(device).%(property)' can't find value '%(value)'",
150
    },
151
    {
152
        .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
153
        .desc      = "Bad QMP input object",
154
    },
155
    {
156
        .error_fmt = QERR_SET_PASSWD_FAILED,
157
        .desc      = "Could not set password",
158
    },
159
    {
160
        .error_fmt = QERR_TOO_MANY_FILES,
161
        .desc      = "Too many open files",
162
    },
163
    {
164
        .error_fmt = QERR_UNDEFINED_ERROR,
165
        .desc      = "An undefined error has ocurred",
166
    },
167
    {
168
        .error_fmt = QERR_VNC_SERVER_FAILED,
169
        .desc      = "Could not start VNC server on %(target)",
170
    },
171
    {}
172
};
173

    
174
/**
175
 * qerror_new(): Create a new QError
176
 *
177
 * Return strong reference.
178
 */
179
QError *qerror_new(void)
180
{
181
    QError *qerr;
182

    
183
    qerr = qemu_mallocz(sizeof(*qerr));
184
    QOBJECT_INIT(qerr, &qerror_type);
185

    
186
    return qerr;
187
}
188

    
189
static void qerror_abort(const QError *qerr, const char *fmt, ...)
190
{
191
    va_list ap;
192

    
193
    fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
194
    fprintf(stderr, "qerror: -> ");
195

    
196
    va_start(ap, fmt);
197
    vfprintf(stderr, fmt, ap);
198
    va_end(ap);
199

    
200
    fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
201
    abort();
202
}
203

    
204
static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
205
{
206
    QObject *obj;
207

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

    
216
    qerr->error = qobject_to_qdict(obj);
217

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

    
235
static void qerror_set_desc(QError *qerr, const char *fmt)
236
{
237
    int i;
238

    
239
    // FIXME: inefficient loop
240

    
241
    for (i = 0; qerror_table[i].error_fmt; i++) {
242
        if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
243
            qerr->entry = &qerror_table[i];
244
            return;
245
        }
246
    }
247

    
248
    qerror_abort(qerr, "error format '%s' not found", fmt);
249
}
250

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

    
270
    qerr = qerror_new();
271
    loc_save(&qerr->loc);
272
    qerr->linenr = linenr;
273
    qerr->file = file;
274
    qerr->func = func;
275

    
276
    if (!fmt) {
277
        qerror_abort(qerr, "QDict not specified");
278
    }
279

    
280
    qerror_set_data(qerr, fmt, va);
281
    qerror_set_desc(qerr, fmt);
282

    
283
    return qerr;
284
}
285

    
286
static void parse_error(const QError *qerror, int c)
287
{
288
    qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
289
}
290

    
291
static const char *append_field(QString *outstr, const QError *qerror,
292
                                const char *start)
293
{
294
    QObject *obj;
295
    QDict *qdict;
296
    QString *key_qs;
297
    const char *end, *key;
298

    
299
    if (*start != '%')
300
        parse_error(qerror, '%');
301
    start++;
302
    if (*start != '(')
303
        parse_error(qerror, '(');
304
    start++;
305

    
306
    end = strchr(start, ')');
307
    if (!end)
308
        parse_error(qerror, ')');
309

    
310
    key_qs = qstring_from_substr(start, 0, end - start - 1);
311
    key = qstring_get_str(key_qs);
312

    
313
    qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
314
    obj = qdict_get(qdict, key);
315
    if (!obj) {
316
        qerror_abort(qerror, "key '%s' not found in QDict", key);
317
    }
318

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

    
330
    QDECREF(key_qs);
331
    return ++end;
332
}
333

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

    
344
    assert(qerror->entry != NULL);
345

    
346
    qstring = qstring_new();
347

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

    
359
    return qstring;
360
}
361

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

    
378
/**
379
 * qobject_to_qerror(): Convert a QObject into a QError
380
 */
381
QError *qobject_to_qerror(const QObject *obj)
382
{
383
    if (qobject_type(obj) != QTYPE_QERROR) {
384
        return NULL;
385
    }
386

    
387
    return container_of(obj, QError, base);
388
}
389

    
390
/**
391
 * qerror_destroy_obj(): Free all memory allocated by a QError
392
 */
393
static void qerror_destroy_obj(QObject *obj)
394
{
395
    QError *qerr;
396

    
397
    assert(obj != NULL);
398
    qerr = qobject_to_qerror(obj);
399

    
400
    QDECREF(qerr->error);
401
    qemu_free(qerr);
402
}