2018-09-19 22:19:51 +02:00
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_PLUGIN_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_PLUGIN_H
|
|
|
|
#include "config.h"
|
2019-07-01 23:55:32 +02:00
|
|
|
#include <ccan/intmap/intmap.h>
|
|
|
|
#include <ccan/io/io.h>
|
2019-09-14 17:35:28 +02:00
|
|
|
#include <ccan/pipecmd/pipecmd.h>
|
2018-09-19 22:19:51 +02:00
|
|
|
#include <ccan/take/take.h>
|
2019-09-14 17:35:28 +02:00
|
|
|
#include <ccan/tal/path/path.h>
|
2018-09-19 22:19:51 +02:00
|
|
|
#include <ccan/tal/tal.h>
|
2019-09-14 17:35:28 +02:00
|
|
|
#include <common/json_command.h>
|
|
|
|
#include <common/jsonrpc_errors.h>
|
|
|
|
#include <common/memleak.h>
|
|
|
|
#include <common/param.h>
|
|
|
|
#include <common/timeout.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <lightningd/io_loop_with_timers.h>
|
2018-09-20 20:43:55 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2019-09-14 17:35:28 +02:00
|
|
|
#include <lightningd/lightningd.h>
|
2018-11-01 18:53:49 +01:00
|
|
|
#include <lightningd/log.h>
|
2019-09-14 17:35:28 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-09-19 22:19:51 +02:00
|
|
|
|
2019-07-29 09:30:51 +02:00
|
|
|
enum plugin_state {
|
2020-05-04 19:19:05 +02:00
|
|
|
/* We have to ask getmanifest */
|
2019-07-29 09:30:51 +02:00
|
|
|
UNCONFIGURED,
|
2020-05-05 03:12:29 +02:00
|
|
|
/* We sent getmanifest, need response. */
|
|
|
|
AWAITING_GETMANIFEST_RESPONSE,
|
2020-05-04 19:19:05 +02:00
|
|
|
/* Got `getmanifest` reply, now we need to send `init`. */
|
|
|
|
NEEDS_INIT,
|
|
|
|
/* We have to get `init` response */
|
|
|
|
AWAITING_INIT_RESPONSE,
|
|
|
|
/* We have `init` response. */
|
|
|
|
INIT_COMPLETE
|
2019-07-29 09:30:51 +02:00
|
|
|
};
|
|
|
|
|
2019-07-01 23:55:32 +02:00
|
|
|
/**
|
|
|
|
* A plugin, exposed as a stub so we can pass it as an argument.
|
|
|
|
*/
|
|
|
|
struct plugin {
|
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
pid_t pid;
|
|
|
|
char *cmd;
|
|
|
|
struct io_conn *stdin_conn, *stdout_conn;
|
|
|
|
struct plugins *plugins;
|
|
|
|
const char **plugin_path;
|
2020-05-05 03:13:14 +02:00
|
|
|
/* If there's a json command which ordered this to start */
|
|
|
|
struct command *start_cmd;
|
2019-07-29 09:30:51 +02:00
|
|
|
|
|
|
|
enum plugin_state plugin_state;
|
|
|
|
|
2020-11-03 00:20:52 +01:00
|
|
|
/* Our unique index, which is default hook ordering. */
|
|
|
|
u64 index;
|
|
|
|
|
2019-07-18 14:20:37 +02:00
|
|
|
/* If this plugin can be restarted without restarting lightningd */
|
|
|
|
bool dynamic;
|
2019-07-01 23:55:32 +02:00
|
|
|
|
|
|
|
/* Stuff we read */
|
|
|
|
char *buffer;
|
|
|
|
size_t used, len_read;
|
2020-08-20 15:40:34 +02:00
|
|
|
jsmn_parser parser;
|
|
|
|
jsmntok_t *toks;
|
2019-07-01 23:55:32 +02:00
|
|
|
|
|
|
|
/* Our json_streams. Since multiple streams could start
|
|
|
|
* returning data at once, we always service these in order,
|
|
|
|
* freeing once empty. */
|
|
|
|
struct json_stream **js_arr;
|
|
|
|
|
|
|
|
struct log *log;
|
|
|
|
|
|
|
|
/* List of options that this plugin registered */
|
|
|
|
struct list_head plugin_opts;
|
|
|
|
|
|
|
|
const char **methods;
|
|
|
|
|
|
|
|
/* Timer to add a timeout to some plugin RPC calls. Used to
|
|
|
|
* guarantee that `getmanifest` doesn't block indefinitely. */
|
|
|
|
const struct oneshot *timeout_timer;
|
|
|
|
|
|
|
|
/* An array of subscribed topics */
|
|
|
|
char **subscriptions;
|
2020-04-09 17:36:04 +02:00
|
|
|
|
|
|
|
/* An array of currently pending RPC method calls, to be killed if the
|
|
|
|
* plugin exits. */
|
|
|
|
struct list_head pending_rpccalls;
|
2020-07-29 13:24:07 +02:00
|
|
|
|
|
|
|
/* If set, the plugin is so important that if it terminates early,
|
|
|
|
* C-lightning should terminate as well. */
|
|
|
|
bool important;
|
2020-12-14 05:58:35 +01:00
|
|
|
|
|
|
|
/* Parameters for dynamically-started plugins. */
|
|
|
|
const char *parambuf;
|
|
|
|
const jsmntok_t *params;
|
2019-07-01 23:55:32 +02:00
|
|
|
};
|
|
|
|
|
2018-09-19 22:19:51 +02:00
|
|
|
/**
|
|
|
|
* A collection of plugins, and some associated information.
|
|
|
|
*
|
|
|
|
* Mainly used as root context for calls in the plugin subsystem.
|
|
|
|
*/
|
2019-07-01 23:55:32 +02:00
|
|
|
struct plugins {
|
|
|
|
struct list_head plugins;
|
2019-07-02 00:00:32 +02:00
|
|
|
bool startup;
|
2018-09-19 22:19:51 +02:00
|
|
|
|
2019-07-01 23:55:32 +02:00
|
|
|
/* Currently pending requests by their request ID */
|
|
|
|
UINTMAP(struct jsonrpc_request *) pending_requests;
|
|
|
|
struct log *log;
|
|
|
|
struct log_book *log_book;
|
|
|
|
|
|
|
|
struct lightningd *ld;
|
2019-09-15 22:41:19 +02:00
|
|
|
const char *default_dir;
|
2020-05-05 03:13:14 +02:00
|
|
|
|
|
|
|
/* If there are json commands waiting for plugin resolutions. */
|
|
|
|
struct command **json_cmds;
|
2020-05-05 03:15:21 +02:00
|
|
|
|
|
|
|
/* Blacklist of plugins from --disable-plugin */
|
|
|
|
const char **blacklist;
|
2020-07-29 13:24:07 +02:00
|
|
|
|
|
|
|
/* Whether we are shutting down (`plugins_free` is called) */
|
|
|
|
bool shutdown;
|
2020-08-04 09:24:42 +02:00
|
|
|
|
2020-11-03 00:20:52 +01:00
|
|
|
/* Index to show what order they were added in */
|
|
|
|
u64 plugin_idx;
|
|
|
|
|
2020-08-04 09:24:42 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
/* Whether builtin plugins should be overridden as unimportant. */
|
|
|
|
bool dev_builtin_plugins_unimportant;
|
|
|
|
#endif /* DEVELOPER */
|
2019-07-01 23:55:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The value of a plugin option, which can have different types.
|
|
|
|
* The presence of the integer and boolean values will depend of
|
|
|
|
* the option type, but the string value will always be filled.
|
2018-12-20 12:00:35 +01:00
|
|
|
*/
|
2019-07-01 23:55:32 +02:00
|
|
|
struct plugin_opt_value {
|
|
|
|
char *as_str;
|
2020-12-14 05:52:23 +01:00
|
|
|
s64 as_int;
|
|
|
|
bool as_bool;
|
2019-07-01 23:55:32 +02:00
|
|
|
};
|
2018-12-20 12:00:35 +01:00
|
|
|
|
2019-02-17 21:44:10 +01:00
|
|
|
/**
|
|
|
|
* Simple storage for plugin options inbetween registering them on the
|
|
|
|
* command line and passing them off to the plugin
|
|
|
|
*/
|
2019-07-01 23:55:32 +02:00
|
|
|
struct plugin_opt {
|
|
|
|
struct list_node list;
|
|
|
|
const char *name;
|
|
|
|
const char *type;
|
|
|
|
const char *description;
|
2020-12-14 05:52:23 +01:00
|
|
|
struct plugin_opt_value **values;
|
|
|
|
/* Might be NULL if no default */
|
|
|
|
struct plugin_opt_value *def;
|
|
|
|
bool multi;
|
2020-08-06 02:28:02 +02:00
|
|
|
bool deprecated;
|
2019-07-01 23:55:32 +02:00
|
|
|
};
|
2019-02-17 21:44:10 +01:00
|
|
|
|
2018-09-19 22:19:51 +02:00
|
|
|
/**
|
|
|
|
* Create a new plugins context.
|
|
|
|
*/
|
2018-11-21 21:54:01 +01:00
|
|
|
struct plugins *plugins_new(const tal_t *ctx, struct log_book *log_book,
|
2019-02-04 11:55:42 +01:00
|
|
|
struct lightningd *ld);
|
2018-09-19 22:19:51 +02:00
|
|
|
|
2019-05-20 11:18:19 +02:00
|
|
|
/**
|
2019-09-15 22:41:19 +02:00
|
|
|
* Recursively add all plugins from the default plugins directory.
|
2019-05-20 11:18:19 +02:00
|
|
|
*/
|
2019-09-15 22:41:19 +02:00
|
|
|
void plugins_add_default_dir(struct plugins *plugins);
|
2019-05-20 11:18:19 +02:00
|
|
|
|
2018-09-19 22:19:51 +02:00
|
|
|
/**
|
|
|
|
* Initialize the registered plugins.
|
|
|
|
*
|
|
|
|
* Initialization includes spinning up the plugins, reading their
|
|
|
|
* manifest, and registering the JSON-RPC passthrough and command line
|
|
|
|
* arguments. In order to read the getmanifest reply from the plugins
|
|
|
|
* we spin up our own io_loop that exits once all plugins have
|
|
|
|
* responded.
|
|
|
|
*/
|
2020-05-05 03:13:34 +02:00
|
|
|
void plugins_init(struct plugins *plugins);
|
2018-09-19 22:19:51 +02:00
|
|
|
|
2020-04-12 15:42:41 +02:00
|
|
|
/**
|
|
|
|
* Free all resources that are held by plugins in the correct order.
|
|
|
|
*
|
|
|
|
* This function ensures that the resources dangling off of the plugins struct
|
|
|
|
* are freed in the correct order. This is necessary since the struct manages
|
|
|
|
* two orthogonal sets of resources:
|
|
|
|
*
|
|
|
|
* - Plugins
|
|
|
|
* - Hook calls and notifications
|
|
|
|
*
|
|
|
|
* The tal hierarchy is organized in a plugin centric way, i.e., the plugins
|
|
|
|
* may exit in an arbitrary order and they'll unregister pointers in the other
|
|
|
|
* resources. However this will fail if `tal_free` decides to free one of the
|
|
|
|
* non-plugin resources (technically a sibling in the allocation tree) before
|
|
|
|
* the plugins we will get a use-after-free. This function fixes this by
|
|
|
|
* freeing in the correct order without adding additional child-relationships
|
|
|
|
* in the allocation structure and without adding destructors.
|
|
|
|
*/
|
|
|
|
void plugins_free(struct plugins *plugins);
|
|
|
|
|
2018-09-19 22:19:51 +02:00
|
|
|
/**
|
|
|
|
* Register a plugin for initialization and execution.
|
|
|
|
*
|
|
|
|
* @param plugins: Plugin context
|
|
|
|
* @param path: The path of the executable for this plugin
|
2020-05-05 03:13:14 +02:00
|
|
|
* @param start_cmd: The optional JSON command which caused this.
|
2020-07-29 13:24:07 +02:00
|
|
|
* @param important: The plugin is important.
|
2020-12-14 05:58:35 +01:00
|
|
|
* @param parambuf: NULL, or the JSON buffer for extra parameters.
|
|
|
|
* @param params: NULL, or the tokens for extra parameters.
|
2020-05-05 03:13:14 +02:00
|
|
|
*
|
|
|
|
* If @start_cmd, then plugin_cmd_killed or plugin_cmd_succeeded will be called
|
|
|
|
* on it eventually.
|
2018-09-19 22:19:51 +02:00
|
|
|
*/
|
2020-05-05 03:13:14 +02:00
|
|
|
struct plugin *plugin_register(struct plugins *plugins,
|
|
|
|
const char* path TAKES,
|
2020-07-29 13:24:07 +02:00
|
|
|
struct command *start_cmd,
|
2020-12-14 05:58:35 +01:00
|
|
|
bool important,
|
|
|
|
const char *parambuf STEALS,
|
|
|
|
const jsmntok_t *params STEALS);
|
|
|
|
|
2018-09-19 22:19:51 +02:00
|
|
|
|
2019-07-01 23:55:32 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the provided name matches a plugin command
|
|
|
|
*/
|
|
|
|
bool plugin_paths_match(const char *cmd, const char *name);
|
2018-12-04 04:04:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a plugin registered for initialization.
|
|
|
|
*
|
|
|
|
* @param plugins: Plugin context
|
|
|
|
* @param arg: The basename or fullname of the executable for this plugin
|
|
|
|
*/
|
2020-05-05 03:15:21 +02:00
|
|
|
void plugin_blacklist(struct plugins *plugins, const char *name);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is a plugin disabled?.
|
|
|
|
*
|
|
|
|
* @param plugins: Plugin context
|
|
|
|
* @param arg: The basename or fullname of the executable for this plugin
|
|
|
|
*/
|
|
|
|
bool plugin_blacklisted(struct plugins *plugins, const char *name);
|
2018-12-04 04:04:14 +01:00
|
|
|
|
2020-05-05 03:13:34 +02:00
|
|
|
/**
|
2020-05-05 03:15:21 +02:00
|
|
|
* Kick off initialization of a plugin.
|
2020-05-05 03:14:32 +02:00
|
|
|
*
|
|
|
|
* Returns error string, or NULL.
|
2020-05-05 03:13:34 +02:00
|
|
|
*/
|
2020-05-05 03:14:32 +02:00
|
|
|
const char *plugin_send_getmanifest(struct plugin *p);
|
2020-05-05 03:13:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Kick of initialization of all plugins which need it/
|
|
|
|
*
|
|
|
|
* Return true if any were started.
|
|
|
|
*/
|
|
|
|
bool plugins_send_getmanifest(struct plugins *plugins);
|
|
|
|
|
2019-07-01 23:55:32 +02:00
|
|
|
/**
|
2020-05-05 03:14:46 +02:00
|
|
|
* Kill a plugin process and free @plugin, with an error message.
|
2019-07-01 23:55:32 +02:00
|
|
|
*/
|
2020-05-05 03:14:46 +02:00
|
|
|
void plugin_kill(struct plugin *plugin, const char *msg);
|
2019-07-01 23:55:32 +02:00
|
|
|
|
2020-01-07 11:59:18 +01:00
|
|
|
/**
|
|
|
|
* Returns the plugin which registers the command with name {cmd_name}
|
|
|
|
*/
|
|
|
|
struct plugin *find_plugin_for_command(struct lightningd *ld,
|
|
|
|
const char *cmd_name);
|
|
|
|
|
|
|
|
|
2020-05-05 03:13:14 +02:00
|
|
|
/**
|
|
|
|
* Call plugin_cmd_all_complete once all plugins are init or killed.
|
|
|
|
*
|
|
|
|
* Returns NULL if it's still pending. otherwise, returns
|
|
|
|
* plugin_cmd_all_complete().
|
|
|
|
*/
|
|
|
|
struct command_result *plugin_register_all_complete(struct lightningd *ld,
|
|
|
|
struct command *cmd);
|
|
|
|
|
2018-11-02 20:04:32 +01:00
|
|
|
/**
|
|
|
|
* Send the configure message to all plugins.
|
|
|
|
*
|
|
|
|
* Once we've collected all the command line arguments we can go ahead
|
|
|
|
* and send them over to the plugin. This finalizes the initialization
|
|
|
|
* of the plugins and signals that lightningd is now ready to process
|
|
|
|
* incoming JSON-RPC calls and messages.
|
|
|
|
*/
|
|
|
|
void plugins_config(struct plugins *plugins);
|
2019-09-15 15:34:09 +02:00
|
|
|
|
2020-05-05 03:12:46 +02:00
|
|
|
/**
|
|
|
|
* Are any plugins at this state still?
|
|
|
|
*/
|
|
|
|
bool plugins_any_in_state(const struct plugins *plugins, enum plugin_state state);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Are all plugins at this state?
|
|
|
|
*/
|
|
|
|
bool plugins_all_in_state(const struct plugins *plugins, enum plugin_state state);
|
|
|
|
|
2019-09-15 15:34:09 +02:00
|
|
|
/**
|
|
|
|
* This populates the jsonrpc request with the plugin/lightningd specifications
|
|
|
|
*/
|
|
|
|
void plugin_populate_init_request(struct plugin *p, struct jsonrpc_request *req);
|
|
|
|
|
2018-09-20 20:43:55 +02:00
|
|
|
/**
|
|
|
|
* Add the plugin option and their respective options to listconfigs.
|
|
|
|
*
|
|
|
|
* This adds a dict that maps the plugin name to a dict of configuration options
|
|
|
|
* for the corresponding plugins.
|
|
|
|
*/
|
|
|
|
void json_add_opt_plugins(struct json_stream *response,
|
|
|
|
const struct plugins *plugins);
|
|
|
|
|
2018-12-03 10:26:29 +01:00
|
|
|
|
2020-05-05 03:15:34 +02:00
|
|
|
/**
|
|
|
|
* Add the disable-plugins options to listconfigs.
|
|
|
|
*/
|
|
|
|
void json_add_opt_disable_plugins(struct json_stream *response,
|
|
|
|
const struct plugins *plugins);
|
|
|
|
|
2019-03-18 03:40:32 +01:00
|
|
|
/**
|
2020-11-23 04:46:13 +01:00
|
|
|
* Used by db hooks which can't have any other I/O while talking to
|
|
|
|
* hooked plugins.
|
2019-03-18 03:40:32 +01:00
|
|
|
*
|
2020-11-23 04:46:13 +01:00
|
|
|
* @param plugins - a `tal`-allocated array of plugins that are the
|
|
|
|
* only ones we talk to.
|
|
|
|
*
|
|
|
|
* @return output of io_loop() (ie. whatever gets passed to io_break()
|
2019-03-18 03:40:32 +01:00
|
|
|
* to end exclusive loop).
|
|
|
|
*/
|
2020-11-23 04:46:13 +01:00
|
|
|
void *plugins_exclusive_loop(struct plugin **plugins);
|
2019-03-18 03:40:32 +01:00
|
|
|
|
2018-12-03 10:26:29 +01:00
|
|
|
/**
|
|
|
|
* Add a directory to the plugin path to automatically load plugins.
|
|
|
|
*/
|
2018-12-04 04:04:14 +01:00
|
|
|
char *add_plugin_dir(struct plugins *plugins, const char *dir,
|
2019-09-10 17:46:08 +02:00
|
|
|
bool error_ok);
|
2018-12-04 04:04:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all plugins registered so far.
|
|
|
|
*/
|
|
|
|
void clear_plugins(struct plugins *plugins);
|
2018-12-03 10:26:29 +01:00
|
|
|
|
2018-12-13 13:23:21 +01:00
|
|
|
void plugins_notify(struct plugins *plugins,
|
|
|
|
const struct jsonrpc_notification *n TAKES);
|
|
|
|
|
2019-01-02 16:04:25 +01:00
|
|
|
/**
|
|
|
|
* Send a jsonrpc_request to the specified plugin
|
|
|
|
*/
|
|
|
|
void plugin_request_send(struct plugin *plugin,
|
|
|
|
struct jsonrpc_request *req TAKES);
|
|
|
|
|
2019-02-17 21:44:10 +01:00
|
|
|
/**
|
|
|
|
* Callback called when parsing options. It just stores the value in
|
|
|
|
* the plugin_opt
|
|
|
|
*/
|
|
|
|
char *plugin_opt_set(const char *arg, struct plugin_opt *popt);
|
|
|
|
|
2020-03-17 02:34:35 +01:00
|
|
|
/**
|
|
|
|
* Callback called when plugin flag-type options.It just stores
|
|
|
|
* the value in the plugin_opt
|
|
|
|
*/
|
|
|
|
char *plugin_opt_flag_set(struct plugin_opt *popt);
|
|
|
|
|
2019-09-15 23:45:33 +02:00
|
|
|
/**
|
|
|
|
* Helpers to initialize a connection to a plugin; we read from their
|
|
|
|
* stdout, and write to their stdin.
|
|
|
|
*/
|
|
|
|
struct io_plan *plugin_stdin_conn_init(struct io_conn *conn,
|
|
|
|
struct plugin *plugin);
|
|
|
|
struct io_plan *plugin_stdout_conn_init(struct io_conn *conn,
|
|
|
|
struct plugin *plugin);
|
2019-05-31 04:01:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Needed for I/O logging for plugin messages.
|
|
|
|
*/
|
|
|
|
struct log *plugin_get_log(struct plugin *plugin);
|
|
|
|
|
2020-07-29 10:20:49 +02:00
|
|
|
/**
|
|
|
|
* Tells the plugin system the directory for builtin plugins.
|
|
|
|
*/
|
|
|
|
void plugins_set_builtin_plugins_dir(struct plugins *plugins,
|
|
|
|
const char *dir);
|
|
|
|
|
2019-12-06 05:08:27 +01:00
|
|
|
/* Pair of functions to detect if plugin destroys itself: must always
|
|
|
|
* call both! */
|
|
|
|
struct plugin_destroyed *plugin_detect_destruction(const struct plugin *plugin);
|
|
|
|
bool was_plugin_destroyed(struct plugin_destroyed *destroyed);
|
2018-09-19 22:19:51 +02:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PLUGIN_H */
|