From patchwork Tue Feb 28 17:38:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 9596413 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 34C5C60453 for ; Tue, 28 Feb 2017 17:39:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2BA6F27FA6 for ; Tue, 28 Feb 2017 17:39:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2093928236; Tue, 28 Feb 2017 17:39:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 80C7F28159 for ; Tue, 28 Feb 2017 17:39:52 +0000 (UTC) Received: from localhost ([::1]:35909 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cilkd-0003gk-G9 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 28 Feb 2017 12:39:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49899) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cilkE-0003cz-Jm for qemu-devel@nongnu.org; Tue, 28 Feb 2017 12:39:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cilkB-0005Pa-Fq for qemu-devel@nongnu.org; Tue, 28 Feb 2017 12:39:26 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49648) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cilkB-0005P0-7Z for qemu-devel@nongnu.org; Tue, 28 Feb 2017 12:39:23 -0500 Received: from smtp.corp.redhat.com (int-mx16.intmail.prod.int.phx2.redhat.com [10.5.11.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 447961555E for ; Tue, 28 Feb 2017 17:39:23 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-55.ams2.redhat.com [10.36.116.55]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D0CC530660; Tue, 28 Feb 2017 17:39:22 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 23C2C1138657; Tue, 28 Feb 2017 18:39:20 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 28 Feb 2017 18:38:58 +0100 Message-Id: <1488303560-18803-5-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.74 on 10.5.11.28 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 28 Feb 2017 17:39:23 +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 04/26] qmp: Dumb down how we run QMP command registration 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The way we get QMP commands registered is high tech: * qapi-commands.py generates qmp_init_marshal() that does the actual work * it also generates the magic to register it as a MODULE_INIT_QAPI function, so it runs when someone calls module_call_init(MODULE_INIT_QAPI) * main() calls module_call_init() QEMU needs to register a few non-qapified commands. Same high tech works: monitor.c has its own qmp_init_marshal() along with the magic to make it run in module_call_init(MODULE_INIT_QAPI). QEMU also needs to unregister commands that are not wanted in this build's configuration (commit 5032a16). Simple enough: qmp_unregister_commands_hack(). The difficulty is to make it run after the generated qmp_init_marshal(). We can't simply run it in monitor.c's qmp_init_marshal(), because the order in which the registered functions run is indeterminate. So qmp_init_marshal() registers qmp_unregister_commands_hack() separately. Since registering *appends* to the list of registered functions, this will make it run after all the functions that have been registered already. I suspect it takes a long and expensive computer science education to not find this silly. Dumb it down as follows: * Drop MODULE_INIT_QAPI entirely * Give the generated qmp_init_marshal() external linkage. * Call it instead of module_call_init(MODULE_INIT_QAPI) * Except in QEMU proper, call new monitor_init_qmp_commands() that in turn calls the generated qmp_init_marshal(), registers the additional commands and unregisters the unwanted ones. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- include/monitor/monitor.h | 1 + include/qemu/module.h | 2 -- monitor.c | 9 ++++----- qga/main.c | 2 +- scripts/qapi-commands.py | 5 ++--- tests/test-qmp-commands.c | 2 +- vl.c | 2 +- 7 files changed, 10 insertions(+), 13 deletions(-) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index e64b944..d2b3aaf 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -16,6 +16,7 @@ extern Monitor *cur_mon; bool monitor_cur_is_qmp(void); +void monitor_init_qmp_commands(void); void monitor_init(Chardev *chr, int flags); void monitor_cleanup(void); diff --git a/include/qemu/module.h b/include/qemu/module.h index 877cca7..56dd218 100644 --- a/include/qemu/module.h +++ b/include/qemu/module.h @@ -42,7 +42,6 @@ static void __attribute__((constructor)) do_qemu_init_ ## function(void) \ typedef enum { MODULE_INIT_BLOCK, MODULE_INIT_OPTS, - MODULE_INIT_QAPI, MODULE_INIT_QOM, MODULE_INIT_TRACE, MODULE_INIT_MAX @@ -50,7 +49,6 @@ typedef enum { #define block_init(function) module_init(function, MODULE_INIT_BLOCK) #define opts_init(function) module_init(function, MODULE_INIT_OPTS) -#define qapi_init(function) module_init(function, MODULE_INIT_QAPI) #define type_init(function) module_init(function, MODULE_INIT_QOM) #define trace_init(function) module_init(function, MODULE_INIT_TRACE) diff --git a/monitor.c b/monitor.c index f8f4a07..1cc2274 100644 --- a/monitor.c +++ b/monitor.c @@ -995,8 +995,10 @@ static void qmp_unregister_commands_hack(void) #endif } -static void qmp_init_marshal(void) +void monitor_init_qmp_commands(void) { + qmp_init_marshal(); + qmp_register_command("query-qmp-schema", qmp_query_qmp_schema, QCO_NO_OPTIONS); qmp_register_command("device_add", qmp_device_add, @@ -1004,12 +1006,9 @@ static void qmp_init_marshal(void) qmp_register_command("netdev_add", qmp_netdev_add, QCO_NO_OPTIONS); - /* call it after the rest of qapi_init() */ - register_module_init(qmp_unregister_commands_hack, MODULE_INIT_QAPI); + qmp_unregister_commands_hack(); } -qapi_init(qmp_init_marshal); - /* set the current CPU defined by the user */ int monitor_set_cpu(int cpu_index) { diff --git a/qga/main.c b/qga/main.c index 538e4ee..6f8c614 100644 --- a/qga/main.c +++ b/qga/main.c @@ -1321,7 +1321,7 @@ int main(int argc, char **argv) config->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; - module_call_init(MODULE_INIT_QAPI); + qmp_init_marshal(); init_dfl_pathnames(); config_load(config); diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 09e8467..a75946f 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -208,14 +208,12 @@ def gen_register_command(name, success_response): def gen_registry(registry): ret = mcgen(''' -static void qmp_init_marshal(void) +void qmp_init_marshal(void) { ''') ret += registry ret += mcgen(''' } - -qapi_init(qmp_init_marshal); ''') return ret @@ -308,6 +306,7 @@ fdecl.write(mcgen(''' #include "qapi/qmp/qdict.h" #include "qapi/error.h" +void qmp_init_marshal(void); ''', prefix=prefix)) diff --git a/tests/test-qmp-commands.c b/tests/test-qmp-commands.c index ff94481..c4e20d1 100644 --- a/tests/test-qmp-commands.c +++ b/tests/test-qmp-commands.c @@ -273,7 +273,7 @@ int main(int argc, char **argv) g_test_add_func("/0.15/dealloc_types", test_dealloc_types); g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial); - module_call_init(MODULE_INIT_QAPI); + qmp_init_marshal(); g_test_run(); return 0; diff --git a/vl.c b/vl.c index e10a27b..c6020b9 100644 --- a/vl.c +++ b/vl.c @@ -2987,7 +2987,7 @@ int main(int argc, char **argv, char **envp) qemu_init_exec_dir(argv[0]); module_call_init(MODULE_INIT_QOM); - module_call_init(MODULE_INIT_QAPI); + monitor_init_qmp_commands(); qemu_add_opts(&qemu_drive_opts); qemu_add_drive_opts(&qemu_legacy_drive_opts);