]> Git Repo - qemu.git/blame - qapi/qmp-dispatch.c
qmp qemu-ga: Fix qemu-ga not to accept "control"
[qemu.git] / qapi / qmp-dispatch.c
CommitLineData
ab02ab2a
MR
1/*
2 * Core Definitions for QAPI/QMP Dispatch
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <[email protected]>
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
cbf21151 14#include "qemu/osdep.h"
da34e65c 15#include "qapi/error.h"
7b1b5d19
PB
16#include "qapi/qmp/dispatch.h"
17#include "qapi/qmp/json-parser.h"
452fcdbc 18#include "qapi/qmp/qdict.h"
c7eb39cb 19#include "qapi/qmp/qjson.h"
cf869d53 20#include "qapi/qmp/qbool.h"
047f7038 21#include "sysemu/sysemu.h"
ab02ab2a 22
674ed722
MA
23QDict *qmp_dispatch_check_obj(const QObject *request, bool allow_oob,
24 Error **errp)
ab02ab2a
MR
25{
26 const QDictEntry *ent;
27 const char *arg_name;
28 const QObject *arg_obj;
29 bool has_exec_key = false;
30 QDict *dict = NULL;
31
7dc847eb 32 dict = qobject_to(QDict, request);
ca6b6e1e 33 if (!dict) {
10e37839 34 error_setg(errp, "QMP input must be a JSON object");
ab02ab2a
MR
35 return NULL;
36 }
37
ab02ab2a
MR
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) {
10e37839
MA
45 error_setg(errp,
46 "QMP input member 'execute' must be a string");
ab02ab2a
MR
47 return NULL;
48 }
49 has_exec_key = true;
74d8c9d9
MA
50 } else if (!strcmp(arg_name, "arguments")) {
51 if (qobject_type(arg_obj) != QTYPE_QDICT) {
10e37839
MA
52 error_setg(errp,
53 "QMP input member 'arguments' must be an object");
74d8c9d9
MA
54 return NULL;
55 }
674ed722 56 } else if (!strcmp(arg_name, "control") && allow_oob) {
cf869d53
PX
57 if (qobject_type(arg_obj) != QTYPE_QDICT) {
58 error_setg(errp,
59 "QMP input member 'control' must be a dict");
60 return NULL;
61 }
74d8c9d9 62 } else {
10e37839 63 error_setg(errp, "QMP input member '%s' is unexpected",
99fb0c53 64 arg_name);
ab02ab2a
MR
65 return NULL;
66 }
67 }
68
69 if (!has_exec_key) {
10e37839 70 error_setg(errp, "QMP input lacks member 'execute'");
ab02ab2a
MR
71 return NULL;
72 }
73
74 return dict;
75}
76
1527badb 77static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request,
674ed722 78 bool allow_oob, Error **errp)
ab02ab2a 79{
ee16ce93 80 Error *local_err = NULL;
ab02ab2a
MR
81 const char *command;
82 QDict *args, *dict;
83 QmpCommand *cmd;
84 QObject *ret = NULL;
85
674ed722 86 dict = qmp_dispatch_check_obj(request, allow_oob, errp);
4af8be1f 87 if (!dict) {
ab02ab2a
MR
88 return NULL;
89 }
90
91 command = qdict_get_str(dict, "execute");
1527badb 92 cmd = qmp_find_command(cmds, command);
ab02ab2a 93 if (cmd == NULL) {
a6c90cbc
MA
94 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
95 "The command %s has not been found", command);
ab02ab2a
MR
96 return NULL;
97 }
abd6cf6d 98 if (!cmd->enabled) {
f231b88d
CR
99 error_setg(errp, "The command %s has been disabled for this instance",
100 command);
abd6cf6d
MR
101 return NULL;
102 }
ab02ab2a 103
047f7038
IM
104 if (runstate_check(RUN_STATE_PRECONFIG) &&
105 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
106 error_setg(errp, "The command '%s' isn't permitted in '%s' state",
107 cmd->name, RunState_str(RUN_STATE_PRECONFIG));
108 return NULL;
109 }
110
ab02ab2a
MR
111 if (!qdict_haskey(dict, "arguments")) {
112 args = qdict_new();
113 } else {
114 args = qdict_get_qdict(dict, "arguments");
cb3e7f08 115 qobject_ref(args);
ab02ab2a
MR
116 }
117
42a502a7
EB
118 cmd->fn(args, &ret, &local_err);
119 if (local_err) {
120 error_propagate(errp, local_err);
121 } else if (cmd->options & QCO_NO_SUCCESS_RESP) {
122 g_assert(!ret);
123 } else if (!ret) {
124 ret = QOBJECT(qdict_new());
ab02ab2a
MR
125 }
126
cb3e7f08 127 qobject_unref(args);
ab02ab2a
MR
128
129 return ret;
130}
131
e940f543 132QObject *qmp_build_error_object(Error *err)
93b91c59
LC
133{
134 return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
977c736f 135 QapiErrorClass_str(error_get_class(err)),
e940f543 136 error_get_pretty(err));
93b91c59
LC
137}
138
cf869d53
PX
139/*
140 * Detect whether a request should be run out-of-band, by quickly
141 * peeking at whether we have: { "control": { "run-oob": true } }. By
142 * default commands are run in-band.
143 */
144bool qmp_is_oob(QDict *dict)
145{
146 QBool *bool_obj;
147
148 dict = qdict_get_qdict(dict, "control");
149 if (!dict) {
150 return false;
151 }
152
153 bool_obj = qobject_to(QBool, qdict_get(dict, "run-oob"));
154 if (!bool_obj) {
155 return false;
156 }
157
158 return qbool_get_bool(bool_obj);
159}
160
674ed722
MA
161QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request,
162 bool allow_oob)
ab02ab2a
MR
163{
164 Error *err = NULL;
165 QObject *ret;
166 QDict *rsp;
167
674ed722 168 ret = do_qmp_dispatch(cmds, request, allow_oob, &err);
ab02ab2a
MR
169
170 rsp = qdict_new();
171 if (err) {
93b91c59 172 qdict_put_obj(rsp, "error", qmp_build_error_object(err));
ab02ab2a
MR
173 error_free(err);
174 } else if (ret) {
175 qdict_put_obj(rsp, "return", ret);
176 } else {
cb3e7f08 177 qobject_unref(rsp);
ab02ab2a
MR
178 return NULL;
179 }
180
181 return QOBJECT(rsp);
182}
This page took 0.410081 seconds and 4 git commands to generate.