]>
Commit | Line | Data |
---|---|---|
ab02ab2a MR |
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 | ||
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" | |
c7eb39cb | 18 | #include "qapi/qmp/qjson.h" |
ab02ab2a MR |
19 | |
20 | static QDict *qmp_dispatch_check_obj(const QObject *request, Error **errp) | |
21 | { | |
22 | const QDictEntry *ent; | |
23 | const char *arg_name; | |
24 | const QObject *arg_obj; | |
25 | bool has_exec_key = false; | |
26 | QDict *dict = NULL; | |
27 | ||
ca6b6e1e MA |
28 | dict = qobject_to_qdict(request); |
29 | if (!dict) { | |
10e37839 | 30 | error_setg(errp, "QMP input must be a JSON object"); |
ab02ab2a MR |
31 | return NULL; |
32 | } | |
33 | ||
ab02ab2a MR |
34 | for (ent = qdict_first(dict); ent; |
35 | ent = qdict_next(dict, ent)) { | |
36 | arg_name = qdict_entry_key(ent); | |
37 | arg_obj = qdict_entry_value(ent); | |
38 | ||
39 | if (!strcmp(arg_name, "execute")) { | |
40 | if (qobject_type(arg_obj) != QTYPE_QSTRING) { | |
10e37839 MA |
41 | error_setg(errp, |
42 | "QMP input member 'execute' must be a string"); | |
ab02ab2a MR |
43 | return NULL; |
44 | } | |
45 | has_exec_key = true; | |
74d8c9d9 MA |
46 | } else if (!strcmp(arg_name, "arguments")) { |
47 | if (qobject_type(arg_obj) != QTYPE_QDICT) { | |
10e37839 MA |
48 | error_setg(errp, |
49 | "QMP input member 'arguments' must be an object"); | |
74d8c9d9 MA |
50 | return NULL; |
51 | } | |
52 | } else { | |
10e37839 | 53 | error_setg(errp, "QMP input member '%s' is unexpected", |
99fb0c53 | 54 | arg_name); |
ab02ab2a MR |
55 | return NULL; |
56 | } | |
57 | } | |
58 | ||
59 | if (!has_exec_key) { | |
10e37839 | 60 | error_setg(errp, "QMP input lacks member 'execute'"); |
ab02ab2a MR |
61 | return NULL; |
62 | } | |
63 | ||
64 | return dict; | |
65 | } | |
66 | ||
1527badb MA |
67 | static QObject *do_qmp_dispatch(QmpCommandList *cmds, QObject *request, |
68 | Error **errp) | |
ab02ab2a | 69 | { |
ee16ce93 | 70 | Error *local_err = NULL; |
ab02ab2a MR |
71 | const char *command; |
72 | QDict *args, *dict; | |
73 | QmpCommand *cmd; | |
74 | QObject *ret = NULL; | |
75 | ||
ab02ab2a | 76 | dict = qmp_dispatch_check_obj(request, errp); |
4af8be1f | 77 | if (!dict) { |
ab02ab2a MR |
78 | return NULL; |
79 | } | |
80 | ||
81 | command = qdict_get_str(dict, "execute"); | |
1527badb | 82 | cmd = qmp_find_command(cmds, command); |
ab02ab2a | 83 | if (cmd == NULL) { |
a6c90cbc MA |
84 | error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, |
85 | "The command %s has not been found", command); | |
ab02ab2a MR |
86 | return NULL; |
87 | } | |
abd6cf6d | 88 | if (!cmd->enabled) { |
f231b88d CR |
89 | error_setg(errp, "The command %s has been disabled for this instance", |
90 | command); | |
abd6cf6d MR |
91 | return NULL; |
92 | } | |
ab02ab2a MR |
93 | |
94 | if (!qdict_haskey(dict, "arguments")) { | |
95 | args = qdict_new(); | |
96 | } else { | |
97 | args = qdict_get_qdict(dict, "arguments"); | |
98 | QINCREF(args); | |
99 | } | |
100 | ||
42a502a7 EB |
101 | cmd->fn(args, &ret, &local_err); |
102 | if (local_err) { | |
103 | error_propagate(errp, local_err); | |
104 | } else if (cmd->options & QCO_NO_SUCCESS_RESP) { | |
105 | g_assert(!ret); | |
106 | } else if (!ret) { | |
107 | ret = QOBJECT(qdict_new()); | |
ab02ab2a MR |
108 | } |
109 | ||
110 | QDECREF(args); | |
111 | ||
112 | return ret; | |
113 | } | |
114 | ||
e940f543 | 115 | QObject *qmp_build_error_object(Error *err) |
93b91c59 LC |
116 | { |
117 | return qobject_from_jsonf("{ 'class': %s, 'desc': %s }", | |
977c736f | 118 | QapiErrorClass_str(error_get_class(err)), |
e940f543 | 119 | error_get_pretty(err)); |
93b91c59 LC |
120 | } |
121 | ||
1527badb | 122 | QObject *qmp_dispatch(QmpCommandList *cmds, QObject *request) |
ab02ab2a MR |
123 | { |
124 | Error *err = NULL; | |
125 | QObject *ret; | |
126 | QDict *rsp; | |
127 | ||
1527badb | 128 | ret = do_qmp_dispatch(cmds, request, &err); |
ab02ab2a MR |
129 | |
130 | rsp = qdict_new(); | |
131 | if (err) { | |
93b91c59 | 132 | qdict_put_obj(rsp, "error", qmp_build_error_object(err)); |
ab02ab2a MR |
133 | error_free(err); |
134 | } else if (ret) { | |
135 | qdict_put_obj(rsp, "return", ret); | |
136 | } else { | |
137 | QDECREF(rsp); | |
138 | return NULL; | |
139 | } | |
140 | ||
141 | return QOBJECT(rsp); | |
142 | } |