qemu-ga: separate out common commands from posix-specific ones
[qemu.git] / qmp.c
blob1f64844095b6c40f732f88220bcadde1ef8471e7
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.h"
18 #include "qmp-commands.h"
19 #include "ui/qemu-spice.h"
20 #include "ui/vnc.h"
21 #include "kvm.h"
22 #include "arch_init.h"
23 #include "hw/qdev.h"
24 #include "blockdev.h"
25 #include "qemu/qom-qobject.h"
27 NameInfo *qmp_query_name(Error **errp)
29 NameInfo *info = g_malloc0(sizeof(*info));
31 if (qemu_name) {
32 info->has_name = true;
33 info->name = g_strdup(qemu_name);
36 return info;
39 VersionInfo *qmp_query_version(Error **err)
41 VersionInfo *info = g_malloc0(sizeof(*info));
42 const char *version = QEMU_VERSION;
43 char *tmp;
45 info->qemu.major = strtol(version, &tmp, 10);
46 tmp++;
47 info->qemu.minor = strtol(tmp, &tmp, 10);
48 tmp++;
49 info->qemu.micro = strtol(tmp, &tmp, 10);
50 info->package = g_strdup(QEMU_PKGVERSION);
52 return info;
55 KvmInfo *qmp_query_kvm(Error **errp)
57 KvmInfo *info = g_malloc0(sizeof(*info));
59 info->enabled = kvm_enabled();
60 info->present = kvm_available();
62 return info;
65 UuidInfo *qmp_query_uuid(Error **errp)
67 UuidInfo *info = g_malloc0(sizeof(*info));
68 char uuid[64];
70 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
71 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
72 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
73 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
74 qemu_uuid[14], qemu_uuid[15]);
76 info->UUID = g_strdup(uuid);
77 return info;
80 void qmp_quit(Error **err)
82 no_shutdown = 0;
83 qemu_system_shutdown_request();
86 void qmp_stop(Error **errp)
88 vm_stop(RUN_STATE_PAUSED);
91 void qmp_system_reset(Error **errp)
93 qemu_system_reset_request();
96 void qmp_system_powerdown(Error **erp)
98 qemu_system_powerdown_request();
101 void qmp_cpu(int64_t index, Error **errp)
103 /* Just do nothing */
106 #ifndef CONFIG_VNC
107 /* If VNC support is enabled, the "true" query-vnc command is
108 defined in the VNC subsystem */
109 VncInfo *qmp_query_vnc(Error **errp)
111 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
112 return NULL;
114 #endif
116 #ifndef CONFIG_SPICE
117 /* If SPICE support is enabled, the "true" query-spice command is
118 defined in the SPICE subsystem. Also note that we use a small
119 trick to maintain query-spice's original behavior, which is not
120 to be available in the namespace if SPICE is not compiled in */
121 SpiceInfo *qmp_query_spice(Error **errp)
123 error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice");
124 return NULL;
126 #endif
128 static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
130 bdrv_iostatus_reset(bs);
133 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
135 Error **err = opaque;
137 if (!error_is_set(err) && bdrv_key_required(bs)) {
138 error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
139 bdrv_get_encrypted_filename(bs));
143 void qmp_cont(Error **errp)
145 Error *local_err = NULL;
147 if (runstate_check(RUN_STATE_INMIGRATE)) {
148 error_set(errp, QERR_MIGRATION_EXPECTED);
149 return;
150 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
151 runstate_check(RUN_STATE_SHUTDOWN)) {
152 error_set(errp, QERR_RESET_REQUIRED);
153 return;
156 bdrv_iterate(iostatus_bdrv_it, NULL);
157 bdrv_iterate(encrypted_bdrv_it, &local_err);
158 if (local_err) {
159 error_propagate(errp, local_err);
160 return;
163 vm_start();
166 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
168 Object *obj;
169 bool ambiguous = false;
170 ObjectPropertyInfoList *props = NULL;
171 ObjectProperty *prop;
173 obj = object_resolve_path(path, &ambiguous);
174 if (obj == NULL) {
175 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
176 return NULL;
179 QTAILQ_FOREACH(prop, &obj->properties, node) {
180 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
182 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
183 entry->next = props;
184 props = entry;
186 entry->value->name = g_strdup(prop->name);
187 entry->value->type = g_strdup(prop->type);
190 return props;
193 /* FIXME: teach qapi about how to pass through Visitors */
194 int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
196 const char *path = qdict_get_str(qdict, "path");
197 const char *property = qdict_get_str(qdict, "property");
198 QObject *value = qdict_get(qdict, "value");
199 Error *local_err = NULL;
200 Object *obj;
202 obj = object_resolve_path(path, NULL);
203 if (!obj) {
204 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
205 goto out;
208 object_property_set_qobject(obj, value, property, &local_err);
210 out:
211 if (local_err) {
212 qerror_report_err(local_err);
213 error_free(local_err);
214 return -1;
217 return 0;
220 int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
222 const char *path = qdict_get_str(qdict, "path");
223 const char *property = qdict_get_str(qdict, "property");
224 Error *local_err = NULL;
225 Object *obj;
227 obj = object_resolve_path(path, NULL);
228 if (!obj) {
229 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
230 goto out;
233 *ret = object_property_get_qobject(obj, property, &local_err);
235 out:
236 if (local_err) {
237 qerror_report_err(local_err);
238 error_free(local_err);
239 return -1;
242 return 0;
245 void qmp_set_password(const char *protocol, const char *password,
246 bool has_connected, const char *connected, Error **errp)
248 int disconnect_if_connected = 0;
249 int fail_if_connected = 0;
250 int rc;
252 if (has_connected) {
253 if (strcmp(connected, "fail") == 0) {
254 fail_if_connected = 1;
255 } else if (strcmp(connected, "disconnect") == 0) {
256 disconnect_if_connected = 1;
257 } else if (strcmp(connected, "keep") == 0) {
258 /* nothing */
259 } else {
260 error_set(errp, QERR_INVALID_PARAMETER, "connected");
261 return;
265 if (strcmp(protocol, "spice") == 0) {
266 if (!using_spice) {
267 /* correct one? spice isn't a device ,,, */
268 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
269 return;
271 rc = qemu_spice_set_passwd(password, fail_if_connected,
272 disconnect_if_connected);
273 if (rc != 0) {
274 error_set(errp, QERR_SET_PASSWD_FAILED);
276 return;
279 if (strcmp(protocol, "vnc") == 0) {
280 if (fail_if_connected || disconnect_if_connected) {
281 /* vnc supports "connected=keep" only */
282 error_set(errp, QERR_INVALID_PARAMETER, "connected");
283 return;
285 /* Note that setting an empty password will not disable login through
286 * this interface. */
287 rc = vnc_display_password(NULL, password);
288 if (rc < 0) {
289 error_set(errp, QERR_SET_PASSWD_FAILED);
291 return;
294 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
297 void qmp_expire_password(const char *protocol, const char *whenstr,
298 Error **errp)
300 time_t when;
301 int rc;
303 if (strcmp(whenstr, "now") == 0) {
304 when = 0;
305 } else if (strcmp(whenstr, "never") == 0) {
306 when = TIME_MAX;
307 } else if (whenstr[0] == '+') {
308 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
309 } else {
310 when = strtoull(whenstr, NULL, 10);
313 if (strcmp(protocol, "spice") == 0) {
314 if (!using_spice) {
315 /* correct one? spice isn't a device ,,, */
316 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
317 return;
319 rc = qemu_spice_set_pw_expire(when);
320 if (rc != 0) {
321 error_set(errp, QERR_SET_PASSWD_FAILED);
323 return;
326 if (strcmp(protocol, "vnc") == 0) {
327 rc = vnc_display_pw_expire(NULL, when);
328 if (rc != 0) {
329 error_set(errp, QERR_SET_PASSWD_FAILED);
331 return;
334 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
337 #ifdef CONFIG_VNC
338 void qmp_change_vnc_password(const char *password, Error **errp)
340 if (vnc_display_password(NULL, password) < 0) {
341 error_set(errp, QERR_SET_PASSWD_FAILED);
345 static void qmp_change_vnc_listen(const char *target, Error **err)
347 if (vnc_display_open(NULL, target) < 0) {
348 error_set(err, QERR_VNC_SERVER_FAILED, target);
352 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
353 Error **errp)
355 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
356 if (!has_arg) {
357 error_set(errp, QERR_MISSING_PARAMETER, "password");
358 } else {
359 qmp_change_vnc_password(arg, errp);
361 } else {
362 qmp_change_vnc_listen(target, errp);
365 #else
366 void qmp_change_vnc_password(const char *password, Error **errp)
368 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
370 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
371 Error **errp)
373 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
375 #endif /* !CONFIG_VNC */
377 void qmp_change(const char *device, const char *target,
378 bool has_arg, const char *arg, Error **err)
380 if (strcmp(device, "vnc") == 0) {
381 qmp_change_vnc(target, has_arg, arg, err);
382 } else {
383 qmp_change_blockdev(device, target, has_arg, arg, err);
387 static void qom_list_types_tramp(ObjectClass *klass, void *data)
389 ObjectTypeInfoList *e, **pret = data;
390 ObjectTypeInfo *info;
392 info = g_malloc0(sizeof(*info));
393 info->name = g_strdup(object_class_get_name(klass));
395 e = g_malloc0(sizeof(*e));
396 e->value = info;
397 e->next = *pret;
398 *pret = e;
401 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
402 const char *implements,
403 bool has_abstract,
404 bool abstract,
405 Error **errp)
407 ObjectTypeInfoList *ret = NULL;
409 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
411 return ret;