qmp-cmds-control.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/error.h"
  28. #include "qapi/qapi-commands-control.h"
  29. #include "qapi/qapi-emit-events.h"
  30. #include "qapi/qapi-introspect.h"
  31. /*
  32. * Accept QMP capabilities in @list for @mon.
  33. * On success, set mon->qmp.capab[], and return true.
  34. * On error, set @errp, and return false.
  35. */
  36. static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
  37. Error **errp)
  38. {
  39. GString *unavailable = NULL;
  40. bool capab[QMP_CAPABILITY__MAX];
  41. memset(capab, 0, sizeof(capab));
  42. for (; list; list = list->next) {
  43. if (!mon->capab_offered[list->value]) {
  44. if (!unavailable) {
  45. unavailable = g_string_new(QMPCapability_str(list->value));
  46. } else {
  47. g_string_append_printf(unavailable, ", %s",
  48. QMPCapability_str(list->value));
  49. }
  50. }
  51. capab[list->value] = true;
  52. }
  53. if (unavailable) {
  54. error_setg(errp, "Capability %s not available", unavailable->str);
  55. g_string_free(unavailable, true);
  56. return false;
  57. }
  58. memcpy(mon->capab, capab, sizeof(capab));
  59. return true;
  60. }
  61. void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
  62. Error **errp)
  63. {
  64. MonitorQMP *mon;
  65. assert(monitor_is_qmp(cur_mon));
  66. mon = container_of(cur_mon, MonitorQMP, common);
  67. if (mon->commands == &qmp_commands) {
  68. error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
  69. "Capabilities negotiation is already complete, command "
  70. "ignored");
  71. return;
  72. }
  73. if (!qmp_caps_accept(mon, enable, errp)) {
  74. return;
  75. }
  76. mon->commands = &qmp_commands;
  77. }
  78. VersionInfo *qmp_query_version(Error **errp)
  79. {
  80. VersionInfo *info = g_new0(VersionInfo, 1);
  81. info->qemu = g_new0(VersionTriple, 1);
  82. info->qemu->major = QEMU_VERSION_MAJOR;
  83. info->qemu->minor = QEMU_VERSION_MINOR;
  84. info->qemu->micro = QEMU_VERSION_MICRO;
  85. info->package = g_strdup(QEMU_PKGVERSION);
  86. return info;
  87. }
  88. static void query_commands_cb(const QmpCommand *cmd, void *opaque)
  89. {
  90. CommandInfoList *info, **list = opaque;
  91. if (!cmd->enabled) {
  92. return;
  93. }
  94. info = g_malloc0(sizeof(*info));
  95. info->value = g_malloc0(sizeof(*info->value));
  96. info->value->name = g_strdup(cmd->name);
  97. info->next = *list;
  98. *list = info;
  99. }
  100. CommandInfoList *qmp_query_commands(Error **errp)
  101. {
  102. CommandInfoList *list = NULL;
  103. MonitorQMP *mon;
  104. assert(monitor_is_qmp(cur_mon));
  105. mon = container_of(cur_mon, MonitorQMP, common);
  106. qmp_for_each_command(mon->commands, query_commands_cb, &list);
  107. return list;
  108. }
  109. EventInfoList *qmp_query_events(Error **errp)
  110. {
  111. /*
  112. * TODO This deprecated command is the only user of
  113. * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
  114. * they should go, too.
  115. */
  116. EventInfoList *info, *ev_list = NULL;
  117. QAPIEvent e;
  118. for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
  119. const char *event_name = QAPIEvent_str(e);
  120. assert(event_name != NULL);
  121. info = g_malloc0(sizeof(*info));
  122. info->value = g_malloc0(sizeof(*info->value));
  123. info->value->name = g_strdup(event_name);
  124. info->next = ev_list;
  125. ev_list = info;
  126. }
  127. return ev_list;
  128. }
  129. /*
  130. * Minor hack: generated marshalling suppressed for this command
  131. * ('gen': false in the schema) so we can parse the JSON string
  132. * directly into QObject instead of first parsing it with
  133. * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
  134. * to QObject with generated output marshallers, every time. Instead,
  135. * we do it in test-qobject-input-visitor.c, just to make sure
  136. * qapi-gen.py's output actually conforms to the schema.
  137. */
  138. void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  139. Error **errp)
  140. {
  141. *ret_data = qobject_from_qlit(&qmp_schema_qlit);
  142. }