Browse Source

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
pull/2938/head
darosior 6 years ago
committed by Rusty Russell
parent
commit
ce12a37a2b
  1. 1
      lightningd/lightningd.c
  2. 48
      lightningd/plugin.c
  3. 4
      lightningd/plugin.h

1
lightningd/lightningd.c

@ -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;

48
lightningd/plugin.c

@ -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,7 +1018,8 @@ void plugins_config(struct plugins *plugins)
{
struct plugin *p;
list_for_each(&plugins->plugins, p, list) {
plugin_config(p);
if (!p->configured)
plugin_config(p);
}
}

4
lightningd/plugin.h

@ -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…
Cancel
Save