Statistics
| Branch: | Revision:

root / qerror.c @ fc5469d8

History | View | Annotate | Download (8.7 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_COMMAND_NOT_FOUND,
45
        .desc      = "The command %(name) has not been found",
46
    },
47
    {
48
        .error_fmt = QERR_DEVICE_ENCRYPTED,
49
        .desc      = "Device '%(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      = "Device '%(device)' has not been activated by the guest",
58
    },
59
    {
60
        .error_fmt = QERR_DEVICE_NOT_FOUND,
61
        .desc      = "Device '%(device)' not 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      = "File descriptor named '%(name)' not found",
70
    },
71
    {
72
        .error_fmt = QERR_FD_NOT_SUPPLIED,
73
        .desc      = "No file descriptor supplied via SCM_RIGHTS",
74
    },
75
    {
76
        .error_fmt = QERR_INVALID_BLOCK_FORMAT,
77
        .desc      = "Invalid block format '%(name)'",
78
    },
79
    {
80
        .error_fmt = QERR_INVALID_PARAMETER,
81
        .desc      = "Invalid parameter '%(name)'",
82
    },
83
    {
84
        .error_fmt = QERR_INVALID_PARAMETER_TYPE,
85
        .desc      = "Invalid parameter type, expected: %(expected)",
86
    },
87
    {
88
        .error_fmt = QERR_INVALID_PASSWORD,
89
        .desc      = "Password incorrect",
90
    },
91
    {
92
        .error_fmt = QERR_JSON_PARSING,
93
        .desc      = "Invalid JSON syntax",
94
    },
95
    {
96
        .error_fmt = QERR_KVM_MISSING_CAP,
97
        .desc      = "Using KVM without %(capability), %(feature) unavailable",
98
    },
99
    {
100
        .error_fmt = QERR_MISSING_PARAMETER,
101
        .desc      = "Parameter '%(name)' is missing",
102
    },
103
    {
104
        .error_fmt = QERR_OPEN_FILE_FAILED,
105
        .desc      = "Could not open '%(filename)'",
106
    },
107
    {
108
        .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
109
        .desc      = "Bad QMP input object",
110
    },
111
    {
112
        .error_fmt = QERR_SET_PASSWD_FAILED,
113
        .desc      = "Could not set password",
114
    },
115
    {
116
        .error_fmt = QERR_TOO_MANY_FILES,
117
        .desc      = "Too many open files",
118
    },
119
    {
120
        .error_fmt = QERR_UNDEFINED_ERROR,
121
        .desc      = "An undefined error has ocurred",
122
    },
123
    {
124
        .error_fmt = QERR_VNC_SERVER_FAILED,
125
        .desc      = "Could not start VNC server on %(target)",
126
    },
127
    {}
128
};
129

    
130
/**
131
 * qerror_new(): Create a new QError
132
 *
133
 * Return strong reference.
134
 */
135
QError *qerror_new(void)
136
{
137
    QError *qerr;
138

    
139
    qerr = qemu_mallocz(sizeof(*qerr));
140
    QOBJECT_INIT(qerr, &qerror_type);
141

    
142
    return qerr;
143
}
144

    
145
static void qerror_abort(const QError *qerr, const char *fmt, ...)
146
{
147
    va_list ap;
148

    
149
    fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
150
    fprintf(stderr, "qerror: -> ");
151

    
152
    va_start(ap, fmt);
153
    vfprintf(stderr, fmt, ap);
154
    va_end(ap);
155

    
156
    fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
157
    abort();
158
}
159

    
160
static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
161
{
162
    QObject *obj;
163

    
164
    obj = qobject_from_jsonv(fmt, va);
165
    if (!obj) {
166
        qerror_abort(qerr, "invalid format '%s'", fmt);
167
    }
168
    if (qobject_type(obj) != QTYPE_QDICT) {
169
        qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
170
    }
171

    
172
    qerr->error = qobject_to_qdict(obj);
173

    
174
    obj = qdict_get(qerr->error, "class");
175
    if (!obj) {
176
        qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
177
    }
178
    if (qobject_type(obj) != QTYPE_QSTRING) {
179
        qerror_abort(qerr, "'class' key value should be a QString");
180
    }
181
    
182
    obj = qdict_get(qerr->error, "data");
183
    if (!obj) {
184
        qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
185
    }
186
    if (qobject_type(obj) != QTYPE_QDICT) {
187
        qerror_abort(qerr, "'data' key value should be a QDICT");
188
    }
189
}
190

    
191
static void qerror_set_desc(QError *qerr, const char *fmt)
192
{
193
    int i;
194

    
195
    // FIXME: inefficient loop
196

    
197
    for (i = 0; qerror_table[i].error_fmt; i++) {
198
        if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
199
            qerr->entry = &qerror_table[i];
200
            return;
201
        }
202
    }
203

    
204
    qerror_abort(qerr, "error format '%s' not found", fmt);
205
}
206

    
207
/**
208
 * qerror_from_info(): Create a new QError from error information
209
 *
210
 * The information consists of:
211
 *
212
 * - file   the file name of where the error occurred
213
 * - linenr the line number of where the error occurred
214
 * - func   the function name of where the error occurred
215
 * - fmt    JSON printf-like dictionary, there must exist keys 'class' and
216
 *          'data'
217
 * - va     va_list of all arguments specified by fmt
218
 *
219
 * Return strong reference.
220
 */
221
QError *qerror_from_info(const char *file, int linenr, const char *func,
222
                         const char *fmt, va_list *va)
223
{
224
    QError *qerr;
225

    
226
    qerr = qerror_new();
227
    loc_save(&qerr->loc);
228
    qerr->linenr = linenr;
229
    qerr->file = file;
230
    qerr->func = func;
231

    
232
    if (!fmt) {
233
        qerror_abort(qerr, "QDict not specified");
234
    }
235

    
236
    qerror_set_data(qerr, fmt, va);
237
    qerror_set_desc(qerr, fmt);
238

    
239
    return qerr;
240
}
241

    
242
static void parse_error(const QError *qerror, int c)
243
{
244
    qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
245
}
246

    
247
static const char *append_field(QString *outstr, const QError *qerror,
248
                                const char *start)
249
{
250
    QObject *obj;
251
    QDict *qdict;
252
    QString *key_qs;
253
    const char *end, *key;
254

    
255
    if (*start != '%')
256
        parse_error(qerror, '%');
257
    start++;
258
    if (*start != '(')
259
        parse_error(qerror, '(');
260
    start++;
261

    
262
    end = strchr(start, ')');
263
    if (!end)
264
        parse_error(qerror, ')');
265

    
266
    key_qs = qstring_from_substr(start, 0, end - start - 1);
267
    key = qstring_get_str(key_qs);
268

    
269
    qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
270
    obj = qdict_get(qdict, key);
271
    if (!obj) {
272
        qerror_abort(qerror, "key '%s' not found in QDict", key);
273
    }
274

    
275
    switch (qobject_type(obj)) {
276
        case QTYPE_QSTRING:
277
            qstring_append(outstr, qdict_get_str(qdict, key));
278
            break;
279
        case QTYPE_QINT:
280
            qstring_append_int(outstr, qdict_get_int(qdict, key));
281
            break;
282
        default:
283
            qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
284
    }
285

    
286
    QDECREF(key_qs);
287
    return ++end;
288
}
289

    
290
/**
291
 * qerror_human(): Format QError data into human-readable string.
292
 *
293
 * Formats according to member 'desc' of the specified QError object.
294
 */
295
QString *qerror_human(const QError *qerror)
296
{
297
    const char *p;
298
    QString *qstring;
299

    
300
    assert(qerror->entry != NULL);
301

    
302
    qstring = qstring_new();
303

    
304
    for (p = qerror->entry->desc; *p != '\0';) {
305
        if (*p != '%') {
306
            qstring_append_chr(qstring, *p++);
307
        } else if (*(p + 1) == '%') {
308
            qstring_append_chr(qstring, '%');
309
            p += 2;
310
        } else {
311
            p = append_field(qstring, qerror, p);
312
        }
313
    }
314

    
315
    return qstring;
316
}
317

    
318
/**
319
 * qerror_print(): Print QError data
320
 *
321
 * This function will print the member 'desc' of the specified QError object,
322
 * it uses error_report() for this, so that the output is routed to the right
323
 * place (ie. stderr or Monitor's device).
324
 */
325
void qerror_print(QError *qerror)
326
{
327
    QString *qstring = qerror_human(qerror);
328
    loc_push_restore(&qerror->loc);
329
    error_report("%s", qstring_get_str(qstring));
330
    loc_pop(&qerror->loc);
331
    QDECREF(qstring);
332
}
333

    
334
/**
335
 * qobject_to_qerror(): Convert a QObject into a QError
336
 */
337
QError *qobject_to_qerror(const QObject *obj)
338
{
339
    if (qobject_type(obj) != QTYPE_QERROR) {
340
        return NULL;
341
    }
342

    
343
    return container_of(obj, QError, base);
344
}
345

    
346
/**
347
 * qerror_destroy_obj(): Free all memory allocated by a QError
348
 */
349
static void qerror_destroy_obj(QObject *obj)
350
{
351
    QError *qerr;
352

    
353
    assert(obj != NULL);
354
    qerr = qobject_to_qerror(obj);
355

    
356
    QDECREF(qerr->error);
357
    qemu_free(qerr);
358
}