trace: [tracetool] Add methods 'Event.copy' and 'Arguments.copy'
[qemu/ar7.git] / qmp.c
blob74107be41be3d1f32337b64822a0b4fcf800a777
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"
32 NameInfo *qmp_query_name(Error **errp)
34 NameInfo *info = g_malloc0(sizeof(*info));
36 if (qemu_name) {
37 info->has_name = true;
38 info->name = g_strdup(qemu_name);
41 return info;
44 VersionInfo *qmp_query_version(Error **err)
46 VersionInfo *info = g_malloc0(sizeof(*info));
47 const char *version = QEMU_VERSION;
48 char *tmp;
50 info->qemu.major = strtol(version, &tmp, 10);
51 tmp++;
52 info->qemu.minor = strtol(tmp, &tmp, 10);
53 tmp++;
54 info->qemu.micro = strtol(tmp, &tmp, 10);
55 info->package = g_strdup(QEMU_PKGVERSION);
57 return info;
60 KvmInfo *qmp_query_kvm(Error **errp)
62 KvmInfo *info = g_malloc0(sizeof(*info));
64 info->enabled = kvm_enabled();
65 info->present = kvm_available();
67 return info;
70 UuidInfo *qmp_query_uuid(Error **errp)
72 UuidInfo *info = g_malloc0(sizeof(*info));
73 char uuid[64];
75 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
76 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
77 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
78 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
79 qemu_uuid[14], qemu_uuid[15]);
81 info->UUID = g_strdup(uuid);
82 return info;
85 void qmp_quit(Error **err)
87 no_shutdown = 0;
88 qemu_system_shutdown_request();
91 void qmp_stop(Error **errp)
93 if (runstate_check(RUN_STATE_INMIGRATE)) {
94 autostart = 0;
95 } else {
96 vm_stop(RUN_STATE_PAUSED);
100 void qmp_system_reset(Error **errp)
102 qemu_system_reset_request();
105 void qmp_system_powerdown(Error **erp)
107 qemu_system_powerdown_request();
110 void qmp_cpu(int64_t index, Error **errp)
112 /* Just do nothing */
115 void qmp_cpu_add(int64_t id, Error **errp)
117 MachineClass *mc;
119 mc = MACHINE_GET_CLASS(current_machine);
120 if (mc->qemu_machine->hot_add_cpu) {
121 mc->qemu_machine->hot_add_cpu(id, errp);
122 } else {
123 error_setg(errp, "Not supported");
127 #ifndef CONFIG_VNC
128 /* If VNC support is enabled, the "true" query-vnc command is
129 defined in the VNC subsystem */
130 VncInfo *qmp_query_vnc(Error **errp)
132 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
133 return NULL;
135 #endif
137 #ifndef CONFIG_SPICE
138 /* If SPICE support is enabled, the "true" query-spice command is
139 defined in the SPICE subsystem. Also note that we use a small
140 trick to maintain query-spice's original behavior, which is not
141 to be available in the namespace if SPICE is not compiled in */
142 SpiceInfo *qmp_query_spice(Error **errp)
144 error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice");
145 return NULL;
147 #endif
149 static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
151 bdrv_iostatus_reset(bs);
154 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
156 Error **err = opaque;
158 if (!error_is_set(err) && bdrv_key_required(bs)) {
159 error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
160 bdrv_get_encrypted_filename(bs));
164 void qmp_cont(Error **errp)
166 Error *local_err = NULL;
168 if (runstate_needs_reset()) {
169 error_setg(errp, "Resetting the Virtual Machine is required");
170 return;
171 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
172 return;
175 bdrv_iterate(iostatus_bdrv_it, NULL);
176 bdrv_iterate(encrypted_bdrv_it, &local_err);
177 if (local_err) {
178 error_propagate(errp, local_err);
179 return;
182 if (runstate_check(RUN_STATE_INMIGRATE)) {
183 autostart = 1;
184 } else {
185 vm_start();
189 void qmp_system_wakeup(Error **errp)
191 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
194 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
196 Object *obj;
197 bool ambiguous = false;
198 ObjectPropertyInfoList *props = NULL;
199 ObjectProperty *prop;
201 obj = object_resolve_path(path, &ambiguous);
202 if (obj == NULL) {
203 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
204 return NULL;
207 QTAILQ_FOREACH(prop, &obj->properties, node) {
208 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
210 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
211 entry->next = props;
212 props = entry;
214 entry->value->name = g_strdup(prop->name);
215 entry->value->type = g_strdup(prop->type);
218 return props;
221 /* FIXME: teach qapi about how to pass through Visitors */
222 int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
224 const char *path = qdict_get_str(qdict, "path");
225 const char *property = qdict_get_str(qdict, "property");
226 QObject *value = qdict_get(qdict, "value");
227 Error *local_err = NULL;
228 Object *obj;
230 obj = object_resolve_path(path, NULL);
231 if (!obj) {
232 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
233 goto out;
236 object_property_set_qobject(obj, value, property, &local_err);
238 out:
239 if (local_err) {
240 qerror_report_err(local_err);
241 error_free(local_err);
242 return -1;
245 return 0;
248 int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
250 const char *path = qdict_get_str(qdict, "path");
251 const char *property = qdict_get_str(qdict, "property");
252 Error *local_err = NULL;
253 Object *obj;
255 obj = object_resolve_path(path, NULL);
256 if (!obj) {
257 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
258 goto out;
261 *ret = object_property_get_qobject(obj, property, &local_err);
263 out:
264 if (local_err) {
265 qerror_report_err(local_err);
266 error_free(local_err);
267 return -1;
270 return 0;
273 void qmp_set_password(const char *protocol, const char *password,
274 bool has_connected, const char *connected, Error **errp)
276 int disconnect_if_connected = 0;
277 int fail_if_connected = 0;
278 int rc;
280 if (has_connected) {
281 if (strcmp(connected, "fail") == 0) {
282 fail_if_connected = 1;
283 } else if (strcmp(connected, "disconnect") == 0) {
284 disconnect_if_connected = 1;
285 } else if (strcmp(connected, "keep") == 0) {
286 /* nothing */
287 } else {
288 error_set(errp, QERR_INVALID_PARAMETER, "connected");
289 return;
293 if (strcmp(protocol, "spice") == 0) {
294 if (!using_spice) {
295 /* correct one? spice isn't a device ,,, */
296 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
297 return;
299 rc = qemu_spice_set_passwd(password, fail_if_connected,
300 disconnect_if_connected);
301 if (rc != 0) {
302 error_set(errp, QERR_SET_PASSWD_FAILED);
304 return;
307 if (strcmp(protocol, "vnc") == 0) {
308 if (fail_if_connected || disconnect_if_connected) {
309 /* vnc supports "connected=keep" only */
310 error_set(errp, QERR_INVALID_PARAMETER, "connected");
311 return;
313 /* Note that setting an empty password will not disable login through
314 * this interface. */
315 rc = vnc_display_password(NULL, password);
316 if (rc < 0) {
317 error_set(errp, QERR_SET_PASSWD_FAILED);
319 return;
322 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
325 void qmp_expire_password(const char *protocol, const char *whenstr,
326 Error **errp)
328 time_t when;
329 int rc;
331 if (strcmp(whenstr, "now") == 0) {
332 when = 0;
333 } else if (strcmp(whenstr, "never") == 0) {
334 when = TIME_MAX;
335 } else if (whenstr[0] == '+') {
336 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
337 } else {
338 when = strtoull(whenstr, NULL, 10);
341 if (strcmp(protocol, "spice") == 0) {
342 if (!using_spice) {
343 /* correct one? spice isn't a device ,,, */
344 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
345 return;
347 rc = qemu_spice_set_pw_expire(when);
348 if (rc != 0) {
349 error_set(errp, QERR_SET_PASSWD_FAILED);
351 return;
354 if (strcmp(protocol, "vnc") == 0) {
355 rc = vnc_display_pw_expire(NULL, when);
356 if (rc != 0) {
357 error_set(errp, QERR_SET_PASSWD_FAILED);
359 return;
362 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
365 #ifdef CONFIG_VNC
366 void qmp_change_vnc_password(const char *password, Error **errp)
368 if (vnc_display_password(NULL, password) < 0) {
369 error_set(errp, QERR_SET_PASSWD_FAILED);
373 static void qmp_change_vnc_listen(const char *target, Error **errp)
375 vnc_display_open(NULL, target, errp);
378 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
379 Error **errp)
381 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
382 if (!has_arg) {
383 error_set(errp, QERR_MISSING_PARAMETER, "password");
384 } else {
385 qmp_change_vnc_password(arg, errp);
387 } else {
388 qmp_change_vnc_listen(target, errp);
391 #else
392 void qmp_change_vnc_password(const char *password, Error **errp)
394 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
396 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
397 Error **errp)
399 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
401 #endif /* !CONFIG_VNC */
403 void qmp_change(const char *device, const char *target,
404 bool has_arg, const char *arg, Error **err)
406 if (strcmp(device, "vnc") == 0) {
407 qmp_change_vnc(target, has_arg, arg, err);
408 } else {
409 qmp_change_blockdev(device, target, arg, err);
413 static void qom_list_types_tramp(ObjectClass *klass, void *data)
415 ObjectTypeInfoList *e, **pret = data;
416 ObjectTypeInfo *info;
418 info = g_malloc0(sizeof(*info));
419 info->name = g_strdup(object_class_get_name(klass));
421 e = g_malloc0(sizeof(*e));
422 e->value = info;
423 e->next = *pret;
424 *pret = e;
427 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
428 const char *implements,
429 bool has_abstract,
430 bool abstract,
431 Error **errp)
433 ObjectTypeInfoList *ret = NULL;
435 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
437 return ret;
440 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
441 Error **errp)
443 ObjectClass *klass;
444 Property *prop;
445 DevicePropertyInfoList *prop_list = NULL;
447 klass = object_class_by_name(typename);
448 if (klass == NULL) {
449 error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
450 return NULL;
453 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
454 if (klass == NULL) {
455 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
456 "name", TYPE_DEVICE);
457 return NULL;
460 do {
461 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
462 DevicePropertyInfoList *entry;
463 DevicePropertyInfo *info;
466 * TODO Properties without a parser are just for dirty hacks.
467 * qdev_prop_ptr is the only such PropertyInfo. It's marked
468 * for removal. This conditional should be removed along with
469 * it.
471 if (!prop->info->set) {
472 continue; /* no way to set it, don't show */
475 info = g_malloc0(sizeof(*info));
476 info->name = g_strdup(prop->name);
477 info->type = g_strdup(prop->info->legacy_name ?: prop->info->name);
479 entry = g_malloc0(sizeof(*entry));
480 entry->value = info;
481 entry->next = prop_list;
482 prop_list = entry;
484 klass = object_class_get_parent(klass);
485 } while (klass != object_class_by_name(TYPE_DEVICE));
487 return prop_list;
490 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
492 return arch_query_cpu_definitions(errp);
495 void qmp_add_client(const char *protocol, const char *fdname,
496 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
497 Error **errp)
499 CharDriverState *s;
500 int fd;
502 fd = monitor_get_fd(cur_mon, fdname, errp);
503 if (fd < 0) {
504 return;
507 if (strcmp(protocol, "spice") == 0) {
508 if (!using_spice) {
509 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
510 close(fd);
511 return;
513 skipauth = has_skipauth ? skipauth : false;
514 tls = has_tls ? tls : false;
515 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
516 error_setg(errp, "spice failed to add client");
517 close(fd);
519 return;
520 #ifdef CONFIG_VNC
521 } else if (strcmp(protocol, "vnc") == 0) {
522 skipauth = has_skipauth ? skipauth : false;
523 vnc_display_add_client(NULL, fd, skipauth);
524 return;
525 #endif
526 } else if ((s = qemu_chr_find(protocol)) != NULL) {
527 if (qemu_chr_add_client(s, fd) < 0) {
528 error_setg(errp, "failed to add client");
529 close(fd);
530 return;
532 return;
535 error_setg(errp, "protocol '%s' is invalid", protocol);
536 close(fd);
539 void object_add(const char *type, const char *id, const QDict *qdict,
540 Visitor *v, Error **errp)
542 Object *obj;
543 ObjectClass *klass;
544 const QDictEntry *e;
545 Error *local_err = NULL;
547 klass = object_class_by_name(type);
548 if (!klass) {
549 error_setg(errp, "invalid class name");
550 return;
553 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
554 error_setg(errp, "object type '%s' isn't supported by object-add",
555 type);
556 return;
559 if (object_class_is_abstract(klass)) {
560 error_setg(errp, "object type '%s' is abstract", type);
561 return;
564 obj = object_new(type);
565 if (qdict) {
566 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
567 object_property_set(obj, v, e->key, &local_err);
568 if (local_err) {
569 goto out;
574 user_creatable_complete(obj, &local_err);
575 if (local_err) {
576 goto out;
579 object_property_add_child(container_get(object_get_root(), "/objects"),
580 id, obj, &local_err);
581 out:
582 if (local_err) {
583 error_propagate(errp, local_err);
585 object_unref(obj);
588 int qmp_object_add(Monitor *mon, const QDict *qdict, QObject **ret)
590 const char *type = qdict_get_str(qdict, "qom-type");
591 const char *id = qdict_get_str(qdict, "id");
592 QObject *props = qdict_get(qdict, "props");
593 const QDict *pdict = NULL;
594 Error *local_err = NULL;
595 QmpInputVisitor *qiv;
597 if (props) {
598 pdict = qobject_to_qdict(props);
599 if (!pdict) {
600 error_set(&local_err, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
601 goto out;
605 qiv = qmp_input_visitor_new(props);
606 object_add(type, id, pdict, qmp_input_get_visitor(qiv), &local_err);
607 qmp_input_visitor_cleanup(qiv);
609 out:
610 if (local_err) {
611 qerror_report_err(local_err);
612 error_free(local_err);
613 return -1;
616 return 0;
619 void qmp_object_del(const char *id, Error **errp)
621 Object *container;
622 Object *obj;
624 container = container_get(object_get_root(), "/objects");
625 obj = object_resolve_path_component(container, id);
626 if (!obj) {
627 error_setg(errp, "object id not found");
628 return;
630 object_unparent(obj);