2020-02-03 12:25:06 +01:00
|
|
|
#include <ccan/opt/opt.h>
|
2019-12-24 23:27:47 +01:00
|
|
|
#include <lightningd/options.h>
|
2019-06-29 13:47:48 +02:00
|
|
|
#include <lightningd/plugin_control.h>
|
|
|
|
#include <lightningd/plugin_hook.h>
|
2019-12-25 00:11:52 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2019-06-29 13:47:48 +02:00
|
|
|
|
2019-09-12 01:26:51 +02:00
|
|
|
/* A dummy structure used to give multiple arguments to callbacks. */
|
|
|
|
struct dynamic_plugin {
|
|
|
|
struct plugin *plugin;
|
|
|
|
struct command *cmd;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returned by all subcommands on success.
|
|
|
|
*/
|
2020-05-05 03:13:14 +02:00
|
|
|
static struct command_result *plugin_dynamic_list_plugins(struct command *cmd,
|
|
|
|
const struct plugins *plugins)
|
2019-09-12 01:26:51 +02:00
|
|
|
{
|
|
|
|
struct json_stream *response;
|
2020-05-05 03:13:14 +02:00
|
|
|
const struct plugin *p;
|
2019-09-12 01:26:51 +02:00
|
|
|
|
|
|
|
response = json_stream_success(cmd);
|
|
|
|
json_array_start(response, "plugins");
|
2020-05-05 03:13:14 +02:00
|
|
|
list_for_each(&plugins->plugins, p, list) {
|
2019-09-12 01:26:51 +02:00
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_string(response, "name", p->cmd);
|
|
|
|
json_add_bool(response, "active",
|
2020-05-04 19:19:05 +02:00
|
|
|
p->plugin_state == INIT_COMPLETE);
|
2019-09-12 01:26:51 +02:00
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
|
|
|
return command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
2020-05-05 03:13:14 +02:00
|
|
|
struct command_result *plugin_cmd_killed(struct command *cmd,
|
|
|
|
struct plugin *plugin, const char *msg)
|
|
|
|
{
|
|
|
|
return command_fail(cmd, PLUGIN_ERROR, "%s: %s", plugin->cmd, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct command_result *plugin_cmd_succeeded(struct command *cmd,
|
|
|
|
struct plugin *plugin)
|
|
|
|
{
|
|
|
|
return plugin_dynamic_list_plugins(cmd, plugin->plugins);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct command_result *plugin_cmd_all_complete(struct plugins *plugins,
|
|
|
|
struct command *cmd)
|
|
|
|
{
|
|
|
|
return plugin_dynamic_list_plugins(cmd, plugins);
|
|
|
|
}
|
|
|
|
|
2019-09-12 01:26:51 +02:00
|
|
|
/**
|
|
|
|
* Called when trying to start a plugin through RPC, it starts the plugin and
|
|
|
|
* will give a result 20 seconds later at the most.
|
|
|
|
*/
|
|
|
|
static struct command_result *
|
|
|
|
plugin_dynamic_start(struct command *cmd, const char *plugin_path)
|
|
|
|
{
|
2020-05-05 03:13:56 +02:00
|
|
|
struct plugin *p = plugin_register(cmd->ld->plugins, plugin_path, cmd);
|
2020-05-05 03:14:32 +02:00
|
|
|
const char *err;
|
2019-09-12 01:26:51 +02:00
|
|
|
|
2020-05-05 03:13:56 +02:00
|
|
|
if (!p)
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"%s: already registered",
|
|
|
|
plugin_path);
|
2019-09-12 01:26:51 +02:00
|
|
|
|
2020-05-05 03:13:56 +02:00
|
|
|
/* This will come back via plugin_cmd_killed or plugin_cmd_succeeded */
|
2020-05-05 03:14:32 +02:00
|
|
|
err = plugin_send_getmanifest(p);
|
|
|
|
if (err)
|
2020-05-05 03:13:56 +02:00
|
|
|
return command_fail(cmd, PLUGIN_ERROR,
|
2020-05-05 03:14:32 +02:00
|
|
|
"%s: %s",
|
|
|
|
plugin_path, err);
|
2020-05-05 03:13:56 +02:00
|
|
|
|
|
|
|
return command_still_pending(cmd);
|
2019-09-12 01:26:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-14 17:35:00 +02:00
|
|
|
/**
|
|
|
|
* Called when trying to start a plugin directory through RPC, it registers
|
|
|
|
* all contained plugins recursively and then starts them.
|
|
|
|
*/
|
|
|
|
static struct command_result *
|
|
|
|
plugin_dynamic_startdir(struct command *cmd, const char *dir_path)
|
|
|
|
{
|
|
|
|
const char *err;
|
2020-05-05 03:13:56 +02:00
|
|
|
struct command_result *res;
|
2019-09-14 17:35:00 +02:00
|
|
|
|
|
|
|
err = add_plugin_dir(cmd->ld->plugins, dir_path, false);
|
|
|
|
if (err)
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS, "%s", err);
|
|
|
|
|
2020-05-05 03:13:56 +02:00
|
|
|
/* If none added, this calls plugin_cmd_all_complete immediately */
|
|
|
|
res = plugin_register_all_complete(cmd->ld, cmd);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2019-09-14 17:35:00 +02:00
|
|
|
|
2020-05-05 03:13:56 +02:00
|
|
|
plugins_send_getmanifest(cmd->ld->plugins);
|
2019-09-14 17:35:00 +02:00
|
|
|
return command_still_pending(cmd);
|
|
|
|
}
|
|
|
|
|
2019-09-15 18:46:01 +02:00
|
|
|
static struct command_result *
|
|
|
|
plugin_dynamic_stop(struct command *cmd, const char *plugin_name)
|
|
|
|
{
|
|
|
|
struct plugin *p;
|
|
|
|
struct json_stream *response;
|
|
|
|
|
|
|
|
list_for_each(&cmd->ld->plugins->plugins, p, list) {
|
|
|
|
if (plugin_paths_match(p->cmd, plugin_name)) {
|
|
|
|
if (!p->dynamic)
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"%s cannot be managed when "
|
|
|
|
"lightningd is up",
|
|
|
|
plugin_name);
|
2020-05-05 03:14:21 +02:00
|
|
|
plugin_kill(p, "stopped by lightningd via RPC");
|
2019-09-15 18:46:01 +02:00
|
|
|
response = json_stream_success(cmd);
|
2019-12-24 23:27:47 +01:00
|
|
|
if (deprecated_apis)
|
|
|
|
json_add_string(response, "",
|
|
|
|
take(tal_fmt(NULL, "Successfully stopped %s.",
|
|
|
|
plugin_name)));
|
|
|
|
json_add_string(response, "result",
|
2019-09-15 18:46:01 +02:00
|
|
|
take(tal_fmt(NULL, "Successfully stopped %s.",
|
|
|
|
plugin_name)));
|
|
|
|
return command_success(cmd, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"Could not find plugin %s", plugin_name);
|
|
|
|
}
|
|
|
|
|
2019-09-15 18:51:41 +02:00
|
|
|
/**
|
|
|
|
* Look for additions in the default plugin directory.
|
|
|
|
*/
|
|
|
|
static struct command_result *
|
|
|
|
plugin_dynamic_rescan_plugins(struct command *cmd)
|
|
|
|
{
|
2020-05-05 03:13:56 +02:00
|
|
|
struct command_result *res;
|
2019-09-15 18:51:41 +02:00
|
|
|
|
|
|
|
/* This will not fail on "already registered" error. */
|
2019-09-15 22:41:19 +02:00
|
|
|
plugins_add_default_dir(cmd->ld->plugins);
|
2019-09-15 18:51:41 +02:00
|
|
|
|
2020-05-05 03:13:56 +02:00
|
|
|
/* If none added, this calls plugin_cmd_all_complete immediately */
|
|
|
|
res = plugin_register_all_complete(cmd->ld, cmd);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2019-09-15 18:51:41 +02:00
|
|
|
|
|
|
|
return command_still_pending(cmd);
|
|
|
|
}
|
|
|
|
|
2019-06-29 13:47:48 +02:00
|
|
|
/**
|
|
|
|
* A plugin command which permits to control plugins without restarting
|
|
|
|
* lightningd. It takes a subcommand, and an optional subcommand parameter.
|
|
|
|
*/
|
|
|
|
static struct command_result *json_plugin_control(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
const char *subcmd;
|
|
|
|
subcmd = param_subcommand(cmd, buffer, params,
|
2019-09-15 18:51:41 +02:00
|
|
|
"start", "stop", "startdir",
|
|
|
|
"rescan", "list", NULL);
|
2019-06-29 13:47:48 +02:00
|
|
|
if (!subcmd)
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
if (streq(subcmd, "stop")) {
|
|
|
|
const char *plugin_name;
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("subcommand", param_ignore, cmd),
|
|
|
|
p_req("plugin", param_string, &plugin_name),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
2019-09-15 18:46:01 +02:00
|
|
|
return plugin_dynamic_stop(cmd, plugin_name);
|
2019-06-29 13:47:48 +02:00
|
|
|
} else if (streq(subcmd, "start")) {
|
|
|
|
const char *plugin_path;
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("subcommand", param_ignore, cmd),
|
|
|
|
p_req("plugin", param_string, &plugin_path),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
if (access(plugin_path, X_OK) == 0)
|
2019-09-12 01:26:51 +02:00
|
|
|
return plugin_dynamic_start(cmd, plugin_path);
|
2019-06-29 13:47:48 +02:00
|
|
|
else
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"%s is not executable: %s",
|
|
|
|
plugin_path, strerror(errno));
|
|
|
|
} else if (streq(subcmd, "startdir")) {
|
|
|
|
const char *dir_path;
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("subcommand", param_ignore, cmd),
|
|
|
|
p_req("directory", param_string, &dir_path),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
if (access(dir_path, F_OK) == 0)
|
2019-09-14 17:35:00 +02:00
|
|
|
return plugin_dynamic_startdir(cmd, dir_path);
|
2019-06-29 13:47:48 +02:00
|
|
|
else
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"Could not open %s", dir_path);
|
|
|
|
} else if (streq(subcmd, "rescan")) {
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("subcommand", param_ignore, cmd),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
2019-09-15 18:51:41 +02:00
|
|
|
return plugin_dynamic_rescan_plugins(cmd);
|
2019-06-29 13:47:48 +02:00
|
|
|
} else if (streq(subcmd, "list")) {
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("subcommand", param_ignore, cmd),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
2020-05-05 03:13:14 +02:00
|
|
|
return plugin_dynamic_list_plugins(cmd, cmd->ld->plugins);
|
2019-06-29 13:47:48 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 18:51:41 +02:00
|
|
|
/* subcmd must be one of the above: param_subcommand checked it! */
|
|
|
|
abort();
|
2019-06-29 13:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command plugin_control_command = {
|
|
|
|
"plugin",
|
|
|
|
"plugin",
|
|
|
|
json_plugin_control,
|
|
|
|
"Control plugins (start, stop, startdir, rescan, list)",
|
|
|
|
.verbose = "Usage :\n"
|
|
|
|
"plugin start /path/to/a/plugin\n"
|
|
|
|
" adds a new plugin to c-lightning\n"
|
|
|
|
"plugin stop plugin_name\n"
|
|
|
|
" stops an already registered plugin\n"
|
|
|
|
"plugin startdir /path/to/a/plugin_dir/\n"
|
|
|
|
" adds a new plugin directory\n"
|
|
|
|
"plugin rescan\n"
|
|
|
|
" loads not-already-loaded plugins from the default plugins dir\n"
|
|
|
|
"plugin list\n"
|
|
|
|
" lists all active plugins\n"
|
|
|
|
"\n"
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &plugin_control_command);
|