Emulated CCID card: QOMify
[qemu/ar7.git] / qmp.c
blob6ae723022eb6c0cf24b46c1f837dd49e3fa1b599
1 /*
2 * QEMU Management Protocol
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #include "qemu-common.h"
18 #include "monitor/monitor.h"
19 #include "sysemu/sysemu.h"
20 #include "qmp-commands.h"
21 #include "sysemu/char.h"
22 #include "ui/qemu-spice.h"
23 #include "ui/vnc.h"
24 #include "sysemu/kvm.h"
25 #include "sysemu/arch_init.h"
26 #include "hw/qdev.h"
27 #include "sysemu/blockdev.h"
28 #include "sysemu/block-backend.h"
29 #include "qom/qom-qobject.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qobject.h"
32 #include "qapi/qmp-input-visitor.h"
33 #include "hw/boards.h"
34 #include "qom/object_interfaces.h"
35 #include "hw/mem/pc-dimm.h"
36 #include "hw/acpi/acpi_dev_interface.h"
38 NameInfo *qmp_query_name(Error **errp)
40 NameInfo *info = g_malloc0(sizeof(*info));
42 if (qemu_name) {
43 info->has_name = true;
44 info->name = g_strdup(qemu_name);
47 return info;
50 VersionInfo *qmp_query_version(Error **errp)
52 VersionInfo *info = g_new0(VersionInfo, 1);
53 const char *version = QEMU_VERSION;
54 const char *tmp;
55 int err;
57 info->qemu = g_new0(VersionTriple, 1);
58 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
59 assert(err == 0);
60 tmp++;
62 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
63 assert(err == 0);
64 tmp++;
66 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
67 assert(err == 0);
68 info->package = g_strdup(QEMU_PKGVERSION);
70 return info;
73 KvmInfo *qmp_query_kvm(Error **errp)
75 KvmInfo *info = g_malloc0(sizeof(*info));
77 info->enabled = kvm_enabled();
78 info->present = kvm_available();
80 return info;
83 UuidInfo *qmp_query_uuid(Error **errp)
85 UuidInfo *info = g_malloc0(sizeof(*info));
86 char uuid[64];
88 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
89 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
90 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
91 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
92 qemu_uuid[14], qemu_uuid[15]);
94 info->UUID = g_strdup(uuid);
95 return info;
98 void qmp_quit(Error **errp)
100 no_shutdown = 0;
101 qemu_system_shutdown_request();
104 void qmp_stop(Error **errp)
106 if (runstate_check(RUN_STATE_INMIGRATE)) {
107 autostart = 0;
108 } else {
109 vm_stop(RUN_STATE_PAUSED);
113 void qmp_system_reset(Error **errp)
115 qemu_system_reset_request();
118 void qmp_system_powerdown(Error **erp)
120 qemu_system_powerdown_request();
123 void qmp_cpu(int64_t index, Error **errp)
125 /* Just do nothing */
128 void qmp_cpu_add(int64_t id, Error **errp)
130 MachineClass *mc;
132 mc = MACHINE_GET_CLASS(current_machine);
133 if (mc->hot_add_cpu) {
134 mc->hot_add_cpu(id, errp);
135 } else {
136 error_setg(errp, "Not supported");
140 #ifndef CONFIG_VNC
141 /* If VNC support is enabled, the "true" query-vnc command is
142 defined in the VNC subsystem */
143 VncInfo *qmp_query_vnc(Error **errp)
145 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
146 return NULL;
149 VncInfo2List *qmp_query_vnc_servers(Error **errp)
151 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
152 return NULL;
154 #endif
156 #ifndef CONFIG_SPICE
158 * qmp-commands.hx ensures that QMP command query-spice exists only
159 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
160 * result. However, the QAPI schema is blissfully unaware of that,
161 * and the QAPI code generator happily generates a dead
162 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
163 * one, or else linking fails. FIXME Educate the QAPI schema on
164 * CONFIG_SPICE.
166 SpiceInfo *qmp_query_spice(Error **errp)
168 abort();
170 #endif
172 void qmp_cont(Error **errp)
174 Error *local_err = NULL;
175 BlockBackend *blk;
176 BlockDriverState *bs;
178 if (runstate_needs_reset()) {
179 error_setg(errp, "Resetting the Virtual Machine is required");
180 return;
181 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
182 return;
185 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
186 blk_iostatus_reset(blk);
188 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
189 bdrv_add_key(bs, NULL, &local_err);
190 if (local_err) {
191 error_propagate(errp, local_err);
192 return;
196 /* Continuing after completed migration. Images have been inactivated to
197 * allow the destination to take control. Need to get control back now. */
198 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
199 runstate_check(RUN_STATE_POSTMIGRATE))
201 bdrv_invalidate_cache_all(&local_err);
202 if (local_err) {
203 error_propagate(errp, local_err);
204 return;
208 if (runstate_check(RUN_STATE_INMIGRATE)) {
209 autostart = 1;
210 } else {
211 vm_start();
215 void qmp_system_wakeup(Error **errp)
217 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
220 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
222 Object *obj;
223 bool ambiguous = false;
224 ObjectPropertyInfoList *props = NULL;
225 ObjectProperty *prop;
226 ObjectPropertyIterator iter;
228 obj = object_resolve_path(path, &ambiguous);
229 if (obj == NULL) {
230 if (ambiguous) {
231 error_setg(errp, "Path '%s' is ambiguous", path);
232 } else {
233 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
234 "Device '%s' not found", path);
236 return NULL;
239 object_property_iter_init(&iter, obj);
240 while ((prop = object_property_iter_next(&iter))) {
241 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
243 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
244 entry->next = props;
245 props = entry;
247 entry->value->name = g_strdup(prop->name);
248 entry->value->type = g_strdup(prop->type);
251 return props;
254 void qmp_qom_set(const char *path, const char *property, QObject *value,
255 Error **errp)
257 Object *obj;
259 obj = object_resolve_path(path, NULL);
260 if (!obj) {
261 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
262 "Device '%s' not found", path);
263 return;
266 object_property_set_qobject(obj, value, property, errp);
269 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
271 Object *obj;
273 obj = object_resolve_path(path, NULL);
274 if (!obj) {
275 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
276 "Device '%s' not found", path);
277 return NULL;
280 return object_property_get_qobject(obj, property, errp);
283 void qmp_set_password(const char *protocol, const char *password,
284 bool has_connected, const char *connected, Error **errp)
286 int disconnect_if_connected = 0;
287 int fail_if_connected = 0;
288 int rc;
290 if (has_connected) {
291 if (strcmp(connected, "fail") == 0) {
292 fail_if_connected = 1;
293 } else if (strcmp(connected, "disconnect") == 0) {
294 disconnect_if_connected = 1;
295 } else if (strcmp(connected, "keep") == 0) {
296 /* nothing */
297 } else {
298 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
299 return;
303 if (strcmp(protocol, "spice") == 0) {
304 if (!qemu_using_spice(errp)) {
305 return;
307 rc = qemu_spice_set_passwd(password, fail_if_connected,
308 disconnect_if_connected);
309 if (rc != 0) {
310 error_setg(errp, QERR_SET_PASSWD_FAILED);
312 return;
315 if (strcmp(protocol, "vnc") == 0) {
316 if (fail_if_connected || disconnect_if_connected) {
317 /* vnc supports "connected=keep" only */
318 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
319 return;
321 /* Note that setting an empty password will not disable login through
322 * this interface. */
323 rc = vnc_display_password(NULL, password);
324 if (rc < 0) {
325 error_setg(errp, QERR_SET_PASSWD_FAILED);
327 return;
330 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
333 void qmp_expire_password(const char *protocol, const char *whenstr,
334 Error **errp)
336 time_t when;
337 int rc;
339 if (strcmp(whenstr, "now") == 0) {
340 when = 0;
341 } else if (strcmp(whenstr, "never") == 0) {
342 when = TIME_MAX;
343 } else if (whenstr[0] == '+') {
344 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
345 } else {
346 when = strtoull(whenstr, NULL, 10);
349 if (strcmp(protocol, "spice") == 0) {
350 if (!qemu_using_spice(errp)) {
351 return;
353 rc = qemu_spice_set_pw_expire(when);
354 if (rc != 0) {
355 error_setg(errp, QERR_SET_PASSWD_FAILED);
357 return;
360 if (strcmp(protocol, "vnc") == 0) {
361 rc = vnc_display_pw_expire(NULL, when);
362 if (rc != 0) {
363 error_setg(errp, QERR_SET_PASSWD_FAILED);
365 return;
368 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
371 #ifdef CONFIG_VNC
372 void qmp_change_vnc_password(const char *password, Error **errp)
374 if (vnc_display_password(NULL, password) < 0) {
375 error_setg(errp, QERR_SET_PASSWD_FAILED);
379 static void qmp_change_vnc_listen(const char *target, Error **errp)
381 QemuOptsList *olist = qemu_find_opts("vnc");
382 QemuOpts *opts;
384 if (strstr(target, "id=")) {
385 error_setg(errp, "id not supported");
386 return;
389 opts = qemu_opts_find(olist, "default");
390 if (opts) {
391 qemu_opts_del(opts);
393 opts = vnc_parse(target, errp);
394 if (!opts) {
395 return;
398 vnc_display_open("default", errp);
401 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
402 Error **errp)
404 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
405 if (!has_arg) {
406 error_setg(errp, QERR_MISSING_PARAMETER, "password");
407 } else {
408 qmp_change_vnc_password(arg, errp);
410 } else {
411 qmp_change_vnc_listen(target, errp);
414 #else
415 void qmp_change_vnc_password(const char *password, Error **errp)
417 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
419 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
420 Error **errp)
422 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
424 #endif /* !CONFIG_VNC */
426 void qmp_change(const char *device, const char *target,
427 bool has_arg, const char *arg, Error **errp)
429 if (strcmp(device, "vnc") == 0) {
430 qmp_change_vnc(target, has_arg, arg, errp);
431 } else {
432 qmp_blockdev_change_medium(device, target, has_arg, arg, false, 0,
433 errp);
437 static void qom_list_types_tramp(ObjectClass *klass, void *data)
439 ObjectTypeInfoList *e, **pret = data;
440 ObjectTypeInfo *info;
442 info = g_malloc0(sizeof(*info));
443 info->name = g_strdup(object_class_get_name(klass));
445 e = g_malloc0(sizeof(*e));
446 e->value = info;
447 e->next = *pret;
448 *pret = e;
451 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
452 const char *implements,
453 bool has_abstract,
454 bool abstract,
455 Error **errp)
457 ObjectTypeInfoList *ret = NULL;
459 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
461 return ret;
464 /* Return a DevicePropertyInfo for a qdev property.
466 * If a qdev property with the given name does not exist, use the given default
467 * type. If the qdev property info should not be shown, return NULL.
469 * The caller must free the return value.
471 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
472 const char *name,
473 const char *default_type,
474 const char *description)
476 DevicePropertyInfo *info;
477 Property *prop;
479 do {
480 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
481 if (strcmp(name, prop->name) != 0) {
482 continue;
486 * TODO Properties without a parser are just for dirty hacks.
487 * qdev_prop_ptr is the only such PropertyInfo. It's marked
488 * for removal. This conditional should be removed along with
489 * it.
491 if (!prop->info->set) {
492 return NULL; /* no way to set it, don't show */
495 info = g_malloc0(sizeof(*info));
496 info->name = g_strdup(prop->name);
497 info->type = g_strdup(prop->info->name);
498 info->has_description = !!prop->info->description;
499 info->description = g_strdup(prop->info->description);
500 return info;
502 klass = object_class_get_parent(klass);
503 } while (klass != object_class_by_name(TYPE_DEVICE));
505 /* Not a qdev property, use the default type */
506 info = g_malloc0(sizeof(*info));
507 info->name = g_strdup(name);
508 info->type = g_strdup(default_type);
509 info->has_description = !!description;
510 info->description = g_strdup(description);
512 return info;
515 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
516 Error **errp)
518 ObjectClass *klass;
519 Object *obj;
520 ObjectProperty *prop;
521 ObjectPropertyIterator iter;
522 DevicePropertyInfoList *prop_list = NULL;
524 klass = object_class_by_name(typename);
525 if (klass == NULL) {
526 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
527 "Device '%s' not found", typename);
528 return NULL;
531 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
532 if (klass == NULL) {
533 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
534 return NULL;
537 if (object_class_is_abstract(klass)) {
538 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
539 "non-abstract device type");
540 return NULL;
543 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
544 error_setg(errp, "Can't list properties of device '%s'", typename);
545 return NULL;
548 obj = object_new(typename);
550 object_property_iter_init(&iter, obj);
551 while ((prop = object_property_iter_next(&iter))) {
552 DevicePropertyInfo *info;
553 DevicePropertyInfoList *entry;
555 /* Skip Object and DeviceState properties */
556 if (strcmp(prop->name, "type") == 0 ||
557 strcmp(prop->name, "realized") == 0 ||
558 strcmp(prop->name, "hotpluggable") == 0 ||
559 strcmp(prop->name, "hotplugged") == 0 ||
560 strcmp(prop->name, "parent_bus") == 0) {
561 continue;
564 /* Skip legacy properties since they are just string versions of
565 * properties that we already list.
567 if (strstart(prop->name, "legacy-", NULL)) {
568 continue;
571 info = make_device_property_info(klass, prop->name, prop->type,
572 prop->description);
573 if (!info) {
574 continue;
577 entry = g_malloc0(sizeof(*entry));
578 entry->value = info;
579 entry->next = prop_list;
580 prop_list = entry;
583 object_unref(obj);
585 return prop_list;
588 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
590 return arch_query_cpu_definitions(errp);
593 void qmp_add_client(const char *protocol, const char *fdname,
594 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
595 Error **errp)
597 CharDriverState *s;
598 int fd;
600 fd = monitor_get_fd(cur_mon, fdname, errp);
601 if (fd < 0) {
602 return;
605 if (strcmp(protocol, "spice") == 0) {
606 if (!qemu_using_spice(errp)) {
607 close(fd);
608 return;
610 skipauth = has_skipauth ? skipauth : false;
611 tls = has_tls ? tls : false;
612 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
613 error_setg(errp, "spice failed to add client");
614 close(fd);
616 return;
617 #ifdef CONFIG_VNC
618 } else if (strcmp(protocol, "vnc") == 0) {
619 skipauth = has_skipauth ? skipauth : false;
620 vnc_display_add_client(NULL, fd, skipauth);
621 return;
622 #endif
623 } else if ((s = qemu_chr_find(protocol)) != NULL) {
624 if (qemu_chr_add_client(s, fd) < 0) {
625 error_setg(errp, "failed to add client");
626 close(fd);
627 return;
629 return;
632 error_setg(errp, "protocol '%s' is invalid", protocol);
633 close(fd);
636 void object_add(const char *type, const char *id, const QDict *qdict,
637 Visitor *v, Error **errp)
639 Object *obj;
640 ObjectClass *klass;
641 const QDictEntry *e;
642 Error *local_err = NULL;
644 klass = object_class_by_name(type);
645 if (!klass) {
646 error_setg(errp, "invalid object type: %s", type);
647 return;
650 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
651 error_setg(errp, "object type '%s' isn't supported by object-add",
652 type);
653 return;
656 if (object_class_is_abstract(klass)) {
657 error_setg(errp, "object type '%s' is abstract", type);
658 return;
661 obj = object_new(type);
662 if (qdict) {
663 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
664 object_property_set(obj, v, e->key, &local_err);
665 if (local_err) {
666 goto out;
671 object_property_add_child(object_get_objects_root(),
672 id, obj, &local_err);
673 if (local_err) {
674 goto out;
677 user_creatable_complete(obj, &local_err);
678 if (local_err) {
679 object_property_del(object_get_objects_root(),
680 id, &error_abort);
681 goto out;
683 out:
684 if (local_err) {
685 error_propagate(errp, local_err);
687 object_unref(obj);
690 void qmp_object_add(const char *type, const char *id,
691 bool has_props, QObject *props, Error **errp)
693 const QDict *pdict = NULL;
694 QmpInputVisitor *qiv;
696 if (props) {
697 pdict = qobject_to_qdict(props);
698 if (!pdict) {
699 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
700 return;
704 qiv = qmp_input_visitor_new(props);
705 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
706 qmp_input_visitor_cleanup(qiv);
709 void qmp_object_del(const char *id, Error **errp)
711 Object *container;
712 Object *obj;
714 container = object_get_objects_root();
715 obj = object_resolve_path_component(container, id);
716 if (!obj) {
717 error_setg(errp, "object id not found");
718 return;
721 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
722 error_setg(errp, "%s is in use, can not be deleted", id);
723 return;
725 object_unparent(obj);
728 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
730 MemoryDeviceInfoList *head = NULL;
731 MemoryDeviceInfoList **prev = &head;
733 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
735 return head;
738 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
740 bool ambig;
741 ACPIOSTInfoList *head = NULL;
742 ACPIOSTInfoList **prev = &head;
743 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
745 if (obj) {
746 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
747 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
749 adevc->ospm_status(adev, &prev);
750 } else {
751 error_setg(errp, "command is not supported, missing ACPI device");
754 return head;