Skip to content

Commit 27c006f

Browse files
rustyrussellcdecker
authored andcommitted
libplugin: make init return a string.
Signed-off-by: Rusty Russell <[email protected]> Changelog-Added: libplugin: init can return a non-NULL string to disable the plugin.
1 parent 529ae0d commit 27c006f

File tree

11 files changed

+69
-34
lines changed

11 files changed

+69
-34
lines changed

plugins/autoclean.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,17 @@ static struct command_result *json_autocleaninvoice(struct command *cmd,
6464
expired_by, cycle_seconds));
6565
}
6666

67-
static void init(struct plugin *p,
68-
const char *buf UNUSED, const jsmntok_t *config UNUSED)
67+
static const char *init(struct plugin *p,
68+
const char *buf UNUSED, const jsmntok_t *config UNUSED)
6969
{
7070
if (cycle_seconds) {
7171
plugin_log(p, LOG_INFORM, "autocleaning every %"PRIu64" seconds", cycle_seconds);
7272
cleantimer = plugin_timer(p, time_from_sec(cycle_seconds),
7373
do_clean, p);
7474
} else
7575
plugin_log(p, LOG_DBG, "autocleaning not active");
76+
77+
return NULL;
7678
}
7779

7880
static const struct plugin_command commands[] = { {

plugins/bcli.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,12 +870,14 @@ static void wait_and_check_bitcoind(struct plugin *p)
870870
tal_free(cmd);
871871
}
872872

873-
static void init(struct plugin *p, const char *buffer UNUSED,
874-
const jsmntok_t *config UNUSED)
873+
static const char *init(struct plugin *p, const char *buffer UNUSED,
874+
const jsmntok_t *config UNUSED)
875875
{
876876
wait_and_check_bitcoind(p);
877877
plugin_log(p, LOG_INFORM,
878878
"bitcoin-cli initialized and connected to bitcoind.");
879+
880+
return NULL;
879881
}
880882

881883
static const struct plugin_command commands[] = {

plugins/fetchinvoice.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,12 +1346,14 @@ static const struct plugin_command commands[] = {
13461346
},
13471347
};
13481348

1349-
static void init(struct plugin *p, const char *buf UNUSED,
1350-
const jsmntok_t *config UNUSED)
1349+
static const char *init(struct plugin *p, const char *buf UNUSED,
1350+
const jsmntok_t *config UNUSED)
13511351
{
13521352
rpc_scan(p, "getinfo",
13531353
take(json_out_obj(NULL, NULL, NULL)),
13541354
"{id:%}", JSON_SCAN(json_to_node_id, &local_id));
1355+
1356+
return NULL;
13551357
}
13561358

13571359
static const struct plugin_hook hooks[] = {

plugins/keysend.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ REGISTER_PAYMENT_MODIFIER(keysend, struct keysend_data *, keysend_init,
9292
* End of keysend modifier
9393
*****************************************************************************/
9494

95-
static void init(struct plugin *p, const char *buf UNUSED,
96-
const jsmntok_t *config UNUSED)
95+
static const char *init(struct plugin *p, const char *buf UNUSED,
96+
const jsmntok_t *config UNUSED)
9797
{
9898
rpc_scan(p, "getinfo", take(json_out_obj(NULL, NULL, NULL)),
9999
"{id:%}", JSON_SCAN(json_to_node_id, &my_id));
@@ -102,6 +102,8 @@ static void init(struct plugin *p, const char *buf UNUSED,
102102
take(json_out_obj(NULL, "config", "max-locktime-blocks")),
103103
"{max-locktime-blocks:%}",
104104
JSON_SCAN(json_to_number, &maxdelay_default));
105+
106+
return NULL;
105107
}
106108

107109
struct payment_modifier *pay_mods[8] = {

plugins/libplugin.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ struct plugin {
7171
struct plugin_option *opts;
7272

7373
/* Anything special to do at init ? */
74-
void (*init)(struct plugin *p,
75-
const char *buf, const jsmntok_t *);
74+
const char *(*init)(struct plugin *p,
75+
const char *buf, const jsmntok_t *);
7676
/* Has the manifest been sent already ? */
7777
bool manifested;
7878
/* Has init been received ? */
@@ -878,8 +878,12 @@ static struct command_result *handle_init(struct command *cmd,
878878
tal_free(opt);
879879
}
880880

881-
if (p->init)
882-
p->init(p, buf, configtok);
881+
if (p->init) {
882+
const char *disable = p->init(p, buf, configtok);
883+
if (disable)
884+
return command_success(cmd, json_out_obj(cmd, "disable",
885+
disable));
886+
}
883887

884888
if (with_rpc)
885889
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,
12961300
}
12971301

12981302
static struct plugin *new_plugin(const tal_t *ctx,
1299-
void (*init)(struct plugin *p,
1300-
const char *buf, const jsmntok_t *),
1303+
const char *(*init)(struct plugin *p,
1304+
const char *buf,
1305+
const jsmntok_t *),
13011306
const enum plugin_restartability restartability,
13021307
bool init_rpc,
13031308
struct feature_set *features,
@@ -1365,8 +1370,8 @@ static struct plugin *new_plugin(const tal_t *ctx,
13651370
}
13661371

13671372
void plugin_main(char *argv[],
1368-
void (*init)(struct plugin *p,
1369-
const char *buf, const jsmntok_t *),
1373+
const char *(*init)(struct plugin *p,
1374+
const char *buf, const jsmntok_t *),
13701375
const enum plugin_restartability restartability,
13711376
bool init_rpc,
13721377
struct feature_set *features,

plugins/libplugin.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,9 @@ char *flag_option(const char *arg, bool *i);
277277

278278
/* The main plugin runner: append with 0 or more plugin_option(), then NULL. */
279279
void NORETURN LAST_ARG_NULL plugin_main(char *argv[],
280-
void (*init)(struct plugin *p,
281-
const char *buf, const jsmntok_t *),
280+
const char *(*init)(struct plugin *p,
281+
const char *buf,
282+
const jsmntok_t *),
282283
const enum plugin_restartability restartability,
283284
bool init_rpc,
284285
struct feature_set *features,

plugins/offers.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,9 @@ static struct command_result *json_decode(struct command *cmd,
665665
return command_finished(cmd, response);
666666
}
667667

668-
static void init(struct plugin *p,
669-
const char *buf UNUSED,
670-
const jsmntok_t *config UNUSED)
668+
static const char *init(struct plugin *p,
669+
const char *buf UNUSED,
670+
const jsmntok_t *config UNUSED)
671671
{
672672
struct pubkey k;
673673

@@ -681,6 +681,8 @@ static void init(struct plugin *p,
681681
rpc_scan(p, "listconfigs",
682682
take(json_out_obj(NULL, "config", "cltv-final")),
683683
"{cltv-final:%}", JSON_SCAN(json_to_number, &cltv_final));
684+
685+
return NULL;
684686
}
685687

686688
static const struct plugin_command commands[] = {

plugins/pay.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,8 +1902,8 @@ static struct command_result *json_listpays(struct command *cmd,
19021902
return send_outreq(cmd->plugin, req);
19031903
}
19041904

1905-
static void init(struct plugin *p,
1906-
const char *buf UNUSED, const jsmntok_t *config UNUSED)
1905+
static const char *init(struct plugin *p,
1906+
const char *buf UNUSED, const jsmntok_t *config UNUSED)
19071907
{
19081908
rpc_scan(p, "getinfo", take(json_out_obj(NULL, NULL, NULL)),
19091909
"{id:%}", JSON_SCAN(json_to_node_id, &my_id));
@@ -1912,6 +1912,8 @@ static void init(struct plugin *p,
19121912
take(json_out_obj(NULL, "config", "max-locktime-blocks")),
19131913
"{max-locktime-blocks:%}",
19141914
JSON_SCAN(json_to_number, &maxdelay_default));
1915+
1916+
return NULL;
19151917
}
19161918

19171919
struct payment_modifier *paymod_mods[] = {

plugins/spender/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
* spending from the onchain wallet. */
1010

1111
static
12-
void spender_init(struct plugin *p, const char *b, const jsmntok_t *t)
12+
const char *spender_init(struct plugin *p, const char *b, const jsmntok_t *t)
1313
{
1414
openchannel_init(p, b, t);
1515
/* whatever_init(p, b, t); */
16+
return NULL;
1617
}
1718

1819
int main(int argc, char **argv)

tests/plugins/test_libplugin.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
const char *name_option;
8-
8+
static bool self_disable = false;
99

1010
static struct command_result *json_helloworld(struct command *cmd,
1111
const char *buf,
@@ -86,10 +86,15 @@ static struct command_result *json_testrpc(struct command *cmd,
8686
return send_outreq(cmd->plugin, req);
8787
}
8888

89-
static void init(struct plugin *p,
90-
const char *buf UNUSED, const jsmntok_t *config UNUSED)
89+
static const char *init(struct plugin *p,
90+
const char *buf UNUSED,
91+
const jsmntok_t *config UNUSED)
9192
{
9293
plugin_log(p, LOG_DBG, "test_libplugin initialised!");
94+
95+
if (self_disable)
96+
return "Disabled via selfdisable option";
97+
return NULL;
9398
}
9499

95100
static const struct plugin_command commands[] = { {
@@ -149,5 +154,9 @@ int main(int argc, char *argv[])
149154
"string",
150155
"Who to say hello to.",
151156
charp_option, &name_option),
157+
plugin_option("selfdisable",
158+
"flag",
159+
"Whether to disable.",
160+
flag_option, &self_disable),
152161
NULL);
153162
}

0 commit comments

Comments
 (0)