libplugin: make init return a string.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Added: libplugin: init can return a non-NULL string to disable the plugin.
This commit is contained in:
Rusty Russell
2021-01-13 13:30:24 +10:30
committed by Christian Decker
parent 529ae0d766
commit 27c006f7aa
11 changed files with 69 additions and 34 deletions

View File

@@ -64,8 +64,8 @@ static struct command_result *json_autocleaninvoice(struct command *cmd,
expired_by, cycle_seconds));
}
static void init(struct plugin *p,
const char *buf UNUSED, const jsmntok_t *config UNUSED)
static const char *init(struct plugin *p,
const char *buf UNUSED, const jsmntok_t *config UNUSED)
{
if (cycle_seconds) {
plugin_log(p, LOG_INFORM, "autocleaning every %"PRIu64" seconds", cycle_seconds);
@@ -73,6 +73,8 @@ static void init(struct plugin *p,
do_clean, p);
} else
plugin_log(p, LOG_DBG, "autocleaning not active");
return NULL;
}
static const struct plugin_command commands[] = { {

View File

@@ -870,12 +870,14 @@ static void wait_and_check_bitcoind(struct plugin *p)
tal_free(cmd);
}
static void init(struct plugin *p, const char *buffer UNUSED,
const jsmntok_t *config UNUSED)
static const char *init(struct plugin *p, const char *buffer UNUSED,
const jsmntok_t *config UNUSED)
{
wait_and_check_bitcoind(p);
plugin_log(p, LOG_INFORM,
"bitcoin-cli initialized and connected to bitcoind.");
return NULL;
}
static const struct plugin_command commands[] = {

View File

@@ -1346,12 +1346,14 @@ static const struct plugin_command commands[] = {
},
};
static void init(struct plugin *p, const char *buf UNUSED,
const jsmntok_t *config UNUSED)
static const char *init(struct plugin *p, const char *buf UNUSED,
const jsmntok_t *config UNUSED)
{
rpc_scan(p, "getinfo",
take(json_out_obj(NULL, NULL, NULL)),
"{id:%}", JSON_SCAN(json_to_node_id, &local_id));
return NULL;
}
static const struct plugin_hook hooks[] = {

View File

@@ -92,8 +92,8 @@ REGISTER_PAYMENT_MODIFIER(keysend, struct keysend_data *, keysend_init,
* End of keysend modifier
*****************************************************************************/
static void init(struct plugin *p, const char *buf UNUSED,
const jsmntok_t *config UNUSED)
static const char *init(struct plugin *p, const char *buf UNUSED,
const jsmntok_t *config UNUSED)
{
rpc_scan(p, "getinfo", take(json_out_obj(NULL, NULL, NULL)),
"{id:%}", JSON_SCAN(json_to_node_id, &my_id));
@@ -102,6 +102,8 @@ static void init(struct plugin *p, const char *buf UNUSED,
take(json_out_obj(NULL, "config", "max-locktime-blocks")),
"{max-locktime-blocks:%}",
JSON_SCAN(json_to_number, &maxdelay_default));
return NULL;
}
struct payment_modifier *pay_mods[8] = {

View File

@@ -71,8 +71,8 @@ struct plugin {
struct plugin_option *opts;
/* Anything special to do at init ? */
void (*init)(struct plugin *p,
const char *buf, const jsmntok_t *);
const char *(*init)(struct plugin *p,
const char *buf, const jsmntok_t *);
/* Has the manifest been sent already ? */
bool manifested;
/* Has init been received ? */
@@ -878,8 +878,12 @@ static struct command_result *handle_init(struct command *cmd,
tal_free(opt);
}
if (p->init)
p->init(p, buf, configtok);
if (p->init) {
const char *disable = p->init(p, buf, configtok);
if (disable)
return command_success(cmd, json_out_obj(cmd, "disable",
disable));
}
if (with_rpc)
io_new_conn(p, p->rpc_conn->fd, rpc_conn_init, p);
@@ -1296,8 +1300,9 @@ static struct io_plan *stdout_conn_init(struct io_conn *conn,
}
static struct plugin *new_plugin(const tal_t *ctx,
void (*init)(struct plugin *p,
const char *buf, const jsmntok_t *),
const char *(*init)(struct plugin *p,
const char *buf,
const jsmntok_t *),
const enum plugin_restartability restartability,
bool init_rpc,
struct feature_set *features,
@@ -1365,8 +1370,8 @@ static struct plugin *new_plugin(const tal_t *ctx,
}
void plugin_main(char *argv[],
void (*init)(struct plugin *p,
const char *buf, const jsmntok_t *),
const char *(*init)(struct plugin *p,
const char *buf, const jsmntok_t *),
const enum plugin_restartability restartability,
bool init_rpc,
struct feature_set *features,

View File

@@ -277,8 +277,9 @@ char *flag_option(const char *arg, bool *i);
/* The main plugin runner: append with 0 or more plugin_option(), then NULL. */
void NORETURN LAST_ARG_NULL plugin_main(char *argv[],
void (*init)(struct plugin *p,
const char *buf, const jsmntok_t *),
const char *(*init)(struct plugin *p,
const char *buf,
const jsmntok_t *),
const enum plugin_restartability restartability,
bool init_rpc,
struct feature_set *features,

View File

@@ -665,9 +665,9 @@ static struct command_result *json_decode(struct command *cmd,
return command_finished(cmd, response);
}
static void init(struct plugin *p,
const char *buf UNUSED,
const jsmntok_t *config UNUSED)
static const char *init(struct plugin *p,
const char *buf UNUSED,
const jsmntok_t *config UNUSED)
{
struct pubkey k;
@@ -681,6 +681,8 @@ static void init(struct plugin *p,
rpc_scan(p, "listconfigs",
take(json_out_obj(NULL, "config", "cltv-final")),
"{cltv-final:%}", JSON_SCAN(json_to_number, &cltv_final));
return NULL;
}
static const struct plugin_command commands[] = {

View File

@@ -1902,8 +1902,8 @@ static struct command_result *json_listpays(struct command *cmd,
return send_outreq(cmd->plugin, req);
}
static void init(struct plugin *p,
const char *buf UNUSED, const jsmntok_t *config UNUSED)
static const char *init(struct plugin *p,
const char *buf UNUSED, const jsmntok_t *config UNUSED)
{
rpc_scan(p, "getinfo", take(json_out_obj(NULL, NULL, NULL)),
"{id:%}", JSON_SCAN(json_to_node_id, &my_id));
@@ -1912,6 +1912,8 @@ static void init(struct plugin *p,
take(json_out_obj(NULL, "config", "max-locktime-blocks")),
"{max-locktime-blocks:%}",
JSON_SCAN(json_to_number, &maxdelay_default));
return NULL;
}
struct payment_modifier *paymod_mods[] = {

View File

@@ -9,10 +9,11 @@
* spending from the onchain wallet. */
static
void spender_init(struct plugin *p, const char *b, const jsmntok_t *t)
const char *spender_init(struct plugin *p, const char *b, const jsmntok_t *t)
{
openchannel_init(p, b, t);
/* whatever_init(p, b, t); */
return NULL;
}
int main(int argc, char **argv)