qmp-dispatch.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "block/aio.h"
  15. #include "qapi/error.h"
  16. #include "qapi/qmp/dispatch.h"
  17. #include "qapi/qmp/qdict.h"
  18. #include "qapi/qmp/qjson.h"
  19. #include "sysemu/runstate.h"
  20. #include "qapi/qmp/qbool.h"
  21. #include "qemu/coroutine.h"
  22. #include "qemu/main-loop.h"
  23. static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob,
  24. Error **errp)
  25. {
  26. const char *exec_key = NULL;
  27. const QDictEntry *ent;
  28. const char *arg_name;
  29. const QObject *arg_obj;
  30. for (ent = qdict_first(dict); ent;
  31. ent = qdict_next(dict, ent)) {
  32. arg_name = qdict_entry_key(ent);
  33. arg_obj = qdict_entry_value(ent);
  34. if (!strcmp(arg_name, "execute")
  35. || (!strcmp(arg_name, "exec-oob") && allow_oob)) {
  36. if (qobject_type(arg_obj) != QTYPE_QSTRING) {
  37. error_setg(errp, "QMP input member '%s' must be a string",
  38. arg_name);
  39. return NULL;
  40. }
  41. if (exec_key) {
  42. error_setg(errp, "QMP input member '%s' clashes with '%s'",
  43. arg_name, exec_key);
  44. return NULL;
  45. }
  46. exec_key = arg_name;
  47. } else if (!strcmp(arg_name, "arguments")) {
  48. if (qobject_type(arg_obj) != QTYPE_QDICT) {
  49. error_setg(errp,
  50. "QMP input member 'arguments' must be an object");
  51. return NULL;
  52. }
  53. } else if (!strcmp(arg_name, "id")) {
  54. continue;
  55. } else {
  56. error_setg(errp, "QMP input member '%s' is unexpected",
  57. arg_name);
  58. return NULL;
  59. }
  60. }
  61. if (!exec_key) {
  62. error_setg(errp, "QMP input lacks member 'execute'");
  63. return NULL;
  64. }
  65. return dict;
  66. }
  67. QDict *qmp_error_response(Error *err)
  68. {
  69. QDict *rsp;
  70. rsp = qdict_from_jsonf_nofail("{ 'error': { 'class': %s, 'desc': %s } }",
  71. QapiErrorClass_str(error_get_class(err)),
  72. error_get_pretty(err));
  73. error_free(err);
  74. return rsp;
  75. }
  76. /*
  77. * Does @qdict look like a command to be run out-of-band?
  78. */
  79. bool qmp_is_oob(const QDict *dict)
  80. {
  81. return qdict_haskey(dict, "exec-oob")
  82. && !qdict_haskey(dict, "execute");
  83. }
  84. typedef struct QmpDispatchBH {
  85. const QmpCommand *cmd;
  86. Monitor *cur_mon;
  87. QDict *args;
  88. QObject **ret;
  89. Error **errp;
  90. Coroutine *co;
  91. } QmpDispatchBH;
  92. static void do_qmp_dispatch_bh(void *opaque)
  93. {
  94. QmpDispatchBH *data = opaque;
  95. assert(monitor_cur() == NULL);
  96. monitor_set_cur(qemu_coroutine_self(), data->cur_mon);
  97. data->cmd->fn(data->args, data->ret, data->errp);
  98. monitor_set_cur(qemu_coroutine_self(), NULL);
  99. aio_co_wake(data->co);
  100. }
  101. /*
  102. * Runs outside of coroutine context for OOB commands, but in coroutine
  103. * context for everything else.
  104. */
  105. QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request,
  106. bool allow_oob, Monitor *cur_mon)
  107. {
  108. Error *err = NULL;
  109. bool oob;
  110. const char *command;
  111. QDict *args;
  112. const QmpCommand *cmd;
  113. QDict *dict;
  114. QObject *id;
  115. QObject *ret = NULL;
  116. QDict *rsp = NULL;
  117. dict = qobject_to(QDict, request);
  118. if (!dict) {
  119. id = NULL;
  120. error_setg(&err, "QMP input must be a JSON object");
  121. goto out;
  122. }
  123. id = qdict_get(dict, "id");
  124. if (!qmp_dispatch_check_obj(dict, allow_oob, &err)) {
  125. goto out;
  126. }
  127. command = qdict_get_try_str(dict, "execute");
  128. oob = false;
  129. if (!command) {
  130. assert(allow_oob);
  131. command = qdict_get_str(dict, "exec-oob");
  132. oob = true;
  133. }
  134. cmd = qmp_find_command(cmds, command);
  135. if (cmd == NULL) {
  136. error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
  137. "The command %s has not been found", command);
  138. goto out;
  139. }
  140. if (!cmd->enabled) {
  141. error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND,
  142. "The command %s has been disabled for this instance",
  143. command);
  144. goto out;
  145. }
  146. if (oob && !(cmd->options & QCO_ALLOW_OOB)) {
  147. error_setg(&err, "The command %s does not support OOB",
  148. command);
  149. goto out;
  150. }
  151. if (runstate_check(RUN_STATE_PRECONFIG) &&
  152. !(cmd->options & QCO_ALLOW_PRECONFIG)) {
  153. error_setg(&err, "The command '%s' isn't permitted in '%s' state",
  154. cmd->name, RunState_str(RUN_STATE_PRECONFIG));
  155. goto out;
  156. }
  157. if (!qdict_haskey(dict, "arguments")) {
  158. args = qdict_new();
  159. } else {
  160. args = qdict_get_qdict(dict, "arguments");
  161. qobject_ref(args);
  162. }
  163. assert(!(oob && qemu_in_coroutine()));
  164. assert(monitor_cur() == NULL);
  165. if (!!(cmd->options & QCO_COROUTINE) == qemu_in_coroutine()) {
  166. monitor_set_cur(qemu_coroutine_self(), cur_mon);
  167. cmd->fn(args, &ret, &err);
  168. monitor_set_cur(qemu_coroutine_self(), NULL);
  169. } else {
  170. /*
  171. * Actual context doesn't match the one the command needs.
  172. *
  173. * Case 1: we are in coroutine context, but command does not
  174. * have QCO_COROUTINE. We need to drop out of coroutine
  175. * context for executing it.
  176. *
  177. * Case 2: we are outside coroutine context, but command has
  178. * QCO_COROUTINE. Can't actually happen, because we get here
  179. * outside coroutine context only when executing a command
  180. * out of band, and OOB commands never have QCO_COROUTINE.
  181. */
  182. assert(!oob && qemu_in_coroutine() && !(cmd->options & QCO_COROUTINE));
  183. QmpDispatchBH data = {
  184. .cur_mon = cur_mon,
  185. .cmd = cmd,
  186. .args = args,
  187. .ret = &ret,
  188. .errp = &err,
  189. .co = qemu_coroutine_self(),
  190. };
  191. aio_bh_schedule_oneshot(qemu_get_aio_context(), do_qmp_dispatch_bh,
  192. &data);
  193. qemu_coroutine_yield();
  194. }
  195. qobject_unref(args);
  196. if (err) {
  197. /* or assert(!ret) after reviewing all handlers: */
  198. qobject_unref(ret);
  199. goto out;
  200. }
  201. if (cmd->options & QCO_NO_SUCCESS_RESP) {
  202. g_assert(!ret);
  203. return NULL;
  204. } else if (!ret) {
  205. /*
  206. * When the command's schema has no 'returns', cmd->fn()
  207. * leaves @ret null. The QMP spec calls for an empty object
  208. * then; supply it.
  209. */
  210. ret = QOBJECT(qdict_new());
  211. }
  212. rsp = qdict_new();
  213. qdict_put_obj(rsp, "return", ret);
  214. out:
  215. if (err) {
  216. assert(!rsp);
  217. rsp = qmp_error_response(err);
  218. }
  219. assert(rsp);
  220. if (id) {
  221. qdict_put_obj(rsp, "id", qobject_ref(id));
  222. }
  223. return rsp;
  224. }