qmp-registry.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * Michael Roth <mdroth@us.ibm.com>
  9. *
  10. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  11. * See the COPYING.LIB file in the top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qapi/qmp-registry.h"
  16. void qmp_register_command(QmpCommandList *cmds, const char *name,
  17. QmpCommandFunc *fn, QmpCommandOptions options,
  18. uint64_t features)
  19. {
  20. QmpCommand *cmd = g_malloc0(sizeof(*cmd));
  21. /* QCO_COROUTINE and QCO_ALLOW_OOB are incompatible for now */
  22. assert(!((options & QCO_COROUTINE) && (options & QCO_ALLOW_OOB)));
  23. cmd->name = name;
  24. cmd->fn = fn;
  25. cmd->enabled = true;
  26. cmd->options = options;
  27. cmd->features = features;
  28. QTAILQ_INSERT_TAIL(cmds, cmd, node);
  29. }
  30. const QmpCommand *qmp_find_command(const QmpCommandList *cmds, const char *name)
  31. {
  32. QmpCommand *cmd;
  33. QTAILQ_FOREACH(cmd, cmds, node) {
  34. if (strcmp(cmd->name, name) == 0) {
  35. return cmd;
  36. }
  37. }
  38. return NULL;
  39. }
  40. static void qmp_toggle_command(QmpCommandList *cmds, const char *name,
  41. bool enabled, const char *disable_reason)
  42. {
  43. QmpCommand *cmd;
  44. QTAILQ_FOREACH(cmd, cmds, node) {
  45. if (strcmp(cmd->name, name) == 0) {
  46. cmd->enabled = enabled;
  47. cmd->disable_reason = disable_reason;
  48. return;
  49. }
  50. }
  51. }
  52. void qmp_disable_command(QmpCommandList *cmds, const char *name,
  53. const char *disable_reason)
  54. {
  55. qmp_toggle_command(cmds, name, false, disable_reason);
  56. }
  57. void qmp_enable_command(QmpCommandList *cmds, const char *name)
  58. {
  59. qmp_toggle_command(cmds, name, true, NULL);
  60. }
  61. bool qmp_command_is_enabled(const QmpCommand *cmd)
  62. {
  63. return cmd->enabled;
  64. }
  65. const char *qmp_command_name(const QmpCommand *cmd)
  66. {
  67. return cmd->name;
  68. }
  69. bool qmp_has_success_response(const QmpCommand *cmd)
  70. {
  71. return !(cmd->options & QCO_NO_SUCCESS_RESP);
  72. }
  73. void qmp_for_each_command(const QmpCommandList *cmds, qmp_cmd_callback_fn fn,
  74. void *opaque)
  75. {
  76. const QmpCommand *cmd;
  77. QTAILQ_FOREACH(cmd, cmds, node) {
  78. fn(cmd, opaque);
  79. }
  80. }