From 2e27e4e4438b04ec2505331ab903851091001a26 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 28 Apr 2021 16:18:40 +0200 Subject: [PATCH] plugin: Move list of notification topics to each plugin We want to ensure that plugins register their topics before sending any notification, so we need to remember which plugin registered which topics. --- lightningd/notification.c | 12 +++++++----- lightningd/plugin.c | 6 +++--- lightningd/plugin.h | 8 ++++---- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lightningd/notification.c b/lightningd/notification.c index 63d2bec60..df9b6deae 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -26,15 +26,17 @@ bool notifications_topic_is_native(const char *topic) bool notifications_have_topic(const struct plugins *plugins, const char *topic) { + struct plugin *plugin; if (notifications_topic_is_native(topic)) return true; /* Some plugin at some point announced it'd be emitting - * notifications to this topic. We don't care if it died, just - * that it was a valid topic at some point in time. */ - for (size_t i=0; inotification_topics); i++) - if (streq(plugins->notification_topics[i], topic)) - return true; + * notifications to this topic. */ + list_for_each(&plugins->plugins, plugin, list) { + for (size_t i = 0; i < tal_count(plugin->notification_topics); i++) + if (streq(plugin->notification_topics[i], topic)) + return true; + } return false; } diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 907e0cf84..57b9e5e9b 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -76,7 +76,6 @@ struct plugins *plugins_new(const tal_t *ctx, struct log_book *log_book, p->startup = true; p->json_cmds = tal_arr(p, struct command *, 0); p->blacklist = tal_arr(p, const char *, 0); - p->notification_topics = tal_arr(p, const char *, 0); p->shutdown = false; p->plugin_idx = 0; #if DEVELOPER @@ -246,6 +245,7 @@ struct plugin *plugin_register(struct plugins *plugins, const char* path TAKES, p->plugin_state = UNCONFIGURED; p->js_arr = tal_arr(p, struct json_stream *, 0); p->used = 0; + p->notification_topics = tal_arr(p, const char *, 0); p->subscriptions = NULL; p->dynamic = false; p->index = plugins->plugin_idx++; @@ -1326,7 +1326,7 @@ static const char *plugin_notifications_add(const char *buffer, "missing or not a string.", i); - name = json_strdup(plugin->plugins, buffer, method); + name = json_strdup(plugin, buffer, method); if (notifications_topic_is_native(name)) return tal_fmt(plugin, @@ -1335,7 +1335,7 @@ static const char *plugin_notifications_add(const char *buffer, "however only be sent by lightningd", name); - tal_arr_expand(&plugin->plugins->notification_topics, name); + tal_arr_expand(&plugin->notification_topics, name); } return NULL; diff --git a/lightningd/plugin.h b/lightningd/plugin.h index 27e2ee777..e31e642b1 100644 --- a/lightningd/plugin.h +++ b/lightningd/plugin.h @@ -93,6 +93,10 @@ struct plugin { /* Parameters for dynamically-started plugins. */ const char *parambuf; const jsmntok_t *params; + + /* Notification topics that this plugin has registered with us + * and that other plugins may subscribe to. */ + const char **notification_topics; }; /** @@ -128,10 +132,6 @@ struct plugins { /* Whether builtin plugins should be overridden as unimportant. */ bool dev_builtin_plugins_unimportant; #endif /* DEVELOPER */ - - /* Notification topics that plugins have registered with us - * and that other plugins may subscribe to. */ - const char **notification_topics; }; /* The value of a plugin option, which can have different types.