blob: e8053686f3f3ae7fa4ef5173075a574b765a48e5 [file] [log] [blame]
Michael Roth43c20a42011-07-19 14:50:36 -05001/*
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
Peter Maydellcbf21152016-01-29 17:49:57 +000015#include "qemu/osdep.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010016#include "qapi/qmp/dispatch.h"
Michael Roth43c20a42011-07-19 14:50:36 -050017
Michael Rothabd6cf62011-12-06 22:03:42 -060018static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
Michael Roth43c20a42011-07-19 14:50:36 -050019 QTAILQ_HEAD_INITIALIZER(qmp_commands);
20
Luiz Capitulinod34b8672012-05-08 14:24:44 -030021void qmp_register_command(const char *name, QmpCommandFunc *fn,
22 QmpCommandOptions options)
Michael Roth43c20a42011-07-19 14:50:36 -050023{
Anthony Liguori7267c092011-08-20 22:09:37 -050024 QmpCommand *cmd = g_malloc0(sizeof(*cmd));
Michael Roth43c20a42011-07-19 14:50:36 -050025
26 cmd->name = name;
Michael Roth43c20a42011-07-19 14:50:36 -050027 cmd->fn = fn;
Michael Rothabd6cf62011-12-06 22:03:42 -060028 cmd->enabled = true;
Luiz Capitulinod34b8672012-05-08 14:24:44 -030029 cmd->options = options;
Michael Roth43c20a42011-07-19 14:50:36 -050030 QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
31}
32
Markus Armbruster60b03e42016-09-12 13:19:01 +040033void qmp_unregister_command(const char *name)
34{
35 QmpCommand *cmd = qmp_find_command(name);
36
37 QTAILQ_REMOVE(&qmp_commands, cmd, node);
38 g_free(cmd);
39}
40
Michael Roth43c20a42011-07-19 14:50:36 -050041QmpCommand *qmp_find_command(const char *name)
42{
Michael Rothabd6cf62011-12-06 22:03:42 -060043 QmpCommand *cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050044
Michael Rothabd6cf62011-12-06 22:03:42 -060045 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
46 if (strcmp(cmd->name, name) == 0) {
47 return cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050048 }
49 }
50 return NULL;
51}
Michael Rothabd6cf62011-12-06 22:03:42 -060052
Michael Rothf22d85e2012-04-17 19:01:45 -050053static void qmp_toggle_command(const char *name, bool enabled)
Michael Rothabd6cf62011-12-06 22:03:42 -060054{
55 QmpCommand *cmd;
56
57 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
58 if (strcmp(cmd->name, name) == 0) {
Michael Rothf22d85e2012-04-17 19:01:45 -050059 cmd->enabled = enabled;
Michael Rothabd6cf62011-12-06 22:03:42 -060060 return;
61 }
62 }
63}
64
Michael Rothf22d85e2012-04-17 19:01:45 -050065void qmp_disable_command(const char *name)
66{
67 qmp_toggle_command(name, false);
68}
69
70void qmp_enable_command(const char *name)
71{
72 qmp_toggle_command(name, true);
73}
74
Mark Wu8dc4d912013-10-09 11:25:07 +080075bool qmp_command_is_enabled(const QmpCommand *cmd)
Michael Rothbf95c0d2011-12-06 22:03:43 -060076{
Mark Wu8dc4d912013-10-09 11:25:07 +080077 return cmd->enabled;
Michael Rothbf95c0d2011-12-06 22:03:43 -060078}
79
Mark Wu8dc4d912013-10-09 11:25:07 +080080const char *qmp_command_name(const QmpCommand *cmd)
81{
82 return cmd->name;
83}
84
Mark Wu0106dc42013-10-09 10:37:26 +080085bool qmp_has_success_response(const QmpCommand *cmd)
86{
87 return !(cmd->options & QCO_NO_SUCCESS_RESP);
88}
89
Mark Wu8dc4d912013-10-09 11:25:07 +080090void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque)
Michael Rothabd6cf62011-12-06 22:03:42 -060091{
92 QmpCommand *cmd;
Michael Rothabd6cf62011-12-06 22:03:42 -060093
94 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
Mark Wu8dc4d912013-10-09 11:25:07 +080095 fn(cmd, opaque);
Michael Rothabd6cf62011-12-06 22:03:42 -060096 }
Michael Rothabd6cf62011-12-06 22:03:42 -060097}