]> git.proxmox.com Git - mirror_qemu.git/blob - qapi/qmp-dispatch.c
cli: add --preconfig option
[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 QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp)
24 {
25 const QDictEntry *ent;
26 const char *arg_name;
27 const QObject *arg_obj;
28 bool has_exec_key = false;
29 QDict *dict = NULL;
30
31 dict = qobject_to(QDict, request);
32 if (!dict) {
33 error_setg(errp, "QMP input must be a JSON object");
34 return NULL;
35 }
36
37 for (ent = qdict_first(dict); ent;
38 ent = qdict_next(dict, ent)) {
39 arg_name = qdict_entry_key(ent);
40 arg_obj = qdict_entry_value(ent);
41
42 if (!strcmp(arg_name, "execute")) {
43 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
44 error_setg(errp,
45 "QMP input member 'execute' must be a string");
46 return NULL;
47 }
48 has_exec_key = true;
49 } else if (!strcmp(arg_name, "arguments")) {
50 if (qobject_type(arg_obj) != QTYPE_QDICT) {
51 error_setg(errp,
52 "QMP input member 'arguments' must be an object");
53 return NULL;
54 }
55 } else if (!strcmp(arg_name, "id")) {
56 continue;
57 } else if (!strcmp(arg_name, "control")) {
58 if (qobject_type(arg_obj) != QTYPE_QDICT) {
59 error_setg(errp,
60 "QMP input member 'control' must be a dict");
61 return NULL;
62 }
63 } else {
64 error_setg(errp, "QMP input member '%s' is unexpected",
65 arg_name);
66 return NULL;
67 }
68 }
69
70 if (!has_exec_key) {
71 error_setg(errp, "QMP input lacks member 'execute'");
72 return NULL;
73 }
74
75 return dict;
76 }
77
78 static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request,
79 Error **errp)
80 {
81 Error *local_err = NULL;
82 const char *command;
83 QDict *args, *dict;
84 QmpCommand *cmd;
85 QObject *ret = NULL;
86
87 dict = qmp_dispatch_check_obj(request, errp);
88 if (!dict) {
89 return NULL;
90 }
91
92 command = qdict_get_str(dict, "execute");
93 cmd = qmp_find_command(cmds, command);
94 if (cmd == NULL) {
95 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
96 "The command %s has not been found", command);
97 return NULL;
98 }
99 if (!cmd->enabled) {
100 error_setg(errp, "The command %s has been disabled for this instance",
101 command);
102 return NULL;
103 }
104
105 if (runstate_check(RUN_STATE_PRECONFIG) &&
106 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
107 error_setg(errp, "The command '%s' isn't permitted in '%s' state",
108 cmd->name, RunState_str(RUN_STATE_PRECONFIG));
109 return NULL;
110 }
111
112 if (!qdict_haskey(dict, "arguments")) {
113 args = qdict_new();
114 } else {
115 args = qdict_get_qdict(dict, "arguments");
116 qobject_ref(args);
117 }
118
119 cmd->fn(args, &ret, &local_err);
120 if (local_err) {
121 error_propagate(errp, local_err);
122 } else if (cmd->options & QCO_NO_SUCCESS_RESP) {
123 g_assert(!ret);
124 } else if (!ret) {
125 ret = QOBJECT(qdict_new());
126 }
127
128 qobject_unref(args);
129
130 return ret;
131 }
132
133 QObject *qmp_build_error_object(Error *err)
134 {
135 return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
136 QapiErrorClass_str(error_get_class(err)),
137 error_get_pretty(err));
138 }
139
140 /*
141 * Detect whether a request should be run out-of-band, by quickly
142 * peeking at whether we have: { "control": { "run-oob": true } }. By
143 * default commands are run in-band.
144 */
145 bool qmp_is_oob(QDict *dict)
146 {
147 QBool *bool_obj;
148
149 dict = qdict_get_qdict(dict, "control");
150 if (!dict) {
151 return false;
152 }
153
154 bool_obj = qobject_to(QBool, qdict_get(dict, "run-oob"));
155 if (!bool_obj) {
156 return false;
157 }
158
159 return qbool_get_bool(bool_obj);
160 }
161
162 QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request)
163 {
164 Error *err = NULL;
165 QObject *ret;
166 QDict *rsp;
167
168 ret = do_qmp_dispatch(cmds, request, &err);
169
170 rsp = qdict_new();
171 if (err) {
172 qdict_put_obj(rsp, "error", qmp_build_error_object(err));
173 error_free(err);
174 } else if (ret) {
175 qdict_put_obj(rsp, "return", ret);
176 } else {
177 qobject_unref(rsp);
178 return NULL;
179 }
180
181 return QOBJECT(rsp);
182 }