]> git.proxmox.com Git - qemu.git/blob - qapi/qmp-dispatch.c
qemu-ga: switch to the new error format on the wire
[qemu.git] / qapi / qmp-dispatch.c
1 /*
2 * Core Definitions for QAPI/QMP Dispatch
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.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 */
13
14 #include "qemu-objects.h"
15 #include "qapi/qmp-core.h"
16 #include "json-parser.h"
17 #include "qapi-types.h"
18 #include "error.h"
19 #include "error_int.h"
20 #include "qerror.h"
21
22 static QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp)
23 {
24 const QDictEntry *ent;
25 const char *arg_name;
26 const QObject *arg_obj;
27 bool has_exec_key = false;
28 QDict *dict = NULL;
29
30 if (qobject_type(request) != QTYPE_QDICT) {
31 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT,
32 "request is not a dictionary");
33 return NULL;
34 }
35
36 dict = qobject_to_qdict(request);
37
38 for (ent = qdict_first(dict); ent;
39 ent = qdict_next(dict, ent)) {
40 arg_name = qdict_entry_key(ent);
41 arg_obj = qdict_entry_value(ent);
42
43 if (!strcmp(arg_name, "execute")) {
44 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
45 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
46 "string");
47 return NULL;
48 }
49 has_exec_key = true;
50 } else if (strcmp(arg_name, "arguments")) {
51 error_set(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
52 return NULL;
53 }
54 }
55
56 if (!has_exec_key) {
57 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
58 return NULL;
59 }
60
61 return dict;
62 }
63
64 static QObject *do_qmp_dispatch(QObject *request, Error **errp)
65 {
66 const char *command;
67 QDict *args, *dict;
68 QmpCommand *cmd;
69 QObject *ret = NULL;
70
71
72 dict = qmp_dispatch_check_obj(request, errp);
73 if (!dict || error_is_set(errp)) {
74 return NULL;
75 }
76
77 command = qdict_get_str(dict, "execute");
78 cmd = qmp_find_command(command);
79 if (cmd == NULL) {
80 error_set(errp, QERR_COMMAND_NOT_FOUND, command);
81 return NULL;
82 }
83 if (!cmd->enabled) {
84 error_set(errp, QERR_COMMAND_DISABLED, command);
85 return NULL;
86 }
87
88 if (!qdict_haskey(dict, "arguments")) {
89 args = qdict_new();
90 } else {
91 args = qdict_get_qdict(dict, "arguments");
92 QINCREF(args);
93 }
94
95 switch (cmd->type) {
96 case QCT_NORMAL:
97 cmd->fn(args, &ret, errp);
98 if (!error_is_set(errp)) {
99 if (cmd->options & QCO_NO_SUCCESS_RESP) {
100 g_assert(!ret);
101 } else if (!ret) {
102 ret = QOBJECT(qdict_new());
103 }
104 }
105 break;
106 }
107
108 QDECREF(args);
109
110 return ret;
111 }
112
113 QObject *qmp_build_error_object(Error *errp)
114 {
115 return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
116 ErrorClass_lookup[error_get_class(errp)],
117 error_get_pretty(errp));
118 }
119
120 QObject *qmp_dispatch(QObject *request)
121 {
122 Error *err = NULL;
123 QObject *ret;
124 QDict *rsp;
125
126 ret = do_qmp_dispatch(request, &err);
127
128 rsp = qdict_new();
129 if (err) {
130 qdict_put_obj(rsp, "error", qmp_build_error_object(err));
131 error_free(err);
132 } else if (ret) {
133 qdict_put_obj(rsp, "return", ret);
134 } else {
135 QDECREF(rsp);
136 return NULL;
137 }
138
139 return QOBJECT(rsp);
140 }