hw: include hw header files with full paths
[qemu.git] / qmp.c
blob55b056b558bd44117df7e7301b3cde31e7c0a608
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 "char/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"
28 NameInfo *qmp_query_name(Error **errp)
30 NameInfo *info = g_malloc0(sizeof(*info));
32 if (qemu_name) {
33 info->has_name = true;
34 info->name = g_strdup(qemu_name);
37 return info;
40 VersionInfo *qmp_query_version(Error **err)
42 VersionInfo *info = g_malloc0(sizeof(*info));
43 const char *version = QEMU_VERSION;
44 char *tmp;
46 info->qemu.major = strtol(version, &tmp, 10);
47 tmp++;
48 info->qemu.minor = strtol(tmp, &tmp, 10);
49 tmp++;
50 info->qemu.micro = strtol(tmp, &tmp, 10);
51 info->package = g_strdup(QEMU_PKGVERSION);
53 return info;
56 KvmInfo *qmp_query_kvm(Error **errp)
58 KvmInfo *info = g_malloc0(sizeof(*info));
60 info->enabled = kvm_enabled();
61 info->present = kvm_available();
63 return info;
66 UuidInfo *qmp_query_uuid(Error **errp)
68 UuidInfo *info = g_malloc0(sizeof(*info));
69 char uuid[64];
71 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
72 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
73 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
74 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
75 qemu_uuid[14], qemu_uuid[15]);
77 info->UUID = g_strdup(uuid);
78 return info;
81 void qmp_quit(Error **err)
83 no_shutdown = 0;
84 qemu_system_shutdown_request();
87 void qmp_stop(Error **errp)
89 if (runstate_check(RUN_STATE_INMIGRATE)) {
90 autostart = 0;
91 } else {
92 vm_stop(RUN_STATE_PAUSED);
96 void qmp_system_reset(Error **errp)
98 qemu_system_reset_request();
101 void qmp_system_powerdown(Error **erp)
103 qemu_system_powerdown_request();
106 void qmp_cpu(int64_t index, Error **errp)
108 /* Just do nothing */
111 #ifndef CONFIG_VNC
112 /* If VNC support is enabled, the "true" query-vnc command is
113 defined in the VNC subsystem */
114 VncInfo *qmp_query_vnc(Error **errp)
116 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
117 return NULL;
119 #endif
121 #ifndef CONFIG_SPICE
122 /* If SPICE support is enabled, the "true" query-spice command is
123 defined in the SPICE subsystem. Also note that we use a small
124 trick to maintain query-spice's original behavior, which is not
125 to be available in the namespace if SPICE is not compiled in */
126 SpiceInfo *qmp_query_spice(Error **errp)
128 error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice");
129 return NULL;
131 #endif
133 static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
135 bdrv_iostatus_reset(bs);
138 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
140 Error **err = opaque;
142 if (!error_is_set(err) && bdrv_key_required(bs)) {
143 error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
144 bdrv_get_encrypted_filename(bs));
148 void qmp_cont(Error **errp)
150 Error *local_err = NULL;
152 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
153 runstate_check(RUN_STATE_SHUTDOWN)) {
154 error_set(errp, QERR_RESET_REQUIRED);
155 return;
156 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
157 return;
160 bdrv_iterate(iostatus_bdrv_it, NULL);
161 bdrv_iterate(encrypted_bdrv_it, &local_err);
162 if (local_err) {
163 error_propagate(errp, local_err);
164 return;
167 if (runstate_check(RUN_STATE_INMIGRATE)) {
168 autostart = 1;
169 } else {
170 vm_start();
174 void qmp_system_wakeup(Error **errp)
176 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
179 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
181 Object *obj;
182 bool ambiguous = false;
183 ObjectPropertyInfoList *props = NULL;
184 ObjectProperty *prop;
186 obj = object_resolve_path(path, &ambiguous);
187 if (obj == NULL) {
188 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
189 return NULL;
192 QTAILQ_FOREACH(prop, &obj->properties, node) {
193 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
195 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
196 entry->next = props;
197 props = entry;
199 entry->value->name = g_strdup(prop->name);
200 entry->value->type = g_strdup(prop->type);
203 return props;
206 /* FIXME: teach qapi about how to pass through Visitors */
207 int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
209 const char *path = qdict_get_str(qdict, "path");
210 const char *property = qdict_get_str(qdict, "property");
211 QObject *value = qdict_get(qdict, "value");
212 Error *local_err = NULL;
213 Object *obj;
215 obj = object_resolve_path(path, NULL);
216 if (!obj) {
217 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
218 goto out;
221 object_property_set_qobject(obj, value, property, &local_err);
223 out:
224 if (local_err) {
225 qerror_report_err(local_err);
226 error_free(local_err);
227 return -1;
230 return 0;
233 int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
235 const char *path = qdict_get_str(qdict, "path");
236 const char *property = qdict_get_str(qdict, "property");
237 Error *local_err = NULL;
238 Object *obj;
240 obj = object_resolve_path(path, NULL);
241 if (!obj) {
242 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
243 goto out;
246 *ret = object_property_get_qobject(obj, property, &local_err);
248 out:
249 if (local_err) {
250 qerror_report_err(local_err);
251 error_free(local_err);
252 return -1;
255 return 0;
258 void qmp_set_password(const char *protocol, const char *password,
259 bool has_connected, const char *connected, Error **errp)
261 int disconnect_if_connected = 0;
262 int fail_if_connected = 0;
263 int rc;
265 if (has_connected) {
266 if (strcmp(connected, "fail") == 0) {
267 fail_if_connected = 1;
268 } else if (strcmp(connected, "disconnect") == 0) {
269 disconnect_if_connected = 1;
270 } else if (strcmp(connected, "keep") == 0) {
271 /* nothing */
272 } else {
273 error_set(errp, QERR_INVALID_PARAMETER, "connected");
274 return;
278 if (strcmp(protocol, "spice") == 0) {
279 if (!using_spice) {
280 /* correct one? spice isn't a device ,,, */
281 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
282 return;
284 rc = qemu_spice_set_passwd(password, fail_if_connected,
285 disconnect_if_connected);
286 if (rc != 0) {
287 error_set(errp, QERR_SET_PASSWD_FAILED);
289 return;
292 if (strcmp(protocol, "vnc") == 0) {
293 if (fail_if_connected || disconnect_if_connected) {
294 /* vnc supports "connected=keep" only */
295 error_set(errp, QERR_INVALID_PARAMETER, "connected");
296 return;
298 /* Note that setting an empty password will not disable login through
299 * this interface. */
300 rc = vnc_display_password(NULL, password);
301 if (rc < 0) {
302 error_set(errp, QERR_SET_PASSWD_FAILED);
304 return;
307 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
310 void qmp_expire_password(const char *protocol, const char *whenstr,
311 Error **errp)
313 time_t when;
314 int rc;
316 if (strcmp(whenstr, "now") == 0) {
317 when = 0;
318 } else if (strcmp(whenstr, "never") == 0) {
319 when = TIME_MAX;
320 } else if (whenstr[0] == '+') {
321 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
322 } else {
323 when = strtoull(whenstr, NULL, 10);
326 if (strcmp(protocol, "spice") == 0) {
327 if (!using_spice) {
328 /* correct one? spice isn't a device ,,, */
329 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
330 return;
332 rc = qemu_spice_set_pw_expire(when);
333 if (rc != 0) {
334 error_set(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_set(errp, QERR_SET_PASSWD_FAILED);
344 return;
347 error_set(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_set(errp, QERR_SET_PASSWD_FAILED);
358 static void qmp_change_vnc_listen(const char *target, Error **errp)
360 vnc_display_open(NULL, target, errp);
363 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
364 Error **errp)
366 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
367 if (!has_arg) {
368 error_set(errp, QERR_MISSING_PARAMETER, "password");
369 } else {
370 qmp_change_vnc_password(arg, errp);
372 } else {
373 qmp_change_vnc_listen(target, errp);
376 #else
377 void qmp_change_vnc_password(const char *password, Error **errp)
379 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
381 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
382 Error **errp)
384 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
386 #endif /* !CONFIG_VNC */
388 void qmp_change(const char *device, const char *target,
389 bool has_arg, const char *arg, Error **err)
391 if (strcmp(device, "vnc") == 0) {
392 qmp_change_vnc(target, has_arg, arg, err);
393 } else {
394 qmp_change_blockdev(device, target, has_arg, arg, err);
398 static void qom_list_types_tramp(ObjectClass *klass, void *data)
400 ObjectTypeInfoList *e, **pret = data;
401 ObjectTypeInfo *info;
403 info = g_malloc0(sizeof(*info));
404 info->name = g_strdup(object_class_get_name(klass));
406 e = g_malloc0(sizeof(*e));
407 e->value = info;
408 e->next = *pret;
409 *pret = e;
412 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
413 const char *implements,
414 bool has_abstract,
415 bool abstract,
416 Error **errp)
418 ObjectTypeInfoList *ret = NULL;
420 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
422 return ret;
425 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
426 Error **errp)
428 ObjectClass *klass;
429 Property *prop;
430 DevicePropertyInfoList *prop_list = NULL;
432 klass = object_class_by_name(typename);
433 if (klass == NULL) {
434 error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
435 return NULL;
438 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
439 if (klass == NULL) {
440 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
441 "name", TYPE_DEVICE);
442 return NULL;
445 do {
446 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
447 DevicePropertyInfoList *entry;
448 DevicePropertyInfo *info;
451 * TODO Properties without a parser are just for dirty hacks.
452 * qdev_prop_ptr is the only such PropertyInfo. It's marked
453 * for removal. This conditional should be removed along with
454 * it.
456 if (!prop->info->set) {
457 continue; /* no way to set it, don't show */
460 info = g_malloc0(sizeof(*info));
461 info->name = g_strdup(prop->name);
462 info->type = g_strdup(prop->info->legacy_name ?: prop->info->name);
464 entry = g_malloc0(sizeof(*entry));
465 entry->value = info;
466 entry->next = prop_list;
467 prop_list = entry;
469 klass = object_class_get_parent(klass);
470 } while (klass != object_class_by_name(TYPE_DEVICE));
472 return prop_list;
475 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
477 return arch_query_cpu_definitions(errp);
480 void qmp_add_client(const char *protocol, const char *fdname,
481 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
482 Error **errp)
484 CharDriverState *s;
485 int fd;
487 fd = monitor_get_fd(cur_mon, fdname, errp);
488 if (fd < 0) {
489 return;
492 if (strcmp(protocol, "spice") == 0) {
493 if (!using_spice) {
494 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
495 close(fd);
496 return;
498 skipauth = has_skipauth ? skipauth : false;
499 tls = has_tls ? tls : false;
500 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
501 error_setg(errp, "spice failed to add client");
502 close(fd);
504 return;
505 #ifdef CONFIG_VNC
506 } else if (strcmp(protocol, "vnc") == 0) {
507 skipauth = has_skipauth ? skipauth : false;
508 vnc_display_add_client(NULL, fd, skipauth);
509 return;
510 #endif
511 } else if ((s = qemu_chr_find(protocol)) != NULL) {
512 if (qemu_chr_add_client(s, fd) < 0) {
513 error_setg(errp, "failed to add client");
514 close(fd);
515 return;
517 return;
520 error_setg(errp, "protocol '%s' is invalid", protocol);
521 close(fd);