Statistics
| Branch: | Revision:

root / qerror.c @ e16a1812

History | View | Annotate | Download (7.3 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 "sysemu.h"
16
#include "qemu-common.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      = "The %(device) is encrypted",
50
    },
51
    {
52
        .error_fmt = QERR_DEVICE_NOT_ACTIVE,
53
        .desc      = "The %(device) device has not been activated by the guest",
54
    },
55
    {
56
        .error_fmt = QERR_DEVICE_NOT_FOUND,
57
        .desc      = "The %(device) device has not been found",
58
    },
59
    {
60
        .error_fmt = QERR_INVALID_PARAMETER_TYPE,
61
        .desc      = "Invalid parameter type, expected: %(expected)",
62
    },
63
    {
64
        .error_fmt = QERR_INVALID_PASSWORD,
65
        .desc      = "The entered password is invalid",
66
    },
67
    {
68
        .error_fmt = QERR_JSON_PARSING,
69
        .desc      = "Invalid JSON syntax",
70
    },
71
    {
72
        .error_fmt = QERR_KVM_MISSING_CAP,
73
        .desc      = "Using KVM without %(capability), %(feature) unavailable",
74
    },
75
    {
76
        .error_fmt = QERR_MISSING_PARAMETER,
77
        .desc      = "Parameter %(name) is missing",
78
    },
79
    {
80
        .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
81
        .desc      = "Bad QMP input object",
82
    },
83
    {
84
        .error_fmt = QERR_UNDEFINED_ERROR,
85
        .desc      = "An undefined error has ocurred",
86
    },
87
    {}
88
};
89

    
90
/**
91
 * qerror_new(): Create a new QError
92
 *
93
 * Return strong reference.
94
 */
95
QError *qerror_new(void)
96
{
97
    QError *qerr;
98

    
99
    qerr = qemu_mallocz(sizeof(*qerr));
100
    QOBJECT_INIT(qerr, &qerror_type);
101

    
102
    return qerr;
103
}
104

    
105
static void qerror_abort(const QError *qerr, const char *fmt, ...)
106
{
107
    va_list ap;
108

    
109
    fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
110
    fprintf(stderr, "qerror: -> ");
111

    
112
    va_start(ap, fmt);
113
    vfprintf(stderr, fmt, ap);
114
    va_end(ap);
115

    
116
    fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
117
    abort();
118
}
119

    
120
static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
121
{
122
    QObject *obj;
123

    
124
    obj = qobject_from_jsonv(fmt, va);
125
    if (!obj) {
126
        qerror_abort(qerr, "invalid format '%s'", fmt);
127
    }
128
    if (qobject_type(obj) != QTYPE_QDICT) {
129
        qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
130
    }
131

    
132
    qerr->error = qobject_to_qdict(obj);
133

    
134
    obj = qdict_get(qerr->error, "class");
135
    if (!obj) {
136
        qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
137
    }
138
    if (qobject_type(obj) != QTYPE_QSTRING) {
139
        qerror_abort(qerr, "'class' key value should be a QString");
140
    }
141
    
142
    obj = qdict_get(qerr->error, "data");
143
    if (!obj) {
144
        qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
145
    }
146
    if (qobject_type(obj) != QTYPE_QDICT) {
147
        qerror_abort(qerr, "'data' key value should be a QDICT");
148
    }
149
}
150

    
151
static void qerror_set_desc(QError *qerr, const char *fmt)
152
{
153
    int i;
154

    
155
    // FIXME: inefficient loop
156

    
157
    for (i = 0; qerror_table[i].error_fmt; i++) {
158
        if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
159
            qerr->entry = &qerror_table[i];
160
            return;
161
        }
162
    }
163

    
164
    qerror_abort(qerr, "error format '%s' not found", fmt);
165
}
166

    
167
/**
168
 * qerror_from_info(): Create a new QError from error information
169
 *
170
 * The information consists of:
171
 *
172
 * - file   the file name of where the error occurred
173
 * - linenr the line number of where the error occurred
174
 * - func   the function name of where the error occurred
175
 * - fmt    JSON printf-like dictionary, there must exist keys 'class' and
176
 *          'data'
177
 * - va     va_list of all arguments specified by fmt
178
 *
179
 * Return strong reference.
180
 */
181
QError *qerror_from_info(const char *file, int linenr, const char *func,
182
                         const char *fmt, va_list *va)
183
{
184
    QError *qerr;
185

    
186
    qerr = qerror_new();
187
    qerr->linenr = linenr;
188
    qerr->file = file;
189
    qerr->func = func;
190

    
191
    if (!fmt) {
192
        qerror_abort(qerr, "QDict not specified");
193
    }
194

    
195
    qerror_set_data(qerr, fmt, va);
196
    qerror_set_desc(qerr, fmt);
197

    
198
    return qerr;
199
}
200

    
201
static void parse_error(const QError *qerror, int c)
202
{
203
    qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
204
}
205

    
206
static const char *append_field(QString *outstr, const QError *qerror,
207
                                const char *start)
208
{
209
    QObject *obj;
210
    QDict *qdict;
211
    QString *key_qs;
212
    const char *end, *key;
213

    
214
    if (*start != '%')
215
        parse_error(qerror, '%');
216
    start++;
217
    if (*start != '(')
218
        parse_error(qerror, '(');
219
    start++;
220

    
221
    end = strchr(start, ')');
222
    if (!end)
223
        parse_error(qerror, ')');
224

    
225
    key_qs = qstring_from_substr(start, 0, end - start - 1);
226
    key = qstring_get_str(key_qs);
227

    
228
    qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
229
    obj = qdict_get(qdict, key);
230
    if (!obj) {
231
        qerror_abort(qerror, "key '%s' not found in QDict", key);
232
    }
233

    
234
    switch (qobject_type(obj)) {
235
        case QTYPE_QSTRING:
236
            qstring_append(outstr, qdict_get_str(qdict, key));
237
            break;
238
        case QTYPE_QINT:
239
            qstring_append_int(outstr, qdict_get_int(qdict, key));
240
            break;
241
        default:
242
            qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
243
    }
244

    
245
    QDECREF(key_qs);
246
    return ++end;
247
}
248

    
249
/**
250
 * qerror_print(): Print QError data
251
 *
252
 * This function will print the member 'desc' of the specified QError object,
253
 * it uses qemu_error() for this, so that the output is routed to the right
254
 * place (ie. stderr or Monitor's device).
255
 */
256
void qerror_print(const QError *qerror)
257
{
258
    const char *p;
259
    QString *qstring;
260

    
261
    assert(qerror->entry != NULL);
262

    
263
    qstring = qstring_new();
264

    
265
    for (p = qerror->entry->desc; *p != '\0';) {
266
        if (*p != '%') {
267
            qstring_append_chr(qstring, *p++);
268
        } else if (*(p + 1) == '%') {
269
            qstring_append_chr(qstring, '%');
270
            p += 2;
271
        } else {
272
            p = append_field(qstring, qerror, p);
273
        }
274
    }
275

    
276
    qemu_error("%s\n", qstring_get_str(qstring));
277
    QDECREF(qstring);
278
}
279

    
280
/**
281
 * qobject_to_qerror(): Convert a QObject into a QError
282
 */
283
QError *qobject_to_qerror(const QObject *obj)
284
{
285
    if (qobject_type(obj) != QTYPE_QERROR) {
286
        return NULL;
287
    }
288

    
289
    return container_of(obj, QError, base);
290
}
291

    
292
/**
293
 * qerror_destroy_obj(): Free all memory allocated by a QError
294
 */
295
static void qerror_destroy_obj(QObject *obj)
296
{
297
    QError *qerr;
298

    
299
    assert(obj != NULL);
300
    qerr = qobject_to_qerror(obj);
301

    
302
    QDECREF(qerr->error);
303
    qemu_free(qerr);
304
}