From e6ef675ea14fd4b0e1d65e1b46aca6305553506d Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 5 Nov 2018 17:01:56 +0100 Subject: [PATCH] plugin: Send the configure request once we collected all options This is the final step to get the plugins working. After parsing the early options (including `--plugin`), then starting and asking the plugins for options, and finally reading in the options we just registered, we just need to assemble the options and send them over. Signed-off-by: Christian Decker <@cdecker> --- lightningd/lightningd.c | 5 +++++ lightningd/plugin.c | 30 +++++++++++++++++++++++++++ lightningd/test/run-find_my_abspath.c | 3 +++ 3 files changed, 38 insertions(+) diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index b646b0d7a..decb926a1 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -607,6 +607,11 @@ int main(int argc, char *argv[]) /*~ Handle options and config; move to .lightningd (--lightning-dir) */ handle_opts(ld, argc, argv); + /*~ Now that we have collected all the early options, gave + * plugins a chance to register theirs and collected all + * remaining options it's time to tell the plugins. */ + plugins_config(ld->plugins); + /*~ Make sure we can reach the subdaemons, and versions match. */ test_subdaemons(ld); diff --git a/lightningd/plugin.c b/lightningd/plugin.c index bef4e4f63..ef6b64f67 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -413,8 +413,38 @@ void plugins_init(struct plugins *plugins) io_loop(NULL, NULL); } +static void plugin_config_cb(const struct plugin_request *req, + struct plugin *plugin) +{ + /* Nothing to be done here, this is just a report */ +} + +/* FIXME(cdecker) This just builds a string for the request because + * the json_stream is tightly bound to the command interface. It + * should probably be generalized and fixed up. */ +static void plugin_config(struct plugin *plugin) +{ + struct plugin_opt *opt; + bool first = true; + const char *name, *sep; + char *conf = tal_fmt(tmpctx, "{\n \"options\": {"); + list_for_each(&plugin->plugin_opts, opt, list) { + /* Trim the `--` that we added before */ + name = opt->name + 2; + /* Separator between elements in the same object */ + sep = first?"":","; + first = false; + tal_append_fmt(&conf, "%s\n \"%s\": \"%s\"", sep, name, opt->value); + } + tal_append_fmt(&conf, "\n }\n}"); + plugin_request_send(plugin, "configure", conf, plugin_config_cb, plugin); +} + void plugins_config(struct plugins *plugins) { + for (size_t i=0; iplugins); i++) { + plugin_config(plugins->plugins[i]); + } } void json_add_opt_plugins(struct json_stream *response, diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index 38df6b72d..2e2e3abf7 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -122,6 +122,9 @@ struct chain_topology *new_topology(struct lightningd *ld UNNEEDED, struct log * /* Generated stub for onchaind_replay_channels */ void onchaind_replay_channels(struct lightningd *ld UNNEEDED) { fprintf(stderr, "onchaind_replay_channels called!\n"); abort(); } +/* Generated stub for plugins_config */ +void plugins_config(struct plugins *plugins UNNEEDED) +{ fprintf(stderr, "plugins_config called!\n"); abort(); } /* Generated stub for plugins_init */ void plugins_init(struct plugins *plugins UNNEEDED) { fprintf(stderr, "plugins_init called!\n"); abort(); }