target/arm: Implement ARMv8.0-PredInv
[qemu/ar7.git] / qapi / qmp-dispatch.c
blob1d922e04f7106d67f458102f50d6f86e8318d6de
1 /*
2 * Core Definitions for QAPI/QMP Dispatch
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/dispatch.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qjson.h"
19 #include "qapi/qmp/qbool.h"
20 #include "sysemu/sysemu.h"
22 static QDict *qmp_dispatch_check_obj(const QObject *request, bool allow_oob,
23 Error **errp)
25 const char *exec_key = NULL;
26 const QDictEntry *ent;
27 const char *arg_name;
28 const QObject *arg_obj;
29 QDict *dict;
31 dict = qobject_to(QDict, request);
32 if (!dict) {
33 error_setg(errp, "QMP input must be a JSON object");
34 return NULL;
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);
42 if (!strcmp(arg_name, "execute")
43 || (!strcmp(arg_name, "exec-oob") && allow_oob)) {
44 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
45 error_setg(errp, "QMP input member '%s' must be a string",
46 arg_name);
47 return NULL;
49 if (exec_key) {
50 error_setg(errp, "QMP input member '%s' clashes with '%s'",
51 arg_name, exec_key);
52 return NULL;
54 exec_key = arg_name;
55 } else if (!strcmp(arg_name, "arguments")) {
56 if (qobject_type(arg_obj) != QTYPE_QDICT) {
57 error_setg(errp,
58 "QMP input member 'arguments' must be an object");
59 return NULL;
61 } else {
62 error_setg(errp, "QMP input member '%s' is unexpected",
63 arg_name);
64 return NULL;
68 if (!exec_key) {
69 error_setg(errp, "QMP input lacks member 'execute'");
70 return NULL;
73 return dict;
76 static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request,
77 bool allow_oob, Error **errp)
79 Error *local_err = NULL;
80 bool oob;
81 const char *command;
82 QDict *args, *dict;
83 QmpCommand *cmd;
84 QObject *ret = NULL;
86 dict = qmp_dispatch_check_obj(request, allow_oob, errp);
87 if (!dict) {
88 return NULL;
91 command = qdict_get_try_str(dict, "execute");
92 oob = false;
93 if (!command) {
94 assert(allow_oob);
95 command = qdict_get_str(dict, "exec-oob");
96 oob = true;
98 cmd = qmp_find_command(cmds, command);
99 if (cmd == NULL) {
100 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
101 "The command %s has not been found", command);
102 return NULL;
104 if (!cmd->enabled) {
105 error_setg(errp, "The command %s has been disabled for this instance",
106 command);
107 return NULL;
109 if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
110 error_setg(errp, "The command %s does not support OOB",
111 command);
112 return false;
115 if (runstate_check(RUN_STATE_PRECONFIG) &&
116 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
117 error_setg(errp, "The command '%s' isn't permitted in '%s' state",
118 cmd->name, RunState_str(RUN_STATE_PRECONFIG));
119 return NULL;
122 if (!qdict_haskey(dict, "arguments")) {
123 args = qdict_new();
124 } else {
125 args = qdict_get_qdict(dict, "arguments");
126 qobject_ref(args);
129 cmd->fn(args, &ret, &local_err);
130 if (local_err) {
131 error_propagate(errp, local_err);
132 } else if (cmd->options & QCO_NO_SUCCESS_RESP) {
133 g_assert(!ret);
134 } else if (!ret) {
135 /* TODO turn into assertion */
136 ret = QOBJECT(qdict_new());
139 qobject_unref(args);
141 return ret;
144 QDict *qmp_error_response(Error *err)
146 QDict *rsp;
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;
156 * Does @qdict look like a command to be run out-of-band?
158 bool qmp_is_oob(const QDict *dict)
160 return qdict_haskey(dict, "exec-oob")
161 && !qdict_haskey(dict, "execute");
164 QDict *qmp_dispatch(QmpCommandList *cmds, QObject *request,
165 bool allow_oob)
167 Error *err = NULL;
168 QObject *ret;
169 QDict *rsp;
171 ret = do_qmp_dispatch(cmds, request, allow_oob, &err);
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 /* Can only happen for commands with QCO_NO_SUCCESS_RESP */
180 rsp = NULL;
183 return rsp;