2021-12-04 21:53:56 +10:30
|
|
|
#include "config.h"
|
2020-01-31 18:36:26 +01:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2020-05-16 10:59:05 +09:30
|
|
|
#include <ccan/tal/str/str.h>
|
2022-07-04 13:19:38 +09:30
|
|
|
#include <common/json_param.h>
|
|
|
|
#include <common/json_stream.h>
|
2021-09-07 13:36:06 +09:30
|
|
|
#include <common/memleak.h>
|
2020-01-31 18:36:26 +01:00
|
|
|
#include <plugins/libplugin.h>
|
|
|
|
|
2023-06-06 10:08:53 +09:30
|
|
|
static char *somearg;
|
2021-01-13 13:30:24 +10:30
|
|
|
static bool self_disable = false;
|
2021-09-03 19:46:21 +09:30
|
|
|
static bool dont_shutdown = false;
|
2020-01-31 18:36:26 +01:00
|
|
|
|
2022-12-12 14:44:15 +10:30
|
|
|
static struct command_result *get_ds_done(struct command *cmd,
|
|
|
|
const char *val,
|
|
|
|
char *arg)
|
|
|
|
{
|
|
|
|
if (!val)
|
|
|
|
val = "NOT FOUND";
|
|
|
|
return command_success(cmd, json_out_obj(cmd, arg, val));
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *get_ds_bin_done(struct command *cmd,
|
|
|
|
const u8 *val,
|
|
|
|
char *arg)
|
|
|
|
{
|
|
|
|
plugin_log(cmd->plugin, LOG_INFORM, "get_ds_bin_done: %s",
|
|
|
|
val ? tal_hex(tmpctx, val) : "NOT FOUND");
|
|
|
|
|
|
|
|
return jsonrpc_get_datastore_string(cmd->plugin, cmd,
|
|
|
|
"test_libplugin/name",
|
|
|
|
get_ds_done, arg);
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:36:26 +01:00
|
|
|
static struct command_result *json_helloworld(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
if (!param(cmd, buf, params,
|
|
|
|
p_opt("name", param_string, &name),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
2020-10-12 16:03:50 +10:30
|
|
|
plugin_notify_message(cmd, LOG_INFORM, "Notification from %s", "json_helloworld");
|
2022-12-12 14:44:15 +10:30
|
|
|
|
2020-01-31 18:36:26 +01:00
|
|
|
if (!name)
|
2022-12-12 14:44:15 +10:30
|
|
|
return jsonrpc_get_datastore_binary(cmd->plugin, cmd,
|
|
|
|
"test_libplugin/name",
|
|
|
|
get_ds_bin_done,
|
|
|
|
"hello");
|
2020-01-31 18:36:26 +01:00
|
|
|
|
2021-05-26 15:12:01 +09:30
|
|
|
return command_success(cmd, json_out_obj(cmd, "hello", name));
|
2020-01-31 18:36:26 +01:00
|
|
|
}
|
|
|
|
|
2020-02-01 16:00:32 +01:00
|
|
|
static struct command_result *
|
|
|
|
json_peer_connected(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
const jsmntok_t *peertok, *idtok;
|
|
|
|
struct json_stream *response;
|
|
|
|
|
|
|
|
peertok = json_get_member(buf, params, "peer");
|
|
|
|
assert(peertok);
|
|
|
|
idtok = json_get_member(buf, peertok, "id");
|
|
|
|
assert(idtok);
|
|
|
|
plugin_log(cmd->plugin, LOG_INFORM, "%s peer_connected",
|
|
|
|
json_strdup(tmpctx, buf, idtok));
|
|
|
|
|
|
|
|
response = jsonrpc_stream_success(cmd);
|
|
|
|
json_add_string(response, "result", "continue");
|
|
|
|
|
|
|
|
return command_finished(cmd, response);
|
|
|
|
}
|
|
|
|
|
plugins/libplugin.c: Allow freeing notification `struct command *`.
We always allocate a new `struct command` when we get a full JSON
object from stdin:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1229-L1233
If it happens to be a notification, we pass the `struct command` to
the handler, and not free it ourselves:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1270-L1275
There are only nine points in `plugins/libplugin.c` where we `tal_free`
anything, and only one of them frees a `struct command`:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L224-L234
The above function `command_complete` is not appropriate for
notification handlers; the above function sends out a response
to our stdout, which a notification handler should not do.
However, as-is, it does mean that notification handling leaks
`struct command` objects, which can be problematic if we ever
have future built-in plugins which are significantly more
dependent on notifications.
This commit changes notification handlers to return
`struct command_result *`, because possibly in the future
notification handlers may want to perform `send_outreq`, so we
might as well use our standard convention for callbacks, and
to encourage future developers to check how to properly
terminate notification handlers (and free up the
`struct command`).
We also now provide a `notification_handled` function which a
notification handler must eventually call, as well as a
`notification_handler_pending` which is just a snowclone of
`command_still_pending`.
2021-10-01 07:59:40 +08:00
|
|
|
static struct command_result *json_connected(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params)
|
2020-02-01 16:00:32 +01:00
|
|
|
{
|
2023-07-07 11:42:26 +09:30
|
|
|
const jsmntok_t *connecttok, *idtok;
|
|
|
|
|
|
|
|
connecttok = json_get_member(buf, params, "connect");
|
|
|
|
assert(connecttok);
|
|
|
|
idtok = json_get_member(buf, connecttok, "id");
|
2020-02-01 16:00:32 +01:00
|
|
|
assert(idtok);
|
|
|
|
plugin_log(cmd->plugin, LOG_INFORM, "%s connected",
|
|
|
|
json_strdup(tmpctx, buf, idtok));
|
plugins/libplugin.c: Allow freeing notification `struct command *`.
We always allocate a new `struct command` when we get a full JSON
object from stdin:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1229-L1233
If it happens to be a notification, we pass the `struct command` to
the handler, and not free it ourselves:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1270-L1275
There are only nine points in `plugins/libplugin.c` where we `tal_free`
anything, and only one of them frees a `struct command`:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L224-L234
The above function `command_complete` is not appropriate for
notification handlers; the above function sends out a response
to our stdout, which a notification handler should not do.
However, as-is, it does mean that notification handling leaks
`struct command` objects, which can be problematic if we ever
have future built-in plugins which are significantly more
dependent on notifications.
This commit changes notification handlers to return
`struct command_result *`, because possibly in the future
notification handlers may want to perform `send_outreq`, so we
might as well use our standard convention for callbacks, and
to encourage future developers to check how to properly
terminate notification handlers (and free up the
`struct command`).
We also now provide a `notification_handled` function which a
notification handler must eventually call, as well as a
`notification_handler_pending` which is just a snowclone of
`command_still_pending`.
2021-10-01 07:59:40 +08:00
|
|
|
return notification_handled(cmd);
|
2020-02-01 16:00:32 +01:00
|
|
|
}
|
|
|
|
|
plugins/libplugin.c: Allow freeing notification `struct command *`.
We always allocate a new `struct command` when we get a full JSON
object from stdin:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1229-L1233
If it happens to be a notification, we pass the `struct command` to
the handler, and not free it ourselves:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1270-L1275
There are only nine points in `plugins/libplugin.c` where we `tal_free`
anything, and only one of them frees a `struct command`:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L224-L234
The above function `command_complete` is not appropriate for
notification handlers; the above function sends out a response
to our stdout, which a notification handler should not do.
However, as-is, it does mean that notification handling leaks
`struct command` objects, which can be problematic if we ever
have future built-in plugins which are significantly more
dependent on notifications.
This commit changes notification handlers to return
`struct command_result *`, because possibly in the future
notification handlers may want to perform `send_outreq`, so we
might as well use our standard convention for callbacks, and
to encourage future developers to check how to properly
terminate notification handlers (and free up the
`struct command`).
We also now provide a `notification_handled` function which a
notification handler must eventually call, as well as a
`notification_handler_pending` which is just a snowclone of
`command_still_pending`.
2021-10-01 07:59:40 +08:00
|
|
|
static struct command_result *json_shutdown(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params)
|
2021-09-03 19:46:21 +09:30
|
|
|
{
|
|
|
|
plugin_log(cmd->plugin, LOG_DBG, "shutdown called");
|
|
|
|
|
|
|
|
if (dont_shutdown)
|
plugins/libplugin.c: Allow freeing notification `struct command *`.
We always allocate a new `struct command` when we get a full JSON
object from stdin:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1229-L1233
If it happens to be a notification, we pass the `struct command` to
the handler, and not free it ourselves:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L1270-L1275
There are only nine points in `plugins/libplugin.c` where we `tal_free`
anything, and only one of them frees a `struct command`:
https://github.com/ElementsProject/lightning/blob/b2df01dc73ea7a51ae2a495281bf4d775eafa0a4/plugins/libplugin.c#L224-L234
The above function `command_complete` is not appropriate for
notification handlers; the above function sends out a response
to our stdout, which a notification handler should not do.
However, as-is, it does mean that notification handling leaks
`struct command` objects, which can be problematic if we ever
have future built-in plugins which are significantly more
dependent on notifications.
This commit changes notification handlers to return
`struct command_result *`, because possibly in the future
notification handlers may want to perform `send_outreq`, so we
might as well use our standard convention for callbacks, and
to encourage future developers to check how to properly
terminate notification handlers (and free up the
`struct command`).
We also now provide a `notification_handled` function which a
notification handler must eventually call, as well as a
`notification_handler_pending` which is just a snowclone of
`command_still_pending`.
2021-10-01 07:59:40 +08:00
|
|
|
return notification_handled(cmd);
|
2021-09-03 19:46:21 +09:30
|
|
|
|
|
|
|
plugin_exit(cmd->plugin, 0);
|
|
|
|
}
|
|
|
|
|
2023-07-11 07:51:36 +09:30
|
|
|
static struct command_result *json_all_notifs(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
plugin_log(cmd->plugin, LOG_DBG, "all: %s: %.*s",
|
|
|
|
cmd->methodname,
|
|
|
|
json_tok_full_len(params),
|
|
|
|
json_tok_full(buf, params));
|
|
|
|
return notification_handled(cmd);
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:25:49 +01:00
|
|
|
static struct command_result *testrpc_cb(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params,
|
|
|
|
void *cb_arg UNUSED)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
const jsmntok_t *t;
|
|
|
|
struct json_stream *response;
|
|
|
|
|
|
|
|
response = jsonrpc_stream_success(cmd);
|
|
|
|
json_for_each_obj(i, t, params)
|
|
|
|
json_add_tok(response, json_strdup(tmpctx, buf, t), t+1, buf);
|
|
|
|
|
|
|
|
return command_finished(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *json_testrpc(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct out_req *req;
|
|
|
|
|
|
|
|
if (!param(cmd, buf, params, NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
req = jsonrpc_request_start(cmd->plugin, cmd, "getinfo", testrpc_cb,
|
|
|
|
testrpc_cb, NULL);
|
|
|
|
return send_outreq(cmd->plugin, req);
|
|
|
|
}
|
|
|
|
|
2021-01-13 13:30:24 +10:30
|
|
|
static const char *init(struct plugin *p,
|
|
|
|
const char *buf UNUSED,
|
|
|
|
const jsmntok_t *config UNUSED)
|
2020-01-31 18:36:26 +01:00
|
|
|
{
|
2023-02-10 14:53:53 +10:30
|
|
|
const char *name, *err_str, *err_hex;
|
2022-12-12 14:44:15 +10:30
|
|
|
const u8 *binname;
|
|
|
|
|
2020-01-31 18:36:26 +01:00
|
|
|
plugin_log(p, LOG_DBG, "test_libplugin initialised!");
|
2022-12-12 14:44:15 +10:30
|
|
|
if (somearg)
|
|
|
|
plugin_log(p, LOG_DBG, "somearg = %s", somearg);
|
|
|
|
somearg = tal_free(somearg);
|
2021-01-13 13:30:24 +10:30
|
|
|
|
|
|
|
if (self_disable)
|
|
|
|
return "Disabled via selfdisable option";
|
2021-09-07 13:36:06 +09:30
|
|
|
|
2022-12-12 14:44:15 +10:30
|
|
|
/* Test rpc_scan_datastore funcs */
|
2023-02-10 14:53:53 +10:30
|
|
|
err_str = rpc_scan_datastore_str(tmpctx, p, "test_libplugin/name",
|
|
|
|
JSON_SCAN_TAL(tmpctx, json_strdup,
|
|
|
|
&name));
|
|
|
|
if (err_str)
|
2022-12-12 14:44:15 +10:30
|
|
|
name = NULL;
|
2023-02-10 14:53:53 +10:30
|
|
|
err_hex = rpc_scan_datastore_hex(tmpctx, p, "test_libplugin/name",
|
|
|
|
JSON_SCAN_TAL(tmpctx, json_tok_bin_from_hex,
|
|
|
|
&binname));
|
|
|
|
if (err_hex)
|
2022-12-12 14:44:15 +10:30
|
|
|
binname = NULL;
|
|
|
|
|
|
|
|
plugin_log(p, LOG_INFORM, "String name from datastore: %s",
|
2023-02-10 14:53:53 +10:30
|
|
|
name ? name : err_str);
|
2022-12-12 14:44:15 +10:30
|
|
|
plugin_log(p, LOG_INFORM, "Hex name from datastore: %s",
|
2023-02-10 14:53:53 +10:30
|
|
|
binname ? tal_hex(tmpctx, binname) : err_hex);
|
2022-12-12 14:44:15 +10:30
|
|
|
|
|
|
|
return NULL;
|
2020-01-31 18:36:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct plugin_command commands[] = { {
|
|
|
|
"helloworld",
|
|
|
|
"utils",
|
|
|
|
"Say hello to the world.",
|
|
|
|
"Returns 'hello world' by default, 'hello {name}' if the name"
|
|
|
|
" option was set, and 'hello {name}' if the name parameter "
|
|
|
|
"was passed (takes over the option)",
|
|
|
|
json_helloworld,
|
2020-02-01 18:25:49 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"testrpc",
|
|
|
|
"utils",
|
|
|
|
"Makes a simple getinfo call, to test rpc socket.",
|
|
|
|
"",
|
|
|
|
json_testrpc,
|
2020-08-06 10:00:51 +09:30
|
|
|
},
|
|
|
|
{
|
|
|
|
"testrpc-deprecated",
|
|
|
|
"utils",
|
|
|
|
"Makes a simple getinfo call, to test rpc socket.",
|
|
|
|
"",
|
|
|
|
json_testrpc,
|
|
|
|
true,
|
2020-01-31 18:36:26 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-30 11:43:42 +10:30
|
|
|
static const char *before[] = { "dummy", NULL };
|
|
|
|
static const char *after[] = { "dummy", NULL };
|
|
|
|
|
2020-02-01 16:00:32 +01:00
|
|
|
static const struct plugin_hook hooks[] = { {
|
|
|
|
"peer_connected",
|
|
|
|
json_peer_connected,
|
2020-10-30 11:43:42 +10:30
|
|
|
before,
|
|
|
|
after
|
2020-02-01 16:00:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct plugin_notification notifs[] = { {
|
|
|
|
"connect",
|
|
|
|
json_connected,
|
2021-09-03 19:46:21 +09:30
|
|
|
}, {
|
|
|
|
"shutdown",
|
|
|
|
json_shutdown
|
2023-07-11 07:51:36 +09:30
|
|
|
}, {
|
|
|
|
"*",
|
|
|
|
json_all_notifs
|
2020-02-01 16:00:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-31 18:36:26 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
setup_locale();
|
2020-07-20 14:39:32 +02:00
|
|
|
plugin_main(argv, init, PLUGIN_RESTARTABLE, true, NULL,
|
|
|
|
commands, ARRAY_SIZE(commands),
|
2020-02-01 16:00:32 +01:00
|
|
|
notifs, ARRAY_SIZE(notifs), hooks, ARRAY_SIZE(hooks),
|
2021-04-28 17:28:27 +02:00
|
|
|
NULL, 0, /* Notification topics we publish */
|
2022-12-12 14:44:15 +10:30
|
|
|
plugin_option("somearg",
|
2020-01-31 18:36:26 +01:00
|
|
|
"string",
|
2022-12-12 14:44:15 +10:30
|
|
|
"Argument to print at init.",
|
|
|
|
charp_option, &somearg),
|
|
|
|
plugin_option_deprecated("somearg-deprecated",
|
2020-08-06 10:00:51 +09:30
|
|
|
"string",
|
2022-12-12 14:44:15 +10:30
|
|
|
"Deprecated arg for init.",
|
|
|
|
charp_option, &somearg),
|
2021-01-13 13:30:24 +10:30
|
|
|
plugin_option("selfdisable",
|
|
|
|
"flag",
|
|
|
|
"Whether to disable.",
|
|
|
|
flag_option, &self_disable),
|
2021-09-03 19:46:21 +09:30
|
|
|
plugin_option("dont_shutdown",
|
|
|
|
"flag",
|
|
|
|
"Whether to timeout when asked to shutdown.",
|
|
|
|
flag_option, &dont_shutdown),
|
2020-01-31 18:36:26 +01:00
|
|
|
NULL);
|
|
|
|
}
|