2
0

qmp-cmds-control.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * QMP commands related to the monitor (common to sysemu and tools)
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "monitor-internal.h"
  26. #include "qemu-version.h"
  27. #include "qapi/compat-policy.h"
  28. #include "qapi/error.h"
  29. #include "qapi/qapi-commands-control.h"
  30. #include "qapi/qapi-commands-introspect.h"
  31. #include "qapi/qapi-emit-events.h"
  32. #include "qapi/qapi-introspect.h"
  33. #include "qapi/qapi-visit-introspect.h"
  34. #include "qapi/qobject-input-visitor.h"
  35. /*
  36. * Accept QMP capabilities in @list for @mon.
  37. * On success, set mon->qmp.capab[], and return true.
  38. * On error, set @errp, and return false.
  39. */
  40. static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
  41. Error **errp)
  42. {
  43. GString *unavailable = NULL;
  44. bool capab[QMP_CAPABILITY__MAX];
  45. memset(capab, 0, sizeof(capab));
  46. for (; list; list = list->next) {
  47. if (!mon->capab_offered[list->value]) {
  48. if (!unavailable) {
  49. unavailable = g_string_new(QMPCapability_str(list->value));
  50. } else {
  51. g_string_append_printf(unavailable, ", %s",
  52. QMPCapability_str(list->value));
  53. }
  54. }
  55. capab[list->value] = true;
  56. }
  57. if (unavailable) {
  58. error_setg(errp, "Capability %s not available", unavailable->str);
  59. g_string_free(unavailable, true);
  60. return false;
  61. }
  62. memcpy(mon->capab, capab, sizeof(capab));
  63. return true;
  64. }
  65. void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
  66. Error **errp)
  67. {
  68. Monitor *cur_mon = monitor_cur();
  69. MonitorQMP *mon;
  70. assert(monitor_is_qmp(cur_mon));
  71. mon = container_of(cur_mon, MonitorQMP, common);
  72. if (mon->commands == &qmp_commands) {
  73. error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
  74. "Capabilities negotiation is already complete, command "
  75. "ignored");
  76. return;
  77. }
  78. if (!qmp_caps_accept(mon, enable, errp)) {
  79. return;
  80. }
  81. mon->commands = &qmp_commands;
  82. }
  83. VersionInfo *qmp_query_version(Error **errp)
  84. {
  85. VersionInfo *info = g_new0(VersionInfo, 1);
  86. info->qemu = g_new0(VersionTriple, 1);
  87. info->qemu->major = QEMU_VERSION_MAJOR;
  88. info->qemu->minor = QEMU_VERSION_MINOR;
  89. info->qemu->micro = QEMU_VERSION_MICRO;
  90. info->package = g_strdup(QEMU_PKGVERSION);
  91. return info;
  92. }
  93. static void query_commands_cb(const QmpCommand *cmd, void *opaque)
  94. {
  95. CommandInfo *info;
  96. CommandInfoList **list = opaque;
  97. if (!cmd->enabled) {
  98. return;
  99. }
  100. info = g_malloc0(sizeof(*info));
  101. info->name = g_strdup(cmd->name);
  102. QAPI_LIST_PREPEND(*list, info);
  103. }
  104. CommandInfoList *qmp_query_commands(Error **errp)
  105. {
  106. CommandInfoList *list = NULL;
  107. Monitor *cur_mon = monitor_cur();
  108. MonitorQMP *mon;
  109. assert(monitor_is_qmp(cur_mon));
  110. mon = container_of(cur_mon, MonitorQMP, common);
  111. qmp_for_each_command(mon->commands, query_commands_cb, &list);
  112. return list;
  113. }
  114. SchemaInfoList *qmp_query_qmp_schema(Error **errp)
  115. {
  116. QObject *obj = qobject_from_qlit(&qmp_schema_qlit);
  117. Visitor *v = qobject_input_visitor_new(obj);
  118. SchemaInfoList *schema = NULL;
  119. /* test_visitor_in_qmp_introspect() ensures this can't fail */
  120. visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
  121. g_assert(schema);
  122. qobject_unref(obj);
  123. visit_free(v);
  124. return schema;
  125. }