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