Browse Source

libplugin: pass configuration to plugin's 'init' callback

So that a plugin can, for example, know if it has been loaded on startup
pull/2938/head
darosior 6 years ago
committed by Christian Decker
parent
commit
0cd3823c98
  1. 3
      plugins/autoclean.c
  2. 18
      plugins/libplugin.c
  3. 3
      plugins/libplugin.h
  4. 3
      plugins/pay.c

3
plugins/autoclean.c

@ -65,7 +65,8 @@ static struct command_result *json_autocleaninvoice(struct command *cmd,
expired_by, cycle_seconds));
}
static void init(struct plugin_conn *prpc)
static void init(struct plugin_conn *prpc,
const char *buf UNUSED, const jsmntok_t *config UNUSED)
{
rpc = prpc;

18
plugins/libplugin.c

@ -513,21 +513,24 @@ static struct command_result *handle_init(struct command *init_cmd,
const char *buf,
const jsmntok_t *params,
const struct plugin_option *opts,
void (*init)(struct plugin_conn *))
void (*init)(struct plugin_conn *,
const char *buf, const jsmntok_t *))
{
const jsmntok_t *rpctok, *dirtok, *opttok, *t;
const jsmntok_t *configtok, *rpctok, *dirtok, *opttok, *t;
struct sockaddr_un addr;
size_t i;
char *dir;
struct json_out *param_obj;
configtok = json_delve(buf, params, ".configuration");
/* Move into lightning directory: other files are relative */
dirtok = json_delve(buf, params, ".configuration.lightning-dir");
dirtok = json_delve(buf, configtok, ".lightning-dir");
dir = json_strdup(tmpctx, buf, dirtok);
if (chdir(dir) != 0)
plugin_err("chdir to %s: %s", dir, strerror(errno));
rpctok = json_delve(buf, params, ".configuration.rpc-file");
rpctok = json_delve(buf, configtok, ".rpc-file");
rpc_conn.fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (rpctok->end - rpctok->start + 1 > sizeof(addr.sun_path))
plugin_err("rpc filename '%.*s' too long",
@ -547,7 +550,7 @@ static struct command_result *handle_init(struct command *init_cmd,
take(param_obj),
&rpc_conn,
".allow-deprecated-apis"),
"true");
"true");
opttok = json_get_member(buf, params, "options");
json_for_each_obj(i, t, opttok) {
char *opt = json_strdup(NULL, buf, t);
@ -566,7 +569,7 @@ static struct command_result *handle_init(struct command *init_cmd,
}
if (init)
init(&rpc_conn);
init(&rpc_conn, buf, configtok);
return command_success_str(init_cmd, NULL);
}
@ -700,7 +703,8 @@ void plugin_log(enum log_level l, const char *fmt, ...)
}
void plugin_main(char *argv[],
void (*init)(struct plugin_conn *rpc),
void (*init)(struct plugin_conn *rpc,
const char *buf, const jsmntok_t *),
const enum plugin_restartability restartability,
const struct plugin_command *commands,
size_t num_commands, ...)

3
plugins/libplugin.h

@ -152,7 +152,8 @@ char *charp_option(const char *arg, char **p);
/* 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_conn *rpc),
void (*init)(struct plugin_conn *rpc,
const char *buf, const jsmntok_t *),
const enum plugin_restartability restartability,
const struct plugin_command *commands,
size_t num_commands, ...);

3
plugins/pay.c

@ -1271,7 +1271,8 @@ static struct command_result *json_listpays(struct command *cmd,
take(json_out_obj(NULL, "bolt11", b11str)));
}
static void init(struct plugin_conn *rpc)
static void init(struct plugin_conn *rpc,
const char *buf UNUSED, const jsmntok_t *config UNUSED)
{
const char *field;

Loading…
Cancel
Save