Statistics
| Branch: | Revision:

root / qerror.c @ 7bbd8237

History | View | Annotate | Download (9.4 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_BUS_NOT_FOUND,
45
        .desc      = "Bus '%(bus)' not found",
46
    },
47
    {
48
        .error_fmt = QERR_COMMAND_NOT_FOUND,
49
        .desc      = "The command %(name) has not been found",
50
    },
51
    {
52
        .error_fmt = QERR_DEVICE_ENCRYPTED,
53
        .desc      = "Device '%(device)' is encrypted",
54
    },
55
    {
56
        .error_fmt = QERR_DEVICE_LOCKED,
57
        .desc      = "Device '%(device)' is locked",
58
    },
59
    {
60
        .error_fmt = QERR_DEVICE_NOT_ACTIVE,
61
        .desc      = "Device '%(device)' has not been activated by the guest",
62
    },
63
    {
64
        .error_fmt = QERR_DEVICE_NOT_FOUND,
65
        .desc      = "Device '%(device)' not found",
66
    },
67
    {
68
        .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
69
        .desc      = "Device '%(device)' is not removable",
70
    },
71
    {
72
        .error_fmt = QERR_FD_NOT_FOUND,
73
        .desc      = "File descriptor named '%(name)' not found",
74
    },
75
    {
76
        .error_fmt = QERR_FD_NOT_SUPPLIED,
77
        .desc      = "No file descriptor supplied via SCM_RIGHTS",
78
    },
79
    {
80
        .error_fmt = QERR_INVALID_BLOCK_FORMAT,
81
        .desc      = "Invalid block format '%(name)'",
82
    },
83
    {
84
        .error_fmt = QERR_INVALID_PARAMETER,
85
        .desc      = "Invalid parameter '%(name)'",
86
    },
87
    {
88
        .error_fmt = QERR_INVALID_PARAMETER_TYPE,
89
        .desc      = "Invalid parameter type, expected: %(expected)",
90
    },
91
    {
92
        .error_fmt = QERR_INVALID_PASSWORD,
93
        .desc      = "Password incorrect",
94
    },
95
    {
96
        .error_fmt = QERR_JSON_PARSING,
97
        .desc      = "Invalid JSON syntax",
98
    },
99
    {
100
        .error_fmt = QERR_KVM_MISSING_CAP,
101
        .desc      = "Using KVM without %(capability), %(feature) unavailable",
102
    },
103
    {
104
        .error_fmt = QERR_MISSING_PARAMETER,
105
        .desc      = "Parameter '%(name)' is missing",
106
    },
107
    {
108
        .error_fmt = QERR_OPEN_FILE_FAILED,
109
        .desc      = "Could not open '%(filename)'",
110
    },
111
    {
112
        .error_fmt = QERR_PROPERTY_NOT_FOUND,
113
        .desc      = "Property '%(device).%(property)' not found",
114
    },
115
    {
116
        .error_fmt = QERR_PROPERTY_VALUE_BAD,
117
        .desc      = "Property '%(device).%(property)' doesn't take value '%(value)'",
118
    },
119
    {
120
        .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
121
        .desc      = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
122
    },
123
    {
124
        .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
125
        .desc      = "Property '%(device).%(property)' can't find value '%(value)'",
126
    },
127
    {
128
        .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
129
        .desc      = "Bad QMP input object",
130
    },
131
    {
132
        .error_fmt = QERR_SET_PASSWD_FAILED,
133
        .desc      = "Could not set password",
134
    },
135
    {
136
        .error_fmt = QERR_TOO_MANY_FILES,
137
        .desc      = "Too many open files",
138
    },
139
    {
140
        .error_fmt = QERR_UNDEFINED_ERROR,
141
        .desc      = "An undefined error has ocurred",
142
    },
143
    {
144
        .error_fmt = QERR_VNC_SERVER_FAILED,
145
        .desc      = "Could not start VNC server on %(target)",
146
    },
147
    {}
148
};
149

    
150
/**
151
 * qerror_new(): Create a new QError
152
 *
153
 * Return strong reference.
154
 */
155
QError *qerror_new(void)
156
{
157
    QError *qerr;
158

    
159
    qerr = qemu_mallocz(sizeof(*qerr));
160
    QOBJECT_INIT(qerr, &qerror_type);
161

    
162
    return qerr;
163
}
164

    
165
static void qerror_abort(const QError *qerr, const char *fmt, ...)
166
{
167
    va_list ap;
168

    
169
    fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
170
    fprintf(stderr, "qerror: -> ");
171

    
172
    va_start(ap, fmt);
173
    vfprintf(stderr, fmt, ap);
174
    va_end(ap);
175

    
176
    fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
177
    abort();
178
}
179

    
180
static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
181
{
182
    QObject *obj;
183

    
184
    obj = qobject_from_jsonv(fmt, va);
185
    if (!obj) {
186
        qerror_abort(qerr, "invalid format '%s'", fmt);
187
    }
188
    if (qobject_type(obj) != QTYPE_QDICT) {
189
        qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
190
    }
191

    
192
    qerr->error = qobject_to_qdict(obj);
193

    
194
    obj = qdict_get(qerr->error, "class");
195
    if (!obj) {
196
        qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
197
    }
198
    if (qobject_type(obj) != QTYPE_QSTRING) {
199
        qerror_abort(qerr, "'class' key value should be a QString");
200
    }
201
    
202
    obj = qdict_get(qerr->error, "data");
203
    if (!obj) {
204
        qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
205
    }
206
    if (qobject_type(obj) != QTYPE_QDICT) {
207
        qerror_abort(qerr, "'data' key value should be a QDICT");
208
    }
209
}
210

    
211
static void qerror_set_desc(QError *qerr, const char *fmt)
212
{
213
    int i;
214

    
215
    // FIXME: inefficient loop
216

    
217
    for (i = 0; qerror_table[i].error_fmt; i++) {
218
        if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
219
            qerr->entry = &qerror_table[i];
220
            return;
221
        }
222
    }
223

    
224
    qerror_abort(qerr, "error format '%s' not found", fmt);
225
}
226

    
227
/**
228
 * qerror_from_info(): Create a new QError from error information
229
 *
230
 * The information consists of:
231
 *
232
 * - file   the file name of where the error occurred
233
 * - linenr the line number of where the error occurred
234
 * - func   the function name of where the error occurred
235
 * - fmt    JSON printf-like dictionary, there must exist keys 'class' and
236
 *          'data'
237
 * - va     va_list of all arguments specified by fmt
238
 *
239
 * Return strong reference.
240
 */
241
QError *qerror_from_info(const char *file, int linenr, const char *func,
242
                         const char *fmt, va_list *va)
243
{
244
    QError *qerr;
245

    
246
    qerr = qerror_new();
247
    loc_save(&qerr->loc);
248
    qerr->linenr = linenr;
249
    qerr->file = file;
250
    qerr->func = func;
251

    
252
    if (!fmt) {
253
        qerror_abort(qerr, "QDict not specified");
254
    }
255

    
256
    qerror_set_data(qerr, fmt, va);
257
    qerror_set_desc(qerr, fmt);
258

    
259
    return qerr;
260
}
261

    
262
static void parse_error(const QError *qerror, int c)
263
{
264
    qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
265
}
266

    
267
static const char *append_field(QString *outstr, const QError *qerror,
268
                                const char *start)
269
{
270
    QObject *obj;
271
    QDict *qdict;
272
    QString *key_qs;
273
    const char *end, *key;
274

    
275
    if (*start != '%')
276
        parse_error(qerror, '%');
277
    start++;
278
    if (*start != '(')
279
        parse_error(qerror, '(');
280
    start++;
281

    
282
    end = strchr(start, ')');
283
    if (!end)
284
        parse_error(qerror, ')');
285

    
286
    key_qs = qstring_from_substr(start, 0, end - start - 1);
287
    key = qstring_get_str(key_qs);
288

    
289
    qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
290
    obj = qdict_get(qdict, key);
291
    if (!obj) {
292
        qerror_abort(qerror, "key '%s' not found in QDict", key);
293
    }
294

    
295
    switch (qobject_type(obj)) {
296
        case QTYPE_QSTRING:
297
            qstring_append(outstr, qdict_get_str(qdict, key));
298
            break;
299
        case QTYPE_QINT:
300
            qstring_append_int(outstr, qdict_get_int(qdict, key));
301
            break;
302
        default:
303
            qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
304
    }
305

    
306
    QDECREF(key_qs);
307
    return ++end;
308
}
309

    
310
/**
311
 * qerror_human(): Format QError data into human-readable string.
312
 *
313
 * Formats according to member 'desc' of the specified QError object.
314
 */
315
QString *qerror_human(const QError *qerror)
316
{
317
    const char *p;
318
    QString *qstring;
319

    
320
    assert(qerror->entry != NULL);
321

    
322
    qstring = qstring_new();
323

    
324
    for (p = qerror->entry->desc; *p != '\0';) {
325
        if (*p != '%') {
326
            qstring_append_chr(qstring, *p++);
327
        } else if (*(p + 1) == '%') {
328
            qstring_append_chr(qstring, '%');
329
            p += 2;
330
        } else {
331
            p = append_field(qstring, qerror, p);
332
        }
333
    }
334

    
335
    return qstring;
336
}
337

    
338
/**
339
 * qerror_print(): Print QError data
340
 *
341
 * This function will print the member 'desc' of the specified QError object,
342
 * it uses error_report() for this, so that the output is routed to the right
343
 * place (ie. stderr or Monitor's device).
344
 */
345
void qerror_print(QError *qerror)
346
{
347
    QString *qstring = qerror_human(qerror);
348
    loc_push_restore(&qerror->loc);
349
    error_report("%s", qstring_get_str(qstring));
350
    loc_pop(&qerror->loc);
351
    QDECREF(qstring);
352
}
353

    
354
/**
355
 * qobject_to_qerror(): Convert a QObject into a QError
356
 */
357
QError *qobject_to_qerror(const QObject *obj)
358
{
359
    if (qobject_type(obj) != QTYPE_QERROR) {
360
        return NULL;
361
    }
362

    
363
    return container_of(obj, QError, base);
364
}
365

    
366
/**
367
 * qerror_destroy_obj(): Free all memory allocated by a QError
368
 */
369
static void qerror_destroy_obj(QObject *obj)
370
{
371
    QError *qerr;
372

    
373
    assert(obj != NULL);
374
    qerr = qobject_to_qerror(obj);
375

    
376
    QDECREF(qerr->error);
377
    qemu_free(qerr);
378
}