2
0

qmp-cmds-control.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. static void *split_off_generic_list(void *list,
  115. bool (*splitp)(void *elt),
  116. void **part)
  117. {
  118. GenericList *keep = NULL, **keep_tailp = &keep;
  119. GenericList *split = NULL, **split_tailp = &split;
  120. GenericList *tail;
  121. for (tail = list; tail; tail = tail->next) {
  122. if (splitp(tail)) {
  123. *split_tailp = tail;
  124. split_tailp = &tail->next;
  125. } else {
  126. *keep_tailp = tail;
  127. keep_tailp = &tail->next;
  128. }
  129. }
  130. *keep_tailp = *split_tailp = NULL;
  131. *part = split;
  132. return keep;
  133. }
  134. static bool is_in(const char *s, strList *list)
  135. {
  136. strList *tail;
  137. for (tail = list; tail; tail = tail->next) {
  138. if (!strcmp(tail->value, s)) {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. static bool is_entity_deprecated(void *link)
  145. {
  146. return is_in("deprecated", ((SchemaInfoList *)link)->value->features);
  147. }
  148. static bool is_member_deprecated(void *link)
  149. {
  150. return is_in("deprecated",
  151. ((SchemaInfoObjectMemberList *)link)->value->features);
  152. }
  153. static SchemaInfoList *zap_deprecated(SchemaInfoList *schema)
  154. {
  155. void *to_zap;
  156. SchemaInfoList *tail;
  157. SchemaInfo *ent;
  158. schema = split_off_generic_list(schema, is_entity_deprecated, &to_zap);
  159. qapi_free_SchemaInfoList(to_zap);
  160. for (tail = schema; tail; tail = tail->next) {
  161. ent = tail->value;
  162. if (ent->meta_type == SCHEMA_META_TYPE_OBJECT) {
  163. ent->u.object.members
  164. = split_off_generic_list(ent->u.object.members,
  165. is_member_deprecated, &to_zap);
  166. qapi_free_SchemaInfoObjectMemberList(to_zap);
  167. }
  168. }
  169. return schema;
  170. }
  171. SchemaInfoList *qmp_query_qmp_schema(Error **errp)
  172. {
  173. QObject *obj = qobject_from_qlit(&qmp_schema_qlit);
  174. Visitor *v = qobject_input_visitor_new(obj);
  175. SchemaInfoList *schema = NULL;
  176. /* test_visitor_in_qmp_introspect() ensures this can't fail */
  177. visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
  178. g_assert(schema);
  179. qobject_unref(obj);
  180. visit_free(v);
  181. if (compat_policy.deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) {
  182. return zap_deprecated(schema);
  183. }
  184. return schema;
  185. }