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