]>
Commit | Line | Data |
---|---|---|
48a32bed AL |
1 | /* |
2 | * QEMU Management Protocol | |
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 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" |
333a96ec | 24 | #include "blockdev.h" |
9f5f1350 | 25 | #include "qemu/qom-qobject.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 | { | |
1e998146 PB |
88 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
89 | autostart = 0; | |
90 | } else { | |
91 | vm_stop(RUN_STATE_PAUSED); | |
92 | } | |
5f158f21 LC |
93 | } |
94 | ||
38d22653 LC |
95 | void qmp_system_reset(Error **errp) |
96 | { | |
97 | qemu_system_reset_request(); | |
98 | } | |
5bc465e4 LC |
99 | |
100 | void qmp_system_powerdown(Error **erp) | |
101 | { | |
102 | qemu_system_powerdown_request(); | |
103 | } | |
755f1968 LC |
104 | |
105 | void qmp_cpu(int64_t index, Error **errp) | |
106 | { | |
107 | /* Just do nothing */ | |
108 | } | |
2b54aa87 LC |
109 | |
110 | #ifndef CONFIG_VNC | |
111 | /* If VNC support is enabled, the "true" query-vnc command is | |
112 | defined in the VNC subsystem */ | |
113 | VncInfo *qmp_query_vnc(Error **errp) | |
114 | { | |
115 | error_set(errp, QERR_FEATURE_DISABLED, "vnc"); | |
116 | return NULL; | |
117 | }; | |
118 | #endif | |
d1f29646 LC |
119 | |
120 | #ifndef CONFIG_SPICE | |
121 | /* If SPICE support is enabled, the "true" query-spice command is | |
122 | defined in the SPICE subsystem. Also note that we use a small | |
123 | trick to maintain query-spice's original behavior, which is not | |
124 | to be available in the namespace if SPICE is not compiled in */ | |
125 | SpiceInfo *qmp_query_spice(Error **errp) | |
126 | { | |
127 | error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice"); | |
128 | return NULL; | |
129 | }; | |
130 | #endif | |
e42e818b LC |
131 | |
132 | static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs) | |
133 | { | |
134 | bdrv_iostatus_reset(bs); | |
135 | } | |
136 | ||
137 | static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs) | |
138 | { | |
139 | Error **err = opaque; | |
140 | ||
141 | if (!error_is_set(err) && bdrv_key_required(bs)) { | |
903a8814 LC |
142 | error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), |
143 | bdrv_get_encrypted_filename(bs)); | |
e42e818b LC |
144 | } |
145 | } | |
146 | ||
147 | void qmp_cont(Error **errp) | |
148 | { | |
149 | Error *local_err = NULL; | |
150 | ||
1e998146 | 151 | if (runstate_check(RUN_STATE_INTERNAL_ERROR) || |
e42e818b LC |
152 | runstate_check(RUN_STATE_SHUTDOWN)) { |
153 | error_set(errp, QERR_RESET_REQUIRED); | |
154 | return; | |
ad02b96a LC |
155 | } else if (runstate_check(RUN_STATE_SUSPENDED)) { |
156 | return; | |
e42e818b LC |
157 | } |
158 | ||
159 | bdrv_iterate(iostatus_bdrv_it, NULL); | |
160 | bdrv_iterate(encrypted_bdrv_it, &local_err); | |
161 | if (local_err) { | |
162 | error_propagate(errp, local_err); | |
163 | return; | |
164 | } | |
165 | ||
1e998146 PB |
166 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
167 | autostart = 1; | |
168 | } else { | |
169 | vm_start(); | |
170 | } | |
e42e818b | 171 | } |
b4b12c62 | 172 | |
9b9df25a GH |
173 | void qmp_system_wakeup(Error **errp) |
174 | { | |
175 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); | |
176 | } | |
177 | ||
57c9fafe | 178 | ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp) |
b4b12c62 | 179 | { |
57c9fafe | 180 | Object *obj; |
b4b12c62 | 181 | bool ambiguous = false; |
57c9fafe AL |
182 | ObjectPropertyInfoList *props = NULL; |
183 | ObjectProperty *prop; | |
b4b12c62 | 184 | |
57c9fafe AL |
185 | obj = object_resolve_path(path, &ambiguous); |
186 | if (obj == NULL) { | |
b4b12c62 AL |
187 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); |
188 | return NULL; | |
189 | } | |
190 | ||
57c9fafe AL |
191 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
192 | ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry)); | |
b4b12c62 | 193 | |
57c9fafe | 194 | entry->value = g_malloc0(sizeof(ObjectPropertyInfo)); |
b4b12c62 AL |
195 | entry->next = props; |
196 | props = entry; | |
197 | ||
198 | entry->value->name = g_strdup(prop->name); | |
199 | entry->value->type = g_strdup(prop->type); | |
200 | } | |
201 | ||
202 | return props; | |
203 | } | |
eb6e8ea5 AL |
204 | |
205 | /* FIXME: teach qapi about how to pass through Visitors */ | |
206 | int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret) | |
207 | { | |
208 | const char *path = qdict_get_str(qdict, "path"); | |
209 | const char *property = qdict_get_str(qdict, "property"); | |
210 | QObject *value = qdict_get(qdict, "value"); | |
211 | Error *local_err = NULL; | |
57c9fafe | 212 | Object *obj; |
eb6e8ea5 | 213 | |
57c9fafe AL |
214 | obj = object_resolve_path(path, NULL); |
215 | if (!obj) { | |
eb6e8ea5 AL |
216 | error_set(&local_err, QERR_DEVICE_NOT_FOUND, path); |
217 | goto out; | |
218 | } | |
219 | ||
9f5f1350 | 220 | object_property_set_qobject(obj, value, property, &local_err); |
eb6e8ea5 AL |
221 | |
222 | out: | |
223 | if (local_err) { | |
224 | qerror_report_err(local_err); | |
225 | error_free(local_err); | |
226 | return -1; | |
227 | } | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
232 | int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret) | |
233 | { | |
234 | const char *path = qdict_get_str(qdict, "path"); | |
235 | const char *property = qdict_get_str(qdict, "property"); | |
236 | Error *local_err = NULL; | |
57c9fafe | 237 | Object *obj; |
eb6e8ea5 | 238 | |
57c9fafe AL |
239 | obj = object_resolve_path(path, NULL); |
240 | if (!obj) { | |
eb6e8ea5 AL |
241 | error_set(&local_err, QERR_DEVICE_NOT_FOUND, path); |
242 | goto out; | |
243 | } | |
244 | ||
9f5f1350 | 245 | *ret = object_property_get_qobject(obj, property, &local_err); |
eb6e8ea5 AL |
246 | |
247 | out: | |
248 | if (local_err) { | |
249 | qerror_report_err(local_err); | |
250 | error_free(local_err); | |
251 | return -1; | |
252 | } | |
253 | ||
254 | return 0; | |
255 | } | |
fbf796fd LC |
256 | |
257 | void qmp_set_password(const char *protocol, const char *password, | |
258 | bool has_connected, const char *connected, Error **errp) | |
259 | { | |
260 | int disconnect_if_connected = 0; | |
261 | int fail_if_connected = 0; | |
262 | int rc; | |
263 | ||
264 | if (has_connected) { | |
265 | if (strcmp(connected, "fail") == 0) { | |
266 | fail_if_connected = 1; | |
267 | } else if (strcmp(connected, "disconnect") == 0) { | |
268 | disconnect_if_connected = 1; | |
269 | } else if (strcmp(connected, "keep") == 0) { | |
270 | /* nothing */ | |
271 | } else { | |
272 | error_set(errp, QERR_INVALID_PARAMETER, "connected"); | |
273 | return; | |
274 | } | |
275 | } | |
276 | ||
277 | if (strcmp(protocol, "spice") == 0) { | |
278 | if (!using_spice) { | |
279 | /* correct one? spice isn't a device ,,, */ | |
280 | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice"); | |
281 | return; | |
282 | } | |
283 | rc = qemu_spice_set_passwd(password, fail_if_connected, | |
284 | disconnect_if_connected); | |
285 | if (rc != 0) { | |
286 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
287 | } | |
288 | return; | |
289 | } | |
290 | ||
291 | if (strcmp(protocol, "vnc") == 0) { | |
292 | if (fail_if_connected || disconnect_if_connected) { | |
293 | /* vnc supports "connected=keep" only */ | |
294 | error_set(errp, QERR_INVALID_PARAMETER, "connected"); | |
295 | return; | |
296 | } | |
297 | /* Note that setting an empty password will not disable login through | |
298 | * this interface. */ | |
299 | rc = vnc_display_password(NULL, password); | |
300 | if (rc < 0) { | |
301 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
302 | } | |
303 | return; | |
304 | } | |
305 | ||
306 | error_set(errp, QERR_INVALID_PARAMETER, "protocol"); | |
307 | } | |
9ad5372d LC |
308 | |
309 | void qmp_expire_password(const char *protocol, const char *whenstr, | |
310 | Error **errp) | |
311 | { | |
312 | time_t when; | |
313 | int rc; | |
314 | ||
315 | if (strcmp(whenstr, "now") == 0) { | |
316 | when = 0; | |
317 | } else if (strcmp(whenstr, "never") == 0) { | |
318 | when = TIME_MAX; | |
319 | } else if (whenstr[0] == '+') { | |
320 | when = time(NULL) + strtoull(whenstr+1, NULL, 10); | |
321 | } else { | |
322 | when = strtoull(whenstr, NULL, 10); | |
323 | } | |
324 | ||
325 | if (strcmp(protocol, "spice") == 0) { | |
326 | if (!using_spice) { | |
327 | /* correct one? spice isn't a device ,,, */ | |
328 | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice"); | |
329 | return; | |
330 | } | |
331 | rc = qemu_spice_set_pw_expire(when); | |
332 | if (rc != 0) { | |
333 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
334 | } | |
335 | return; | |
336 | } | |
337 | ||
338 | if (strcmp(protocol, "vnc") == 0) { | |
339 | rc = vnc_display_pw_expire(NULL, when); | |
340 | if (rc != 0) { | |
341 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
342 | } | |
343 | return; | |
344 | } | |
345 | ||
346 | error_set(errp, QERR_INVALID_PARAMETER, "protocol"); | |
347 | } | |
270b243f | 348 | |
333a96ec | 349 | #ifdef CONFIG_VNC |
270b243f LC |
350 | void qmp_change_vnc_password(const char *password, Error **errp) |
351 | { | |
352 | if (vnc_display_password(NULL, password) < 0) { | |
353 | error_set(errp, QERR_SET_PASSWD_FAILED); | |
354 | } | |
355 | } | |
333a96ec | 356 | |
007fcd3e | 357 | static void qmp_change_vnc_listen(const char *target, Error **errp) |
333a96ec | 358 | { |
007fcd3e | 359 | vnc_display_open(NULL, target, errp); |
333a96ec LC |
360 | } |
361 | ||
362 | static void qmp_change_vnc(const char *target, bool has_arg, const char *arg, | |
363 | Error **errp) | |
364 | { | |
365 | if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) { | |
366 | if (!has_arg) { | |
367 | error_set(errp, QERR_MISSING_PARAMETER, "password"); | |
368 | } else { | |
369 | qmp_change_vnc_password(arg, errp); | |
370 | } | |
371 | } else { | |
372 | qmp_change_vnc_listen(target, errp); | |
373 | } | |
374 | } | |
375 | #else | |
376 | void qmp_change_vnc_password(const char *password, Error **errp) | |
377 | { | |
378 | error_set(errp, QERR_FEATURE_DISABLED, "vnc"); | |
379 | } | |
380 | static void qmp_change_vnc(const char *target, bool has_arg, const char *arg, | |
381 | Error **errp) | |
382 | { | |
383 | error_set(errp, QERR_FEATURE_DISABLED, "vnc"); | |
384 | } | |
385 | #endif /* !CONFIG_VNC */ | |
386 | ||
387 | void qmp_change(const char *device, const char *target, | |
388 | bool has_arg, const char *arg, Error **err) | |
389 | { | |
390 | if (strcmp(device, "vnc") == 0) { | |
391 | qmp_change_vnc(target, has_arg, arg, err); | |
392 | } else { | |
393 | qmp_change_blockdev(device, target, has_arg, arg, err); | |
394 | } | |
395 | } | |
5eeee3fa AL |
396 | |
397 | static void qom_list_types_tramp(ObjectClass *klass, void *data) | |
398 | { | |
399 | ObjectTypeInfoList *e, **pret = data; | |
400 | ObjectTypeInfo *info; | |
401 | ||
402 | info = g_malloc0(sizeof(*info)); | |
403 | info->name = g_strdup(object_class_get_name(klass)); | |
404 | ||
405 | e = g_malloc0(sizeof(*e)); | |
406 | e->value = info; | |
407 | e->next = *pret; | |
408 | *pret = e; | |
409 | } | |
410 | ||
411 | ObjectTypeInfoList *qmp_qom_list_types(bool has_implements, | |
412 | const char *implements, | |
413 | bool has_abstract, | |
414 | bool abstract, | |
415 | Error **errp) | |
416 | { | |
417 | ObjectTypeInfoList *ret = NULL; | |
418 | ||
419 | object_class_foreach(qom_list_types_tramp, implements, abstract, &ret); | |
420 | ||
421 | return ret; | |
422 | } | |
1daa31b9 AL |
423 | |
424 | DevicePropertyInfoList *qmp_device_list_properties(const char *typename, | |
425 | Error **errp) | |
426 | { | |
427 | ObjectClass *klass; | |
428 | Property *prop; | |
429 | DevicePropertyInfoList *prop_list = NULL; | |
430 | ||
431 | klass = object_class_by_name(typename); | |
432 | if (klass == NULL) { | |
433 | error_set(errp, QERR_DEVICE_NOT_FOUND, typename); | |
434 | return NULL; | |
435 | } | |
436 | ||
437 | klass = object_class_dynamic_cast(klass, TYPE_DEVICE); | |
438 | if (klass == NULL) { | |
439 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, | |
440 | "name", TYPE_DEVICE); | |
441 | return NULL; | |
442 | } | |
443 | ||
444 | do { | |
445 | for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) { | |
446 | DevicePropertyInfoList *entry; | |
447 | DevicePropertyInfo *info; | |
448 | ||
449 | /* | |
450 | * TODO Properties without a parser are just for dirty hacks. | |
451 | * qdev_prop_ptr is the only such PropertyInfo. It's marked | |
452 | * for removal. This conditional should be removed along with | |
453 | * it. | |
454 | */ | |
455 | if (!prop->info->set) { | |
456 | continue; /* no way to set it, don't show */ | |
457 | } | |
458 | ||
459 | info = g_malloc0(sizeof(*info)); | |
460 | info->name = g_strdup(prop->name); | |
461 | info->type = g_strdup(prop->info->legacy_name ?: prop->info->name); | |
462 | ||
463 | entry = g_malloc0(sizeof(*entry)); | |
464 | entry->value = info; | |
465 | entry->next = prop_list; | |
466 | prop_list = entry; | |
467 | } | |
468 | klass = object_class_get_parent(klass); | |
469 | } while (klass != object_class_by_name(TYPE_DEVICE)); | |
470 | ||
471 | return prop_list; | |
472 | } | |
e4e31c63 | 473 | |
76b64a7a | 474 | CpuDefinitionInfoList GCC_WEAK *arch_query_cpu_definitions(Error **errp) |
e4e31c63 AL |
475 | { |
476 | error_set(errp, QERR_NOT_SUPPORTED); | |
477 | return NULL; | |
478 | } | |
76b64a7a AL |
479 | |
480 | CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp) | |
481 | { | |
482 | return arch_query_cpu_definitions(errp); | |
483 | } | |
484 | ||
b224e5e2 LC |
485 | void qmp_add_client(const char *protocol, const char *fdname, |
486 | bool has_skipauth, bool skipauth, bool has_tls, bool tls, | |
487 | Error **errp) | |
488 | { | |
489 | CharDriverState *s; | |
490 | int fd; | |
491 | ||
492 | fd = monitor_get_fd(cur_mon, fdname, errp); | |
493 | if (fd < 0) { | |
494 | return; | |
495 | } | |
496 | ||
497 | if (strcmp(protocol, "spice") == 0) { | |
498 | if (!using_spice) { | |
499 | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice"); | |
500 | close(fd); | |
501 | return; | |
502 | } | |
503 | skipauth = has_skipauth ? skipauth : false; | |
504 | tls = has_tls ? tls : false; | |
505 | if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) { | |
506 | error_setg(errp, "spice failed to add client"); | |
507 | close(fd); | |
508 | } | |
509 | return; | |
510 | #ifdef CONFIG_VNC | |
511 | } else if (strcmp(protocol, "vnc") == 0) { | |
512 | skipauth = has_skipauth ? skipauth : false; | |
513 | vnc_display_add_client(NULL, fd, skipauth); | |
514 | return; | |
515 | #endif | |
516 | } else if ((s = qemu_chr_find(protocol)) != NULL) { | |
517 | if (qemu_chr_add_client(s, fd) < 0) { | |
518 | error_setg(errp, "failed to add client"); | |
519 | close(fd); | |
520 | return; | |
521 | } | |
522 | return; | |
523 | } | |
524 | ||
525 | error_setg(errp, "protocol '%s' is invalid", protocol); | |
526 | close(fd); | |
527 | } |