qmp-registry.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <glib.h>
  15. #include <string.h>
  16. #include "qapi/qmp/dispatch.h"
  17. static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
  18. QTAILQ_HEAD_INITIALIZER(qmp_commands);
  19. void qmp_register_command(const char *name, QmpCommandFunc *fn,
  20. QmpCommandOptions options)
  21. {
  22. QmpCommand *cmd = g_malloc0(sizeof(*cmd));
  23. cmd->name = name;
  24. cmd->type = QCT_NORMAL;
  25. cmd->fn = fn;
  26. cmd->enabled = true;
  27. cmd->options = options;
  28. QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
  29. }
  30. QmpCommand *qmp_find_command(const char *name)
  31. {
  32. QmpCommand *cmd;
  33. QTAILQ_FOREACH(cmd, &qmp_commands, node) {
  34. if (strcmp(cmd->name, name) == 0) {
  35. return cmd;
  36. }
  37. }
  38. return NULL;
  39. }
  40. static void qmp_toggle_command(const char *name, bool enabled)
  41. {
  42. QmpCommand *cmd;
  43. QTAILQ_FOREACH(cmd, &qmp_commands, node) {
  44. if (strcmp(cmd->name, name) == 0) {
  45. cmd->enabled = enabled;
  46. return;
  47. }
  48. }
  49. }
  50. void qmp_disable_command(const char *name)
  51. {
  52. qmp_toggle_command(name, false);
  53. }
  54. void qmp_enable_command(const char *name)
  55. {
  56. qmp_toggle_command(name, true);
  57. }
  58. bool qmp_command_is_enabled(const QmpCommand *cmd)
  59. {
  60. return cmd->enabled;
  61. }
  62. const char *qmp_command_name(const QmpCommand *cmd)
  63. {
  64. return cmd->name;
  65. }
  66. bool qmp_has_success_response(const QmpCommand *cmd)
  67. {
  68. return !(cmd->options & QCO_NO_SUCCESS_RESP);
  69. }
  70. void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque)
  71. {
  72. QmpCommand *cmd;
  73. QTAILQ_FOREACH(cmd, &qmp_commands, node) {
  74. fn(cmd, opaque);
  75. }
  76. }