1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
39
40
41
42static 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
72void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
73 Error **errp)
74{
75 Monitor *cur_mon = monitor_cur();
76 MonitorQMP *mon;
77
78 assert(monitor_is_qmp(cur_mon));
79 mon = container_of(cur_mon, MonitorQMP, common);
80
81 if (mon->commands == &qmp_commands) {
82 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
83 "Capabilities negotiation is already complete, command "
84 "ignored");
85 return;
86 }
87
88 if (!qmp_caps_accept(mon, enable, errp)) {
89 return;
90 }
91
92 mon->commands = &qmp_commands;
93}
94
95VersionInfo *qmp_query_version(Error **errp)
96{
97 VersionInfo *info = g_new0(VersionInfo, 1);
98
99 info->qemu = g_new0(VersionTriple, 1);
100 info->qemu->major = QEMU_VERSION_MAJOR;
101 info->qemu->minor = QEMU_VERSION_MINOR;
102 info->qemu->micro = QEMU_VERSION_MICRO;
103 info->package = g_strdup(QEMU_PKGVERSION);
104
105 return info;
106}
107
108static void query_commands_cb(const QmpCommand *cmd, void *opaque)
109{
110 CommandInfo *info;
111 CommandInfoList **list = opaque;
112
113 if (!cmd->enabled) {
114 return;
115 }
116
117 info = g_malloc0(sizeof(*info));
118 info->name = g_strdup(cmd->name);
119 QAPI_LIST_PREPEND(*list, info);
120}
121
122CommandInfoList *qmp_query_commands(Error **errp)
123{
124 CommandInfoList *list = NULL;
125 Monitor *cur_mon = monitor_cur();
126 MonitorQMP *mon;
127
128 assert(monitor_is_qmp(cur_mon));
129 mon = container_of(cur_mon, MonitorQMP, common);
130
131 qmp_for_each_command(mon->commands, query_commands_cb, &list);
132
133 return list;
134}
135
136static void *split_off_generic_list(void *list,
137 bool (*splitp)(void *elt),
138 void **part)
139{
140 GenericList *keep = NULL, **keep_tailp = &keep;
141 GenericList *split = NULL, **split_tailp = &split;
142 GenericList *tail;
143
144 for (tail = list; tail; tail = tail->next) {
145 if (splitp(tail)) {
146 *split_tailp = tail;
147 split_tailp = &tail->next;
148 } else {
149 *keep_tailp = tail;
150 keep_tailp = &tail->next;
151 }
152 }
153
154 *keep_tailp = *split_tailp = NULL;
155 *part = split;
156 return keep;
157}
158
159static bool is_in(const char *s, strList *list)
160{
161 strList *tail;
162
163 for (tail = list; tail; tail = tail->next) {
164 if (!strcmp(tail->value, s)) {
165 return true;
166 }
167 }
168 return false;
169}
170
171static bool is_entity_deprecated(void *link)
172{
173 return is_in("deprecated", ((SchemaInfoList *)link)->value->features);
174}
175
176static bool is_member_deprecated(void *link)
177{
178 return is_in("deprecated",
179 ((SchemaInfoObjectMemberList *)link)->value->features);
180}
181
182static SchemaInfoList *zap_deprecated(SchemaInfoList *schema)
183{
184 void *to_zap;
185 SchemaInfoList *tail;
186 SchemaInfo *ent;
187
188 schema = split_off_generic_list(schema, is_entity_deprecated, &to_zap);
189 qapi_free_SchemaInfoList(to_zap);
190
191 for (tail = schema; tail; tail = tail->next) {
192 ent = tail->value;
193 if (ent->meta_type == SCHEMA_META_TYPE_OBJECT) {
194 ent->u.object.members
195 = split_off_generic_list(ent->u.object.members,
196 is_member_deprecated, &to_zap);
197 qapi_free_SchemaInfoObjectMemberList(to_zap);
198 }
199 }
200
201 return schema;
202}
203
204SchemaInfoList *qmp_query_qmp_schema(Error **errp)
205{
206 QObject *obj = qobject_from_qlit(&qmp_schema_qlit);
207 Visitor *v = qobject_input_visitor_new(obj);
208 SchemaInfoList *schema = NULL;
209
210
211 visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
212 g_assert(schema);
213
214 qobject_unref(obj);
215 visit_free(v);
216
217 if (compat_policy.deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) {
218 return zap_deprecated(schema);
219 }
220 return schema;
221}
222