qmp-cmds-control.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * QMP commands related to the monitor (common to system 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-introspect.h"
  32. #include "qapi/qapi-visit-introspect.h"
  33. #include "qapi/qobject-input-visitor.h"
  34. /*
  35. * Accept QMP capabilities in @list for @mon.
  36. * On success, set mon->qmp.capab[], and return true.
  37. * On error, set @errp, and return false.
  38. */
  39. static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
  40. Error **errp)
  41. {
  42. GString *unavailable = NULL;
  43. bool capab[QMP_CAPABILITY__MAX];
  44. memset(capab, 0, sizeof(capab));
  45. for (; list; list = list->next) {
  46. if (!mon->capab_offered[list->value]) {
  47. if (!unavailable) {
  48. unavailable = g_string_new(QMPCapability_str(list->value));
  49. } else {
  50. g_string_append_printf(unavailable, ", %s",
  51. QMPCapability_str(list->value));
  52. }
  53. }
  54. capab[list->value] = true;
  55. }
  56. if (unavailable) {
  57. error_setg(errp, "Capability %s not available", unavailable->str);
  58. g_string_free(unavailable, true);
  59. return false;
  60. }
  61. memcpy(mon->capab, capab, sizeof(capab));
  62. return true;
  63. }
  64. void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
  65. Error **errp)
  66. {
  67. Monitor *cur_mon = monitor_cur();
  68. MonitorQMP *mon;
  69. assert(monitor_is_qmp(cur_mon));
  70. mon = container_of(cur_mon, MonitorQMP, common);
  71. if (mon->commands == &qmp_commands) {
  72. error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
  73. "Capabilities negotiation is already complete, command "
  74. "ignored");
  75. return;
  76. }
  77. if (!qmp_caps_accept(mon, enable, errp)) {
  78. return;
  79. }
  80. mon->commands = &qmp_commands;
  81. }
  82. VersionInfo *qmp_query_version(Error **errp)
  83. {
  84. VersionInfo *info = g_new0(VersionInfo, 1);
  85. info->qemu = g_new0(VersionTriple, 1);
  86. info->qemu->major = QEMU_VERSION_MAJOR;
  87. info->qemu->minor = QEMU_VERSION_MINOR;
  88. info->qemu->micro = QEMU_VERSION_MICRO;
  89. info->package = g_strdup(QEMU_PKGVERSION);
  90. return info;
  91. }
  92. static void query_commands_cb(const QmpCommand *cmd, void *opaque)
  93. {
  94. CommandInfo *info;
  95. CommandInfoList **list = opaque;
  96. if (!cmd->enabled) {
  97. return;
  98. }
  99. info = g_malloc0(sizeof(*info));
  100. info->name = g_strdup(cmd->name);
  101. QAPI_LIST_PREPEND(*list, info);
  102. }
  103. CommandInfoList *qmp_query_commands(Error **errp)
  104. {
  105. CommandInfoList *list = NULL;
  106. Monitor *cur_mon = monitor_cur();
  107. MonitorQMP *mon;
  108. assert(monitor_is_qmp(cur_mon));
  109. mon = container_of(cur_mon, MonitorQMP, common);
  110. qmp_for_each_command(mon->commands, query_commands_cb, &list);
  111. return list;
  112. }
  113. static void *split_off_generic_list(void *list,
  114. bool (*splitp)(void *elt),
  115. void **part)
  116. {
  117. GenericList *keep = NULL, **keep_tailp = &keep;
  118. GenericList *split = NULL, **split_tailp = &split;
  119. GenericList *tail;
  120. for (tail = list; tail; tail = tail->next) {
  121. if (splitp(tail)) {
  122. *split_tailp = tail;
  123. split_tailp = &tail->next;
  124. } else {
  125. *keep_tailp = tail;
  126. keep_tailp = &tail->next;
  127. }
  128. }
  129. *keep_tailp = *split_tailp = NULL;
  130. *part = split;
  131. return keep;
  132. }
  133. static bool is_in(const char *s, strList *list)
  134. {
  135. strList *tail;
  136. for (tail = list; tail; tail = tail->next) {
  137. if (!strcmp(tail->value, s)) {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. static bool is_entity_deprecated(void *link)
  144. {
  145. return is_in("deprecated", ((SchemaInfoList *)link)->value->features);
  146. }
  147. static bool is_member_deprecated(void *link)
  148. {
  149. return is_in("deprecated",
  150. ((SchemaInfoObjectMemberList *)link)->value->features);
  151. }
  152. static SchemaInfoList *zap_deprecated(SchemaInfoList *schema)
  153. {
  154. void *to_zap;
  155. SchemaInfoList *tail;
  156. SchemaInfo *ent;
  157. schema = split_off_generic_list(schema, is_entity_deprecated, &to_zap);
  158. qapi_free_SchemaInfoList(to_zap);
  159. for (tail = schema; tail; tail = tail->next) {
  160. ent = tail->value;
  161. if (ent->meta_type == SCHEMA_META_TYPE_OBJECT) {
  162. ent->u.object.members
  163. = split_off_generic_list(ent->u.object.members,
  164. is_member_deprecated, &to_zap);
  165. qapi_free_SchemaInfoObjectMemberList(to_zap);
  166. }
  167. }
  168. return schema;
  169. }
  170. SchemaInfoList *qmp_query_qmp_schema(Error **errp)
  171. {
  172. QObject *obj = qobject_from_qlit(&qmp_schema_qlit);
  173. Visitor *v = qobject_input_visitor_new(obj);
  174. SchemaInfoList *schema = NULL;
  175. /* test_visitor_in_qmp_introspect() ensures this can't fail */
  176. visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
  177. g_assert(schema);
  178. qobject_unref(obj);
  179. visit_free(v);
  180. if (compat_policy.deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) {
  181. return zap_deprecated(schema);
  182. }
  183. return schema;
  184. }