qmp-cmds-control.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. Monitor *cur_mon = monitor_cur();
  65. MonitorQMP *mon;
  66. assert(monitor_is_qmp(cur_mon));
  67. mon = container_of(cur_mon, MonitorQMP, common);
  68. if (mon->commands == &qmp_commands) {
  69. error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
  70. "Capabilities negotiation is already complete, command "
  71. "ignored");
  72. return;
  73. }
  74. if (!qmp_caps_accept(mon, enable, errp)) {
  75. return;
  76. }
  77. mon->commands = &qmp_commands;
  78. }
  79. VersionInfo *qmp_query_version(Error **errp)
  80. {
  81. VersionInfo *info = g_new0(VersionInfo, 1);
  82. info->qemu = g_new0(VersionTriple, 1);
  83. info->qemu->major = QEMU_VERSION_MAJOR;
  84. info->qemu->minor = QEMU_VERSION_MINOR;
  85. info->qemu->micro = QEMU_VERSION_MICRO;
  86. info->package = g_strdup(QEMU_PKGVERSION);
  87. return info;
  88. }
  89. static void query_commands_cb(const QmpCommand *cmd, void *opaque)
  90. {
  91. CommandInfoList *info, **list = opaque;
  92. if (!cmd->enabled) {
  93. return;
  94. }
  95. info = g_malloc0(sizeof(*info));
  96. info->value = g_malloc0(sizeof(*info->value));
  97. info->value->name = g_strdup(cmd->name);
  98. info->next = *list;
  99. *list = info;
  100. }
  101. CommandInfoList *qmp_query_commands(Error **errp)
  102. {
  103. CommandInfoList *list = NULL;
  104. Monitor *cur_mon = monitor_cur();
  105. MonitorQMP *mon;
  106. assert(monitor_is_qmp(cur_mon));
  107. mon = container_of(cur_mon, MonitorQMP, common);
  108. qmp_for_each_command(mon->commands, query_commands_cb, &list);
  109. return list;
  110. }
  111. EventInfoList *qmp_query_events(Error **errp)
  112. {
  113. /*
  114. * TODO This deprecated command is the only user of
  115. * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
  116. * they should go, too.
  117. */
  118. EventInfoList *info, *ev_list = NULL;
  119. QAPIEvent e;
  120. for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
  121. const char *event_name = QAPIEvent_str(e);
  122. assert(event_name != NULL);
  123. info = g_malloc0(sizeof(*info));
  124. info->value = g_malloc0(sizeof(*info->value));
  125. info->value->name = g_strdup(event_name);
  126. info->next = ev_list;
  127. ev_list = info;
  128. }
  129. return ev_list;
  130. }
  131. /*
  132. * Minor hack: generated marshalling suppressed for this command
  133. * ('gen': false in the schema) so we can parse the JSON string
  134. * directly into QObject instead of first parsing it with
  135. * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
  136. * to QObject with generated output marshallers, every time. Instead,
  137. * we do it in test-qobject-input-visitor.c, just to make sure
  138. * qapi-gen.py's output actually conforms to the schema.
  139. */
  140. void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  141. Error **errp)
  142. {
  143. *ret_data = qobject_from_qlit(&qmp_schema_qlit);
  144. }