Statistics
| Branch: | Revision:

root / qerror.c @ 4d9a1a15

History | View | Annotate | Download (10 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_OPEN_FILE_FAILED,
129
        .desc      = "Could not open '%(filename)'",
130
    },
131
    {
132
        .error_fmt = QERR_PROPERTY_NOT_FOUND,
133
        .desc      = "Property '%(device).%(property)' not found",
134
    },
135
    {
136
        .error_fmt = QERR_PROPERTY_VALUE_BAD,
137
        .desc      = "Property '%(device).%(property)' doesn't take value '%(value)'",
138
    },
139
    {
140
        .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
141
        .desc      = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
142
    },
143
    {
144
        .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
145
        .desc      = "Property '%(device).%(property)' can't find value '%(value)'",
146
    },
147
    {
148
        .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
149
        .desc      = "Bad QMP input object",
150
    },
151
    {
152
        .error_fmt = QERR_SET_PASSWD_FAILED,
153
        .desc      = "Could not set password",
154
    },
155
    {
156
        .error_fmt = QERR_TOO_MANY_FILES,
157
        .desc      = "Too many open files",
158
    },
159
    {
160
        .error_fmt = QERR_UNDEFINED_ERROR,
161
        .desc      = "An undefined error has ocurred",
162
    },
163
    {
164
        .error_fmt = QERR_VNC_SERVER_FAILED,
165
        .desc      = "Could not start VNC server on %(target)",
166
    },
167
    {}
168
};
169

    
170
/**
171
 * qerror_new(): Create a new QError
172
 *
173
 * Return strong reference.
174
 */
175
QError *qerror_new(void)
176
{
177
    QError *qerr;
178

    
179
    qerr = qemu_mallocz(sizeof(*qerr));
180
    QOBJECT_INIT(qerr, &qerror_type);
181

    
182
    return qerr;
183
}
184

    
185
static void qerror_abort(const QError *qerr, const char *fmt, ...)
186
{
187
    va_list ap;
188

    
189
    fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
190
    fprintf(stderr, "qerror: -> ");
191

    
192
    va_start(ap, fmt);
193
    vfprintf(stderr, fmt, ap);
194
    va_end(ap);
195

    
196
    fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
197
    abort();
198
}
199

    
200
static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
201
{
202
    QObject *obj;
203

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

    
212
    qerr->error = qobject_to_qdict(obj);
213

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

    
231
static void qerror_set_desc(QError *qerr, const char *fmt)
232
{
233
    int i;
234

    
235
    // FIXME: inefficient loop
236

    
237
    for (i = 0; qerror_table[i].error_fmt; i++) {
238
        if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
239
            qerr->entry = &qerror_table[i];
240
            return;
241
        }
242
    }
243

    
244
    qerror_abort(qerr, "error format '%s' not found", fmt);
245
}
246

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

    
266
    qerr = qerror_new();
267
    loc_save(&qerr->loc);
268
    qerr->linenr = linenr;
269
    qerr->file = file;
270
    qerr->func = func;
271

    
272
    if (!fmt) {
273
        qerror_abort(qerr, "QDict not specified");
274
    }
275

    
276
    qerror_set_data(qerr, fmt, va);
277
    qerror_set_desc(qerr, fmt);
278

    
279
    return qerr;
280
}
281

    
282
static void parse_error(const QError *qerror, int c)
283
{
284
    qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
285
}
286

    
287
static const char *append_field(QString *outstr, const QError *qerror,
288
                                const char *start)
289
{
290
    QObject *obj;
291
    QDict *qdict;
292
    QString *key_qs;
293
    const char *end, *key;
294

    
295
    if (*start != '%')
296
        parse_error(qerror, '%');
297
    start++;
298
    if (*start != '(')
299
        parse_error(qerror, '(');
300
    start++;
301

    
302
    end = strchr(start, ')');
303
    if (!end)
304
        parse_error(qerror, ')');
305

    
306
    key_qs = qstring_from_substr(start, 0, end - start - 1);
307
    key = qstring_get_str(key_qs);
308

    
309
    qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
310
    obj = qdict_get(qdict, key);
311
    if (!obj) {
312
        qerror_abort(qerror, "key '%s' not found in QDict", key);
313
    }
314

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

    
326
    QDECREF(key_qs);
327
    return ++end;
328
}
329

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

    
340
    assert(qerror->entry != NULL);
341

    
342
    qstring = qstring_new();
343

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

    
355
    return qstring;
356
}
357

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

    
374
/**
375
 * qobject_to_qerror(): Convert a QObject into a QError
376
 */
377
QError *qobject_to_qerror(const QObject *obj)
378
{
379
    if (qobject_type(obj) != QTYPE_QERROR) {
380
        return NULL;
381
    }
382

    
383
    return container_of(obj, QError, base);
384
}
385

    
386
/**
387
 * qerror_destroy_obj(): Free all memory allocated by a QError
388
 */
389
static void qerror_destroy_obj(QObject *obj)
390
{
391
    QError *qerr;
392

    
393
    assert(obj != NULL);
394
    qerr = qobject_to_qerror(obj);
395

    
396
    QDECREF(qerr->error);
397
    qemu_free(qerr);
398
}