Anthony Liguori | 48a32be | 2011-09-02 12:34:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Management Protocol |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2011 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 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. |
Anthony Liguori | 48a32be | 2011-09-02 12:34:48 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "qemu-common.h" |
| 17 | #include "sysemu.h" |
| 18 | #include "qmp-commands.h" |
Luiz Capitulino | fbf796f | 2011-12-07 11:17:51 -0200 | [diff] [blame] | 19 | #include "ui/qemu-spice.h" |
| 20 | #include "ui/vnc.h" |
Luiz Capitulino | 292a260 | 2011-09-12 15:10:53 -0300 | [diff] [blame] | 21 | #include "kvm.h" |
| 22 | #include "arch_init.h" |
Anthony Liguori | b4b12c6 | 2011-12-12 14:29:34 -0600 | [diff] [blame] | 23 | #include "hw/qdev.h" |
Anthony Liguori | eb6e8ea | 2011-12-12 14:29:35 -0600 | [diff] [blame] | 24 | #include "qapi/qmp-input-visitor.h" |
| 25 | #include "qapi/qmp-output-visitor.h" |
Anthony Liguori | 48a32be | 2011-09-02 12:34:48 -0500 | [diff] [blame] | 26 | |
| 27 | NameInfo *qmp_query_name(Error **errp) |
| 28 | { |
| 29 | NameInfo *info = g_malloc0(sizeof(*info)); |
| 30 | |
| 31 | if (qemu_name) { |
| 32 | info->has_name = true; |
| 33 | info->name = g_strdup(qemu_name); |
| 34 | } |
| 35 | |
| 36 | return info; |
| 37 | } |
Luiz Capitulino | b9c15f1 | 2011-08-26 17:38:13 -0300 | [diff] [blame] | 38 | |
| 39 | VersionInfo *qmp_query_version(Error **err) |
| 40 | { |
| 41 | VersionInfo *info = g_malloc0(sizeof(*info)); |
| 42 | const char *version = QEMU_VERSION; |
| 43 | char *tmp; |
| 44 | |
| 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); |
| 51 | |
| 52 | return info; |
| 53 | } |
Luiz Capitulino | 292a260 | 2011-09-12 15:10:53 -0300 | [diff] [blame] | 54 | |
| 55 | KvmInfo *qmp_query_kvm(Error **errp) |
| 56 | { |
| 57 | KvmInfo *info = g_malloc0(sizeof(*info)); |
| 58 | |
| 59 | info->enabled = kvm_enabled(); |
| 60 | info->present = kvm_available(); |
| 61 | |
| 62 | return info; |
| 63 | } |
| 64 | |
Luiz Capitulino | efab767 | 2011-09-13 17:16:25 -0300 | [diff] [blame] | 65 | UuidInfo *qmp_query_uuid(Error **errp) |
| 66 | { |
| 67 | UuidInfo *info = g_malloc0(sizeof(*info)); |
| 68 | char uuid[64]; |
| 69 | |
| 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]); |
| 75 | |
| 76 | info->UUID = g_strdup(uuid); |
| 77 | return info; |
| 78 | } |
| 79 | |
Luiz Capitulino | 7a7f325 | 2011-09-15 14:20:28 -0300 | [diff] [blame] | 80 | void qmp_quit(Error **err) |
| 81 | { |
| 82 | no_shutdown = 0; |
| 83 | qemu_system_shutdown_request(); |
| 84 | } |
| 85 | |
Luiz Capitulino | 5f158f2 | 2011-09-15 14:34:39 -0300 | [diff] [blame] | 86 | void qmp_stop(Error **errp) |
| 87 | { |
| 88 | vm_stop(RUN_STATE_PAUSED); |
| 89 | } |
| 90 | |
Luiz Capitulino | 38d2265 | 2011-09-15 14:41:46 -0300 | [diff] [blame] | 91 | void qmp_system_reset(Error **errp) |
| 92 | { |
| 93 | qemu_system_reset_request(); |
| 94 | } |
Luiz Capitulino | 5bc465e | 2011-09-28 11:06:15 -0300 | [diff] [blame] | 95 | |
| 96 | void qmp_system_powerdown(Error **erp) |
| 97 | { |
| 98 | qemu_system_powerdown_request(); |
| 99 | } |
Luiz Capitulino | 755f196 | 2011-10-06 14:31:39 -0300 | [diff] [blame] | 100 | |
| 101 | void qmp_cpu(int64_t index, Error **errp) |
| 102 | { |
| 103 | /* Just do nothing */ |
| 104 | } |
Luiz Capitulino | 2b54aa8 | 2011-10-17 16:41:22 -0200 | [diff] [blame] | 105 | |
| 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) |
| 110 | { |
| 111 | error_set(errp, QERR_FEATURE_DISABLED, "vnc"); |
| 112 | return NULL; |
| 113 | }; |
| 114 | #endif |
Luiz Capitulino | d1f2964 | 2011-10-20 17:01:33 -0200 | [diff] [blame] | 115 | |
| 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) |
| 122 | { |
| 123 | error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice"); |
| 124 | return NULL; |
| 125 | }; |
| 126 | #endif |
Luiz Capitulino | e42e818 | 2011-11-22 17:58:31 -0200 | [diff] [blame] | 127 | |
| 128 | static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs) |
| 129 | { |
| 130 | bdrv_iostatus_reset(bs); |
| 131 | } |
| 132 | |
| 133 | static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs) |
| 134 | { |
| 135 | Error **err = opaque; |
| 136 | |
| 137 | if (!error_is_set(err) && bdrv_key_required(bs)) { |
| 138 | error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void qmp_cont(Error **errp) |
| 143 | { |
| 144 | Error *local_err = NULL; |
| 145 | |
| 146 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
| 147 | error_set(errp, QERR_MIGRATION_EXPECTED); |
| 148 | return; |
| 149 | } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) || |
| 150 | runstate_check(RUN_STATE_SHUTDOWN)) { |
| 151 | error_set(errp, QERR_RESET_REQUIRED); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | bdrv_iterate(iostatus_bdrv_it, NULL); |
| 156 | bdrv_iterate(encrypted_bdrv_it, &local_err); |
| 157 | if (local_err) { |
| 158 | error_propagate(errp, local_err); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | vm_start(); |
| 163 | } |
Anthony Liguori | b4b12c6 | 2011-12-12 14:29:34 -0600 | [diff] [blame] | 164 | |
| 165 | DevicePropertyInfoList *qmp_qom_list(const char *path, Error **errp) |
| 166 | { |
| 167 | DeviceState *dev; |
| 168 | bool ambiguous = false; |
| 169 | DevicePropertyInfoList *props = NULL; |
| 170 | DeviceProperty *prop; |
| 171 | |
| 172 | dev = qdev_resolve_path(path, &ambiguous); |
| 173 | if (dev == NULL) { |
| 174 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | QTAILQ_FOREACH(prop, &dev->properties, node) { |
| 179 | DevicePropertyInfoList *entry = g_malloc0(sizeof(*entry)); |
| 180 | |
| 181 | entry->value = g_malloc0(sizeof(DevicePropertyInfo)); |
| 182 | entry->next = props; |
| 183 | props = entry; |
| 184 | |
| 185 | entry->value->name = g_strdup(prop->name); |
| 186 | entry->value->type = g_strdup(prop->type); |
| 187 | } |
| 188 | |
| 189 | return props; |
| 190 | } |
Anthony Liguori | eb6e8ea | 2011-12-12 14:29:35 -0600 | [diff] [blame] | 191 | |
| 192 | /* FIXME: teach qapi about how to pass through Visitors */ |
| 193 | int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret) |
| 194 | { |
| 195 | const char *path = qdict_get_str(qdict, "path"); |
| 196 | const char *property = qdict_get_str(qdict, "property"); |
| 197 | QObject *value = qdict_get(qdict, "value"); |
| 198 | Error *local_err = NULL; |
| 199 | QmpInputVisitor *mi; |
| 200 | DeviceState *dev; |
| 201 | |
| 202 | dev = qdev_resolve_path(path, NULL); |
| 203 | if (!dev) { |
| 204 | error_set(&local_err, QERR_DEVICE_NOT_FOUND, path); |
| 205 | goto out; |
| 206 | } |
| 207 | |
| 208 | mi = qmp_input_visitor_new(value); |
| 209 | qdev_property_set(dev, qmp_input_get_visitor(mi), property, &local_err); |
| 210 | |
| 211 | qmp_input_visitor_cleanup(mi); |
| 212 | |
| 213 | out: |
| 214 | if (local_err) { |
| 215 | qerror_report_err(local_err); |
| 216 | error_free(local_err); |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret) |
| 224 | { |
| 225 | const char *path = qdict_get_str(qdict, "path"); |
| 226 | const char *property = qdict_get_str(qdict, "property"); |
| 227 | Error *local_err = NULL; |
| 228 | QmpOutputVisitor *mo; |
| 229 | DeviceState *dev; |
| 230 | |
| 231 | dev = qdev_resolve_path(path, NULL); |
| 232 | if (!dev) { |
| 233 | error_set(&local_err, QERR_DEVICE_NOT_FOUND, path); |
| 234 | goto out; |
| 235 | } |
| 236 | |
| 237 | mo = qmp_output_visitor_new(); |
| 238 | qdev_property_get(dev, qmp_output_get_visitor(mo), property, &local_err); |
| 239 | if (!local_err) { |
| 240 | *ret = qmp_output_get_qobject(mo); |
| 241 | } |
| 242 | |
| 243 | qmp_output_visitor_cleanup(mo); |
| 244 | |
| 245 | out: |
| 246 | if (local_err) { |
| 247 | qerror_report_err(local_err); |
| 248 | error_free(local_err); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
Luiz Capitulino | fbf796f | 2011-12-07 11:17:51 -0200 | [diff] [blame] | 254 | |
| 255 | void qmp_set_password(const char *protocol, const char *password, |
| 256 | bool has_connected, const char *connected, Error **errp) |
| 257 | { |
| 258 | int disconnect_if_connected = 0; |
| 259 | int fail_if_connected = 0; |
| 260 | int rc; |
| 261 | |
| 262 | if (has_connected) { |
| 263 | if (strcmp(connected, "fail") == 0) { |
| 264 | fail_if_connected = 1; |
| 265 | } else if (strcmp(connected, "disconnect") == 0) { |
| 266 | disconnect_if_connected = 1; |
| 267 | } else if (strcmp(connected, "keep") == 0) { |
| 268 | /* nothing */ |
| 269 | } else { |
| 270 | error_set(errp, QERR_INVALID_PARAMETER, "connected"); |
| 271 | return; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (strcmp(protocol, "spice") == 0) { |
| 276 | if (!using_spice) { |
| 277 | /* correct one? spice isn't a device ,,, */ |
| 278 | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice"); |
| 279 | return; |
| 280 | } |
| 281 | rc = qemu_spice_set_passwd(password, fail_if_connected, |
| 282 | disconnect_if_connected); |
| 283 | if (rc != 0) { |
| 284 | error_set(errp, QERR_SET_PASSWD_FAILED); |
| 285 | } |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | if (strcmp(protocol, "vnc") == 0) { |
| 290 | if (fail_if_connected || disconnect_if_connected) { |
| 291 | /* vnc supports "connected=keep" only */ |
| 292 | error_set(errp, QERR_INVALID_PARAMETER, "connected"); |
| 293 | return; |
| 294 | } |
| 295 | /* Note that setting an empty password will not disable login through |
| 296 | * this interface. */ |
| 297 | rc = vnc_display_password(NULL, password); |
| 298 | if (rc < 0) { |
| 299 | error_set(errp, QERR_SET_PASSWD_FAILED); |
| 300 | } |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | error_set(errp, QERR_INVALID_PARAMETER, "protocol"); |
| 305 | } |
Luiz Capitulino | 9ad5372 | 2011-12-07 11:47:57 -0200 | [diff] [blame^] | 306 | |
| 307 | void qmp_expire_password(const char *protocol, const char *whenstr, |
| 308 | Error **errp) |
| 309 | { |
| 310 | time_t when; |
| 311 | int rc; |
| 312 | |
| 313 | if (strcmp(whenstr, "now") == 0) { |
| 314 | when = 0; |
| 315 | } else if (strcmp(whenstr, "never") == 0) { |
| 316 | when = TIME_MAX; |
| 317 | } else if (whenstr[0] == '+') { |
| 318 | when = time(NULL) + strtoull(whenstr+1, NULL, 10); |
| 319 | } else { |
| 320 | when = strtoull(whenstr, NULL, 10); |
| 321 | } |
| 322 | |
| 323 | if (strcmp(protocol, "spice") == 0) { |
| 324 | if (!using_spice) { |
| 325 | /* correct one? spice isn't a device ,,, */ |
| 326 | error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice"); |
| 327 | return; |
| 328 | } |
| 329 | rc = qemu_spice_set_pw_expire(when); |
| 330 | if (rc != 0) { |
| 331 | error_set(errp, QERR_SET_PASSWD_FAILED); |
| 332 | } |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | if (strcmp(protocol, "vnc") == 0) { |
| 337 | rc = vnc_display_pw_expire(NULL, when); |
| 338 | if (rc != 0) { |
| 339 | error_set(errp, QERR_SET_PASSWD_FAILED); |
| 340 | } |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | error_set(errp, QERR_INVALID_PARAMETER, "protocol"); |
| 345 | } |