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>
|
2018-09-19 22:19:51 +02:00
|
|
|
#include <ccan/take/take.h>
|
|
|
|
#include <ccan/tal/tal.h>
|
2018-09-20 20:43:55 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2018-11-01 18:53:49 +01:00
|
|
|
#include <lightningd/log.h>
|
2018-09-19 22:19:51 +02:00
|
|
|
|
2019-07-29 09:30:51 +02:00
|
|
|
enum plugin_state {
|
|
|
|
UNCONFIGURED,
|
|
|
|
CONFIGURING,
|
|
|
|
CONFIGURED
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
bool stop;
|
|
|
|
struct plugins *plugins;
|
|
|
|
const char **plugin_path;
|
2019-07-29 09:30:51 +02:00
|
|
|
|
|
|
|
enum plugin_state plugin_state;
|
|
|
|
|
2019-07-18 14:20:37 +02:00
|
|
|
/* If this plugin can be restarted without restarting lightningd */
|
|
|
|
bool dynamic;
|
2019-07-18 15:42:52 +02:00
|
|
|
bool signal_startup;
|
2019-07-01 23:55:32 +02:00
|
|
|
|
|
|
|
/* Stuff we read */
|
|
|
|
char *buffer;
|
|
|
|
size_t used, len_read;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
size_t pending_manifests;
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
int *as_int;
|
|
|
|
bool *as_bool;
|
|
|
|
};
|
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;
|
|
|
|
struct plugin_opt_value *value;
|
|
|
|
};
|
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
|
|
|
/**
|
|
|
|
* Search for `default_dir`, and if it exists add every directory it
|
|
|
|
* contains as a plugin dir.
|
|
|
|
*/
|
|
|
|
void plugins_add_default_dir(struct plugins *plugins, const char *default_dir);
|
|
|
|
|
2019-07-02 00:00:32 +02:00
|
|
|
void plugins_start(struct plugins *plugins, const char *dev_plugin_debug);
|
|
|
|
|
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.
|
2018-12-08 01:30:56 +01:00
|
|
|
*
|
|
|
|
* The dev_plugin_debug arg comes from --dev-debugger if DEVELOPER.
|
2018-09-19 22:19:51 +02:00
|
|
|
*/
|
2018-12-08 01:30:56 +01:00
|
|
|
void plugins_init(struct plugins *plugins, const char *dev_plugin_debug);
|
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
|
|
|
|
*/
|
|
|
|
void plugin_register(struct plugins *plugins, const char* path TAKES);
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
bool plugin_remove(struct plugins *plugins, const char *name);
|
|
|
|
|
2019-07-01 23:55:32 +02:00
|
|
|
/**
|
|
|
|
* Kill a plugin process, with an error message.
|
|
|
|
*/
|
|
|
|
void PRINTF_FMT(2,3) plugin_kill(struct plugin *plugin, char *fmt, ...);
|
|
|
|
|
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);
|
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
|
|
|
|
2019-03-18 03:40:32 +01:00
|
|
|
/**
|
|
|
|
* Used by db hooks which can't have any other I/O while talking to plugin.
|
|
|
|
*
|
|
|
|
* Returns output of io_loop() (ie. whatever gets passed to io_break()
|
|
|
|
* to end exclusive loop).
|
|
|
|
*/
|
|
|
|
void *plugin_exclusive_loop(struct plugin *plugin);
|
|
|
|
|
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,
|
|
|
|
bool nonexist_ok);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2019-05-31 04:01:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Needed for I/O logging for plugin messages.
|
|
|
|
*/
|
|
|
|
struct log *plugin_get_log(struct plugin *plugin);
|
|
|
|
|
2018-09-19 22:19:51 +02:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PLUGIN_H */
|