aboutsummaryrefslogtreecommitdiff
path: root/monitor.c
diff options
context:
space:
mode:
authorPeter Xu <peterx@redhat.com>2018-03-09 16:59:54 +0800
committerEric Blake <eblake@redhat.com>2018-03-19 14:58:37 -0500
commit546aa56674305a4ab45bea4f36956651fd892b04 (patch)
tree37d5d5871e5b610b119e7dbcf946785e5d0efdfb /monitor.c
parent02130314d8c71743e6d1fefc2b08a608516bccc7 (diff)
monitor: introduce monitor_qmp_respond()
A tiny refactoring, preparing to split the QMP dispatcher away. Reviewed-by: Fam Zheng <famz@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Peter Xu <peterx@redhat.com> Message-Id: <20180309090006.10018-12-peterx@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> [eblake: rebase to qobject_to() usage] Signed-off-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'monitor.c')
-rw-r--r--monitor.c50
1 files changed, 33 insertions, 17 deletions
diff --git a/monitor.c b/monitor.c
index d31ec703e3..ec209c9141 100644
--- a/monitor.c
+++ b/monitor.c
@@ -3822,6 +3822,38 @@ static int monitor_can_read(void *opaque)
return (mon->suspend_cnt == 0) ? 1 : 0;
}
+/*
+ * 1. This function takes ownership of rsp, err, and id.
+ * 2. rsp, err, and id may be NULL.
+ * 3. If err != NULL then rsp must be NULL.
+ */
+static void monitor_qmp_respond(Monitor *mon, QObject *rsp,
+ Error *err, QObject *id)
+{
+ QDict *qdict = NULL;
+
+ if (err) {
+ assert(!rsp);
+ qdict = qdict_new();
+ qdict_put_obj(qdict, "error", qmp_build_error_object(err));
+ error_free(err);
+ rsp = QOBJECT(qdict);
+ }
+
+ if (rsp) {
+ if (id) {
+ /* This is for the qdict below. */
+ qobject_incref(id);
+ qdict_put_obj(qobject_to(QDict, rsp), "id", id);
+ }
+
+ monitor_json_emitter(mon, rsp);
+ }
+
+ qobject_decref(id);
+ qobject_decref(rsp);
+}
+
static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
{
QObject *req, *rsp = NULL, *id = NULL;
@@ -3873,24 +3905,8 @@ static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
}
err_out:
- if (err) {
- qdict = qdict_new();
- qdict_put_obj(qdict, "error", qmp_build_error_object(err));
- error_free(err);
- rsp = QOBJECT(qdict);
- }
+ monitor_qmp_respond(mon, rsp, err, id);
- if (rsp) {
- if (id) {
- qdict_put_obj(qobject_to(QDict, rsp), "id", id);
- id = NULL;
- }
-
- monitor_json_emitter(mon, rsp);
- }
-
- qobject_decref(id);
- qobject_decref(rsp);
qobject_decref(req);
}