2
0

qmp-dispatch.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Core Definitions for QAPI/QMP Dispatch
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "qapi/qmp/dispatch.h"
  16. #include "qapi/qmp/qdict.h"
  17. #include "qapi/qmp/qjson.h"
  18. #include "sysemu/runstate.h"
  19. #include "qapi/qmp/qbool.h"
  20. static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob,
  21. Error **errp)
  22. {
  23. const char *exec_key = NULL;
  24. const QDictEntry *ent;
  25. const char *arg_name;
  26. const QObject *arg_obj;
  27. for (ent = qdict_first(dict); ent;
  28. ent = qdict_next(dict, ent)) {
  29. arg_name = qdict_entry_key(ent);
  30. arg_obj = qdict_entry_value(ent);
  31. if (!strcmp(arg_name, "execute")
  32. || (!strcmp(arg_name, "exec-oob") && allow_oob)) {
  33. if (qobject_type(arg_obj) != QTYPE_QSTRING) {
  34. error_setg(errp, "QMP input member '%s' must be a string",
  35. arg_name);
  36. return NULL;
  37. }
  38. if (exec_key) {
  39. error_setg(errp, "QMP input member '%s' clashes with '%s'",
  40. arg_name, exec_key);
  41. return NULL;
  42. }
  43. exec_key = arg_name;
  44. } else if (!strcmp(arg_name, "arguments")) {
  45. if (qobject_type(arg_obj) != QTYPE_QDICT) {
  46. error_setg(errp,
  47. "QMP input member 'arguments' must be an object");
  48. return NULL;
  49. }
  50. } else if (!strcmp(arg_name, "id")) {
  51. continue;
  52. } else {
  53. error_setg(errp, "QMP input member '%s' is unexpected",
  54. arg_name);
  55. return NULL;
  56. }
  57. }
  58. if (!exec_key) {
  59. error_setg(errp, "QMP input lacks member 'execute'");
  60. return NULL;
  61. }
  62. return dict;
  63. }
  64. QDict *qmp_error_response(Error *err)
  65. {
  66. QDict *rsp;
  67. rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
  68. QapiErrorClass_str(error_get_class(err)),
  69. error_get_pretty(err));
  70. error_free(err);
  71. return rsp;
  72. }
  73. /*
  74. * Does @qdict look like a command to be run out-of-band?
  75. */
  76. bool qmp_is_oob(const QDict *dict)
  77. {
  78. return qdict_haskey(dict, "exec-oob")
  79. && !qdict_haskey(dict, "execute");
  80. }
  81. QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request,
  82. bool allow_oob)
  83. {
  84. Error *err = NULL;
  85. bool oob;
  86. const char *command;
  87. QDict *args;
  88. const QmpCommand *cmd;
  89. QDict *dict;
  90. QObject *id;
  91. QObject *ret = NULL;
  92. QDict *rsp = NULL;
  93. dict = qobject_to(QDict, request);
  94. if (!dict) {
  95. id = NULL;
  96. error_setg(&err, "QMP input must be a JSON object");
  97. goto out;
  98. }
  99. id = qdict_get(dict, "id");
  100. if (!qmp_dispatch_check_obj(dict, allow_oob, &err)) {
  101. goto out;
  102. }
  103. command = qdict_get_try_str(dict, "execute");
  104. oob = false;
  105. if (!command) {
  106. assert(allow_oob);
  107. command = qdict_get_str(dict, "exec-oob");
  108. oob = true;
  109. }
  110. cmd = qmp_find_command(cmds, command);
  111. if (cmd == NULL) {
  112. error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
  113. "The command %s has not been found", command);
  114. goto out;
  115. }
  116. if (!cmd->enabled) {
  117. error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
  118. "The command %s has been disabled for this instance",
  119. command);
  120. goto out;
  121. }
  122. if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
  123. error_setg(&err, "The command %s does not support OOB",
  124. command);
  125. goto out;
  126. }
  127. if (runstate_check(RUN_STATE_PRECONFIG) &&
  128. !(cmd->options & QCO_ALLOW_PRECONFIG)) {
  129. error_setg(&err, "The command '%s' isn't permitted in '%s' state",
  130. cmd->name, RunState_str(RUN_STATE_PRECONFIG));
  131. goto out;
  132. }
  133. if (!qdict_haskey(dict, "arguments")) {
  134. args = qdict_new();
  135. } else {
  136. args = qdict_get_qdict(dict, "arguments");
  137. qobject_ref(args);
  138. }
  139. cmd->fn(args, &ret, &err);
  140. qobject_unref(args);
  141. if (err) {
  142. /* or assert(!ret) after reviewing all handlers: */
  143. qobject_unref(ret);
  144. goto out;
  145. }
  146. if (cmd->options & QCO_NO_SUCCESS_RESP) {
  147. g_assert(!ret);
  148. return NULL;
  149. } else if (!ret) {
  150. /*
  151. * When the command's schema has no 'returns', cmd->fn()
  152. * leaves @ret null. The QMP spec calls for an empty object
  153. * then; supply it.
  154. */
  155. ret = QOBJECT(qdict_new());
  156. }
  157. rsp = qdict_new();
  158. qdict_put_obj(rsp, "return", ret);
  159. out:
  160. if (err) {
  161. assert(!rsp);
  162. rsp = qmp_error_response(err);
  163. }
  164. assert(rsp);
  165. if (id) {
  166. qdict_put_obj(rsp, "id", qobject_ref(id));
  167. }
  168. return rsp;
  169. }