From 296e7fba0335252f8c81c63a6c45d34ac054bc75 Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Fri, 25 Oct 2019 16:04:51 -0700 Subject: [PATCH] rpc: add spdk_rpc_verify_methods() This returns true if all registered methods and aliases are correct. False means that an error like one of the following occurred: - duplicate method with same name - alias specified for non-existant method - alias specified for another alias Also plumb this so that incorrect RPCs cause an SPDK application to exit. Note: there are cases where this would have been helpful during the recent RPC renaming. Fixes issue #940. Signed-off-by: Jim Harris Change-Id: I235a433c9b8c01e82f16288a8d295e96c54e4eb1 Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/472441 Tested-by: SPDK CI Jenkins Reviewed-by: Shuhei Matsumoto Reviewed-by: Tomasz Zawadzki --- include/spdk/rpc.h | 12 ++++++++++++ lib/event/rpc.c | 5 +++++ lib/rpc/rpc.c | 12 +++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/spdk/rpc.h b/include/spdk/rpc.h index e48c86dc5..b85606e43 100644 --- a/include/spdk/rpc.h +++ b/include/spdk/rpc.h @@ -42,6 +42,18 @@ extern "C" { #endif +/** + * Verify correctness of registered RPC methods and aliases. + * + * Incorrect registrations include: + * - multiple RPC methods registered with the same name + * - RPC alias registered with a method that does not exist + * - RPC alias registered that points to another alias + * + * \return true if registrations are all correct, false otherwise + */ +bool spdk_rpc_verify_methods(void); + /** * Start listening for RPC connections. * diff --git a/lib/event/rpc.c b/lib/event/rpc.c index f84143496..5f20fe5f7 100644 --- a/lib/event/rpc.c +++ b/lib/event/rpc.c @@ -61,6 +61,11 @@ spdk_rpc_initialize(const char *listen_addr) return; } + if (!spdk_rpc_verify_methods()) { + spdk_app_stop(-EINVAL); + return; + } + /* Listen on the requested address */ rc = spdk_rpc_listen(listen_addr); if (rc != 0) { diff --git a/lib/rpc/rpc.c b/lib/rpc/rpc.c index 1fac2b5e5..192d85986 100644 --- a/lib/rpc/rpc.c +++ b/lib/rpc/rpc.c @@ -51,6 +51,7 @@ static int g_rpc_lock_fd = -1; static struct spdk_jsonrpc_server *g_jsonrpc_server = NULL; static uint32_t g_rpc_state; +static bool g_rpcs_correct = true; struct spdk_rpc_method { const char *name; @@ -249,7 +250,8 @@ spdk_rpc_register_method(const char *method, spdk_rpc_method_handler func, uint3 m = _get_rpc_method_raw(method); if (m != NULL) { - SPDK_ERRLOG("duplicate RPC %s registered - ignoring...\n", method); + SPDK_ERRLOG("duplicate RPC %s registered...\n", method); + g_rpcs_correct = false; return; } @@ -275,11 +277,13 @@ spdk_rpc_register_alias_deprecated(const char *method, const char *alias) if (base == NULL) { SPDK_ERRLOG("cannot create alias %s - method %s does not exist\n", alias, method); + g_rpcs_correct = false; return; } if (base->is_alias_of != NULL) { SPDK_ERRLOG("cannot create alias %s of alias %s\n", alias, method); + g_rpcs_correct = false; return; } @@ -297,6 +301,12 @@ spdk_rpc_register_alias_deprecated(const char *method, const char *alias) SLIST_INSERT_HEAD(&g_rpc_methods, m, slist); } +bool +spdk_rpc_verify_methods(void) +{ + return g_rpcs_correct; +} + int spdk_rpc_is_method_allowed(const char *method, uint32_t state_mask) {