mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 17:47:30 +01:00
lightningd/plugin: Add a 'configured' member to the plugin struct, split 'plugins_init'
This adds a 'configured' boolean member to the plugin struct so that we can add plugins to ld->plugins' list and differenciate fresh plugins. This also adds 'plugins_start' so that new plugins can be started without calling 'plugins_init' and running an io loop
This commit is contained in:
parent
2e25c87bd4
commit
ce12a37a2b
3 changed files with 41 additions and 12 deletions
|
@ -221,6 +221,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
|
|||
*the plugins.
|
||||
*/
|
||||
ld->plugins = plugins_new(ld, ld->log_book, ld);
|
||||
ld->plugins->startup = true;
|
||||
|
||||
/*~ This is set when a JSON RPC command comes in to shut us down. */
|
||||
ld->stop_conn = NULL;
|
||||
|
|
|
@ -51,11 +51,22 @@ static void destroy_plugin(struct plugin *p)
|
|||
|
||||
void plugin_register(struct plugins *plugins, const char* path TAKES)
|
||||
{
|
||||
struct plugin *p;
|
||||
struct plugin *p, *p_temp;
|
||||
|
||||
/* Don't register an already registered plugin */
|
||||
list_for_each(&plugins->plugins, p_temp, list) {
|
||||
if (streq(path, p_temp->cmd)) {
|
||||
if (taken(path))
|
||||
tal_free(path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
p = tal(plugins, struct plugin);
|
||||
list_add_tail(&plugins->plugins, &p->list);
|
||||
p->plugins = plugins;
|
||||
p->cmd = tal_strdup(p, path);
|
||||
p->configured = false;
|
||||
p->js_arr = tal_arr(p, struct json_stream *, 0);
|
||||
p->used = 0;
|
||||
|
||||
|
@ -781,9 +792,9 @@ static void plugin_manifest_cb(const char *buffer,
|
|||
const jsmntok_t *resulttok;
|
||||
|
||||
/* Check if all plugins have replied to getmanifest, and break
|
||||
* if they are */
|
||||
* if they have and this is the startup init */
|
||||
plugin->plugins->pending_manifests--;
|
||||
if (plugin->plugins->pending_manifests == 0)
|
||||
if (plugin->plugins->startup && plugin->plugins->pending_manifests == 0)
|
||||
io_break(plugin->plugins);
|
||||
|
||||
resulttok = json_get_member(buffer, toks, "result");
|
||||
|
@ -893,20 +904,17 @@ void plugins_add_default_dir(struct plugins *plugins, const char *default_dir)
|
|||
}
|
||||
}
|
||||
|
||||
void plugins_init(struct plugins *plugins, const char *dev_plugin_debug)
|
||||
void plugins_start(struct plugins *plugins, const char *dev_plugin_debug)
|
||||
{
|
||||
struct plugin *p;
|
||||
char **cmd;
|
||||
int stdin, stdout;
|
||||
struct jsonrpc_request *req;
|
||||
plugins->pending_manifests = 0;
|
||||
uintmap_init(&plugins->pending_requests);
|
||||
|
||||
plugins_add_default_dir(plugins, path_join(tmpctx, plugins->ld->config_dir, "plugins"));
|
||||
|
||||
setenv("LIGHTNINGD_PLUGIN", "1", 1);
|
||||
/* Spawn the plugin processes before entering the io_loop */
|
||||
list_for_each(&plugins->plugins, p, list) {
|
||||
if (p->configured)
|
||||
continue;
|
||||
|
||||
bool debug;
|
||||
|
||||
debug = dev_plugin_debug && strends(p->cmd, dev_plugin_debug);
|
||||
|
@ -945,9 +953,24 @@ void plugins_init(struct plugins *plugins, const char *dev_plugin_debug)
|
|||
}
|
||||
tal_free(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
void plugins_init(struct plugins *plugins, const char *dev_plugin_debug)
|
||||
{
|
||||
plugins->pending_manifests = 0;
|
||||
uintmap_init(&plugins->pending_requests);
|
||||
|
||||
plugins_add_default_dir(plugins,
|
||||
path_join(tmpctx, plugins->ld->config_dir, "plugins"));
|
||||
|
||||
setenv("LIGHTNINGD_PLUGIN", "1", 1);
|
||||
/* Spawn the plugin processes before entering the io_loop */
|
||||
plugins_start(plugins, dev_plugin_debug);
|
||||
|
||||
if (plugins->pending_manifests > 0)
|
||||
io_loop_with_timers(plugins->ld);
|
||||
// There won't be io_loop anymore to wait for plugins
|
||||
plugins->startup = false;
|
||||
}
|
||||
|
||||
static void plugin_config_cb(const char *buffer,
|
||||
|
@ -955,7 +978,7 @@ static void plugin_config_cb(const char *buffer,
|
|||
const jsmntok_t *idtok,
|
||||
struct plugin *plugin)
|
||||
{
|
||||
/* Nothing to be done here, this is just a report */
|
||||
plugin->configured = true;
|
||||
}
|
||||
|
||||
/* FIXME(cdecker) This just builds a string for the request because
|
||||
|
@ -995,6 +1018,7 @@ void plugins_config(struct plugins *plugins)
|
|||
{
|
||||
struct plugin *p;
|
||||
list_for_each(&plugins->plugins, p, list) {
|
||||
if (!p->configured)
|
||||
plugin_config(p);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ struct plugin {
|
|||
bool stop;
|
||||
struct plugins *plugins;
|
||||
const char **plugin_path;
|
||||
bool configured;
|
||||
|
||||
/* Stuff we read */
|
||||
char *buffer;
|
||||
|
@ -53,6 +54,7 @@ struct plugin {
|
|||
struct plugins {
|
||||
struct list_head plugins;
|
||||
size_t pending_manifests;
|
||||
bool startup;
|
||||
|
||||
/* Currently pending requests by their request ID */
|
||||
UINTMAP(struct jsonrpc_request *) pending_requests;
|
||||
|
@ -96,6 +98,8 @@ struct plugins *plugins_new(const tal_t *ctx, struct log_book *log_book,
|
|||
*/
|
||||
void plugins_add_default_dir(struct plugins *plugins, const char *default_dir);
|
||||
|
||||
void plugins_start(struct plugins *plugins, const char *dev_plugin_debug);
|
||||
|
||||
/**
|
||||
* Initialize the registered plugins.
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue