mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
plugin: Do not automatically initialize the RPC connection in bcli
Changelog-Fixed: plugin: `bcli` no longer logs a harmless warning about being unable to connect to the JSON-RPC interface. Changelog-Added: plugin: Plugins can opt out of having an RPC connection automatically initialized on startup.
This commit is contained in:
committed by
Rusty Russell
parent
54888d454b
commit
eb322b114b
@@ -88,7 +88,7 @@ static const struct plugin_command commands[] = { {
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
setup_locale();
|
setup_locale();
|
||||||
plugin_main(argv, init, PLUGIN_STATIC, NULL, commands, ARRAY_SIZE(commands),
|
plugin_main(argv, init, PLUGIN_STATIC, true, NULL, commands, ARRAY_SIZE(commands),
|
||||||
NULL, 0, NULL, 0,
|
NULL, 0, NULL, 0,
|
||||||
plugin_option("autocleaninvoice-cycle",
|
plugin_option("autocleaninvoice-cycle",
|
||||||
"string",
|
"string",
|
||||||
|
|||||||
@@ -961,7 +961,8 @@ int main(int argc, char *argv[])
|
|||||||
/* Initialize our global context object here to handle startup options. */
|
/* Initialize our global context object here to handle startup options. */
|
||||||
bitcoind = new_bitcoind(NULL);
|
bitcoind = new_bitcoind(NULL);
|
||||||
|
|
||||||
plugin_main(argv, init, PLUGIN_STATIC, NULL, commands, ARRAY_SIZE(commands),
|
plugin_main(argv, init, PLUGIN_STATIC, false /* Do not init RPC on startup*/,
|
||||||
|
NULL, commands, ARRAY_SIZE(commands),
|
||||||
NULL, 0, NULL, 0,
|
NULL, 0, NULL, 0,
|
||||||
plugin_option("bitcoin-datadir",
|
plugin_option("bitcoin-datadir",
|
||||||
"string",
|
"string",
|
||||||
|
|||||||
@@ -446,6 +446,6 @@ static const struct plugin_command commands[] = { {
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
setup_locale();
|
setup_locale();
|
||||||
plugin_main(argv, init, PLUGIN_RESTARTABLE, NULL, commands,
|
plugin_main(argv, init, PLUGIN_RESTARTABLE, true, NULL, commands,
|
||||||
ARRAY_SIZE(commands), NULL, 0, NULL, 0, NULL);
|
ARRAY_SIZE(commands), NULL, 0, NULL, 0, NULL);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -358,7 +358,7 @@ int main(int argc, char *argv[])
|
|||||||
features.bits[i] = tal_arr(NULL, u8, 0);
|
features.bits[i] = tal_arr(NULL, u8, 0);
|
||||||
set_feature_bit(&features.bits[NODE_ANNOUNCE_FEATURE], KEYSEND_FEATUREBIT);
|
set_feature_bit(&features.bits[NODE_ANNOUNCE_FEATURE], KEYSEND_FEATUREBIT);
|
||||||
|
|
||||||
plugin_main(argv, init, PLUGIN_STATIC, &features, commands,
|
plugin_main(argv, init, PLUGIN_STATIC, true, &features, commands,
|
||||||
ARRAY_SIZE(commands), NULL, 0, hooks, ARRAY_SIZE(hooks),
|
ARRAY_SIZE(commands), NULL, 0, hooks, ARRAY_SIZE(hooks),
|
||||||
NULL);
|
NULL);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,6 +83,10 @@ struct plugin {
|
|||||||
|
|
||||||
/* Feature set for lightningd */
|
/* Feature set for lightningd */
|
||||||
struct feature_set *our_features;
|
struct feature_set *our_features;
|
||||||
|
|
||||||
|
/* Location of the RPC filename in case we need to defer RPC
|
||||||
|
* initialization or need to recover from a disconnect. */
|
||||||
|
const char *rpc_location;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* command_result is mainly used as a compile-time check to encourage you
|
/* command_result is mainly used as a compile-time check to encourage you
|
||||||
@@ -763,7 +767,7 @@ static struct command_result *handle_init(struct command *cmd,
|
|||||||
char *dir, *network;
|
char *dir, *network;
|
||||||
struct json_out *param_obj;
|
struct json_out *param_obj;
|
||||||
struct plugin *p = cmd->plugin;
|
struct plugin *p = cmd->plugin;
|
||||||
bool with_rpc = true;
|
bool with_rpc = p->rpc_conn != NULL;
|
||||||
|
|
||||||
configtok = json_delve(buf, params, ".configuration");
|
configtok = json_delve(buf, params, ".configuration");
|
||||||
|
|
||||||
@@ -780,27 +784,39 @@ static struct command_result *handle_init(struct command *cmd,
|
|||||||
fsettok = json_delve(buf, configtok, ".feature_set");
|
fsettok = json_delve(buf, configtok, ".feature_set");
|
||||||
p->our_features = json_to_feature_set(p, buf, fsettok);
|
p->our_features = json_to_feature_set(p, buf, fsettok);
|
||||||
|
|
||||||
|
/* Only attempt to connect if the plugin has configured the rpc_conn
|
||||||
|
* already, if that's not the case we were told to run without an RPC
|
||||||
|
* connection, so don't even log an error. */
|
||||||
rpctok = json_delve(buf, configtok, ".rpc-file");
|
rpctok = json_delve(buf, configtok, ".rpc-file");
|
||||||
p->rpc_conn->fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
p->rpc_location = json_strdup(p, buf, rpctok);
|
||||||
if (rpctok->end - rpctok->start + 1 > sizeof(addr.sun_path))
|
/* FIXME: Move this to its own function so we can initialize at a
|
||||||
plugin_err(p, "rpc filename '%.*s' too long",
|
* later point in time. */
|
||||||
rpctok->end - rpctok->start,
|
if (p->rpc_conn != NULL) {
|
||||||
buf + rpctok->start);
|
p->rpc_conn->fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
memcpy(addr.sun_path, buf + rpctok->start, rpctok->end - rpctok->start);
|
if (strlen(p->rpc_location) + 1 > sizeof(addr.sun_path))
|
||||||
addr.sun_path[rpctok->end - rpctok->start] = '\0';
|
plugin_err(p, "rpc filename '%s' too long",
|
||||||
addr.sun_family = AF_UNIX;
|
p->rpc_location);
|
||||||
|
memcpy(addr.sun_path, buf + rpctok->start,
|
||||||
|
rpctok->end - rpctok->start);
|
||||||
|
addr.sun_path[rpctok->end - rpctok->start] = '\0';
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
|
||||||
|
if (connect(p->rpc_conn->fd, (struct sockaddr *)&addr,
|
||||||
|
sizeof(addr)) != 0) {
|
||||||
|
with_rpc = false;
|
||||||
|
plugin_log(p, LOG_UNUSUAL,
|
||||||
|
"Could not connect to '%s': %s",
|
||||||
|
p->rpc_location, strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
membuf_init(&p->rpc_conn->mb, tal_arr(p, char, READ_CHUNKSIZE),
|
||||||
|
READ_CHUNKSIZE, membuf_tal_realloc);
|
||||||
|
|
||||||
if (connect(p->rpc_conn->fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
|
||||||
with_rpc = false;
|
|
||||||
plugin_log(p, LOG_UNUSUAL, "Could not connect to '%.*s': %s",
|
|
||||||
rpctok->end - rpctok->start, buf + rpctok->start,
|
|
||||||
strerror(errno));
|
|
||||||
} else {
|
|
||||||
param_obj = json_out_obj(NULL, "config", "allow-deprecated-apis");
|
param_obj = json_out_obj(NULL, "config", "allow-deprecated-apis");
|
||||||
deprecated_apis = streq(rpc_delve(tmpctx, p, "listconfigs",
|
deprecated_apis =
|
||||||
take(param_obj),
|
streq(rpc_delve(tmpctx, p, "listconfigs", take(param_obj),
|
||||||
".allow-deprecated-apis"),
|
".allow-deprecated-apis"),
|
||||||
"true");
|
"true");
|
||||||
}
|
}
|
||||||
|
|
||||||
opttok = json_get_member(buf, params, "options");
|
opttok = json_get_member(buf, params, "options");
|
||||||
@@ -1182,6 +1198,7 @@ static struct plugin *new_plugin(const tal_t *ctx,
|
|||||||
void (*init)(struct plugin *p,
|
void (*init)(struct plugin *p,
|
||||||
const char *buf, const jsmntok_t *),
|
const char *buf, const jsmntok_t *),
|
||||||
const enum plugin_restartability restartability,
|
const enum plugin_restartability restartability,
|
||||||
|
bool init_rpc,
|
||||||
struct feature_set *features,
|
struct feature_set *features,
|
||||||
const struct plugin_command *commands,
|
const struct plugin_command *commands,
|
||||||
size_t num_commands,
|
size_t num_commands,
|
||||||
@@ -1207,11 +1224,12 @@ static struct plugin *new_plugin(const tal_t *ctx,
|
|||||||
uintmap_init(&p->out_reqs);
|
uintmap_init(&p->out_reqs);
|
||||||
|
|
||||||
p->our_features = features;
|
p->our_features = features;
|
||||||
/* Sync RPC FIXME: maybe go full async ? */
|
if (init_rpc) {
|
||||||
p->rpc_conn = tal(p, struct rpc_conn);
|
/* Sync RPC FIXME: maybe go full async ? */
|
||||||
membuf_init(&p->rpc_conn->mb,
|
p->rpc_conn = tal(p, struct rpc_conn);
|
||||||
tal_arr(p, char, READ_CHUNKSIZE), READ_CHUNKSIZE,
|
} else {
|
||||||
membuf_tal_realloc);
|
p->rpc_conn = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
p->init = init;
|
p->init = init;
|
||||||
p->manifested = p->initialized = false;
|
p->manifested = p->initialized = false;
|
||||||
@@ -1244,6 +1262,7 @@ void plugin_main(char *argv[],
|
|||||||
void (*init)(struct plugin *p,
|
void (*init)(struct plugin *p,
|
||||||
const char *buf, const jsmntok_t *),
|
const char *buf, const jsmntok_t *),
|
||||||
const enum plugin_restartability restartability,
|
const enum plugin_restartability restartability,
|
||||||
|
bool init_rpc,
|
||||||
struct feature_set *features,
|
struct feature_set *features,
|
||||||
const struct plugin_command *commands,
|
const struct plugin_command *commands,
|
||||||
size_t num_commands,
|
size_t num_commands,
|
||||||
@@ -1264,7 +1283,7 @@ void plugin_main(char *argv[],
|
|||||||
daemon_setup(argv[0], NULL, NULL);
|
daemon_setup(argv[0], NULL, NULL);
|
||||||
|
|
||||||
va_start(ap, num_hook_subs);
|
va_start(ap, num_hook_subs);
|
||||||
plugin = new_plugin(NULL, init, restartability, features, commands,
|
plugin = new_plugin(NULL, init, restartability, init_rpc, features, commands,
|
||||||
num_commands, notif_subs, num_notif_subs, hook_subs,
|
num_commands, notif_subs, num_notif_subs, hook_subs,
|
||||||
num_hook_subs, ap);
|
num_hook_subs, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|||||||
@@ -248,6 +248,7 @@ void NORETURN LAST_ARG_NULL plugin_main(char *argv[],
|
|||||||
void (*init)(struct plugin *p,
|
void (*init)(struct plugin *p,
|
||||||
const char *buf, const jsmntok_t *),
|
const char *buf, const jsmntok_t *),
|
||||||
const enum plugin_restartability restartability,
|
const enum plugin_restartability restartability,
|
||||||
|
bool init_rpc,
|
||||||
struct feature_set *features,
|
struct feature_set *features,
|
||||||
const struct plugin_command *commands,
|
const struct plugin_command *commands,
|
||||||
size_t num_commands,
|
size_t num_commands,
|
||||||
|
|||||||
@@ -2007,7 +2007,7 @@ static const struct plugin_command commands[] = {
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
setup_locale();
|
setup_locale();
|
||||||
plugin_main(argv, init, PLUGIN_RESTARTABLE, NULL, commands,
|
plugin_main(argv, init, PLUGIN_RESTARTABLE, true, NULL, commands,
|
||||||
ARRAY_SIZE(commands), NULL, 0, NULL, 0,
|
ARRAY_SIZE(commands), NULL, 0, NULL, 0,
|
||||||
plugin_option("disable-mpp", "flag",
|
plugin_option("disable-mpp", "flag",
|
||||||
"Disable multi-part payments.",
|
"Disable multi-part payments.",
|
||||||
|
|||||||
@@ -124,7 +124,8 @@ static const struct plugin_notification notifs[] = { {
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
setup_locale();
|
setup_locale();
|
||||||
plugin_main(argv, init, PLUGIN_RESTARTABLE, NULL, commands, ARRAY_SIZE(commands),
|
plugin_main(argv, init, PLUGIN_RESTARTABLE, true, NULL,
|
||||||
|
commands, ARRAY_SIZE(commands),
|
||||||
notifs, ARRAY_SIZE(notifs), hooks, ARRAY_SIZE(hooks),
|
notifs, ARRAY_SIZE(notifs), hooks, ARRAY_SIZE(hooks),
|
||||||
plugin_option("name",
|
plugin_option("name",
|
||||||
"string",
|
"string",
|
||||||
|
|||||||
Reference in New Issue
Block a user