From d299420fbe9737a91176296c81abd0753e1f9061 Mon Sep 17 00:00:00 2001 From: darosior Date: Sat, 29 Jun 2019 13:44:20 +0200 Subject: [PATCH] lightningd/plugin_hook: make it possible to unregister a hook This adds 'plugin_unregister_hook' and 'plugin_unregister_hook_all' functions to unregister a given hook a plugin registered, or all hooks a plugin registered for. Since hooks can only be registered once, it's useful in the case a new plugin is added which would be prefered for hook registration over an already loaded plugin. --- lightningd/plugin_hook.c | 26 ++++++++++++++++++++++++++ lightningd/plugin_hook.h | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/lightningd/plugin_hook.c b/lightningd/plugin_hook.c index 54e7098ee..bd620db70 100644 --- a/lightningd/plugin_hook.c +++ b/lightningd/plugin_hook.c @@ -39,6 +39,32 @@ bool plugin_hook_register(struct plugin *plugin, const char *method) return true; } +bool plugin_hook_unregister(struct plugin *plugin, const char *method) +{ + struct plugin_hook *hook = plugin_hook_by_name(method); + if (!hook) { + /* No such hook name registered */ + return false; + } else if (hook->plugin == NULL) { + /* This name is not registered */ + return false; + } + hook->plugin = NULL; + return true; +} + +void plugin_hook_unregister_all(struct plugin *plugin) +{ + static struct plugin_hook **hooks = NULL; + static size_t num_hooks; + if (!hooks) + hooks = autodata_get(hooks, &num_hooks); + + for (size_t i = 0; i < num_hooks; i++) + if (hooks[i]->plugin == plugin) + hooks[i]->plugin = NULL; +} + /** * Callback to be passed to the jsonrpc_request. * diff --git a/lightningd/plugin_hook.h b/lightningd/plugin_hook.h index 76d9b6fea..4869d69e9 100644 --- a/lightningd/plugin_hook.h +++ b/lightningd/plugin_hook.h @@ -102,6 +102,12 @@ void plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook, bool plugin_hook_register(struct plugin *plugin, const char *method); +/* Unregister a hook a plugin has registered for */ +bool plugin_hook_unregister(struct plugin *plugin, const char *method); + +/* Unregister all hooks a plugin has registered for */ +void plugin_hook_unregister_all(struct plugin *plugin); + /* Special sync plugin hook for db: changes[] are SQL statements, with optional * final command appended. */ void plugin_hook_db_sync(struct db *db, const char **changes, const char *final);