qmp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. *
  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.
  14. */
  15. #include "qemu-common.h"
  16. #include "sysemu.h"
  17. #include "qmp-commands.h"
  18. #include "ui/qemu-spice.h"
  19. #include "ui/vnc.h"
  20. #include "kvm.h"
  21. #include "arch_init.h"
  22. #include "hw/qdev.h"
  23. #include "blockdev.h"
  24. #include "qemu/qom-qobject.h"
  25. NameInfo *qmp_query_name(Error **errp)
  26. {
  27. NameInfo *info = g_malloc0(sizeof(*info));
  28. if (qemu_name) {
  29. info->has_name = true;
  30. info->name = g_strdup(qemu_name);
  31. }
  32. return info;
  33. }
  34. VersionInfo *qmp_query_version(Error **err)
  35. {
  36. VersionInfo *info = g_malloc0(sizeof(*info));
  37. const char *version = QEMU_VERSION;
  38. char *tmp;
  39. info->qemu.major = strtol(version, &tmp, 10);
  40. tmp++;
  41. info->qemu.minor = strtol(tmp, &tmp, 10);
  42. tmp++;
  43. info->qemu.micro = strtol(tmp, &tmp, 10);
  44. info->package = g_strdup(QEMU_PKGVERSION);
  45. return info;
  46. }
  47. KvmInfo *qmp_query_kvm(Error **errp)
  48. {
  49. KvmInfo *info = g_malloc0(sizeof(*info));
  50. info->enabled = kvm_enabled();
  51. info->present = kvm_available();
  52. return info;
  53. }
  54. UuidInfo *qmp_query_uuid(Error **errp)
  55. {
  56. UuidInfo *info = g_malloc0(sizeof(*info));
  57. char uuid[64];
  58. snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
  59. qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
  60. qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
  61. qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
  62. qemu_uuid[14], qemu_uuid[15]);
  63. info->UUID = g_strdup(uuid);
  64. return info;
  65. }
  66. void qmp_quit(Error **err)
  67. {
  68. no_shutdown = 0;
  69. qemu_system_shutdown_request();
  70. }
  71. void qmp_stop(Error **errp)
  72. {
  73. vm_stop(RUN_STATE_PAUSED);
  74. }
  75. void qmp_system_reset(Error **errp)
  76. {
  77. qemu_system_reset_request();
  78. }
  79. void qmp_system_powerdown(Error **erp)
  80. {
  81. qemu_system_powerdown_request();
  82. }
  83. void qmp_cpu(int64_t index, Error **errp)
  84. {
  85. /* Just do nothing */
  86. }
  87. #ifndef CONFIG_VNC
  88. /* If VNC support is enabled, the "true" query-vnc command is
  89. defined in the VNC subsystem */
  90. VncInfo *qmp_query_vnc(Error **errp)
  91. {
  92. error_set(errp, QERR_FEATURE_DISABLED, "vnc");
  93. return NULL;
  94. };
  95. #endif
  96. #ifndef CONFIG_SPICE
  97. /* If SPICE support is enabled, the "true" query-spice command is
  98. defined in the SPICE subsystem. Also note that we use a small
  99. trick to maintain query-spice's original behavior, which is not
  100. to be available in the namespace if SPICE is not compiled in */
  101. SpiceInfo *qmp_query_spice(Error **errp)
  102. {
  103. error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice");
  104. return NULL;
  105. };
  106. #endif
  107. static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
  108. {
  109. bdrv_iostatus_reset(bs);
  110. }
  111. static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
  112. {
  113. Error **err = opaque;
  114. if (!error_is_set(err) && bdrv_key_required(bs)) {
  115. error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
  116. bdrv_get_encrypted_filename(bs));
  117. }
  118. }
  119. void qmp_cont(Error **errp)
  120. {
  121. Error *local_err = NULL;
  122. if (runstate_check(RUN_STATE_INMIGRATE)) {
  123. error_set(errp, QERR_MIGRATION_EXPECTED);
  124. return;
  125. } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
  126. runstate_check(RUN_STATE_SHUTDOWN)) {
  127. error_set(errp, QERR_RESET_REQUIRED);
  128. return;
  129. } else if (runstate_check(RUN_STATE_SUSPENDED)) {
  130. return;
  131. }
  132. bdrv_iterate(iostatus_bdrv_it, NULL);
  133. bdrv_iterate(encrypted_bdrv_it, &local_err);
  134. if (local_err) {
  135. error_propagate(errp, local_err);
  136. return;
  137. }
  138. vm_start();
  139. }
  140. void qmp_system_wakeup(Error **errp)
  141. {
  142. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
  143. }
  144. ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
  145. {
  146. Object *obj;
  147. bool ambiguous = false;
  148. ObjectPropertyInfoList *props = NULL;
  149. ObjectProperty *prop;
  150. obj = object_resolve_path(path, &ambiguous);
  151. if (obj == NULL) {
  152. error_set(errp, QERR_DEVICE_NOT_FOUND, path);
  153. return NULL;
  154. }
  155. QTAILQ_FOREACH(prop, &obj->properties, node) {
  156. ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
  157. entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
  158. entry->next = props;
  159. props = entry;
  160. entry->value->name = g_strdup(prop->name);
  161. entry->value->type = g_strdup(prop->type);
  162. }
  163. return props;
  164. }
  165. /* FIXME: teach qapi about how to pass through Visitors */
  166. int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
  167. {
  168. const char *path = qdict_get_str(qdict, "path");
  169. const char *property = qdict_get_str(qdict, "property");
  170. QObject *value = qdict_get(qdict, "value");
  171. Error *local_err = NULL;
  172. Object *obj;
  173. obj = object_resolve_path(path, NULL);
  174. if (!obj) {
  175. error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
  176. goto out;
  177. }
  178. object_property_set_qobject(obj, value, property, &local_err);
  179. out:
  180. if (local_err) {
  181. qerror_report_err(local_err);
  182. error_free(local_err);
  183. return -1;
  184. }
  185. return 0;
  186. }
  187. int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
  188. {
  189. const char *path = qdict_get_str(qdict, "path");
  190. const char *property = qdict_get_str(qdict, "property");
  191. Error *local_err = NULL;
  192. Object *obj;
  193. obj = object_resolve_path(path, NULL);
  194. if (!obj) {
  195. error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
  196. goto out;
  197. }
  198. *ret = object_property_get_qobject(obj, property, &local_err);
  199. out:
  200. if (local_err) {
  201. qerror_report_err(local_err);
  202. error_free(local_err);
  203. return -1;
  204. }
  205. return 0;
  206. }
  207. void qmp_set_password(const char *protocol, const char *password,
  208. bool has_connected, const char *connected, Error **errp)
  209. {
  210. int disconnect_if_connected = 0;
  211. int fail_if_connected = 0;
  212. int rc;
  213. if (has_connected) {
  214. if (strcmp(connected, "fail") == 0) {
  215. fail_if_connected = 1;
  216. } else if (strcmp(connected, "disconnect") == 0) {
  217. disconnect_if_connected = 1;
  218. } else if (strcmp(connected, "keep") == 0) {
  219. /* nothing */
  220. } else {
  221. error_set(errp, QERR_INVALID_PARAMETER, "connected");
  222. return;
  223. }
  224. }
  225. if (strcmp(protocol, "spice") == 0) {
  226. if (!using_spice) {
  227. /* correct one? spice isn't a device ,,, */
  228. error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
  229. return;
  230. }
  231. rc = qemu_spice_set_passwd(password, fail_if_connected,
  232. disconnect_if_connected);
  233. if (rc != 0) {
  234. error_set(errp, QERR_SET_PASSWD_FAILED);
  235. }
  236. return;
  237. }
  238. if (strcmp(protocol, "vnc") == 0) {
  239. if (fail_if_connected || disconnect_if_connected) {
  240. /* vnc supports "connected=keep" only */
  241. error_set(errp, QERR_INVALID_PARAMETER, "connected");
  242. return;
  243. }
  244. /* Note that setting an empty password will not disable login through
  245. * this interface. */
  246. rc = vnc_display_password(NULL, password);
  247. if (rc < 0) {
  248. error_set(errp, QERR_SET_PASSWD_FAILED);
  249. }
  250. return;
  251. }
  252. error_set(errp, QERR_INVALID_PARAMETER, "protocol");
  253. }
  254. void qmp_expire_password(const char *protocol, const char *whenstr,
  255. Error **errp)
  256. {
  257. time_t when;
  258. int rc;
  259. if (strcmp(whenstr, "now") == 0) {
  260. when = 0;
  261. } else if (strcmp(whenstr, "never") == 0) {
  262. when = TIME_MAX;
  263. } else if (whenstr[0] == '+') {
  264. when = time(NULL) + strtoull(whenstr+1, NULL, 10);
  265. } else {
  266. when = strtoull(whenstr, NULL, 10);
  267. }
  268. if (strcmp(protocol, "spice") == 0) {
  269. if (!using_spice) {
  270. /* correct one? spice isn't a device ,,, */
  271. error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
  272. return;
  273. }
  274. rc = qemu_spice_set_pw_expire(when);
  275. if (rc != 0) {
  276. error_set(errp, QERR_SET_PASSWD_FAILED);
  277. }
  278. return;
  279. }
  280. if (strcmp(protocol, "vnc") == 0) {
  281. rc = vnc_display_pw_expire(NULL, when);
  282. if (rc != 0) {
  283. error_set(errp, QERR_SET_PASSWD_FAILED);
  284. }
  285. return;
  286. }
  287. error_set(errp, QERR_INVALID_PARAMETER, "protocol");
  288. }
  289. #ifdef CONFIG_VNC
  290. void qmp_change_vnc_password(const char *password, Error **errp)
  291. {
  292. if (vnc_display_password(NULL, password) < 0) {
  293. error_set(errp, QERR_SET_PASSWD_FAILED);
  294. }
  295. }
  296. static void qmp_change_vnc_listen(const char *target, Error **err)
  297. {
  298. if (vnc_display_open(NULL, target) < 0) {
  299. error_set(err, QERR_VNC_SERVER_FAILED, target);
  300. }
  301. }
  302. static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
  303. Error **errp)
  304. {
  305. if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
  306. if (!has_arg) {
  307. error_set(errp, QERR_MISSING_PARAMETER, "password");
  308. } else {
  309. qmp_change_vnc_password(arg, errp);
  310. }
  311. } else {
  312. qmp_change_vnc_listen(target, errp);
  313. }
  314. }
  315. #else
  316. void qmp_change_vnc_password(const char *password, Error **errp)
  317. {
  318. error_set(errp, QERR_FEATURE_DISABLED, "vnc");
  319. }
  320. static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
  321. Error **errp)
  322. {
  323. error_set(errp, QERR_FEATURE_DISABLED, "vnc");
  324. }
  325. #endif /* !CONFIG_VNC */
  326. void qmp_change(const char *device, const char *target,
  327. bool has_arg, const char *arg, Error **err)
  328. {
  329. if (strcmp(device, "vnc") == 0) {
  330. qmp_change_vnc(target, has_arg, arg, err);
  331. } else {
  332. qmp_change_blockdev(device, target, has_arg, arg, err);
  333. }
  334. }
  335. static void qom_list_types_tramp(ObjectClass *klass, void *data)
  336. {
  337. ObjectTypeInfoList *e, **pret = data;
  338. ObjectTypeInfo *info;
  339. info = g_malloc0(sizeof(*info));
  340. info->name = g_strdup(object_class_get_name(klass));
  341. e = g_malloc0(sizeof(*e));
  342. e->value = info;
  343. e->next = *pret;
  344. *pret = e;
  345. }
  346. ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
  347. const char *implements,
  348. bool has_abstract,
  349. bool abstract,
  350. Error **errp)
  351. {
  352. ObjectTypeInfoList *ret = NULL;
  353. object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
  354. return ret;
  355. }
  356. DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
  357. Error **errp)
  358. {
  359. ObjectClass *klass;
  360. Property *prop;
  361. DevicePropertyInfoList *prop_list = NULL;
  362. klass = object_class_by_name(typename);
  363. if (klass == NULL) {
  364. error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
  365. return NULL;
  366. }
  367. klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
  368. if (klass == NULL) {
  369. error_set(errp, QERR_INVALID_PARAMETER_VALUE,
  370. "name", TYPE_DEVICE);
  371. return NULL;
  372. }
  373. do {
  374. for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
  375. DevicePropertyInfoList *entry;
  376. DevicePropertyInfo *info;
  377. /*
  378. * TODO Properties without a parser are just for dirty hacks.
  379. * qdev_prop_ptr is the only such PropertyInfo. It's marked
  380. * for removal. This conditional should be removed along with
  381. * it.
  382. */
  383. if (!prop->info->set) {
  384. continue; /* no way to set it, don't show */
  385. }
  386. info = g_malloc0(sizeof(*info));
  387. info->name = g_strdup(prop->name);
  388. info->type = g_strdup(prop->info->legacy_name ?: prop->info->name);
  389. entry = g_malloc0(sizeof(*entry));
  390. entry->value = info;
  391. entry->next = prop_list;
  392. prop_list = entry;
  393. }
  394. klass = object_class_get_parent(klass);
  395. } while (klass != object_class_by_name(TYPE_DEVICE));
  396. return prop_list;
  397. }
  398. CpuDefinitionInfoList GCC_WEAK *arch_query_cpu_definitions(Error **errp)
  399. {
  400. error_set(errp, QERR_NOT_SUPPORTED);
  401. return NULL;
  402. }
  403. CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
  404. {
  405. return arch_query_cpu_definitions(errp);
  406. }