2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-03-18 03:40:32 +01:00
|
|
|
#include <ccan/io/io.h>
|
2022-09-12 23:19:12 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2022-07-04 05:49:38 +02:00
|
|
|
#include <common/json_parse.h>
|
2019-01-02 19:23:18 +01:00
|
|
|
#include <common/memleak.h>
|
2022-01-03 19:45:35 +01:00
|
|
|
#include <db/exec.h>
|
|
|
|
#include <db/utils.h>
|
2018-12-20 12:00:35 +01:00
|
|
|
#include <lightningd/plugin_hook.h>
|
|
|
|
|
2019-01-02 19:23:18 +01:00
|
|
|
/* Struct containing all the information needed to deserialize and
|
|
|
|
* dispatch an eventual plugin_hook response. */
|
|
|
|
struct plugin_hook_request {
|
2022-09-12 23:19:12 +02:00
|
|
|
const char *cmd_id;
|
2019-01-02 19:23:18 +01:00
|
|
|
const struct plugin_hook *hook;
|
|
|
|
void *cb_arg;
|
2023-10-24 03:41:29 +02:00
|
|
|
/* db_hook doesn't have ld yet */
|
2019-01-03 17:56:51 +01:00
|
|
|
struct db *db;
|
2020-02-13 21:01:23 +01:00
|
|
|
struct lightningd *ld;
|
2023-10-24 03:41:29 +02:00
|
|
|
|
|
|
|
/* Where are we up to in the hooks[] array below */
|
|
|
|
size_t hook_index;
|
|
|
|
/* A snapshot taken at the start: destructors may NULL some out! */
|
|
|
|
struct hook_instance **hooks;
|
2019-01-02 19:23:18 +01:00
|
|
|
};
|
|
|
|
|
2023-10-24 03:41:29 +02:00
|
|
|
static void destroy_hook_in_ph_req(struct hook_instance *hook,
|
|
|
|
struct plugin_hook_request *ph_req)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(ph_req->hooks); i++) {
|
|
|
|
if (ph_req->hooks[i] == hook) {
|
|
|
|
ph_req->hooks[i] = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2020-10-30 02:13:37 +01:00
|
|
|
struct hook_instance {
|
|
|
|
/* What plugin registered */
|
|
|
|
struct plugin *plugin;
|
|
|
|
|
|
|
|
/* Dependencies it asked for. */
|
|
|
|
const char **before, **after;
|
|
|
|
};
|
|
|
|
|
2020-11-02 03:36:29 +01:00
|
|
|
static struct plugin_hook **get_hooks(size_t *num)
|
2018-12-28 21:23:08 +01:00
|
|
|
{
|
|
|
|
static struct plugin_hook **hooks = NULL;
|
|
|
|
static size_t num_hooks;
|
|
|
|
if (!hooks)
|
|
|
|
hooks = autodata_get(hooks, &num_hooks);
|
2020-11-02 03:36:29 +01:00
|
|
|
*num = num_hooks;
|
|
|
|
return hooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct plugin_hook *plugin_hook_by_name(const char *name)
|
|
|
|
{
|
|
|
|
size_t num_hooks;
|
|
|
|
struct plugin_hook **hooks = get_hooks(&num_hooks);
|
2018-12-28 21:23:08 +01:00
|
|
|
|
|
|
|
for (size_t i=0; i<num_hooks; i++)
|
|
|
|
if (streq(hooks[i]->name, name))
|
|
|
|
return hooks[i];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-30 02:13:37 +01:00
|
|
|
/* When we destroy a plugin, we remove its hooks */
|
|
|
|
static void destroy_hook_instance(struct hook_instance *h,
|
|
|
|
struct plugin_hook *hook)
|
2018-12-28 21:23:08 +01:00
|
|
|
{
|
2020-10-30 02:13:37 +01:00
|
|
|
for (size_t i = 0; i < tal_count(hook->hooks); i++) {
|
|
|
|
if (h == hook->hooks[i]) {
|
|
|
|
tal_arr_remove(&hook->hooks, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct plugin_hook *plugin_hook_register(struct plugin *plugin, const char *method)
|
|
|
|
{
|
|
|
|
struct hook_instance *h;
|
2018-12-28 21:23:08 +01:00
|
|
|
struct plugin_hook *hook = plugin_hook_by_name(method);
|
|
|
|
if (!hook) {
|
|
|
|
/* No such hook name registered */
|
2020-10-30 02:13:37 +01:00
|
|
|
return NULL;
|
2018-12-28 21:23:08 +01:00
|
|
|
}
|
2020-02-04 16:53:04 +01:00
|
|
|
|
2020-10-30 02:13:37 +01:00
|
|
|
/* Make sure the hook_elements array is initialized. */
|
|
|
|
if (hook->hooks == NULL)
|
|
|
|
hook->hooks = notleak(tal_arr(NULL, struct hook_instance *, 0));
|
2020-02-04 16:53:04 +01:00
|
|
|
|
|
|
|
/* Ensure we don't register the same plugin multple times. */
|
2020-10-30 02:13:37 +01:00
|
|
|
for (size_t i=0; i<tal_count(hook->hooks); i++)
|
|
|
|
if (hook->hooks[i]->plugin == plugin)
|
|
|
|
return NULL;
|
2020-02-04 16:53:04 +01:00
|
|
|
|
|
|
|
/* Ok, we're sure they can register and they aren't yet registered, so
|
|
|
|
* register them. */
|
2020-10-30 02:13:37 +01:00
|
|
|
h = tal(plugin, struct hook_instance);
|
|
|
|
h->plugin = plugin;
|
|
|
|
h->before = tal_arr(h, const char *, 0);
|
|
|
|
h->after = tal_arr(h, const char *, 0);
|
|
|
|
tal_add_destructor2(h, destroy_hook_instance, hook);
|
|
|
|
|
|
|
|
tal_arr_expand(&hook->hooks, h);
|
|
|
|
return hook;
|
2019-06-29 13:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:21:48 +01:00
|
|
|
/* Mutual recursion */
|
|
|
|
static void plugin_hook_call_next(struct plugin_hook_request *ph_req);
|
2020-02-12 19:44:01 +01:00
|
|
|
static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
|
|
|
|
const jsmntok_t *idtok,
|
|
|
|
struct plugin_hook_request *r);
|
|
|
|
|
2020-04-15 12:21:34 +02:00
|
|
|
bool plugin_hook_continue(void *unused, const char *buffer, const jsmntok_t *toks)
|
|
|
|
{
|
|
|
|
const jsmntok_t *resrestok = json_get_member(buffer, toks, "result");
|
|
|
|
return resrestok && json_tok_streq(buffer, resrestok, "continue");
|
|
|
|
}
|
|
|
|
|
2024-04-02 05:03:49 +02:00
|
|
|
static void cleanup_ph_req(struct plugin_hook_request *ph_req)
|
|
|
|
{
|
|
|
|
/* We need to remove the destructors from the remaining
|
|
|
|
* call-chain, otherwise they'd still be called when the
|
|
|
|
* plugin dies or we shut down. */
|
|
|
|
for (size_t i=0; i<tal_count(ph_req->hooks); i++) {
|
|
|
|
tal_del_destructor2(ph_req->hooks[i],
|
|
|
|
destroy_hook_in_ph_req, ph_req);
|
|
|
|
}
|
|
|
|
|
|
|
|
tal_free(ph_req);
|
|
|
|
}
|
|
|
|
|
2019-01-02 19:23:18 +01:00
|
|
|
/**
|
|
|
|
* Callback to be passed to the jsonrpc_request.
|
|
|
|
*
|
|
|
|
* Unbundles the arguments, deserializes the response and dispatches
|
|
|
|
* it to the hook callback.
|
|
|
|
*/
|
|
|
|
static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
|
|
|
|
const jsmntok_t *idtok,
|
2023-10-24 03:41:29 +02:00
|
|
|
struct plugin_hook_request *ph_req)
|
2019-01-02 19:23:18 +01:00
|
|
|
{
|
2020-04-15 12:21:34 +02:00
|
|
|
const jsmntok_t *resulttok;
|
2023-10-24 03:41:29 +02:00
|
|
|
const struct hook_instance *h;
|
|
|
|
enum jsonrpc_errcode ecode;
|
|
|
|
|
|
|
|
assert(ph_req->hook_index < tal_count(ph_req->hooks));
|
|
|
|
h = ph_req->hooks[ph_req->hook_index];
|
|
|
|
|
|
|
|
/* destructor NULLs out hooks[], but we get called first at the moment.
|
|
|
|
* We handle either */
|
|
|
|
ecode = 0;
|
|
|
|
json_scan(tmpctx, buffer, toks, "{error:{code:%}}",
|
|
|
|
JSON_SCAN(json_to_jsonrpc_errcode, &ecode));
|
|
|
|
if (ecode == PLUGIN_TERMINATED)
|
|
|
|
h = NULL;
|
|
|
|
|
|
|
|
/* We really only handle plugins dying: other errors are fatal. */
|
|
|
|
if (h) {
|
2024-04-25 23:35:57 +02:00
|
|
|
log_trace(ph_req->ld->log,
|
2023-10-24 03:41:29 +02:00
|
|
|
"Plugin %s returned from %s hook call",
|
|
|
|
h->plugin->shortname, ph_req->hook->name);
|
2020-02-12 19:44:01 +01:00
|
|
|
resulttok = json_get_member(buffer, toks, "result");
|
|
|
|
if (!resulttok)
|
2023-10-24 03:41:29 +02:00
|
|
|
fatal("Plugin %s for %s returned non-result response %.*s",
|
|
|
|
h->plugin->shortname,
|
|
|
|
ph_req->hook->name, toks->end - toks->start,
|
2020-02-12 19:44:01 +01:00
|
|
|
buffer + toks->start);
|
2019-04-16 02:10:07 +02:00
|
|
|
|
2023-10-24 03:41:29 +02:00
|
|
|
if (!ph_req->hook->deserialize_cb(ph_req->cb_arg,
|
|
|
|
buffer, resulttok)) {
|
|
|
|
tal_free(ph_req->cb_arg);
|
2024-04-02 05:03:49 +02:00
|
|
|
cleanup_ph_req(ph_req);
|
|
|
|
return;
|
2020-04-15 12:21:34 +02:00
|
|
|
}
|
2020-02-12 19:44:01 +01:00
|
|
|
} else {
|
2023-10-24 03:41:29 +02:00
|
|
|
log_debug(ph_req->ld->log, "Plugin died from %s hook call",
|
|
|
|
ph_req->hook->name);
|
2020-02-12 19:44:01 +01:00
|
|
|
}
|
2020-02-05 15:21:48 +01:00
|
|
|
|
2023-10-24 03:41:29 +02:00
|
|
|
plugin_hook_call_next(ph_req);
|
2019-01-02 19:23:18 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 13:49:23 +01:00
|
|
|
static void plugin_hook_call_next(struct plugin_hook_request *ph_req)
|
|
|
|
{
|
|
|
|
struct jsonrpc_request *req;
|
|
|
|
const struct plugin_hook *hook = ph_req->hook;
|
2023-10-24 03:41:29 +02:00
|
|
|
struct plugin *plugin;
|
|
|
|
|
|
|
|
/* Find next non-NULL hook: call final if we're done */
|
|
|
|
do {
|
|
|
|
ph_req->hook_index++;
|
|
|
|
if (ph_req->hook_index >= tal_count(ph_req->hooks)) {
|
|
|
|
ph_req->hook->final_cb(ph_req->cb_arg);
|
2024-04-02 05:03:49 +02:00
|
|
|
cleanup_ph_req(ph_req);
|
2023-10-24 03:41:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (ph_req->hooks[ph_req->hook_index] == NULL);
|
2020-02-05 13:49:23 +01:00
|
|
|
|
2023-10-24 03:41:29 +02:00
|
|
|
plugin = ph_req->hooks[ph_req->hook_index]->plugin;
|
2024-04-25 23:35:57 +02:00
|
|
|
log_trace(ph_req->ld->log, "Calling %s hook of plugin %s",
|
2023-10-24 03:41:29 +02:00
|
|
|
ph_req->hook->name, plugin->shortname);
|
2022-09-12 23:19:12 +02:00
|
|
|
req = jsonrpc_request_start(NULL, hook->name, ph_req->cmd_id,
|
2023-10-24 03:41:29 +02:00
|
|
|
plugin->non_numeric_ids,
|
|
|
|
plugin_get_logger(plugin),
|
2020-10-12 07:33:50 +02:00
|
|
|
NULL,
|
2020-02-05 13:49:23 +01:00
|
|
|
plugin_hook_callback, ph_req);
|
|
|
|
|
2023-10-24 03:41:29 +02:00
|
|
|
hook->serialize_payload(ph_req->cb_arg, req->stream, plugin);
|
2020-02-05 13:49:23 +01:00
|
|
|
jsonrpc_request_end(req);
|
2023-10-24 03:41:29 +02:00
|
|
|
plugin_request_send(plugin, req);
|
2020-02-05 13:49:23 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 03:11:59 +02:00
|
|
|
bool plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
|
2022-09-12 23:19:12 +02:00
|
|
|
const char *cmd_id TAKES,
|
2020-04-15 12:20:41 +02:00
|
|
|
tal_t *cb_arg STEALS)
|
2018-12-20 12:00:35 +01:00
|
|
|
{
|
2020-10-30 02:13:37 +01:00
|
|
|
if (tal_count(hook->hooks)) {
|
2019-01-02 19:23:18 +01:00
|
|
|
/* If we have a plugin that has registered for this
|
|
|
|
* hook, serialize and call it */
|
|
|
|
/* FIXME: technically this is a leak, but we don't
|
|
|
|
* currently have a list to store these. We might want
|
|
|
|
* to eventually to inspect in-flight requests. */
|
2023-10-24 03:41:29 +02:00
|
|
|
struct plugin_hook_request *ph_req;
|
|
|
|
|
2020-10-30 02:13:37 +01:00
|
|
|
ph_req = notleak(tal(hook->hooks, struct plugin_hook_request));
|
2019-01-02 19:23:18 +01:00
|
|
|
ph_req->hook = hook;
|
2020-04-15 12:20:41 +02:00
|
|
|
ph_req->cb_arg = tal_steal(ph_req, cb_arg);
|
2019-01-03 17:56:51 +01:00
|
|
|
ph_req->db = ld->wallet->db;
|
2020-02-13 21:01:23 +01:00
|
|
|
ph_req->ld = ld;
|
2023-07-22 09:12:57 +02:00
|
|
|
ph_req->cmd_id = tal_strdup_or_null(ph_req, cmd_id);
|
2023-10-24 03:41:29 +02:00
|
|
|
ph_req->hooks = tal_dup_talarr(ph_req,
|
|
|
|
struct hook_instance *,
|
|
|
|
hook->hooks);
|
|
|
|
/* If hook goes away, NULL out our snapshot */
|
|
|
|
for (size_t i=0; i<tal_count(ph_req->hooks); i++)
|
|
|
|
tal_add_destructor2(ph_req->hooks[i],
|
|
|
|
destroy_hook_in_ph_req, ph_req);
|
|
|
|
ph_req->hook_index = -1;
|
2020-02-05 13:49:23 +01:00
|
|
|
plugin_hook_call_next(ph_req);
|
2020-05-05 03:11:59 +02:00
|
|
|
return false;
|
2019-01-02 19:23:18 +01:00
|
|
|
} else {
|
|
|
|
/* If no plugin has registered for this hook, just
|
|
|
|
* call the callback with a NULL result. Saves us the
|
|
|
|
* roundtrip to the serializer and deserializer. If we
|
|
|
|
* were expecting a default response it should have
|
|
|
|
* been part of the `cb_arg`. */
|
2021-04-07 04:28:05 +02:00
|
|
|
hook->final_cb(cb_arg);
|
2020-05-05 03:11:59 +02:00
|
|
|
return true;
|
2019-01-02 19:23:18 +01:00
|
|
|
}
|
2018-12-20 12:00:35 +01:00
|
|
|
}
|
2019-03-18 03:40:32 +01:00
|
|
|
|
|
|
|
/* We open-code this, because it's just different and special enough to be
|
|
|
|
* annoying, and to make it clear that it's totally synchronous. */
|
|
|
|
|
|
|
|
/* Special synchronous hook for db */
|
2021-04-07 04:28:05 +02:00
|
|
|
static struct plugin_hook db_write_hook = {"db_write", NULL, NULL};
|
2019-03-18 03:40:32 +01:00
|
|
|
AUTODATA(hooks, &db_write_hook);
|
|
|
|
|
2020-11-23 06:05:56 +01:00
|
|
|
/* A `db_write` for one particular plugin hook. */
|
|
|
|
struct db_write_hook_req {
|
|
|
|
struct plugin *plugin;
|
|
|
|
struct plugin_hook_request *ph_req;
|
|
|
|
size_t *num_hooks;
|
|
|
|
};
|
|
|
|
|
2019-03-18 03:40:32 +01:00
|
|
|
static void db_hook_response(const char *buffer, const jsmntok_t *toks,
|
|
|
|
const jsmntok_t *idtok,
|
2020-11-23 06:05:56 +01:00
|
|
|
struct db_write_hook_req *dwh_req)
|
2019-03-18 03:40:32 +01:00
|
|
|
{
|
|
|
|
const jsmntok_t *resulttok;
|
|
|
|
|
|
|
|
resulttok = json_get_member(buffer, toks, "result");
|
|
|
|
if (!resulttok)
|
2020-11-23 06:05:56 +01:00
|
|
|
fatal("Plugin '%s' returned an invalid response to the "
|
2022-06-26 06:46:01 +02:00
|
|
|
"db_write hook: %.*s",
|
|
|
|
dwh_req->plugin->cmd,
|
|
|
|
json_tok_full_len(toks),
|
|
|
|
json_tok_full(buffer, toks));
|
2019-03-18 03:40:32 +01:00
|
|
|
|
2020-01-31 06:40:26 +01:00
|
|
|
/* We expect result: { 'result' : 'continue' }.
|
|
|
|
* Anything else we abort.
|
|
|
|
*/
|
|
|
|
resulttok = json_get_member(buffer, resulttok, "result");
|
|
|
|
if (resulttok) {
|
|
|
|
if (!json_tok_streq(buffer, resulttok, "continue"))
|
2022-06-26 06:46:01 +02:00
|
|
|
fatal("Plugin '%s' returned failed db_write: %.*s.",
|
2020-11-23 06:05:56 +01:00
|
|
|
dwh_req->plugin->cmd,
|
2022-06-26 06:46:01 +02:00
|
|
|
json_tok_full_len(toks),
|
|
|
|
json_tok_full(buffer, toks));
|
2020-01-31 06:40:26 +01:00
|
|
|
} else
|
2020-11-23 06:05:56 +01:00
|
|
|
fatal("Plugin '%s' returned an invalid result to the db_write "
|
2022-06-26 06:46:01 +02:00
|
|
|
"hook: %.*s",
|
2020-11-23 06:05:56 +01:00
|
|
|
dwh_req->plugin->cmd,
|
2022-06-26 06:46:01 +02:00
|
|
|
json_tok_full_len(toks),
|
|
|
|
json_tok_full(buffer, toks));
|
2020-11-23 06:05:56 +01:00
|
|
|
|
|
|
|
assert((*dwh_req->num_hooks) != 0);
|
|
|
|
--(*dwh_req->num_hooks);
|
|
|
|
/* If there are other runners, do not exit yet. */
|
|
|
|
if ((*dwh_req->num_hooks) != 0)
|
|
|
|
return;
|
2019-03-18 03:40:32 +01:00
|
|
|
|
|
|
|
/* We're done, exit exclusive loop. */
|
2022-06-26 06:51:01 +02:00
|
|
|
log_debug(dwh_req->plugin->plugins->ld->log, "io_break: %s", __func__);
|
2020-11-23 06:05:56 +01:00
|
|
|
io_break(dwh_req->ph_req);
|
2019-03-18 03:40:32 +01:00
|
|
|
}
|
|
|
|
|
2019-12-18 11:06:09 +01:00
|
|
|
void plugin_hook_db_sync(struct db *db)
|
2019-03-18 03:40:32 +01:00
|
|
|
{
|
|
|
|
const struct plugin_hook *hook = &db_write_hook;
|
|
|
|
struct jsonrpc_request *req;
|
|
|
|
struct plugin_hook_request *ph_req;
|
|
|
|
void *ret;
|
2023-10-24 03:41:28 +02:00
|
|
|
struct plugin **plugin_arr;
|
|
|
|
struct plugins *plugins;
|
2020-11-23 04:46:13 +01:00
|
|
|
size_t i;
|
2020-11-23 06:05:56 +01:00
|
|
|
size_t num_hooks;
|
2019-03-18 03:40:32 +01:00
|
|
|
|
2019-12-18 11:06:09 +01:00
|
|
|
const char **changes = db_changes(db);
|
2020-11-23 06:05:56 +01:00
|
|
|
num_hooks = tal_count(hook->hooks);
|
|
|
|
if (num_hooks == 0)
|
2019-03-18 03:40:32 +01:00
|
|
|
return;
|
|
|
|
|
2023-10-24 03:41:28 +02:00
|
|
|
plugin_arr = notleak(tal_arr(NULL, struct plugin *,
|
2020-11-23 06:05:56 +01:00
|
|
|
num_hooks));
|
|
|
|
for (i = 0; i < num_hooks; ++i)
|
2023-10-24 03:41:28 +02:00
|
|
|
plugin_arr[i] = hook->hooks[i]->plugin;
|
2020-11-23 04:46:13 +01:00
|
|
|
|
2023-10-24 03:41:28 +02:00
|
|
|
plugins = plugin_arr[0]->plugins;
|
2020-10-30 02:13:37 +01:00
|
|
|
ph_req = notleak(tal(hook->hooks, struct plugin_hook_request));
|
2019-03-18 03:40:32 +01:00
|
|
|
ph_req->hook = hook;
|
|
|
|
ph_req->db = db;
|
2020-11-23 06:05:56 +01:00
|
|
|
ph_req->cb_arg = &num_hooks;
|
|
|
|
|
|
|
|
for (i = 0; i < num_hooks; ++i) {
|
|
|
|
/* Create an object for this plugin. */
|
|
|
|
struct db_write_hook_req *dwh_req;
|
|
|
|
dwh_req = tal(ph_req, struct db_write_hook_req);
|
2023-10-24 03:41:28 +02:00
|
|
|
dwh_req->plugin = plugin_arr[i];
|
2020-11-23 06:05:56 +01:00
|
|
|
dwh_req->ph_req = ph_req;
|
|
|
|
dwh_req->num_hooks = &num_hooks;
|
|
|
|
|
2022-09-12 23:19:11 +02:00
|
|
|
/* FIXME: id_prefix from caller? */
|
2020-11-23 06:05:56 +01:00
|
|
|
/* FIXME: do IO logging for this! */
|
2022-11-21 02:48:32 +01:00
|
|
|
req = jsonrpc_request_start(NULL, hook->name, NULL,
|
|
|
|
dwh_req->plugin->non_numeric_ids,
|
|
|
|
NULL, NULL,
|
2020-11-23 06:05:56 +01:00
|
|
|
db_hook_response,
|
|
|
|
dwh_req);
|
|
|
|
|
|
|
|
json_add_num(req->stream, "data_version",
|
|
|
|
db_data_version_get(db));
|
|
|
|
|
|
|
|
json_array_start(req->stream, "writes");
|
2022-07-25 09:00:09 +02:00
|
|
|
for (size_t j = 0; j < tal_count(changes); j++)
|
|
|
|
json_add_string(req->stream, NULL, changes[j]);
|
2020-11-23 06:05:56 +01:00
|
|
|
json_array_end(req->stream);
|
|
|
|
jsonrpc_request_end(req);
|
|
|
|
|
2023-10-24 03:41:28 +02:00
|
|
|
plugin_request_send(plugin_arr[i], req);
|
2020-11-23 06:05:56 +01:00
|
|
|
}
|
2019-03-18 03:40:32 +01:00
|
|
|
|
2023-10-24 03:41:28 +02:00
|
|
|
ret = plugins_exclusive_loop(plugin_arr);
|
|
|
|
|
2019-03-18 03:40:32 +01:00
|
|
|
/* We can be called on way out of an io_loop, which is already breaking.
|
|
|
|
* That will make this immediately return; save the break value and call
|
|
|
|
* again, then hand it onwards. */
|
|
|
|
if (ret != ph_req) {
|
2023-10-24 03:41:28 +02:00
|
|
|
void *ret2 = plugins_exclusive_loop(plugin_arr);
|
2019-03-18 03:40:32 +01:00
|
|
|
assert(ret2 == ph_req);
|
2023-10-24 03:41:28 +02:00
|
|
|
log_debug(plugins->ld->log, "io_break: %s", __func__);
|
2019-03-18 03:40:32 +01:00
|
|
|
io_break(ret);
|
|
|
|
}
|
2020-11-23 06:05:56 +01:00
|
|
|
assert(num_hooks == 0);
|
2023-10-24 03:41:28 +02:00
|
|
|
tal_free(plugin_arr);
|
2020-11-23 06:05:56 +01:00
|
|
|
tal_free(ph_req);
|
2019-03-18 03:40:32 +01:00
|
|
|
}
|
2020-10-30 02:13:37 +01:00
|
|
|
|
|
|
|
static void add_deps(const char ***arr,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *arrtok)
|
|
|
|
{
|
|
|
|
const jsmntok_t *t;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!arrtok)
|
|
|
|
return;
|
|
|
|
|
|
|
|
json_for_each_arr(i, t, arrtok)
|
|
|
|
tal_arr_expand(arr, json_strdup(*arr, buffer, t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void plugin_hook_add_deps(struct plugin_hook *hook,
|
|
|
|
struct plugin *plugin,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *before,
|
|
|
|
const jsmntok_t *after)
|
|
|
|
{
|
|
|
|
struct hook_instance *h = NULL;
|
|
|
|
|
|
|
|
/* We just added this, it must exist */
|
|
|
|
for (size_t i = 0; i < tal_count(hook->hooks); i++) {
|
|
|
|
if (hook->hooks[i]->plugin == plugin) {
|
|
|
|
h = hook->hooks[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(h);
|
|
|
|
|
|
|
|
add_deps(&h->before, buffer, before);
|
|
|
|
add_deps(&h->after, buffer, after);
|
|
|
|
}
|
2020-10-30 02:13:42 +01:00
|
|
|
|
|
|
|
struct hook_node {
|
2020-11-03 00:20:52 +01:00
|
|
|
/* Is this copied into the ordered array yet? */
|
|
|
|
bool finished;
|
2020-10-30 02:13:42 +01:00
|
|
|
struct hook_instance *hook;
|
|
|
|
size_t num_incoming;
|
|
|
|
struct hook_node **outgoing;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct hook_node *find_hook(struct hook_node *graph, const char *name)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(graph); i++) {
|
|
|
|
if (plugin_paths_match(graph[i].hook->plugin->cmd, name))
|
|
|
|
return graph + i;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:20:52 +01:00
|
|
|
/* Sometimes naive is best. */
|
|
|
|
static struct hook_node *get_best_candidate(struct hook_node *graph)
|
|
|
|
{
|
|
|
|
struct hook_node *best = NULL;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < tal_count(graph); i++) {
|
|
|
|
if (graph[i].finished)
|
|
|
|
continue;
|
|
|
|
if (graph[i].num_incoming != 0)
|
|
|
|
continue;
|
|
|
|
if (!best
|
|
|
|
|| best->hook->plugin->index > graph[i].hook->plugin->index)
|
|
|
|
best = &graph[i];
|
|
|
|
}
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2020-11-02 03:36:29 +01:00
|
|
|
static struct plugin **plugin_hook_make_ordered(const tal_t *ctx,
|
|
|
|
struct plugin_hook *hook)
|
2020-10-30 02:13:42 +01:00
|
|
|
{
|
2020-11-03 00:20:52 +01:00
|
|
|
struct hook_node *graph, *n;
|
|
|
|
struct hook_instance **done;
|
2020-10-30 02:13:42 +01:00
|
|
|
|
|
|
|
/* Populate graph nodes */
|
|
|
|
graph = tal_arr(tmpctx, struct hook_node, tal_count(hook->hooks));
|
|
|
|
for (size_t i = 0; i < tal_count(graph); i++) {
|
2020-11-03 00:20:52 +01:00
|
|
|
graph[i].finished = false;
|
2020-10-30 02:13:42 +01:00
|
|
|
graph[i].hook = hook->hooks[i];
|
|
|
|
graph[i].num_incoming = 0;
|
|
|
|
graph[i].outgoing = tal_arr(graph, struct hook_node *, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add edges. */
|
|
|
|
for (size_t i = 0; i < tal_count(graph); i++) {
|
|
|
|
for (size_t j = 0; j < tal_count(graph[i].hook->before); j++) {
|
2022-07-25 09:00:09 +02:00
|
|
|
n = find_hook(graph, graph[i].hook->before[j]);
|
2020-10-30 02:13:42 +01:00
|
|
|
if (!n) {
|
|
|
|
/* This is useful for typos! */
|
|
|
|
log_debug(graph[i].hook->plugin->log,
|
|
|
|
"hook %s before unknown plugin %s",
|
|
|
|
hook->name,
|
|
|
|
graph[i].hook->before[j]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tal_arr_expand(&graph[i].outgoing, n);
|
|
|
|
n->num_incoming++;
|
|
|
|
}
|
|
|
|
for (size_t j = 0; j < tal_count(graph[i].hook->after); j++) {
|
2022-07-25 09:00:09 +02:00
|
|
|
n = find_hook(graph, graph[i].hook->after[j]);
|
2020-10-30 02:13:42 +01:00
|
|
|
if (!n) {
|
|
|
|
/* This is useful for typos! */
|
|
|
|
log_debug(graph[i].hook->plugin->log,
|
|
|
|
"hook %s after unknown plugin %s",
|
|
|
|
hook->name,
|
|
|
|
graph[i].hook->after[j]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tal_arr_expand(&n->outgoing, &graph[i]);
|
|
|
|
graph[i].num_incoming++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:20:52 +01:00
|
|
|
done = tal_arr(tmpctx, struct hook_instance *, 0);
|
|
|
|
while ((n = get_best_candidate(graph)) != NULL) {
|
|
|
|
tal_arr_expand(&done, n->hook);
|
|
|
|
n->finished = true;
|
|
|
|
for (size_t i = 0; i < tal_count(n->outgoing); i++)
|
|
|
|
n->outgoing[i]->num_incoming--;
|
2020-10-30 02:13:42 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:20:52 +01:00
|
|
|
if (tal_count(done) != tal_count(hook->hooks)) {
|
|
|
|
struct plugin **ret = tal_arr(ctx, struct plugin *, 0);
|
|
|
|
for (size_t i = 0; i < tal_count(graph); i++) {
|
|
|
|
if (!graph[i].finished)
|
|
|
|
tal_arr_expand(&ret, graph[i].hook->plugin);
|
2020-10-30 02:13:42 +01:00
|
|
|
}
|
2020-11-02 03:36:29 +01:00
|
|
|
return ret;
|
2020-11-03 00:20:52 +01:00
|
|
|
}
|
2020-11-02 03:36:29 +01:00
|
|
|
|
2020-11-03 00:20:52 +01:00
|
|
|
/* Success! Copy ordered hooks back. */
|
2023-01-12 02:13:14 +01:00
|
|
|
if (hook->hooks)
|
|
|
|
memcpy(hook->hooks, done, tal_bytelen(hook->hooks));
|
2020-11-03 00:20:52 +01:00
|
|
|
return NULL;
|
2020-11-02 03:36:29 +01:00
|
|
|
}
|
2020-10-30 02:13:42 +01:00
|
|
|
|
2020-11-02 03:36:29 +01:00
|
|
|
/* Plugins could fail due to multiple hooks, but only add once. */
|
|
|
|
static void append_plugin_once(struct plugin ***ret, struct plugin *p)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(*ret); i++) {
|
|
|
|
if ((*ret)[i] == p)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tal_arr_expand(ret, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct plugin **plugin_hooks_make_ordered(const tal_t *ctx)
|
|
|
|
{
|
|
|
|
size_t num_hooks;
|
|
|
|
struct plugin_hook **hooks = get_hooks(&num_hooks);
|
|
|
|
struct plugin **ret = tal_arr(ctx, struct plugin *, 0);
|
|
|
|
|
|
|
|
for (size_t i=0; i<num_hooks; i++) {
|
|
|
|
struct plugin **these = plugin_hook_make_ordered(ctx, hooks[i]);
|
|
|
|
for (size_t j = 0; j < tal_count(these); j++)
|
|
|
|
append_plugin_once(&ret, these[j]);
|
2020-10-30 02:13:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|