]> git.proxmox.com Git - mirror_qemu.git/blob - qapi/qmp-dispatch.c
qmp: Use QDict * instead of QObject * for response objects
[mirror_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/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/dispatch.h"
17 #include "qapi/qmp/json-parser.h"
18 #include "qapi/qmp/qdict.h"
19 #include "qapi/qmp/qjson.h"
20 #include "qapi/qmp/qbool.h"
21 #include "sysemu/sysemu.h"
22
23 static QDict *qmp_dispatch_check_obj(const QObject *request, bool allow_oob,
24 Error **errp)
25 {
26 const char *exec_key = NULL;
27 const QDictEntry *ent;
28 const char *arg_name;
29 const QObject *arg_obj;
30 QDict *dict;
31
32 dict = qobject_to(QDict, request);
33 if (!dict) {
34 error_setg(errp, "QMP input must be a JSON object");
35 return NULL;
36 }
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 || (!strcmp(arg_name, "exec-oob") && allow_oob)) {
45 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
46 error_setg(errp, "QMP input member '%s' must be a string",
47 arg_name);
48 return NULL;
49 }
50 if (exec_key) {
51 error_setg(errp, "QMP input member '%s' clashes with '%s'",
52 arg_name, exec_key);
53 return NULL;
54 }
55 exec_key = arg_name;
56 } else if (!strcmp(arg_name, "arguments")) {
57 if (qobject_type(arg_obj) != QTYPE_QDICT) {
58 error_setg(errp,
59 "QMP input member 'arguments' must be an object");
60 return NULL;
61 }
62 } else {
63 error_setg(errp, "QMP input member '%s' is unexpected",
64 arg_name);
65 return NULL;
66 }
67 }
68
69 if (!exec_key) {
70 error_setg(errp, "QMP input lacks member 'execute'");
71 return NULL;
72 }
73
74 return dict;
75 }
76
77 static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request,
78 bool allow_oob, Error **errp)
79 {
80 Error *local_err = NULL;
81 bool oob;
82 const char *command;
83 QDict *args, *dict;
84 QmpCommand *cmd;
85 QObject *ret = NULL;
86
87 dict = qmp_dispatch_check_obj(request, allow_oob, errp);
88 if (!dict) {
89 return NULL;
90 }
91
92 command = qdict_get_try_str(dict, "execute");
93 oob = false;
94 if (!command) {
95 assert(allow_oob);
96 command = qdict_get_str(dict, "exec-oob");
97 oob = true;
98 }
99 cmd = qmp_find_command(cmds, command);
100 if (cmd == NULL) {
101 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
102 "The command %s has not been found", command);
103 return NULL;
104 }
105 if (!cmd->enabled) {
106 error_setg(errp, "The command %s has been disabled for this instance",
107 command);
108 return NULL;
109 }
110 if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
111 error_setg(errp, "The command %s does not support OOB",
112 command);
113 return false;
114 }
115
116 if (runstate_check(RUN_STATE_PRECONFIG) &&
117 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
118 error_setg(errp, "The command '%s' isn't permitted in '%s' state",
119 cmd->name, RunState_str(RUN_STATE_PRECONFIG));
120 return NULL;
121 }
122
123 if (!qdict_haskey(dict, "arguments")) {
124 args = qdict_new();
125 } else {
126 args = qdict_get_qdict(dict, "arguments");
127 qobject_ref(args);
128 }
129
130 cmd->fn(args, &ret, &local_err);
131 if (local_err) {
132 error_propagate(errp, local_err);
133 } else if (cmd->options & QCO_NO_SUCCESS_RESP) {
134 g_assert(!ret);
135 } else if (!ret) {
136 ret = QOBJECT(qdict_new());
137 }
138
139 qobject_unref(args);
140
141 return ret;
142 }
143
144 QDict *qmp_error_response(Error *err)
145 {
146 QDict *rsp;
147
148 rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
149 QapiErrorClass_str(error_get_class(err)),
150 error_get_pretty(err));
151 error_free(err);
152 return rsp;
153 }
154
155 /*
156 * Does @qdict look like a command to be run out-of-band?
157 */
158 bool qmp_is_oob(QDict *dict)
159 {
160 return qdict_haskey(dict, "exec-oob")
161 && !qdict_haskey(dict, "execute");
162 }
163
164 QDict *qmp_dispatch(QmpCommandList *cmds, QObject *request,
165 bool allow_oob)
166 {
167 Error *err = NULL;
168 QObject *ret;
169 QDict *rsp;
170
171 ret = do_qmp_dispatch(cmds, request, allow_oob, &err);
172
173 if (err) {
174 rsp = qmp_error_response(err);
175 } else if (ret) {
176 rsp = qdict_new();
177 qdict_put_obj(rsp, "return", ret);
178 } else {
179 rsp = NULL;
180 }
181
182 return rsp;
183 }