From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56049) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eeDoi-0001yZ-Qc for qemu-devel@nongnu.org; Wed, 24 Jan 2018 00:41:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eeDof-00087E-ND for qemu-devel@nongnu.org; Wed, 24 Jan 2018 00:41:48 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55810) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eeDof-00086N-HZ for qemu-devel@nongnu.org; Wed, 24 Jan 2018 00:41:45 -0500 From: Peter Xu Date: Wed, 24 Jan 2018 13:39:45 +0800 Message-Id: <20180124053957.29145-12-peterx@redhat.com> In-Reply-To: <20180124053957.29145-1-peterx@redhat.com> References: <20180124053957.29145-1-peterx@redhat.com> Subject: [Qemu-devel] [PATCH v7 11/23] monitor: introduce monitor_qmp_respond() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi , "Daniel P . Berrange" , Paolo Bonzini , Fam Zheng , Juan Quintela , mdroth@linux.vnet.ibm.com, peterx@redhat.com, Eric Blake , Laurent Vivier , Markus Armbruster , marcandre.lureau@redhat.com, "Dr . David Alan Gilbert" A tiny refactoring, preparing to split the QMP dispatcher away. Reviewed-by: Fam Zheng Reviewed-by: Stefan Hajnoczi Signed-off-by: Peter Xu --- monitor.c | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/monitor.c b/monitor.c index 3e07798442..92114aaa80 100644 --- a/monitor.c +++ b/monitor.c @@ -3867,6 +3867,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; @@ -3918,24 +3950,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); } -- 2.14.3