]>
Commit | Line | Data |
---|---|---|
48a32bed AL |
1 | /* |
2 | * QEMU Management Protocol | |
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 GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
6b620ca3 PB |
12 | * Contributions after 2012-01-13 are licensed under the terms of the |
13 | * GNU GPL, version 2 or (at your option) any later version. | |
48a32bed AL |
14 | */ |
15 | ||
16 | #include "qemu-common.h" | |
17 | #include "sysemu.h" | |
18 | #include "qmp-commands.h" | |
fbf796fd LC |
19 | #include "ui/qemu-spice.h" |
20 | #include "ui/vnc.h" | |
292a2602 LC |
21 | #include "kvm.h" |
22 | #include "arch_init.h" | |
b4b12c62 | 23 | #include "hw/qdev.h" |
eb6e8ea5 AL |
24 | #include "qapi/qmp-input-visitor.h" |
25 | #include "qapi/qmp-output-visitor.h" | |
48a32bed AL |
26 | |
27 | NameInfo *qmp_query_name(Error **errp) | |
28 | { | |
29 | NameInfo *info = g_malloc0(sizeof(*info)); | |
30 | ||
31 | if (qemu_name) { | |
32 | info->has_name = true; | |
33 | info->name = g_strdup(qemu_name); | |
34 | } | |
35 | ||
36 | return info; | |
37 | } | |
b9c15f16 LC |
38 | |
39 | VersionInfo *qmp_query_version(Error **err) | |
40 | { | |
41 | VersionInfo *info = g_malloc0(sizeof(*info)); | |
42 | const char *version = QEMU_VERSION; | |
43 | char *tmp; | |
44 | ||
45 | info->qemu.major = strtol(version, &tmp, 10); | |
46 | tmp++; | |
47 | info->qemu.minor = strtol(tmp, &tmp, 10); | |
48 | tmp++; | |
49 | info->qemu.micro = strtol(tmp, &tmp, 10); | |
50 | info->package = g_strdup(QEMU_PKGVERSION); | |
51 | ||
52 | return info; | |
53 | } | |
292a2602 LC |
54 | |
55 | KvmInfo *qmp_query_kvm(Error **errp) | |
56 | { | |
57 | KvmInfo *info = g_malloc0(sizeof(*info)); | |
58 | ||
59 | info->enabled = kvm_enabled(); | |
60 | info->present = kvm_available(); | |
61 | ||
62 | return info; | |
63 | } | |
64 | ||
efab767e LC |
65 | UuidInfo *qmp_query_uuid(Error **errp) |
66 | { | |
67 | UuidInfo *info = g_malloc0(sizeof(*info)); | |
68 | char uuid[64]; | |
69 | ||
70 | snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1], | |
71 | qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], | |
72 | qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], | |
73 | qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], | |
74 | qemu_uuid[14], qemu_uuid[15]); | |
75 | ||
76 | info->UUID = g_strdup(uuid); | |
77 | return info; | |
78 | } | |
79 | ||
7a7f325e LC |
80 | void qmp_quit(Error **err) |
81 | { | |
82 | no_shutdown = 0; | |
83 | qemu_system_shutdown_request(); | |
84 | } | |
85 | ||
5f158f21 LC |
86 | void qmp_stop(Error **errp) |
87 | { | |
88 | vm_stop(RUN_STATE_PAUSED); | |
89 | } | |
90 | ||
38d22653 LC |
91 | void qmp_system_reset(Error **errp) |
92 | { | |
93 | qemu_system_reset_request(); | |
94 | } | |
5bc465e4 LC |
95 | |
96 | void qmp_system_powerdown(Error **erp) | |
97 | { | |
98 | qemu_system_powerdown_request(); | |
99 | } | |
755f1968 LC |
100 | |
101 | void qmp_cpu(int64_t index, Error **errp) | |
102 | { | |
103 | /* Just do nothing */ | |
104 | } | |
2b54aa87 LC |
105 | |
106 | #ifndef CONFIG_VNC | |
107 | /* If VNC support is enabled, the "true" query-vnc command is | |
108 | defined in the VNC subsystem */ | |
109 | VncInfo *qmp_query_vnc(Error **errp) | |
110 | { | |
111 | error_set(errp, QERR_FEATURE_DISABLED, "vnc"); | |
112 | return NULL; | |
113 | }; | |
114 | #endif | |
d1f29646 LC |
115 | |
116 | #ifndef CONFIG_SPICE | |
117 | /* If SPICE support is enabled, the "true" query-spice command is | |
118 | defined in the SPICE subsystem. Also note that we use a small | |
119 | trick to maintain query-spice's original behavior, which is not | |
120 | to be available in the namespace if SPICE is not compiled in */ | |
121 | SpiceInfo *qmp_query_spice(Error **errp) | |
122 | { | |
123 | error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice"); | |
124 | return NULL; | |
125 | }; | |
126 | #endif | |
e42e818b LC |
127 | |
128 | static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs) | |
129 | { | |
130 | bdrv_iostatus_reset(bs); | |
131 | } | |
132 | ||
133 | static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs) | |
134 | { | |
135 | Error **err = opaque; | |
136 | ||
137 | if (!error_is_set(err) && bdrv_key_required(bs)) { | |
903a8814 LC |
138 | error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), |
139 | bdrv_get_encrypted_filename(bs)); | |
e42e818b LC |
140 | } |
141 | } | |
142 | ||
143 | void qmp_cont(Error **errp) | |
144 | { | |
145 | Error *local_err = NULL; | |
146 | ||
147 | if (runstate_check(RUN_STATE_INMIGRATE)) { | |
148 | error_set(errp, QERR_MIGRATION_EXPECTED); | |
149 | return; | |
150 | } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) || | |
151 | runstate_check(RUN_STATE_SHUTDOWN)) { | |
152 | error_set(errp, QERR_RESET_REQUIRED); | |
153 | return; | |
154 | } | |
155 | ||
156 | bdrv_iterate(iostatus_bdrv_it, NULL); | |
157 | bdrv_iterate(encrypted_bdrv_it, &local_err); | |
158 | if (local_err) { | |
159 | error_propagate(errp, local_err); | |
160 | return; | |
161 | } | |
162 | ||
163 | vm_start(); | |
164 | } | |
b4b12c62 AL |
165 | |
166 | DevicePropertyInfoList *qmp_qom_list(const char *path, Error **errp) | |
167 | { | |
168 | DeviceState *dev; | |
169 | bool ambiguous = false; | |
170 | DevicePropertyInfoList *props = NULL; | |
171 | DeviceProperty *prop; | |
172 | ||
173 | dev = qdev_resolve_path(path, &ambiguous); | |
174 | if (dev == NULL) { | |
175 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); | |
176 | return NULL; | |
177 | } | |
178 | ||
179 | QTAILQ_FOREACH(prop, &dev->properties, node) { | |
180 | DevicePropertyInfoList *entry = g_malloc0(sizeof(*entry)); | |
181 | ||
182 | entry->value = g_malloc0(sizeof(DevicePropertyInfo)); | |
183 | entry->next = props; | |
184 | props = entry; | |
185 | ||
186 | entry->value->name = g_strdup(prop->name); | |
187 | entry->value->type = g_strdup(prop->type); | |
188 | } | |
189 | ||
190 | return props; | |
191 | } | |
eb6e8ea5 AL |
192 | |
193 | /* FIXME: teach qapi about how to pass through Visitors */ | |
194 | int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret) | |
195 | { | |
196 | const char *path = qdict_get_str(qdict, "path"); | |
197 | const char *property = qdict_get_str(qdict, "property"); | |
198 | QObject *value = qdict_get(qdict, "value"); | |
199 | Error *local_err = NULL; | |
200 | QmpInputVisitor *mi; | |
201 | DeviceState *dev; | |
202 | ||
203 | dev = qdev_resolve_path(path, NULL); | |
204 | if (!dev) { | |
205 | error_set(&local_err, QERR_DEVICE_NOT_FOUND, path); | |
206 | goto out; | |
207 | } | |
208 | ||
209 | mi = qmp_input_visitor_new(value); | |
210 | qdev_property_set(dev, qmp_input_get_visitor(mi), property, &local_err); | |
211 | ||
212 | qmp_input_visitor_cleanup(mi); | |
213 | ||
214 | out: | |
215 | if (local_err) { | |
216 | qerror_report_err(local_err); | |
217 | error_free(local_err); | |
218 | return -1; | |
219 | } | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
224 | int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret) | |
225 | { | |
226 | const char *path = qdict_get_str(qdict, "path"); | |
227 | const char *property = qdict_get_str(qdict, "property"); | |
228 | Error *local_err = NULL; | |
229 | QmpOutputVisitor *mo; | |
230 | DeviceState *dev; | |
231 | ||
232 | dev = qdev_resolve_path(path, NULL); | |
233 | if (!dev) { | |
234 | error_set(&local_err, QERR_DEVICE_NOT_FOUND, path); | |
235 | goto out; | |
236 | } | |
237 | ||
238 | mo = qmp_output_visitor_new(); | |
239 | qdev_property_get(dev, qmp_output_get_visitor(mo), property, &local_err); | |
240 | if (!local_err) { | |
241 | *ret = qmp_output_get_qobject(mo); | |
242 | } | |
243 | ||
244 | qmp_output_visitor_cleanup(mo); | |
245 | ||
246 | out: | |
247 | if (local_err) { | |
248 | qerror_report_err(local_err); | |
249 | error_free(local_err); | |
250 | return -1; | |
251 | } | |
252 | ||
253 | return 0; | |
254 | } | |
fbf796fd LC |
255 | |
256 | void qmp_set_password(const char *protocol, const char *password, | |
257 | bool has_connected, const char *connected, Error **errp) | |
258 | { | |
259 | int disconnect_if_connected = 0; | |
260 | int fail_if_connected = 0; | |
261 | int rc; | |
262 | ||
263 | if (has_connected) { | |
264 | if (strcmp(connected, "fail") == 0) { | |
265 | fail_if_connected = 1; | |
266 | } else if (strcmp(connected, "disconnect") == 0) { | |
267 | disconnect_if_connected = 1; | |
268 | } else if (strcmp(connected, "keep") == 0) { | |
269 | /* nothing */ | |
270 | } else { | |
271 | error_set(errp, QERR_INVALID_PARAMETER, "connected"); | |
272 | return; | |
273 | } | |
274 | } | |
275 | ||
276 | if (strcmp(protocol, "spice") == 0) { | |
277 | if (!using_spice) { | |
278 | /* correct one? spice isn't a device ,,, */ | |
279 | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice"); | |
280 | return; | |
281 | } | |
282 | rc = qemu_spice_set_passwd(password, fail_if_connected, | |
283 | disconnect_if_connected); | |
284 | if (rc != 0) { | |
285 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
286 | } | |
287 | return; | |
288 | } | |
289 | ||
290 | if (strcmp(protocol, "vnc") == 0) { | |
291 | if (fail_if_connected || disconnect_if_connected) { | |
292 | /* vnc supports "connected=keep" only */ | |
293 | error_set(errp, QERR_INVALID_PARAMETER, "connected"); | |
294 | return; | |
295 | } | |
296 | /* Note that setting an empty password will not disable login through | |
297 | * this interface. */ | |
298 | rc = vnc_display_password(NULL, password); | |
299 | if (rc < 0) { | |
300 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
301 | } | |
302 | return; | |
303 | } | |
304 | ||
305 | error_set(errp, QERR_INVALID_PARAMETER, "protocol"); | |
306 | } | |
9ad5372d LC |
307 | |
308 | void qmp_expire_password(const char *protocol, const char *whenstr, | |
309 | Error **errp) | |
310 | { | |
311 | time_t when; | |
312 | int rc; | |
313 | ||
314 | if (strcmp(whenstr, "now") == 0) { | |
315 | when = 0; | |
316 | } else if (strcmp(whenstr, "never") == 0) { | |
317 | when = TIME_MAX; | |
318 | } else if (whenstr[0] == '+') { | |
319 | when = time(NULL) + strtoull(whenstr+1, NULL, 10); | |
320 | } else { | |
321 | when = strtoull(whenstr, NULL, 10); | |
322 | } | |
323 | ||
324 | if (strcmp(protocol, "spice") == 0) { | |
325 | if (!using_spice) { | |
326 | /* correct one? spice isn't a device ,,, */ | |
327 | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice"); | |
328 | return; | |
329 | } | |
330 | rc = qemu_spice_set_pw_expire(when); | |
331 | if (rc != 0) { | |
332 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
333 | } | |
334 | return; | |
335 | } | |
336 | ||
337 | if (strcmp(protocol, "vnc") == 0) { | |
338 | rc = vnc_display_pw_expire(NULL, when); | |
339 | if (rc != 0) { | |
340 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
341 | } | |
342 | return; | |
343 | } | |
344 | ||
345 | error_set(errp, QERR_INVALID_PARAMETER, "protocol"); | |
346 | } | |
270b243f LC |
347 | |
348 | void qmp_change_vnc_password(const char *password, Error **errp) | |
349 | { | |
350 | if (vnc_display_password(NULL, password) < 0) { | |
351 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
352 | } | |
353 | } |