]> git.proxmox.com Git - mirror_qemu.git/blob - qapi/qmp-dispatch.c
Drop superfluous includes of qapi-types.h and test-qapi-types.h
[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/types.h"
17 #include "qapi/qmp/dispatch.h"
18 #include "qapi/qmp/json-parser.h"
19 #include "qapi/qmp/qjson.h"
20 #include "qapi/qmp/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 dict = qobject_to_qdict(request);
31 if (!dict) {
32 error_setg(errp, "QMP input must be a JSON object");
33 return NULL;
34 }
35
36 for (ent = qdict_first(dict); ent;
37 ent = qdict_next(dict, ent)) {
38 arg_name = qdict_entry_key(ent);
39 arg_obj = qdict_entry_value(ent);
40
41 if (!strcmp(arg_name, "execute")) {
42 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
43 error_setg(errp,
44 "QMP input member 'execute' must be a string");
45 return NULL;
46 }
47 has_exec_key = true;
48 } else if (!strcmp(arg_name, "arguments")) {
49 if (qobject_type(arg_obj) != QTYPE_QDICT) {
50 error_setg(errp,
51 "QMP input member 'arguments' must be an object");
52 return NULL;
53 }
54 } else {
55 error_setg(errp, "QMP input member '%s' is unexpected",
56 arg_name);
57 return NULL;
58 }
59 }
60
61 if (!has_exec_key) {
62 error_setg(errp, "QMP input lacks member 'execute'");
63 return NULL;
64 }
65
66 return dict;
67 }
68
69 static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request,
70 Error **errp)
71 {
72 Error *local_err = NULL;
73 const char *command;
74 QDict *args, *dict;
75 QmpCommand *cmd;
76 QObject *ret = NULL;
77
78 dict = qmp_dispatch_check_obj(request, errp);
79 if (!dict) {
80 return NULL;
81 }
82
83 command = qdict_get_str(dict, "execute");
84 cmd = qmp_find_command(cmds, command);
85 if (cmd == NULL) {
86 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
87 "The command %s has not been found", command);
88 return NULL;
89 }
90 if (!cmd->enabled) {
91 error_setg(errp, "The command %s has been disabled for this instance",
92 command);
93 return NULL;
94 }
95
96 if (!qdict_haskey(dict, "arguments")) {
97 args = qdict_new();
98 } else {
99 args = qdict_get_qdict(dict, "arguments");
100 QINCREF(args);
101 }
102
103 cmd->fn(args, &ret, &local_err);
104 if (local_err) {
105 error_propagate(errp, local_err);
106 } else if (cmd->options & QCO_NO_SUCCESS_RESP) {
107 g_assert(!ret);
108 } else if (!ret) {
109 ret = QOBJECT(qdict_new());
110 }
111
112 QDECREF(args);
113
114 return ret;
115 }
116
117 QObject *qmp_build_error_object(Error *err)
118 {
119 return qobject_from_jsonf("{ 'class': %s, 'desc': %s }",
120 QapiErrorClass_str(error_get_class(err)),
121 error_get_pretty(err));
122 }
123
124 QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request)
125 {
126 Error *err = NULL;
127 QObject *ret;
128 QDict *rsp;
129
130 ret = do_qmp_dispatch(cmds, request, &err);
131
132 rsp = qdict_new();
133 if (err) {
134 qdict_put_obj(rsp, "error", qmp_build_error_object(err));
135 error_free(err);
136 } else if (ret) {
137 qdict_put_obj(rsp, "return", ret);
138 } else {
139 QDECREF(rsp);
140 return NULL;
141 }
142
143 return QOBJECT(rsp);
144 }