diff mbox

[11/11] QMP: Enable feature negotiation support

Message ID 1264108180-3666-12-git-send-email-lcapitulino@redhat.com
State New
Headers show

Commit Message

Luiz Capitulino Jan. 21, 2010, 9:09 p.m. UTC
This is done by enforcing the following mode-oriented rules:

- QMP is started in handshake mode
- In handshake mode all protocol capabilities are disabled
  and (apart of a few exceptions) only commands which
  query/enable/disable them are allowed
- Asynchronous messages are now considered a capability
- Clients can change to the operational mode (where capabilities'
  changes take effect and most commands are allowed) at any
  time
- Each QMP Monitor has its own set of capabilities, changes
  made to one of them don't affect the others

Also note that all these changes should have no effect in
the user Monitor.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 monitor.c |   43 ++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 42 insertions(+), 1 deletions(-)
diff mbox

Patch

diff --git a/monitor.c b/monitor.c
index 34df1cc..0ca0941 100644
--- a/monitor.c
+++ b/monitor.c
@@ -410,7 +410,8 @@  void monitor_protocol_event(MonitorEvent event, QObject *data)
 
     QLIST_FOREACH(mon, &mon_list, entry) {
         if (monitor_ctrl_mode(mon) &&
-            qevent_enabled(mon, event)) {
+            qevent_enabled(mon, event) &&
+            mon->mc->mode == QMODE_OPERATIONAL) {
             monitor_json_emitter(mon, QOBJECT(qmp));
         }
     }
@@ -4166,12 +4167,38 @@  static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
     return err;
 }
 
+static int qmp_mode_invalid(const Monitor *mon, unsigned int cmd_flags)
+{
+    switch (mon->mc->mode) {
+        case QMODE_OPERATIONAL:
+            if (cmd_flags & HANDLER_HANDSHAKE_ONLY) {
+                goto mode_error;
+            }
+            break;
+        case QMODE_HANDSHAKE:
+            if (!((cmd_flags & HANDLER_HANDSHAKE) ||
+                (cmd_flags & HANDLER_HANDSHAKE_ONLY))) {
+                goto mode_error;
+            }
+            break;
+        default:
+            abort();
+    }
+
+    return 0;
+
+mode_error:
+    qemu_error_new(QERR_QMP_INVALID_MODE_COMMAND);
+    return 1;
+}
+
 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
 {
     int err;
     QObject *obj;
     QDict *input, *args;
     const mon_cmd_t *cmd;
+    unsigned int cmd_flags;
     Monitor *mon = cur_mon;
     const char *cmd_name, *info_item;
 
@@ -4213,6 +4240,15 @@  static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
         qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
         goto err_input;
     } else if (strstart(cmd_name, "query-", &info_item)) {
+        /* check it exists and get its flags */
+        cmd = monitor_find_info_command(info_item);
+        if (!cmd) {
+            qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
+            goto err_input;
+        }
+        cmd_flags = cmd->flags;
+
+        /* setup 'info' to call it */
         cmd = monitor_find_command("info");
         qdict_put_obj(input, "arguments",
                       qobject_from_jsonf("{ 'item': %s }", info_item));
@@ -4222,6 +4258,11 @@  static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
             qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
             goto err_input;
         }
+        cmd_flags = cmd->flags;
+    }
+
+    if (qmp_mode_invalid(mon, cmd_flags)) {
+        goto err_input;
     }
 
     obj = qdict_get(input, "arguments");