/src/qemu/monitor/qmp-cmds-control.c
Line | Count | Source |
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 | 0 | { |
45 | 0 | GString *unavailable = NULL; |
46 | 0 | bool capab[QMP_CAPABILITY__MAX]; |
47 | |
|
48 | 0 | memset(capab, 0, sizeof(capab)); |
49 | |
|
50 | 0 | for (; list; list = list->next) { |
51 | 0 | if (!mon->capab_offered[list->value]) { |
52 | 0 | if (!unavailable) { |
53 | 0 | unavailable = g_string_new(QMPCapability_str(list->value)); |
54 | 0 | } else { |
55 | 0 | g_string_append_printf(unavailable, ", %s", |
56 | 0 | QMPCapability_str(list->value)); |
57 | 0 | } |
58 | 0 | } |
59 | 0 | capab[list->value] = true; |
60 | 0 | } |
61 | |
|
62 | 0 | if (unavailable) { |
63 | 0 | error_setg(errp, "Capability %s not available", unavailable->str); |
64 | 0 | g_string_free(unavailable, true); |
65 | 0 | return false; |
66 | 0 | } |
67 | | |
68 | 0 | memcpy(mon->capab, capab, sizeof(capab)); |
69 | 0 | return true; |
70 | 0 | } |
71 | | |
72 | | void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable, |
73 | | Error **errp) |
74 | 0 | { |
75 | 0 | MonitorQMP *mon = MONITOR_QMP(monitor_cur()); |
76 | |
|
77 | 0 | if (mon->commands == &qmp_commands) { |
78 | 0 | error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, |
79 | 0 | "Capabilities negotiation is already complete, command " |
80 | 0 | "ignored"); |
81 | 0 | return; |
82 | 0 | } |
83 | | |
84 | 0 | if (!qmp_caps_accept(mon, enable, errp)) { |
85 | 0 | return; |
86 | 0 | } |
87 | | |
88 | 0 | mon->commands = &qmp_commands; |
89 | 0 | } |
90 | | |
91 | | VersionInfo *qmp_query_version(Error **errp) |
92 | 0 | { |
93 | 0 | VersionInfo *info = g_new0(VersionInfo, 1); |
94 | |
|
95 | 0 | info->qemu = g_new0(VersionTriple, 1); |
96 | 0 | info->qemu->major = QEMU_VERSION_MAJOR; |
97 | 0 | info->qemu->minor = QEMU_VERSION_MINOR; |
98 | 0 | info->qemu->micro = QEMU_VERSION_MICRO; |
99 | 0 | info->package = g_strdup(QEMU_PKGVERSION); |
100 | |
|
101 | 0 | return info; |
102 | 0 | } |
103 | | |
104 | | static void query_commands_cb(const QmpCommand *cmd, void *opaque) |
105 | 0 | { |
106 | 0 | CommandInfo *info; |
107 | 0 | CommandInfoList **list = opaque; |
108 | |
|
109 | 0 | if (!cmd->enabled) { |
110 | 0 | return; |
111 | 0 | } |
112 | | |
113 | 0 | info = g_malloc0(sizeof(*info)); |
114 | 0 | info->name = g_strdup(cmd->name); |
115 | 0 | QAPI_LIST_PREPEND(*list, info); |
116 | 0 | } |
117 | | |
118 | | CommandInfoList *qmp_query_commands(Error **errp) |
119 | 0 | { |
120 | 0 | CommandInfoList *list = NULL; |
121 | 0 | MonitorQMP *mon = MONITOR_QMP(monitor_cur()); |
122 | |
|
123 | 0 | qmp_for_each_command(mon->commands, query_commands_cb, &list); |
124 | |
|
125 | 0 | return list; |
126 | 0 | } |
127 | | |
128 | | static void *split_off_generic_list(void *list, |
129 | | bool (*splitp)(void *elt), |
130 | | void **part) |
131 | 0 | { |
132 | 0 | GenericList *keep = NULL, **keep_tailp = &keep; |
133 | 0 | GenericList *split = NULL, **split_tailp = &split; |
134 | 0 | GenericList *tail; |
135 | |
|
136 | 0 | for (tail = list; tail; tail = tail->next) { |
137 | 0 | if (splitp(tail)) { |
138 | 0 | *split_tailp = tail; |
139 | 0 | split_tailp = &tail->next; |
140 | 0 | } else { |
141 | 0 | *keep_tailp = tail; |
142 | 0 | keep_tailp = &tail->next; |
143 | 0 | } |
144 | 0 | } |
145 | |
|
146 | 0 | *keep_tailp = *split_tailp = NULL; |
147 | 0 | *part = split; |
148 | 0 | return keep; |
149 | 0 | } |
150 | | |
151 | | static bool is_in(const char *s, strList *list) |
152 | 0 | { |
153 | 0 | strList *tail; |
154 | |
|
155 | 0 | for (tail = list; tail; tail = tail->next) { |
156 | 0 | if (!strcmp(tail->value, s)) { |
157 | 0 | return true; |
158 | 0 | } |
159 | 0 | } |
160 | 0 | return false; |
161 | 0 | } |
162 | | |
163 | | static bool is_entity_deprecated(void *link) |
164 | 0 | { |
165 | 0 | return is_in("deprecated", ((SchemaInfoList *)link)->value->features); |
166 | 0 | } |
167 | | |
168 | | static bool is_member_deprecated(void *link) |
169 | 0 | { |
170 | 0 | return is_in("deprecated", |
171 | 0 | ((SchemaInfoObjectMemberList *)link)->value->features); |
172 | 0 | } |
173 | | |
174 | | static SchemaInfoList *zap_deprecated(SchemaInfoList *schema) |
175 | 0 | { |
176 | 0 | void *to_zap; |
177 | 0 | SchemaInfoList *tail; |
178 | 0 | SchemaInfo *ent; |
179 | |
|
180 | 0 | schema = split_off_generic_list(schema, is_entity_deprecated, &to_zap); |
181 | 0 | qapi_free_SchemaInfoList(to_zap); |
182 | |
|
183 | 0 | for (tail = schema; tail; tail = tail->next) { |
184 | 0 | ent = tail->value; |
185 | 0 | if (ent->meta_type == SCHEMA_META_TYPE_OBJECT) { |
186 | 0 | ent->u.object.members |
187 | 0 | = split_off_generic_list(ent->u.object.members, |
188 | 0 | is_member_deprecated, &to_zap); |
189 | 0 | qapi_free_SchemaInfoObjectMemberList(to_zap); |
190 | 0 | } |
191 | 0 | } |
192 | |
|
193 | 0 | return schema; |
194 | 0 | } |
195 | | |
196 | | SchemaInfoList *qmp_query_qmp_schema(Error **errp) |
197 | 0 | { |
198 | 0 | QObject *obj = qobject_from_qlit(&qmp_schema_qlit); |
199 | 0 | Visitor *v = qobject_input_visitor_new(obj); |
200 | 0 | SchemaInfoList *schema = NULL; |
201 | | |
202 | | /* test_visitor_in_qmp_introspect() ensures this can't fail */ |
203 | 0 | visit_type_SchemaInfoList(v, NULL, &schema, &error_abort); |
204 | 0 | g_assert(schema); |
205 | | |
206 | 0 | qobject_unref(obj); |
207 | 0 | visit_free(v); |
208 | |
|
209 | 0 | if (compat_policy.deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) { |
210 | 0 | return zap_deprecated(schema); |
211 | 0 | } |
212 | 0 | return schema; |
213 | 0 | } |