Skip to content

Commit 203dbaa

Browse files
committed
lightningd: wean pligun_log_handle/plugin_notify_handle/plugin_response_handle off plugin->buffer.
Hand buffer in as a parameter to reduce churn in the next patch. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent eb0d042 commit 203dbaa

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

lightningd/plugin.c

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -480,16 +480,18 @@ static void plugin_send(struct plugin *plugin, struct json_stream *stream)
480480

481481
/* Returns the error string, or NULL */
482482
static const char *plugin_log_handle(struct plugin *plugin,
483+
const char *buffer,
483484
const jsmntok_t *paramstok)
484485
WARN_UNUSED_RESULT;
485486
static const char *plugin_log_handle(struct plugin *plugin,
487+
const char *buffer,
486488
const jsmntok_t *paramstok)
487489
{
488490
const jsmntok_t *msgtok, *leveltok;
489491
enum log_level level;
490492
bool call_notifier;
491-
msgtok = json_get_member(plugin->buffer, paramstok, "message");
492-
leveltok = json_get_member(plugin->buffer, paramstok, "level");
493+
msgtok = json_get_member(buffer, paramstok, "message");
494+
leveltok = json_get_member(buffer, paramstok, "level");
493495

494496
if (!msgtok || msgtok->type != JSMN_STRING) {
495497
return tal_fmt(plugin, "Log notification from plugin doesn't have "
@@ -498,7 +500,7 @@ static const char *plugin_log_handle(struct plugin *plugin,
498500

499501
if (!leveltok)
500502
level = LOG_INFORM;
501-
else if (!log_level_parse(plugin->buffer + leveltok->start,
503+
else if (!log_level_parse(buffer + leveltok->start,
502504
leveltok->end - leveltok->start,
503505
&level)
504506
/* FIXME: Allow io logging? */
@@ -508,15 +510,15 @@ static const char *plugin_log_handle(struct plugin *plugin,
508510
"Unknown log-level %.*s, valid values are "
509511
"\"trace\", \"debug\", \"info\", \"warn\", or \"error\".",
510512
json_tok_full_len(leveltok),
511-
json_tok_full(plugin->buffer, leveltok));
513+
json_tok_full(buffer, leveltok));
512514
}
513515

514516
call_notifier = (level == LOG_BROKEN || level == LOG_UNUSUAL)? true : false;
515517

516518
/* Only bother unescaping and splitting if it has \ */
517-
if (memchr(plugin->buffer + msgtok->start, '\\', msgtok->end - msgtok->start)) {
519+
if (memchr(buffer + msgtok->start, '\\', msgtok->end - msgtok->start)) {
518520
const char *log_msg = json_escape_unescape_len(tmpctx,
519-
plugin->buffer + msgtok->start,
521+
buffer + msgtok->start,
520522
msgtok->end - msgtok->start);
521523
char **lines;
522524

@@ -534,37 +536,38 @@ static const char *plugin_log_handle(struct plugin *plugin,
534536
print_raw:
535537
log_(plugin->log, level, NULL, call_notifier, "%.*s",
536538
msgtok->end - msgtok->start,
537-
plugin->buffer + msgtok->start);
539+
buffer + msgtok->start);
538540
}
539541

540542
return NULL;
541543
}
542544

543545
static const char *plugin_notify_handle(struct plugin *plugin,
546+
const char *buffer,
544547
const jsmntok_t *methodtok,
545548
const jsmntok_t *paramstok)
546549
{
547550
const jsmntok_t *idtok;
548551
struct jsonrpc_request *request;
549552

550553
/* id inside params tells us which id to redirect to. */
551-
idtok = json_get_member(plugin->buffer, paramstok, "id");
554+
idtok = json_get_member(buffer, paramstok, "id");
552555
if (!idtok) {
553556
return tal_fmt(plugin,
554557
"JSON-RPC notify \"id\"-field is not present");
555558
}
556559

557560
/* Include any "" in id */
558561
request = strmap_getn(&plugin->pending_requests,
559-
json_tok_full(plugin->buffer, idtok),
562+
json_tok_full(buffer, idtok),
560563
json_tok_full_len(idtok));
561564
if (!request) {
562565
return NULL;
563566
}
564567

565568
/* Ignore if they don't have a callback */
566569
if (request->notify_cb)
567-
request->notify_cb(plugin->buffer, methodtok, paramstok, idtok,
570+
request->notify_cb(buffer, methodtok, paramstok, idtok,
568571
request->response_cb_arg);
569572
return NULL;
570573
}
@@ -583,38 +586,40 @@ static bool plugin_notification_allowed(const struct plugin *plugin, const char
583586

584587
/* Returns the error string, or NULL */
585588
static const char *plugin_notification_handle(struct plugin *plugin,
589+
const char *buffer,
586590
const jsmntok_t *toks)
587591
WARN_UNUSED_RESULT;
588592

589593
static const char *plugin_notification_handle(struct plugin *plugin,
594+
const char *buffer,
590595
const jsmntok_t *toks)
591596
{
592597
const jsmntok_t *methtok, *paramstok;
593598
const char *methname;
594599
struct jsonrpc_notification *n;
595-
methtok = json_get_member(plugin->buffer, toks, "method");
596-
paramstok = json_get_member(plugin->buffer, toks, "params");
600+
methtok = json_get_member(buffer, toks, "method");
601+
paramstok = json_get_member(buffer, toks, "params");
597602

598603
if (!methtok || !paramstok) {
599604
return tal_fmt(plugin,
600605
"Malformed JSON-RPC notification missing "
601606
"\"method\" or \"params\": %.*s",
602607
toks->end - toks->start,
603-
plugin->buffer + toks->start);
608+
buffer + toks->start);
604609
}
605610

606611
/* Dispatch incoming notifications. This is currently limited
607612
* to just a few method types, should this ever become
608613
* unwieldy we can switch to the AUTODATA construction to
609614
* register notification handlers in a variety of places. */
610-
if (json_tok_streq(plugin->buffer, methtok, "log")) {
611-
return plugin_log_handle(plugin, paramstok);
612-
} else if (json_tok_streq(plugin->buffer, methtok, "message")
613-
|| json_tok_streq(plugin->buffer, methtok, "progress")) {
614-
return plugin_notify_handle(plugin, methtok, paramstok);
615+
if (json_tok_streq(buffer, methtok, "log")) {
616+
return plugin_log_handle(plugin, buffer, paramstok);
617+
} else if (json_tok_streq(buffer, methtok, "message")
618+
|| json_tok_streq(buffer, methtok, "progress")) {
619+
return plugin_notify_handle(plugin, buffer, methtok, paramstok);
615620
}
616621

617-
methname = json_strdup(tmpctx, plugin->buffer, methtok);
622+
methname = json_strdup(tmpctx, buffer, methtok);
618623

619624
if (!plugin_notification_allowed(plugin, methname)) {
620625
log_unusual(plugin->log,
@@ -625,7 +630,7 @@ static const char *plugin_notification_handle(struct plugin *plugin,
625630
} else if (notifications_have_topic(plugin->plugins, methname)) {
626631
n = jsonrpc_notification_start_noparams(NULL, methname);
627632
json_add_string(n->stream, "origin", plugin->shortname);
628-
json_add_tok(n->stream, "params", paramstok, plugin->buffer);
633+
json_add_tok(n->stream, "params", paramstok, buffer);
629634
jsonrpc_notification_end_noparams(n);
630635

631636
plugins_notify(plugin->plugins, take(n));
@@ -670,14 +675,15 @@ static void destroy_request(struct jsonrpc_request *req,
670675
}
671676

672677
static void plugin_response_handle(struct plugin *plugin,
678+
const char *buffer,
673679
const jsmntok_t *toks,
674680
const jsmntok_t *idtok)
675681
{
676682
struct jsonrpc_request *request;
677683
const tal_t *ctx;
678684

679685
request = strmap_getn(&plugin->pending_requests,
680-
json_tok_full(plugin->buffer, idtok),
686+
json_tok_full(buffer, idtok),
681687
json_tok_full_len(idtok));
682688
/* Can happen if request was freed before plugin responded */
683689
if (!request) {
@@ -691,7 +697,7 @@ static void plugin_response_handle(struct plugin *plugin,
691697
/* Don't keep track of this request; we will terminate it */
692698
tal_del_destructor2(request, destroy_request, plugin);
693699
destroy_request(request, plugin);
694-
request->response_cb(plugin->buffer, toks, idtok, request->response_cb_arg);
700+
request->response_cb(buffer, toks, idtok, request->response_cb_arg);
695701
tal_free(ctx);
696702
}
697703

@@ -773,7 +779,7 @@ static const char *plugin_read_json_one(struct plugin *plugin,
773779
*
774780
* https://www.jsonrpc.org/specification#notification
775781
*/
776-
err = plugin_notification_handle(plugin, plugin->toks);
782+
err = plugin_notification_handle(plugin, plugin->buffer, plugin->toks);
777783

778784
} else {
779785
/* When a rpc call is made, the Server MUST reply with
@@ -803,7 +809,7 @@ static const char *plugin_read_json_one(struct plugin *plugin,
803809
*
804810
* https://www.jsonrpc.org/specification#response_object
805811
*/
806-
plugin_response_handle(plugin, plugin->toks, idtok);
812+
plugin_response_handle(plugin, plugin->buffer, plugin->toks, idtok);
807813
err = NULL;
808814
}
809815
if (want_transaction)
@@ -1522,7 +1528,7 @@ static const char *plugin_subscriptions_add(struct plugin *plugin,
15221528
* manifest, without checking that they exist, since
15231529
* later plugins may also emit notifications of custom
15241530
* types that we don't know about yet. */
1525-
sub.topic = json_strdup(plugin, plugin->buffer, s);
1531+
sub.topic = json_strdup(plugin, buffer, s);
15261532
sub.owner = plugin;
15271533
tal_arr_expand(&plugin->subscriptions, sub);
15281534
}
@@ -1563,7 +1569,7 @@ static const char *plugin_hooks_add(struct plugin *plugin, const char *buffer,
15631569
aftertok = json_get_member(buffer, t, "after");
15641570
} else {
15651571
/* FIXME: deprecate in 3 releases after v0.9.2! */
1566-
name = json_strdup(tmpctx, plugin->buffer, t);
1572+
name = json_strdup(tmpctx, buffer, t);
15671573
beforetok = aftertok = NULL;
15681574
}
15691575

0 commit comments

Comments
 (0)