master
c 213 lines 6.06 KB
Raw
1 /*
2 * QMP commands related to the monitor (common to system and tools)
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "qemu/osdep.h"
26
27 #include "monitor-internal.h"
28 #include "qemu-version.h"
29 #include "qapi/compat-policy.h"
30 #include "qapi/error.h"
31 #include "qapi/qapi-commands-control.h"
32 #include "qapi/qapi-commands-introspect.h"
33 #include "qapi/qapi-introspect.h"
34 #include "qapi/qapi-visit-introspect.h"
35 #include "qapi/qobject-input-visitor.h"
36
37 /*
38 * Accept QMP capabilities in @list for @mon.
39 * On success, set mon->qmp.capab[], and return true.
40 * On error, set @errp, and return false.
41 */
42 static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
43 Error **errp)
44 {
45 GString *unavailable = NULL;
46 bool capab[QMP_CAPABILITY__MAX];
47
48 memset(capab, 0, sizeof(capab));
49
50 for (; list; list = list->next) {
51 if (!mon->capab_offered[list->value]) {
52 if (!unavailable) {
53 unavailable = g_string_new(QMPCapability_str(list->value));
54 } else {
55 g_string_append_printf(unavailable, ", %s",
56 QMPCapability_str(list->value));
57 }
58 }
59 capab[list->value] = true;
60 }
61
62 if (unavailable) {
63 error_setg(errp, "Capability %s not available", unavailable->str);
64 g_string_free(unavailable, true);
65 return false;
66 }
67
68 memcpy(mon->capab, capab, sizeof(capab));
69 return true;
70 }
71
72 void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
73 Error **errp)
74 {
75 MonitorQMP *mon = MONITOR_QMP(monitor_cur());
76
77 if (mon->commands == &qmp_commands) {
78 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
79 "Capabilities negotiation is already complete, command "
80 "ignored");
81 return;
82 }
83
84 if (!qmp_caps_accept(mon, enable, errp)) {
85 return;
86 }
87
88 mon->commands = &qmp_commands;
89 }
90
91 VersionInfo *qmp_query_version(Error **errp)
92 {
93 VersionInfo *info = g_new0(VersionInfo, 1);
94
95 info->qemu = g_new0(VersionTriple, 1);
96 info->qemu->major = QEMU_VERSION_MAJOR;
97 info->qemu->minor = QEMU_VERSION_MINOR;
98 info->qemu->micro = QEMU_VERSION_MICRO;
99 info->package = g_strdup(QEMU_PKGVERSION);
100
101 return info;
102 }
103
104 static void query_commands_cb(const QmpCommand *cmd, void *opaque)
105 {
106 CommandInfo *info;
107 CommandInfoList **list = opaque;
108
109 if (!cmd->enabled) {
110 return;
111 }
112
113 info = g_malloc0(sizeof(*info));
114 info->name = g_strdup(cmd->name);
115 QAPI_LIST_PREPEND(*list, info);
116 }
117
118 CommandInfoList *qmp_query_commands(Error **errp)
119 {
120 CommandInfoList *list = NULL;
121 MonitorQMP *mon = MONITOR_QMP(monitor_cur());
122
123 qmp_for_each_command(mon->commands, query_commands_cb, &list);
124
125 return list;
126 }
127
128 static void *split_off_generic_list(void *list,
129 bool (*splitp)(void *elt),
130 void **part)
131 {
132 GenericList *keep = NULL, **keep_tailp = &keep;
133 GenericList *split = NULL, **split_tailp = &split;
134 GenericList *tail;
135
136 for (tail = list; tail; tail = tail->next) {
137 if (splitp(tail)) {
138 *split_tailp = tail;
139 split_tailp = &tail->next;
140 } else {
141 *keep_tailp = tail;
142 keep_tailp = &tail->next;
143 }
144 }
145
146 *keep_tailp = *split_tailp = NULL;
147 *part = split;
148 return keep;
149 }
150
151 static bool is_in(const char *s, strList *list)
152 {
153 strList *tail;
154
155 for (tail = list; tail; tail = tail->next) {
156 if (!strcmp(tail->value, s)) {
157 return true;
158 }
159 }
160 return false;
161 }
162
163 static bool is_entity_deprecated(void *link)
164 {
165 return is_in("deprecated", ((SchemaInfoList *)link)->value->features);
166 }
167
168 static bool is_member_deprecated(void *link)
169 {
170 return is_in("deprecated",
171 ((SchemaInfoObjectMemberList *)link)->value->features);
172 }
173
174 static SchemaInfoList *zap_deprecated(SchemaInfoList *schema)
175 {
176 void *to_zap;
177 SchemaInfoList *tail;
178 SchemaInfo *ent;
179
180 schema = split_off_generic_list(schema, is_entity_deprecated, &to_zap);
181 qapi_free_SchemaInfoList(to_zap);
182
183 for (tail = schema; tail; tail = tail->next) {
184 ent = tail->value;
185 if (ent->meta_type == SCHEMA_META_TYPE_OBJECT) {
186 ent->u.object.members
187 = split_off_generic_list(ent->u.object.members,
188 is_member_deprecated, &to_zap);
189 qapi_free_SchemaInfoObjectMemberList(to_zap);
190 }
191 }
192
193 return schema;
194 }
195
196 SchemaInfoList *qmp_query_qmp_schema(Error **errp)
197 {
198 QObject *obj = qobject_from_qlit(&qmp_schema_qlit);
199 Visitor *v = qobject_input_visitor_new(obj);
200 SchemaInfoList *schema = NULL;
201
202 /* test_visitor_in_qmp_introspect() ensures this can't fail */
203 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
204 g_assert(schema);
205
206 qobject_unref(obj);
207 visit_free(v);
208
209 if (compat_policy.deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) {
210 return zap_deprecated(schema);
211 }
212 return schema;
213 }