From nobody Mon Feb 9 21:22:12 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1488304543185216.95294545751437; Tue, 28 Feb 2017 09:55:43 -0800 (PST) Received: from localhost ([::1]:35991 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cilzy-00004x-1p for importer@patchew.org; Tue, 28 Feb 2017 12:55:42 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50015) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cilkH-0003fG-HA for qemu-devel@nongnu.org; Tue, 28 Feb 2017 12:39:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cilkD-0005R2-Ds for qemu-devel@nongnu.org; Tue, 28 Feb 2017 12:39:29 -0500 Received: from mx1.redhat.com ([209.132.183.28]:57050) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cilkC-0005Q5-Ua for qemu-devel@nongnu.org; Tue, 28 Feb 2017 12:39:25 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1C65AC01C787 for ; Tue, 28 Feb 2017 17:39:25 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-55.ams2.redhat.com [10.36.116.55]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1SHdNjN023043 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 28 Feb 2017 12:39:24 -0500 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 2695111386CE; Tue, 28 Feb 2017 18:39:20 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 28 Feb 2017 18:38:59 +0100 Message-Id: <1488303560-18803-6-git-send-email-armbru@redhat.com> In-Reply-To: <1488303560-18803-1-git-send-email-armbru@redhat.com> References: <1488303560-18803-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 28 Feb 2017 17:39:25 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 05/26] qmp: Clean up how we enforce capability negotiation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" To enforce capability negotiation before normal operation, handle_qmp_command() inspects every command before it's handed off to qmp_dispatch(). This is a bit of a layering violation, and results in duplicated code. Before capability negotiation (!cur_mon->in_command_mode), we fail commands other than "qmp_capabilities". This is what enforces capability negotiation. Afterwards, we fail command "qmp_capabilities". Clean this up as follows. The obvious place to fail a command is the command itself, so move the "afterwards" check to qmp_qmp_capabilities(). We do the "before" check in every other command, but that would be bothersome. Instead, start without all the other commands, by registering only qmp_qmp_capabilities(). Register the others in qmp_qmp_capabilities(). Additionally, replace the generic human-readable error message for CommandNotFound by one that reminds the user to run qmp_capabilities. Without that, we'd regress commit 2d5a834. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- monitor.c | 70 ++++++++++++++++++++++++++++++++++++-----------------------= ---- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/monitor.c b/monitor.c index 1cc2274..e522b6f 100644 --- a/monitor.c +++ b/monitor.c @@ -563,11 +563,6 @@ static void monitor_qapi_event_init(void) qmp_event_set_func_emit(monitor_qapi_event_queue); } =20 -void qmp_qmp_capabilities(Error **errp) -{ - cur_mon->qmp.in_command_mode =3D true; -} - static void handle_hmp_command(Monitor *mon, const char *cmdline); =20 static void monitor_data_init(Monitor *mon) @@ -995,7 +990,7 @@ static void qmp_unregister_commands_hack(void) #endif } =20 -void monitor_init_qmp_commands(void) +static void monitor_init_real_qmp_commands(void) { qmp_init_marshal(); =20 @@ -1009,6 +1004,32 @@ void monitor_init_qmp_commands(void) qmp_unregister_commands_hack(); } =20 +void monitor_init_qmp_commands(void) +{ + /* + * Start with just qmp_capabilities, to enforce capability + * negotiation. qmp_capabilities will register the other + * commands. See also the error message replacement hack in + * handle_qmp_command(). + */ + qmp_register_command("qmp_capabilities", + qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS); +} + +void qmp_qmp_capabilities(Error **errp) +{ + if (cur_mon->qmp.in_command_mode) { + error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, + "Capabilities negotiation is already complete, command " + "ignored"); + return; + } + + cur_mon->qmp.in_command_mode =3D true; + qmp_unregister_command("qmp_capabilities"); + monitor_init_real_qmp_commands(); +} + /* set the current CPU defined by the user */ int monitor_set_cpu(int cpu_index) { @@ -3676,26 +3697,6 @@ static int monitor_can_read(void *opaque) return (mon->suspend_cnt =3D=3D 0) ? 1 : 0; } =20 -static bool invalid_qmp_mode(const Monitor *mon, const char *cmd, - Error **errp) -{ - bool is_cap =3D g_str_equal(cmd, "qmp_capabilities"); - - if (is_cap && mon->qmp.in_command_mode) { - error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, - "Capabilities negotiation is already complete, command " - "'%s' ignored", cmd); - return true; - } - if (!is_cap && !mon->qmp.in_command_mode) { - error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, - "Expecting capabilities negotiation with " - "'qmp_capabilities' before command '%s'", cmd); - return true; - } - return false; -} - /* * Input object checking rules * @@ -3781,12 +3782,21 @@ static void handle_qmp_command(JSONMessageParser *p= arser, GQueue *tokens) cmd_name =3D qdict_get_str(qdict, "execute"); trace_handle_qmp_command(mon, cmd_name); =20 - if (invalid_qmp_mode(mon, cmd_name, &err)) { - goto err_out; - } - rsp =3D qmp_dispatch(req); =20 + qdict =3D qdict_get_qdict(qobject_to_qdict(rsp), "error"); + if (qdict) { + if (!mon->qmp.in_command_mode + && !g_strcmp0(qdict_get_try_str(qdict, "class"), + QapiErrorClass_lookup[ERROR_CLASS_COMMAND_NOT_FOUND]))= { + /* Provide a more useful error message */ + qdict_del(qdict, "desc"); + qdict_put(qdict, "desc", + qstring_from_str("Expecting capabilities negotiation" + " with 'qmp_capabilities'")); + } + } + err_out: if (err) { qdict =3D qdict_new(); --=20 2.7.4