qemu-img: Move quiet into ImgConvertState
[qemu.git] / qmp.c
blob679756844459ba4ada0288505a96e70b0bb793bf
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 "qemu-version.h"
19 #include "qemu/cutils.h"
20 #include "qemu/option.h"
21 #include "monitor/monitor.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/uuid.h"
25 #include "chardev/char.h"
26 #include "ui/qemu-spice.h"
27 #include "ui/vnc.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/arch_init.h"
30 #include "hw/qdev.h"
31 #include "sysemu/blockdev.h"
32 #include "sysemu/block-backend.h"
33 #include "qom/qom-qobject.h"
34 #include "qapi/error.h"
35 #include "qapi/qapi-commands-block-core.h"
36 #include "qapi/qapi-commands-misc.h"
37 #include "qapi/qapi-commands-ui.h"
38 #include "qapi/qmp/qdict.h"
39 #include "qapi/qmp/qerror.h"
40 #include "qapi/qobject-input-visitor.h"
41 #include "hw/boards.h"
42 #include "qom/object_interfaces.h"
43 #include "hw/mem/memory-device.h"
44 #include "hw/acpi/acpi_dev_interface.h"
46 NameInfo *qmp_query_name(Error **errp)
48 NameInfo *info = g_malloc0(sizeof(*info));
50 if (qemu_name) {
51 info->has_name = true;
52 info->name = g_strdup(qemu_name);
55 return info;
58 VersionInfo *qmp_query_version(Error **errp)
60 VersionInfo *info = g_new0(VersionInfo, 1);
62 info->qemu = g_new0(VersionTriple, 1);
63 info->qemu->major = QEMU_VERSION_MAJOR;
64 info->qemu->minor = QEMU_VERSION_MINOR;
65 info->qemu->micro = QEMU_VERSION_MICRO;
66 info->package = g_strdup(QEMU_PKGVERSION);
68 return info;
71 KvmInfo *qmp_query_kvm(Error **errp)
73 KvmInfo *info = g_malloc0(sizeof(*info));
75 info->enabled = kvm_enabled();
76 info->present = kvm_available();
78 return info;
81 UuidInfo *qmp_query_uuid(Error **errp)
83 UuidInfo *info = g_malloc0(sizeof(*info));
85 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
86 return info;
89 void qmp_quit(Error **errp)
91 no_shutdown = 0;
92 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
95 void qmp_stop(Error **errp)
97 /* if there is a dump in background, we should wait until the dump
98 * finished */
99 if (dump_in_progress()) {
100 error_setg(errp, "There is a dump in process, please wait.");
101 return;
104 if (runstate_check(RUN_STATE_INMIGRATE)) {
105 autostart = 0;
106 } else {
107 vm_stop(RUN_STATE_PAUSED);
111 void qmp_system_reset(Error **errp)
113 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
116 void qmp_system_powerdown(Error **erp)
118 qemu_system_powerdown_request();
121 void qmp_cpu_add(int64_t id, Error **errp)
123 MachineClass *mc;
125 mc = MACHINE_GET_CLASS(current_machine);
126 if (mc->hot_add_cpu) {
127 mc->hot_add_cpu(id, errp);
128 } else {
129 error_setg(errp, "Not supported");
133 void qmp_x_exit_preconfig(Error **errp)
135 if (!runstate_check(RUN_STATE_PRECONFIG)) {
136 error_setg(errp, "The command is permitted only in '%s' state",
137 RunState_str(RUN_STATE_PRECONFIG));
138 return;
140 qemu_exit_preconfig_request();
143 void qmp_cont(Error **errp)
145 BlockBackend *blk;
146 Error *local_err = NULL;
148 /* if there is a dump in background, we should wait until the dump
149 * finished */
150 if (dump_in_progress()) {
151 error_setg(errp, "There is a dump in process, please wait.");
152 return;
155 if (runstate_needs_reset()) {
156 error_setg(errp, "Resetting the Virtual Machine is required");
157 return;
158 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
159 return;
160 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
161 error_setg(errp, "Migration is not finalized yet");
162 return;
165 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
166 blk_iostatus_reset(blk);
169 /* Continuing after completed migration. Images have been inactivated to
170 * allow the destination to take control. Need to get control back now.
172 * If there are no inactive block nodes (e.g. because the VM was just
173 * paused rather than completing a migration), bdrv_inactivate_all() simply
174 * doesn't do anything. */
175 bdrv_invalidate_cache_all(&local_err);
176 if (local_err) {
177 error_propagate(errp, local_err);
178 return;
181 if (runstate_check(RUN_STATE_INMIGRATE)) {
182 autostart = 1;
183 } else {
184 vm_start();
188 void qmp_system_wakeup(Error **errp)
190 if (!qemu_wakeup_suspend_enabled()) {
191 error_setg(errp,
192 "wake-up from suspend is not supported by this guest");
193 return;
196 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
199 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
201 Object *obj;
202 bool ambiguous = false;
203 ObjectPropertyInfoList *props = NULL;
204 ObjectProperty *prop;
205 ObjectPropertyIterator iter;
207 obj = object_resolve_path(path, &ambiguous);
208 if (obj == NULL) {
209 if (ambiguous) {
210 error_setg(errp, "Path '%s' is ambiguous", path);
211 } else {
212 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
213 "Device '%s' not found", path);
215 return NULL;
218 object_property_iter_init(&iter, obj);
219 while ((prop = object_property_iter_next(&iter))) {
220 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
222 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
223 entry->next = props;
224 props = entry;
226 entry->value->name = g_strdup(prop->name);
227 entry->value->type = g_strdup(prop->type);
230 return props;
233 void qmp_qom_set(const char *path, const char *property, QObject *value,
234 Error **errp)
236 Object *obj;
238 obj = object_resolve_path(path, NULL);
239 if (!obj) {
240 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
241 "Device '%s' not found", path);
242 return;
245 object_property_set_qobject(obj, value, property, errp);
248 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
250 Object *obj;
252 obj = object_resolve_path(path, NULL);
253 if (!obj) {
254 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
255 "Device '%s' not found", path);
256 return NULL;
259 return object_property_get_qobject(obj, property, errp);
262 void qmp_set_password(const char *protocol, const char *password,
263 bool has_connected, const char *connected, Error **errp)
265 int disconnect_if_connected = 0;
266 int fail_if_connected = 0;
267 int rc;
269 if (has_connected) {
270 if (strcmp(connected, "fail") == 0) {
271 fail_if_connected = 1;
272 } else if (strcmp(connected, "disconnect") == 0) {
273 disconnect_if_connected = 1;
274 } else if (strcmp(connected, "keep") == 0) {
275 /* nothing */
276 } else {
277 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
278 return;
282 if (strcmp(protocol, "spice") == 0) {
283 if (!qemu_using_spice(errp)) {
284 return;
286 rc = qemu_spice_set_passwd(password, fail_if_connected,
287 disconnect_if_connected);
288 if (rc != 0) {
289 error_setg(errp, QERR_SET_PASSWD_FAILED);
291 return;
294 if (strcmp(protocol, "vnc") == 0) {
295 if (fail_if_connected || disconnect_if_connected) {
296 /* vnc supports "connected=keep" only */
297 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
298 return;
300 /* Note that setting an empty password will not disable login through
301 * this interface. */
302 rc = vnc_display_password(NULL, password);
303 if (rc < 0) {
304 error_setg(errp, QERR_SET_PASSWD_FAILED);
306 return;
309 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
312 void qmp_expire_password(const char *protocol, const char *whenstr,
313 Error **errp)
315 time_t when;
316 int rc;
318 if (strcmp(whenstr, "now") == 0) {
319 when = 0;
320 } else if (strcmp(whenstr, "never") == 0) {
321 when = TIME_MAX;
322 } else if (whenstr[0] == '+') {
323 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
324 } else {
325 when = strtoull(whenstr, NULL, 10);
328 if (strcmp(protocol, "spice") == 0) {
329 if (!qemu_using_spice(errp)) {
330 return;
332 rc = qemu_spice_set_pw_expire(when);
333 if (rc != 0) {
334 error_setg(errp, QERR_SET_PASSWD_FAILED);
336 return;
339 if (strcmp(protocol, "vnc") == 0) {
340 rc = vnc_display_pw_expire(NULL, when);
341 if (rc != 0) {
342 error_setg(errp, QERR_SET_PASSWD_FAILED);
344 return;
347 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
350 #ifdef CONFIG_VNC
351 void qmp_change_vnc_password(const char *password, Error **errp)
353 if (vnc_display_password(NULL, password) < 0) {
354 error_setg(errp, QERR_SET_PASSWD_FAILED);
358 static void qmp_change_vnc_listen(const char *target, Error **errp)
360 QemuOptsList *olist = qemu_find_opts("vnc");
361 QemuOpts *opts;
363 if (strstr(target, "id=")) {
364 error_setg(errp, "id not supported");
365 return;
368 opts = qemu_opts_find(olist, "default");
369 if (opts) {
370 qemu_opts_del(opts);
372 opts = vnc_parse(target, errp);
373 if (!opts) {
374 return;
377 vnc_display_open("default", errp);
380 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
381 Error **errp)
383 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
384 if (!has_arg) {
385 error_setg(errp, QERR_MISSING_PARAMETER, "password");
386 } else {
387 qmp_change_vnc_password(arg, errp);
389 } else {
390 qmp_change_vnc_listen(target, errp);
393 #endif /* !CONFIG_VNC */
395 void qmp_change(const char *device, const char *target,
396 bool has_arg, const char *arg, Error **errp)
398 if (strcmp(device, "vnc") == 0) {
399 #ifdef CONFIG_VNC
400 qmp_change_vnc(target, has_arg, arg, errp);
401 #else
402 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
403 #endif
404 } else {
405 qmp_blockdev_change_medium(true, device, false, NULL, target,
406 has_arg, arg, false, 0, errp);
410 static void qom_list_types_tramp(ObjectClass *klass, void *data)
412 ObjectTypeInfoList *e, **pret = data;
413 ObjectTypeInfo *info;
414 ObjectClass *parent = object_class_get_parent(klass);
416 info = g_malloc0(sizeof(*info));
417 info->name = g_strdup(object_class_get_name(klass));
418 info->has_abstract = info->abstract = object_class_is_abstract(klass);
419 if (parent) {
420 info->has_parent = true;
421 info->parent = g_strdup(object_class_get_name(parent));
424 e = g_malloc0(sizeof(*e));
425 e->value = info;
426 e->next = *pret;
427 *pret = e;
430 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
431 const char *implements,
432 bool has_abstract,
433 bool abstract,
434 Error **errp)
436 ObjectTypeInfoList *ret = NULL;
438 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
440 return ret;
443 /* Return a DevicePropertyInfo for a qdev property.
445 * If a qdev property with the given name does not exist, use the given default
446 * type. If the qdev property info should not be shown, return NULL.
448 * The caller must free the return value.
450 static ObjectPropertyInfo *make_device_property_info(ObjectClass *klass,
451 const char *name,
452 const char *default_type,
453 const char *description)
455 ObjectPropertyInfo *info;
456 Property *prop;
458 do {
459 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
460 if (strcmp(name, prop->name) != 0) {
461 continue;
465 * TODO Properties without a parser are just for dirty hacks.
466 * qdev_prop_ptr is the only such PropertyInfo. It's marked
467 * for removal. This conditional should be removed along with
468 * it.
470 if (!prop->info->set && !prop->info->create) {
471 return NULL; /* no way to set it, don't show */
474 info = g_malloc0(sizeof(*info));
475 info->name = g_strdup(prop->name);
476 info->type = default_type ? g_strdup(default_type)
477 : g_strdup(prop->info->name);
478 info->has_description = !!prop->info->description;
479 info->description = g_strdup(prop->info->description);
480 return info;
482 klass = object_class_get_parent(klass);
483 } while (klass != object_class_by_name(TYPE_DEVICE));
485 /* Not a qdev property, use the default type */
486 info = g_malloc0(sizeof(*info));
487 info->name = g_strdup(name);
488 info->type = g_strdup(default_type);
489 info->has_description = !!description;
490 info->description = g_strdup(description);
492 return info;
495 ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
496 Error **errp)
498 ObjectClass *klass;
499 Object *obj;
500 ObjectProperty *prop;
501 ObjectPropertyIterator iter;
502 ObjectPropertyInfoList *prop_list = NULL;
504 klass = object_class_by_name(typename);
505 if (klass == NULL) {
506 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
507 "Device '%s' not found", typename);
508 return NULL;
511 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
512 if (klass == NULL) {
513 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
514 return NULL;
517 if (object_class_is_abstract(klass)) {
518 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
519 "non-abstract device type");
520 return NULL;
523 obj = object_new(typename);
525 object_property_iter_init(&iter, obj);
526 while ((prop = object_property_iter_next(&iter))) {
527 ObjectPropertyInfo *info;
528 ObjectPropertyInfoList *entry;
530 /* Skip Object and DeviceState properties */
531 if (strcmp(prop->name, "type") == 0 ||
532 strcmp(prop->name, "realized") == 0 ||
533 strcmp(prop->name, "hotpluggable") == 0 ||
534 strcmp(prop->name, "hotplugged") == 0 ||
535 strcmp(prop->name, "parent_bus") == 0) {
536 continue;
539 /* Skip legacy properties since they are just string versions of
540 * properties that we already list.
542 if (strstart(prop->name, "legacy-", NULL)) {
543 continue;
546 info = make_device_property_info(klass, prop->name, prop->type,
547 prop->description);
548 if (!info) {
549 continue;
552 entry = g_malloc0(sizeof(*entry));
553 entry->value = info;
554 entry->next = prop_list;
555 prop_list = entry;
558 object_unref(obj);
560 return prop_list;
563 ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
564 Error **errp)
566 ObjectClass *klass;
567 Object *obj = NULL;
568 ObjectProperty *prop;
569 ObjectPropertyIterator iter;
570 ObjectPropertyInfoList *prop_list = NULL;
572 klass = object_class_by_name(typename);
573 if (klass == NULL) {
574 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
575 "Class '%s' not found", typename);
576 return NULL;
579 klass = object_class_dynamic_cast(klass, TYPE_OBJECT);
580 if (klass == NULL) {
581 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_OBJECT);
582 return NULL;
585 if (object_class_is_abstract(klass)) {
586 object_class_property_iter_init(&iter, klass);
587 } else {
588 obj = object_new(typename);
589 object_property_iter_init(&iter, obj);
591 while ((prop = object_property_iter_next(&iter))) {
592 ObjectPropertyInfo *info;
593 ObjectPropertyInfoList *entry;
595 info = g_malloc0(sizeof(*info));
596 info->name = g_strdup(prop->name);
597 info->type = g_strdup(prop->type);
598 info->has_description = !!prop->description;
599 info->description = g_strdup(prop->description);
601 entry = g_malloc0(sizeof(*entry));
602 entry->value = info;
603 entry->next = prop_list;
604 prop_list = entry;
607 object_unref(obj);
609 return prop_list;
612 void qmp_add_client(const char *protocol, const char *fdname,
613 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
614 Error **errp)
616 Chardev *s;
617 int fd;
619 fd = monitor_get_fd(cur_mon, fdname, errp);
620 if (fd < 0) {
621 return;
624 if (strcmp(protocol, "spice") == 0) {
625 if (!qemu_using_spice(errp)) {
626 close(fd);
627 return;
629 skipauth = has_skipauth ? skipauth : false;
630 tls = has_tls ? tls : false;
631 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
632 error_setg(errp, "spice failed to add client");
633 close(fd);
635 return;
636 #ifdef CONFIG_VNC
637 } else if (strcmp(protocol, "vnc") == 0) {
638 skipauth = has_skipauth ? skipauth : false;
639 vnc_display_add_client(NULL, fd, skipauth);
640 return;
641 #endif
642 } else if ((s = qemu_chr_find(protocol)) != NULL) {
643 if (qemu_chr_add_client(s, fd) < 0) {
644 error_setg(errp, "failed to add client");
645 close(fd);
646 return;
648 return;
651 error_setg(errp, "protocol '%s' is invalid", protocol);
652 close(fd);
656 void qmp_object_add(const char *type, const char *id,
657 bool has_props, QObject *props, Error **errp)
659 QDict *pdict;
660 Visitor *v;
661 Object *obj;
663 if (props) {
664 pdict = qobject_to(QDict, props);
665 if (!pdict) {
666 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
667 return;
669 qobject_ref(pdict);
670 } else {
671 pdict = qdict_new();
674 v = qobject_input_visitor_new(QOBJECT(pdict));
675 obj = user_creatable_add_type(type, id, pdict, v, errp);
676 visit_free(v);
677 if (obj) {
678 object_unref(obj);
680 qobject_unref(pdict);
683 void qmp_object_del(const char *id, Error **errp)
685 user_creatable_del(id, errp);
688 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
690 return qmp_memory_device_list();
693 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
695 bool ambig;
696 ACPIOSTInfoList *head = NULL;
697 ACPIOSTInfoList **prev = &head;
698 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
700 if (obj) {
701 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
702 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
704 adevc->ospm_status(adev, &prev);
705 } else {
706 error_setg(errp, "command is not supported, missing ACPI device");
709 return head;
712 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
714 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
716 mem_info->base_memory = ram_size;
718 mem_info->plugged_memory = get_plugged_memory_size();
719 mem_info->has_plugged_memory =
720 mem_info->plugged_memory != (uint64_t)-1;
722 return mem_info;