tpm: Avoid qerror_report() outside QMP command handlers
[qemu/ar7.git] / qmp.c
blob3ffb34915c7fd480a14c2276e803543e5c1a4b40
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-common.h"
17 #include "sysemu/sysemu.h"
18 #include "qmp-commands.h"
19 #include "sysemu/char.h"
20 #include "ui/qemu-spice.h"
21 #include "ui/vnc.h"
22 #include "sysemu/kvm.h"
23 #include "sysemu/arch_init.h"
24 #include "hw/qdev.h"
25 #include "sysemu/blockdev.h"
26 #include "qom/qom-qobject.h"
27 #include "qapi/qmp/qobject.h"
28 #include "qapi/qmp-input-visitor.h"
29 #include "hw/boards.h"
30 #include "qom/object_interfaces.h"
31 #include "hw/mem/pc-dimm.h"
32 #include "hw/acpi/acpi_dev_interface.h"
34 NameInfo *qmp_query_name(Error **errp)
36 NameInfo *info = g_malloc0(sizeof(*info));
38 if (qemu_name) {
39 info->has_name = true;
40 info->name = g_strdup(qemu_name);
43 return info;
46 VersionInfo *qmp_query_version(Error **errp)
48 VersionInfo *info = g_new0(VersionInfo, 1);
49 const char *version = QEMU_VERSION;
50 char *tmp;
52 info->qemu = g_new0(VersionTriple, 1);
53 info->qemu->major = strtol(version, &tmp, 10);
54 tmp++;
55 info->qemu->minor = strtol(tmp, &tmp, 10);
56 tmp++;
57 info->qemu->micro = strtol(tmp, &tmp, 10);
58 info->package = g_strdup(QEMU_PKGVERSION);
60 return info;
63 KvmInfo *qmp_query_kvm(Error **errp)
65 KvmInfo *info = g_malloc0(sizeof(*info));
67 info->enabled = kvm_enabled();
68 info->present = kvm_available();
70 return info;
73 UuidInfo *qmp_query_uuid(Error **errp)
75 UuidInfo *info = g_malloc0(sizeof(*info));
76 char uuid[64];
78 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
79 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
80 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
81 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
82 qemu_uuid[14], qemu_uuid[15]);
84 info->UUID = g_strdup(uuid);
85 return info;
88 void qmp_quit(Error **errp)
90 no_shutdown = 0;
91 qemu_system_shutdown_request();
94 void qmp_stop(Error **errp)
96 if (runstate_check(RUN_STATE_INMIGRATE)) {
97 autostart = 0;
98 } else {
99 vm_stop(RUN_STATE_PAUSED);
103 void qmp_system_reset(Error **errp)
105 qemu_system_reset_request();
108 void qmp_system_powerdown(Error **erp)
110 qemu_system_powerdown_request();
113 void qmp_cpu(int64_t index, Error **errp)
115 /* Just do nothing */
118 void qmp_cpu_add(int64_t id, Error **errp)
120 MachineClass *mc;
122 mc = MACHINE_GET_CLASS(current_machine);
123 if (mc->hot_add_cpu) {
124 mc->hot_add_cpu(id, errp);
125 } else {
126 error_setg(errp, "Not supported");
130 #ifndef CONFIG_VNC
131 /* If VNC support is enabled, the "true" query-vnc command is
132 defined in the VNC subsystem */
133 VncInfo *qmp_query_vnc(Error **errp)
135 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
136 return NULL;
139 VncInfo2List *qmp_query_vnc_servers(Error **errp)
141 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
142 return NULL;
144 #endif
146 #ifndef CONFIG_SPICE
148 * qmp-commands.hx ensures that QMP command query-spice exists only
149 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
150 * result. However, the QAPI schema is blissfully unaware of that,
151 * and the QAPI code generator happily generates a dead
152 * qmp_marshal_input_query_spice() that calls qmp_query_spice().
153 * Provide it one, or else linking fails.
154 * FIXME Educate the QAPI schema on CONFIG_SPICE.
156 SpiceInfo *qmp_query_spice(Error **errp)
158 abort();
160 #endif
162 void qmp_cont(Error **errp)
164 Error *local_err = NULL;
165 BlockDriverState *bs;
167 if (runstate_needs_reset()) {
168 error_setg(errp, "Resetting the Virtual Machine is required");
169 return;
170 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
171 return;
174 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
175 bdrv_iostatus_reset(bs);
177 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
178 bdrv_add_key(bs, NULL, &local_err);
179 if (local_err) {
180 error_propagate(errp, local_err);
181 return;
185 if (runstate_check(RUN_STATE_INMIGRATE)) {
186 autostart = 1;
187 } else {
188 vm_start();
192 void qmp_system_wakeup(Error **errp)
194 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
197 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
199 Object *obj;
200 bool ambiguous = false;
201 ObjectPropertyInfoList *props = NULL;
202 ObjectProperty *prop;
204 obj = object_resolve_path(path, &ambiguous);
205 if (obj == NULL) {
206 if (ambiguous) {
207 error_setg(errp, "Path '%s' is ambiguous", path);
208 } else {
209 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
210 "Device '%s' not found", path);
212 return NULL;
215 QTAILQ_FOREACH(prop, &obj->properties, node) {
216 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
218 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
219 entry->next = props;
220 props = entry;
222 entry->value->name = g_strdup(prop->name);
223 entry->value->type = g_strdup(prop->type);
226 return props;
229 /* FIXME: teach qapi about how to pass through Visitors */
230 int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
232 const char *path = qdict_get_str(qdict, "path");
233 const char *property = qdict_get_str(qdict, "property");
234 QObject *value = qdict_get(qdict, "value");
235 Error *local_err = NULL;
236 Object *obj;
238 obj = object_resolve_path(path, NULL);
239 if (!obj) {
240 error_set(&local_err, ERROR_CLASS_DEVICE_NOT_FOUND,
241 "Device '%s' not found", path);
242 goto out;
245 object_property_set_qobject(obj, value, property, &local_err);
247 out:
248 if (local_err) {
249 qerror_report_err(local_err);
250 error_free(local_err);
251 return -1;
254 return 0;
257 int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
259 const char *path = qdict_get_str(qdict, "path");
260 const char *property = qdict_get_str(qdict, "property");
261 Error *local_err = NULL;
262 Object *obj;
264 obj = object_resolve_path(path, NULL);
265 if (!obj) {
266 error_set(&local_err, ERROR_CLASS_DEVICE_NOT_FOUND,
267 "Device '%s' not found", path);
268 goto out;
271 *ret = object_property_get_qobject(obj, property, &local_err);
273 out:
274 if (local_err) {
275 qerror_report_err(local_err);
276 error_free(local_err);
277 return -1;
280 return 0;
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_change_blockdev(device, target, arg, errp);
436 static void qom_list_types_tramp(ObjectClass *klass, void *data)
438 ObjectTypeInfoList *e, **pret = data;
439 ObjectTypeInfo *info;
441 info = g_malloc0(sizeof(*info));
442 info->name = g_strdup(object_class_get_name(klass));
444 e = g_malloc0(sizeof(*e));
445 e->value = info;
446 e->next = *pret;
447 *pret = e;
450 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
451 const char *implements,
452 bool has_abstract,
453 bool abstract,
454 Error **errp)
456 ObjectTypeInfoList *ret = NULL;
458 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
460 return ret;
463 /* Return a DevicePropertyInfo for a qdev property.
465 * If a qdev property with the given name does not exist, use the given default
466 * type. If the qdev property info should not be shown, return NULL.
468 * The caller must free the return value.
470 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
471 const char *name,
472 const char *default_type,
473 const char *description)
475 DevicePropertyInfo *info;
476 Property *prop;
478 do {
479 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
480 if (strcmp(name, prop->name) != 0) {
481 continue;
485 * TODO Properties without a parser are just for dirty hacks.
486 * qdev_prop_ptr is the only such PropertyInfo. It's marked
487 * for removal. This conditional should be removed along with
488 * it.
490 if (!prop->info->set) {
491 return NULL; /* no way to set it, don't show */
494 info = g_malloc0(sizeof(*info));
495 info->name = g_strdup(prop->name);
496 info->type = g_strdup(prop->info->name);
497 info->has_description = !!prop->info->description;
498 info->description = g_strdup(prop->info->description);
499 return info;
501 klass = object_class_get_parent(klass);
502 } while (klass != object_class_by_name(TYPE_DEVICE));
504 /* Not a qdev property, use the default type */
505 info = g_malloc0(sizeof(*info));
506 info->name = g_strdup(name);
507 info->type = g_strdup(default_type);
508 info->has_description = !!description;
509 info->description = g_strdup(description);
511 return info;
514 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
515 Error **errp)
517 ObjectClass *klass;
518 Object *obj;
519 ObjectProperty *prop;
520 DevicePropertyInfoList *prop_list = NULL;
522 klass = object_class_by_name(typename);
523 if (klass == NULL) {
524 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
525 "Device '%s' not found", typename);
526 return NULL;
529 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
530 if (klass == NULL) {
531 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
532 return NULL;
535 obj = object_new(typename);
537 QTAILQ_FOREACH(prop, &obj->properties, node) {
538 DevicePropertyInfo *info;
539 DevicePropertyInfoList *entry;
541 /* Skip Object and DeviceState properties */
542 if (strcmp(prop->name, "type") == 0 ||
543 strcmp(prop->name, "realized") == 0 ||
544 strcmp(prop->name, "hotpluggable") == 0 ||
545 strcmp(prop->name, "hotplugged") == 0 ||
546 strcmp(prop->name, "parent_bus") == 0) {
547 continue;
550 /* Skip legacy properties since they are just string versions of
551 * properties that we already list.
553 if (strstart(prop->name, "legacy-", NULL)) {
554 continue;
557 info = make_device_property_info(klass, prop->name, prop->type,
558 prop->description);
559 if (!info) {
560 continue;
563 entry = g_malloc0(sizeof(*entry));
564 entry->value = info;
565 entry->next = prop_list;
566 prop_list = entry;
569 object_unref(obj);
571 return prop_list;
574 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
576 return arch_query_cpu_definitions(errp);
579 void qmp_add_client(const char *protocol, const char *fdname,
580 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
581 Error **errp)
583 CharDriverState *s;
584 int fd;
586 fd = monitor_get_fd(cur_mon, fdname, errp);
587 if (fd < 0) {
588 return;
591 if (strcmp(protocol, "spice") == 0) {
592 if (!qemu_using_spice(errp)) {
593 close(fd);
594 return;
596 skipauth = has_skipauth ? skipauth : false;
597 tls = has_tls ? tls : false;
598 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
599 error_setg(errp, "spice failed to add client");
600 close(fd);
602 return;
603 #ifdef CONFIG_VNC
604 } else if (strcmp(protocol, "vnc") == 0) {
605 skipauth = has_skipauth ? skipauth : false;
606 vnc_display_add_client(NULL, fd, skipauth);
607 return;
608 #endif
609 } else if ((s = qemu_chr_find(protocol)) != NULL) {
610 if (qemu_chr_add_client(s, fd) < 0) {
611 error_setg(errp, "failed to add client");
612 close(fd);
613 return;
615 return;
618 error_setg(errp, "protocol '%s' is invalid", protocol);
619 close(fd);
622 void object_add(const char *type, const char *id, const QDict *qdict,
623 Visitor *v, Error **errp)
625 Object *obj;
626 ObjectClass *klass;
627 const QDictEntry *e;
628 Error *local_err = NULL;
630 klass = object_class_by_name(type);
631 if (!klass) {
632 error_setg(errp, "invalid object type: %s", type);
633 return;
636 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
637 error_setg(errp, "object type '%s' isn't supported by object-add",
638 type);
639 return;
642 if (object_class_is_abstract(klass)) {
643 error_setg(errp, "object type '%s' is abstract", type);
644 return;
647 obj = object_new(type);
648 if (qdict) {
649 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
650 object_property_set(obj, v, e->key, &local_err);
651 if (local_err) {
652 goto out;
657 object_property_add_child(object_get_objects_root(),
658 id, obj, &local_err);
659 if (local_err) {
660 goto out;
663 user_creatable_complete(obj, &local_err);
664 if (local_err) {
665 object_property_del(object_get_objects_root(),
666 id, &error_abort);
667 goto out;
669 out:
670 if (local_err) {
671 error_propagate(errp, local_err);
673 object_unref(obj);
676 int qmp_object_add(Monitor *mon, const QDict *qdict, QObject **ret)
678 const char *type = qdict_get_str(qdict, "qom-type");
679 const char *id = qdict_get_str(qdict, "id");
680 QObject *props = qdict_get(qdict, "props");
681 const QDict *pdict = NULL;
682 Error *local_err = NULL;
683 QmpInputVisitor *qiv;
685 if (props) {
686 pdict = qobject_to_qdict(props);
687 if (!pdict) {
688 error_setg(&local_err, QERR_INVALID_PARAMETER_TYPE,
689 "props", "dict");
690 goto out;
694 qiv = qmp_input_visitor_new(props);
695 object_add(type, id, pdict, qmp_input_get_visitor(qiv), &local_err);
696 qmp_input_visitor_cleanup(qiv);
698 out:
699 if (local_err) {
700 qerror_report_err(local_err);
701 error_free(local_err);
702 return -1;
705 return 0;
708 void qmp_object_del(const char *id, Error **errp)
710 Object *container;
711 Object *obj;
713 container = object_get_objects_root();
714 obj = object_resolve_path_component(container, id);
715 if (!obj) {
716 error_setg(errp, "object id not found");
717 return;
720 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
721 error_setg(errp, "%s is in use, can not be deleted", id);
722 return;
724 object_unparent(obj);
727 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
729 MemoryDeviceInfoList *head = NULL;
730 MemoryDeviceInfoList **prev = &head;
732 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
734 return head;
737 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
739 bool ambig;
740 ACPIOSTInfoList *head = NULL;
741 ACPIOSTInfoList **prev = &head;
742 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
744 if (obj) {
745 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
746 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
748 adevc->ospm_status(adev, &prev);
749 } else {
750 error_setg(errp, "command is not supported, missing ACPI device");
753 return head;