Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libplugin: use json stream helpers and add sanity tests #3480

Merged
merged 10 commits into from
Feb 9, 2020
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ include doc/Makefile
include devtools/Makefile
include tools/Makefile
include plugins/Makefile
include tests/plugins/Makefile

# Git doesn't maintain timestamps, so we only regen if git says we should.
CHANGED_FROM_GIT = [ x"`git log $@ | head -n1`" != x"`git log $< | head -n1`" -o x"`git diff $<`" != x"" ]
Expand Down
17 changes: 17 additions & 0 deletions ccan/ccan/opt/opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ void _opt_register(const char *names, enum opt_type type,
add_opt(&opt);
}

bool opt_unregister(const char *names)
{
int found = -1, i;

for (i = 0; i < opt_count; i++) {
if (opt_table[i].type == OPT_SUBTABLE)
continue;
if (strcmp(opt_table[i].names, names) == 0)
found = i;
}
if (found == -1)
return false;
opt_count--;
memmove(&opt_table[found], &opt_table[found+1], opt_count - found);
return true;
}

void opt_register_table(const struct opt_table entry[], const char *desc)
{
unsigned int i, start = opt_count;
Expand Down
9 changes: 9 additions & 0 deletions ccan/ccan/opt/opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ void opt_register_table(const struct opt_table *table, const char *desc);
_opt_register((names), OPT_CB_ARG((cb), OPT_EARLY, (show),(arg)), \
(arg), (desc))

/**
* opt_unregister - unregister an option.
* @names: the names it was registered with.
*
* This undoes opt_register[_early]_[no]arg. Returns true if the option was
* found, otherwise false.
*/
bool opt_unregister(const char *names);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, CCAN updates should be done via "make update-ccan" (optional: CCAN_NEW="". Assumes that ccan you want is in ../ccan.

Easy to fix this post, however.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it this way then withdrawn it because of the huge diff, will fix!


/**
* opt_parse - parse arguments.
* @argc: pointer to argc
Expand Down
11 changes: 6 additions & 5 deletions lightningd/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct plugin *plugin_register(struct plugins *plugins, const char* path TAKES)
p->js_arr = tal_arr(p, struct json_stream *, 0);
p->used = 0;
p->subscriptions = NULL;
p->dynamic = false;

p->log = new_log(p, plugins->log_book, NULL, "plugin-%s",
path_basename(tmpctx, p->cmd));
Expand Down Expand Up @@ -486,7 +487,7 @@ static bool plugin_opt_add(struct plugin *plugin, const char *buffer,
popt = tal(plugin, struct plugin_opt);
popt->value = talz(popt, struct plugin_opt_value);

popt->name = tal_fmt(plugin, "--%.*s", nametok->end - nametok->start,
popt->name = tal_fmt(popt, "--%.*s", nametok->end - nametok->start,
buffer + nametok->start);
if (json_tok_streq(buffer, typetok, "string")) {
popt->type = "string";
Expand Down Expand Up @@ -810,22 +811,22 @@ static void plugin_manifest_timeout(struct plugin *plugin)
fatal("Can't recover from plugin failure, terminating.");
}


bool plugin_parse_getmanifest_response(const char *buffer,
const jsmntok_t *toks,
const jsmntok_t *idtok,
struct plugin *plugin)
{
const jsmntok_t *resulttok, *dynamictok;
bool dynamic_plugin;

resulttok = json_get_member(buffer, toks, "result");
if (!resulttok || resulttok->type != JSMN_OBJECT)
return false;

dynamictok = json_get_member(buffer, resulttok, "dynamic");
if (dynamictok && json_to_bool(buffer, dynamictok, &dynamic_plugin))
plugin->dynamic = dynamic_plugin;
if (dynamictok && !json_to_bool(buffer, dynamictok, &plugin->dynamic))
plugin_kill(plugin, "Bad 'dynamic' field ('%.*s')",
json_tok_full_len(dynamictok),
json_tok_full(buffer, dynamictok));

if (!plugin_opts_add(plugin, buffer, resulttok) ||
!plugin_rpcmethods_add(plugin, buffer, resulttok) ||
Expand Down
19 changes: 15 additions & 4 deletions lightningd/plugin_control.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <ccan/opt/opt.h>
#include <lightningd/options.h>
#include <lightningd/plugin_control.h>
#include <lightningd/plugin_hook.h>
Expand Down Expand Up @@ -115,7 +116,7 @@ static struct command_result *plugin_start(struct dynamic_plugin *dp)
struct jsonrpc_request *req;
struct plugin *p = dp->plugin;

p->dynamic = true;
p->dynamic = false;
p_cmd = tal_arrz(NULL, char *, 2);
p_cmd[0] = p->cmd;
/* In case the plugin create files, this is a better default. */
Expand Down Expand Up @@ -196,6 +197,18 @@ plugin_dynamic_startdir(struct command *cmd, const char *dir_path)
return command_still_pending(cmd);
}

static void clear_plugin(struct plugin *p, const char *name)
{
struct plugin_opt *opt;

list_for_each(&p->plugin_opts, opt, list)
if (!opt_unregister(opt->name))
fatal("Could not unregister %s from plugin %s",
opt->name, name);
plugin_kill(p, "%s stopped by lightningd via RPC", name);
tal_free(p);
}

static struct command_result *
plugin_dynamic_stop(struct command *cmd, const char *plugin_name)
{
Expand All @@ -209,9 +222,7 @@ plugin_dynamic_stop(struct command *cmd, const char *plugin_name)
"%s cannot be managed when "
"lightningd is up",
plugin_name);
plugin_hook_unregister_all(p);
plugin_kill(p, "%s stopped by lightningd via RPC", plugin_name);
tal_free(p);
clear_plugin(p, plugin_name);
response = json_stream_success(cmd);
if (deprecated_apis)
json_add_string(response, "",
Expand Down
13 changes: 5 additions & 8 deletions plugins/autoclean.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ static struct command_result *ignore(struct command *timer,

static struct command_result *do_clean(struct plugin *p)
{
struct json_out *params = json_out_new(NULL);
json_out_start(params, NULL, '{');
json_out_add(params, "maxexpirytime", false, "%"PRIu64,
/* FIXME: delexpiredinvoice should be in our plugin too! */
struct out_req *req = jsonrpc_request_start(p, NULL, "delexpiredinvoice",
ignore, ignore, p);
json_add_u64(req->js, "maxexpirytime",
time_now().ts.tv_sec - expired_by);
json_out_end(params, '}');
json_out_finished(params);

/* FIXME: delexpiredinvoice should be in our plugin too! */
return send_outreq(p, NULL, "delexpiredinvoice", ignore, ignore, p,
take(params));
return send_outreq(p, req);
}

static struct command_result *json_autocleaninvoice(struct command *cmd,
Expand Down
Loading