2016-01-21 21:11:48 +01:00
|
|
|
|
/*
|
|
|
|
|
* Helper to submit via JSON-RPC and get back response.
|
|
|
|
|
*/
|
cli: handle OOM by directly streaming output.
We use raw malloc here, again, to handle the failure cases more easily.
I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.
diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index f840c0786..d83555a51 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
exit(0);
}
+static void *xrealloc(void *p, size_t len)
+{
+ if (len > 1000000)
+ return NULL;
+ return realloc(p, len);
+}
+#define realloc xrealloc
+
int main(int argc, char *argv[])
{
setup_locale();
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-21 09:13:24 +02:00
|
|
|
|
#include "config.h"
|
2022-01-12 21:36:56 +01:00
|
|
|
|
#include "config_cli.h"
|
2019-05-23 15:10:41 +02:00
|
|
|
|
#include <ccan/asort/asort.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
#include <ccan/err/err.h>
|
2019-06-12 02:38:54 +02:00
|
|
|
|
#include <ccan/json_escape/json_escape.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
#include <ccan/opt/opt.h>
|
|
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2019-12-18 00:08:30 +01:00
|
|
|
|
#include <ccan/tal/path/path.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
#include <ccan/tal/str/str.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
|
#include <common/configdir.h>
|
2019-05-23 15:10:41 +02:00
|
|
|
|
#include <common/json_command.h>
|
2020-10-12 07:33:50 +02:00
|
|
|
|
#include <common/status_levels.h>
|
2018-04-25 12:55:34 +02:00
|
|
|
|
#include <common/utils.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
|
#include <common/version.h>
|
2019-01-11 01:38:05 +01:00
|
|
|
|
#include <libgen.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
2018-10-09 03:14:18 +02:00
|
|
|
|
#include <sys/wait.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#define NO_ERROR 0
|
|
|
|
|
#define ERROR_FROM_LIGHTNINGD 1
|
|
|
|
|
#define ERROR_TALKING_TO_LIGHTNINGD 2
|
|
|
|
|
#define ERROR_USAGE 3
|
|
|
|
|
|
2016-11-04 01:47:03 +01:00
|
|
|
|
struct netaddr;
|
|
|
|
|
|
2018-01-29 01:30:15 +01:00
|
|
|
|
/* Returns number of tokens digested */
|
|
|
|
|
static size_t human_readable(const char *buffer, const jsmntok_t *t, char term)
|
|
|
|
|
{
|
|
|
|
|
size_t i, n;
|
|
|
|
|
|
|
|
|
|
switch (t->type) {
|
|
|
|
|
case JSMN_PRIMITIVE:
|
|
|
|
|
case JSMN_STRING:
|
|
|
|
|
for (i = t->start; i < t->end; i++) {
|
|
|
|
|
/* We only translate \n and \t. */
|
|
|
|
|
if (buffer[i] == '\\' && i + 1 < t->end) {
|
|
|
|
|
if (buffer[i+1] == 'n') {
|
|
|
|
|
fputc('\n', stdout);
|
|
|
|
|
i++;
|
|
|
|
|
continue;
|
|
|
|
|
} else if (buffer[i+1] == 't') {
|
|
|
|
|
fputc('\t', stdout);
|
|
|
|
|
i++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fputc(buffer[i], stdout);
|
|
|
|
|
}
|
|
|
|
|
fputc(term, stdout);
|
|
|
|
|
return 1;
|
|
|
|
|
case JSMN_ARRAY:
|
|
|
|
|
n = 1;
|
|
|
|
|
for (i = 0; i < t->size; i++)
|
|
|
|
|
n += human_readable(buffer, t + n, '\n');
|
|
|
|
|
return n;
|
|
|
|
|
case JSMN_OBJECT:
|
|
|
|
|
/* Elide single-field objects */
|
|
|
|
|
if (t->size == 1)
|
2020-09-08 07:20:13 +02:00
|
|
|
|
return human_readable(buffer, t + 2, '\n') + 2;
|
2018-01-29 01:30:15 +01:00
|
|
|
|
n = 1;
|
|
|
|
|
for (i = 0; i < t->size; i++) {
|
|
|
|
|
n += human_readable(buffer, t + n, '=');
|
|
|
|
|
n += human_readable(buffer, t + n, '\n');
|
|
|
|
|
}
|
|
|
|
|
return n;
|
2018-02-05 04:23:19 +01:00
|
|
|
|
case JSMN_UNDEFINED:
|
|
|
|
|
break;
|
2018-01-29 01:30:15 +01:00
|
|
|
|
}
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 03:03:09 +02:00
|
|
|
|
/* Returns number of tokens digested */
|
|
|
|
|
static size_t flat_json(const char *prefix,
|
|
|
|
|
const char *buffer, const jsmntok_t *t)
|
|
|
|
|
{
|
|
|
|
|
size_t i, n;
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
switch (t->type) {
|
|
|
|
|
case JSMN_PRIMITIVE:
|
|
|
|
|
case JSMN_STRING:
|
|
|
|
|
printf("%s=%.*s\n",
|
|
|
|
|
prefix, t->end - t->start, buffer + t->start);
|
|
|
|
|
return 1;
|
|
|
|
|
case JSMN_ARRAY:
|
|
|
|
|
n = 1;
|
|
|
|
|
for (i = 0; i < t->size; i++) {
|
|
|
|
|
p = tal_fmt(NULL, "%s[%zi]", prefix, i);
|
|
|
|
|
n += flat_json(p, buffer, t + n);
|
|
|
|
|
tal_free(p);
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
case JSMN_OBJECT:
|
|
|
|
|
n = 1;
|
|
|
|
|
for (i = 0; i < t->size; i++) {
|
|
|
|
|
if (streq(prefix, ""))
|
|
|
|
|
p = tal_fmt(NULL, "%.*s",
|
|
|
|
|
t[n].end - t[n].start,
|
|
|
|
|
buffer + t[n].start);
|
|
|
|
|
else
|
|
|
|
|
p = tal_fmt(NULL, "%s.%.*s", prefix,
|
|
|
|
|
t[n].end - t[n].start,
|
|
|
|
|
buffer + t[n].start);
|
|
|
|
|
n++;
|
|
|
|
|
n += flat_json(p, buffer, t + n);
|
|
|
|
|
tal_free(p);
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
case JSMN_UNDEFINED:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 15:10:41 +02:00
|
|
|
|
static int compare_tok(const jsmntok_t *a, const jsmntok_t *b,
|
|
|
|
|
const char *buffer)
|
|
|
|
|
{
|
|
|
|
|
int a_len = a->end - a->start, b_len = b->end - b->start, min_len, cmp;
|
|
|
|
|
|
|
|
|
|
if (a_len > b_len)
|
|
|
|
|
min_len = b_len;
|
|
|
|
|
else
|
|
|
|
|
min_len = a_len;
|
|
|
|
|
|
|
|
|
|
cmp = memcmp(buffer + a->start, buffer + b->start, min_len);
|
|
|
|
|
if (cmp != 0)
|
|
|
|
|
return cmp;
|
|
|
|
|
/* If equal, shorter one wins. */
|
|
|
|
|
return a_len - b_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int compare_help(const jsmntok_t *const *a,
|
|
|
|
|
const jsmntok_t *const *b,
|
|
|
|
|
char *buffer)
|
|
|
|
|
{
|
|
|
|
|
const jsmntok_t *cat_a, *cat_b;
|
|
|
|
|
bool a_is_developer, b_is_developer;
|
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
|
|
cat_a = json_get_member(buffer, *a, "category");
|
|
|
|
|
cat_b = json_get_member(buffer, *b, "category");
|
|
|
|
|
|
|
|
|
|
/* Just in case it's an older lightningd! */
|
|
|
|
|
if (!cat_a)
|
|
|
|
|
goto same_category;
|
|
|
|
|
|
|
|
|
|
/* We always tweak "developer" category to last. */
|
|
|
|
|
a_is_developer = json_tok_streq(buffer, cat_a, "developer");
|
|
|
|
|
b_is_developer = json_tok_streq(buffer, cat_b, "developer");
|
|
|
|
|
|
|
|
|
|
if (a_is_developer && b_is_developer)
|
|
|
|
|
cmp = 0;
|
|
|
|
|
else if (a_is_developer)
|
|
|
|
|
cmp = 1;
|
|
|
|
|
else if (b_is_developer)
|
|
|
|
|
cmp = -1;
|
|
|
|
|
else
|
|
|
|
|
/* Otherwise we order category alphabetically. */
|
|
|
|
|
cmp = compare_tok(cat_a, cat_b, buffer);
|
|
|
|
|
|
|
|
|
|
if (cmp != 0)
|
|
|
|
|
return cmp;
|
|
|
|
|
|
|
|
|
|
/* After category, we order by name */
|
|
|
|
|
same_category:
|
|
|
|
|
return compare_tok(json_get_member(buffer, *a, "command"),
|
|
|
|
|
json_get_member(buffer, *b, "command"),
|
|
|
|
|
buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 04:23:46 +02:00
|
|
|
|
static void human_help(char *buffer, const jsmntok_t *result)
|
|
|
|
|
{
|
2019-05-23 15:10:41 +02:00
|
|
|
|
unsigned int i;
|
|
|
|
|
/* `curr` is used as a temporary token */
|
|
|
|
|
const jsmntok_t *curr;
|
|
|
|
|
const char *prev_cat;
|
|
|
|
|
/* Contains all commands objects, which have the following structure :
|
|
|
|
|
* {
|
|
|
|
|
* "command": "The command name and usage",
|
|
|
|
|
* "category": "The command category",
|
|
|
|
|
* "description": "The command's description",
|
|
|
|
|
* "verbose": "The command's detailed description"
|
|
|
|
|
* }
|
|
|
|
|
*/
|
|
|
|
|
const jsmntok_t * help_array = json_get_member(buffer, result, "help");
|
|
|
|
|
const jsmntok_t **help = tal_arr(NULL, const jsmntok_t *,
|
|
|
|
|
help_array->size);
|
|
|
|
|
|
|
|
|
|
/* Populate an array for easy sorting with asort */
|
|
|
|
|
json_for_each_arr(i, curr, help_array)
|
|
|
|
|
help[i] = curr;
|
|
|
|
|
|
|
|
|
|
asort(help, tal_count(help), compare_help, buffer);
|
|
|
|
|
|
|
|
|
|
prev_cat = "";
|
|
|
|
|
for (i = 0; i < tal_count(help); i++) {
|
|
|
|
|
const jsmntok_t *category, *command, *desc;
|
|
|
|
|
|
|
|
|
|
category = json_get_member(buffer, help[i], "category");
|
|
|
|
|
if (category && !json_tok_streq(buffer, category, prev_cat)) {
|
|
|
|
|
prev_cat = json_strdup(help, buffer, category);
|
2019-07-26 04:23:46 +02:00
|
|
|
|
printf("=== %s ===\n\n", prev_cat);
|
2019-05-23 15:10:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
command = json_get_member(buffer, help[i], "command");
|
|
|
|
|
desc = json_get_member(buffer, help[i], "description");
|
|
|
|
|
printf("%.*s\n",
|
|
|
|
|
command->end - command->start, buffer + command->start);
|
|
|
|
|
printf(" %.*s\n\n",
|
|
|
|
|
desc->end - desc->start, buffer + desc->start);
|
2018-03-02 17:00:45 +01:00
|
|
|
|
}
|
2019-05-23 15:10:41 +02:00
|
|
|
|
tal_free(help);
|
2018-07-01 21:13:22 +02:00
|
|
|
|
|
2019-07-26 04:23:46 +02:00
|
|
|
|
printf("---\nrun `lightning-cli help <command>` for more information on a specific command\n");
|
2018-03-02 17:00:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 01:30:15 +01:00
|
|
|
|
enum format {
|
|
|
|
|
JSON,
|
|
|
|
|
HUMAN,
|
2019-07-26 04:27:01 +02:00
|
|
|
|
HELPLIST,
|
2020-05-11 03:03:09 +02:00
|
|
|
|
FLAT,
|
2019-04-08 11:58:44 +02:00
|
|
|
|
DEFAULT_FORMAT,
|
|
|
|
|
RAW
|
2018-01-29 01:30:15 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static char *opt_set_human(enum format *format)
|
|
|
|
|
{
|
|
|
|
|
*format = HUMAN;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 03:03:09 +02:00
|
|
|
|
static char *opt_set_flat(enum format *format)
|
|
|
|
|
{
|
|
|
|
|
*format = FLAT;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 01:30:15 +01:00
|
|
|
|
static char *opt_set_json(enum format *format)
|
|
|
|
|
{
|
|
|
|
|
*format = JSON;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 11:58:44 +02:00
|
|
|
|
static char *opt_set_raw(enum format *format)
|
|
|
|
|
{
|
|
|
|
|
*format = RAW;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 01:30:15 +01:00
|
|
|
|
enum input {
|
|
|
|
|
KEYWORDS,
|
|
|
|
|
ORDERED,
|
|
|
|
|
DEFAULT_INPUT
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static char *opt_set_keywords(enum input *input)
|
|
|
|
|
{
|
|
|
|
|
*input = KEYWORDS;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *opt_set_ordered(enum input *input)
|
|
|
|
|
{
|
|
|
|
|
*input = ORDERED;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool is_literal(const char *arg)
|
|
|
|
|
{
|
2018-02-25 16:37:01 +01:00
|
|
|
|
size_t arglen = strlen(arg);
|
2018-08-06 15:54:16 +02:00
|
|
|
|
if (arglen == 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-04-06 03:54:09 +02:00
|
|
|
|
return strspn(arg, "0123456789.") == arglen
|
2018-01-29 01:30:15 +01:00
|
|
|
|
|| streq(arg, "true")
|
|
|
|
|
|| streq(arg, "false")
|
|
|
|
|
|| streq(arg, "null")
|
2018-02-25 16:37:01 +01:00
|
|
|
|
|| (arg[0] == '{' && arg[arglen - 1] == '}')
|
|
|
|
|
|| (arg[0] == '[' && arg[arglen - 1] == ']')
|
|
|
|
|
|| (arg[0] == '"' && arg[arglen - 1] == '"');
|
2018-01-29 01:30:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void add_input(char **cmd, const char *input,
|
|
|
|
|
int i, int argc)
|
|
|
|
|
{
|
|
|
|
|
/* Numbers, bools, objects and arrays are left unquoted,
|
|
|
|
|
* and quoted things left alone. */
|
|
|
|
|
if (is_literal(input))
|
|
|
|
|
tal_append_fmt(cmd, "%s", input);
|
|
|
|
|
else
|
2019-04-10 05:14:23 +02:00
|
|
|
|
tal_append_fmt(cmd, "\"%s\"", json_escape(*cmd, input)->s);
|
2018-01-29 01:30:15 +01:00
|
|
|
|
if (i != argc - 1)
|
|
|
|
|
tal_append_fmt(cmd, ", ");
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 15:48:03 +02:00
|
|
|
|
static void
|
2019-01-11 01:38:05 +01:00
|
|
|
|
try_exec_man (const char *page, char *relative_to) {
|
2018-10-09 03:14:18 +02:00
|
|
|
|
int status;
|
|
|
|
|
|
|
|
|
|
switch (fork()) {
|
|
|
|
|
case -1:
|
|
|
|
|
err(1, "Forking man command");
|
|
|
|
|
case 0:
|
|
|
|
|
/* child, run man command. */
|
2019-01-11 01:38:05 +01:00
|
|
|
|
if (relative_to != NULL) {
|
|
|
|
|
page = tal_fmt(page, "%s/../doc/%s.7", relative_to, page);
|
|
|
|
|
execlp("man", "man", "-l", page, (char *)NULL);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
execlp("man", "man", page, (char *)NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 03:14:18 +02:00
|
|
|
|
err(1, "Running man command");
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wait(&status);
|
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
|
|
|
|
|
exit(0);
|
2018-07-03 15:48:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 11:58:44 +02:00
|
|
|
|
static void print_json(const char *str, const jsmntok_t *tok, const char *indent)
|
|
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
const jsmntok_t *t;
|
|
|
|
|
bool first;
|
|
|
|
|
char next_indent[strlen(indent) + 3 + 1];
|
|
|
|
|
|
|
|
|
|
memset(next_indent, ' ', sizeof(next_indent)-1);
|
|
|
|
|
next_indent[sizeof(next_indent)-1] = '\0';
|
|
|
|
|
|
|
|
|
|
switch (tok->type) {
|
|
|
|
|
case JSMN_PRIMITIVE:
|
|
|
|
|
case JSMN_STRING:
|
|
|
|
|
printf("%.*s", json_tok_full_len(tok), json_tok_full(str, tok));
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
case JSMN_ARRAY:
|
|
|
|
|
first = true;
|
|
|
|
|
json_for_each_arr(i, t, tok) {
|
|
|
|
|
if (first)
|
|
|
|
|
printf("[\n%s", next_indent);
|
|
|
|
|
else
|
|
|
|
|
printf(",\n%s", next_indent);
|
|
|
|
|
print_json(str, t, next_indent);
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
if (first)
|
|
|
|
|
printf("[]");
|
|
|
|
|
else
|
|
|
|
|
printf("\n%s]", indent);
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
case JSMN_OBJECT:
|
|
|
|
|
first = true;
|
|
|
|
|
json_for_each_obj(i, t, tok) {
|
|
|
|
|
if (first)
|
|
|
|
|
printf("{\n%s", next_indent);
|
|
|
|
|
else
|
|
|
|
|
printf(",\n%s", next_indent);
|
|
|
|
|
print_json(str, t, next_indent);
|
2019-08-08 04:36:42 +02:00
|
|
|
|
printf(": ");
|
2019-04-08 11:58:44 +02:00
|
|
|
|
print_json(str, t + 1, next_indent);
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
if (first)
|
|
|
|
|
printf("{}");
|
|
|
|
|
else
|
|
|
|
|
printf("\n%s}", indent);
|
|
|
|
|
return;
|
|
|
|
|
case JSMN_UNDEFINED:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
cli: handle OOM by directly streaming output.
We use raw malloc here, again, to handle the failure cases more easily.
I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.
diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index f840c0786..d83555a51 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
exit(0);
}
+static void *xrealloc(void *p, size_t len)
+{
+ if (len > 1000000)
+ return NULL;
+ return realloc(p, len);
+}
+#define realloc xrealloc
+
int main(int argc, char *argv[])
{
setup_locale();
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-21 09:13:24 +02:00
|
|
|
|
/* Always returns a positive number < len. len must be > 0! */
|
|
|
|
|
static size_t read_nofail(int fd, void *buf, size_t len)
|
|
|
|
|
{
|
|
|
|
|
ssize_t i;
|
|
|
|
|
assert(len > 0);
|
|
|
|
|
|
2022-01-12 21:36:56 +01:00
|
|
|
|
i = cli_read(fd, buf, len);
|
cli: handle OOM by directly streaming output.
We use raw malloc here, again, to handle the failure cases more easily.
I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.
diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index f840c0786..d83555a51 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
exit(0);
}
+static void *xrealloc(void *p, size_t len)
+{
+ if (len > 1000000)
+ return NULL;
+ return realloc(p, len);
+}
+#define realloc xrealloc
+
int main(int argc, char *argv[])
{
setup_locale();
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-21 09:13:24 +02:00
|
|
|
|
if (i == 0)
|
|
|
|
|
errx(ERROR_TALKING_TO_LIGHTNINGD,
|
|
|
|
|
"reading response: socket closed");
|
|
|
|
|
else if (i < 0)
|
|
|
|
|
err(ERROR_TALKING_TO_LIGHTNINGD, "reading response");
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We rely on the fact that lightningd terminates all JSON RPC responses with
|
|
|
|
|
* "\n\n", so we can stream even if we can't parse. */
|
2019-07-26 04:23:44 +02:00
|
|
|
|
static void oom_dump(int fd, char *resp, size_t off)
|
cli: handle OOM by directly streaming output.
We use raw malloc here, again, to handle the failure cases more easily.
I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.
diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index f840c0786..d83555a51 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
exit(0);
}
+static void *xrealloc(void *p, size_t len)
+{
+ if (len > 1000000)
+ return NULL;
+ return realloc(p, len);
+}
+#define realloc xrealloc
+
int main(int argc, char *argv[])
{
setup_locale();
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-21 09:13:24 +02:00
|
|
|
|
{
|
|
|
|
|
warnx("Out of memory: sending raw output");
|
|
|
|
|
|
|
|
|
|
/* Note: resp does not already end in '\n\n', and resp_len is > 0 */
|
|
|
|
|
do {
|
|
|
|
|
/* Keep last char, to avoid splitting \n\n */
|
|
|
|
|
write_all(STDOUT_FILENO, resp, off-1);
|
|
|
|
|
resp[0] = resp[off-1];
|
2019-07-26 04:23:44 +02:00
|
|
|
|
off = 1 + read_nofail(fd, resp + 1, tal_bytelen(resp) - 1);
|
cli: handle OOM by directly streaming output.
We use raw malloc here, again, to handle the failure cases more easily.
I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.
diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index f840c0786..d83555a51 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
exit(0);
}
+static void *xrealloc(void *p, size_t len)
+{
+ if (len > 1000000)
+ return NULL;
+ return realloc(p, len);
+}
+#define realloc xrealloc
+
int main(int argc, char *argv[])
{
setup_locale();
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-21 09:13:24 +02:00
|
|
|
|
} while (resp[off-2] != '\n' || resp[off-1] != '\n');
|
|
|
|
|
write_all(STDOUT_FILENO, resp, off-1);
|
|
|
|
|
/* We assume giant answer means "success" */
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 04:23:44 +02:00
|
|
|
|
/* We want to return failure if tal_resize fails */
|
|
|
|
|
static void tal_error(const char *msg)
|
|
|
|
|
{
|
|
|
|
|
if (streq(msg, "Reallocation failure"))
|
|
|
|
|
return;
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 02:41:16 +02:00
|
|
|
|
static enum format delete_format_hint(const char *resp, jsmntok_t **toks)
|
2019-07-26 04:27:01 +02:00
|
|
|
|
{
|
2020-05-11 02:41:16 +02:00
|
|
|
|
const jsmntok_t *result = json_get_member(resp, *toks, "result");
|
2019-07-26 04:27:01 +02:00
|
|
|
|
const jsmntok_t *hint;
|
|
|
|
|
enum format format = JSON;
|
|
|
|
|
|
2020-05-11 02:41:16 +02:00
|
|
|
|
if (!result)
|
|
|
|
|
return format;
|
|
|
|
|
|
2019-07-26 04:27:01 +02:00
|
|
|
|
hint = json_get_member(resp, result, "format-hint");
|
|
|
|
|
if (!hint)
|
|
|
|
|
return format;
|
|
|
|
|
|
|
|
|
|
if (json_tok_streq(resp, hint, "simple"))
|
|
|
|
|
format = HUMAN;
|
|
|
|
|
|
|
|
|
|
/* Don't let hint appear in the output! */
|
2020-05-11 02:41:16 +02:00
|
|
|
|
/* Note the aritmetic on *toks for const-washing */
|
|
|
|
|
json_tok_remove(toks, *toks + (result - *toks), hint-1, 1);
|
2019-07-26 04:27:01 +02:00
|
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 07:09:19 +02:00
|
|
|
|
static enum format choose_format(const char *resp,
|
|
|
|
|
jsmntok_t **toks,
|
|
|
|
|
const char *method,
|
|
|
|
|
const char *command,
|
|
|
|
|
enum format format)
|
|
|
|
|
{
|
|
|
|
|
/* If they specify a format, that's what we use. */
|
2020-05-11 02:41:16 +02:00
|
|
|
|
if (format != DEFAULT_FORMAT) {
|
|
|
|
|
/* But humans don't want to see the format hint! */
|
|
|
|
|
if (format == HUMAN)
|
|
|
|
|
delete_format_hint(resp, toks);
|
2019-08-13 07:09:19 +02:00
|
|
|
|
return format;
|
2020-05-11 02:41:16 +02:00
|
|
|
|
}
|
2019-08-13 07:09:19 +02:00
|
|
|
|
|
|
|
|
|
/* This works best when we order it. */
|
|
|
|
|
if (streq(method, "help") && command == NULL)
|
|
|
|
|
format = HELPLIST;
|
2020-05-11 02:41:16 +02:00
|
|
|
|
else
|
|
|
|
|
format = delete_format_hint(resp, toks);
|
|
|
|
|
|
2019-08-13 07:09:19 +02:00
|
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-12 07:33:50 +02:00
|
|
|
|
static bool handle_notify(const char *buf, jsmntok_t *toks,
|
|
|
|
|
enum log_level notification_level,
|
|
|
|
|
bool *last_was_progress)
|
|
|
|
|
{
|
|
|
|
|
const jsmntok_t *id, *method, *params;
|
|
|
|
|
|
|
|
|
|
if (toks->type != JSMN_OBJECT)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
id = json_get_member(buf, toks, "id");
|
|
|
|
|
if (id)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
method = json_get_member(buf, toks, "method");
|
|
|
|
|
if (!method)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
params = json_get_member(buf, toks, "params");
|
|
|
|
|
if (!params)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
/* Print nothing if --notifications=none */
|
|
|
|
|
if (notification_level == LOG_LEVEL_MAX + 1)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
/* We try to be robust if malformed */
|
|
|
|
|
if (json_tok_streq(buf, method, "message")) {
|
|
|
|
|
const jsmntok_t *message, *leveltok;
|
|
|
|
|
enum log_level level;
|
|
|
|
|
|
|
|
|
|
leveltok = json_get_member(buf, params, "level");
|
|
|
|
|
if (!leveltok
|
|
|
|
|
|| !log_level_parse(buf + leveltok->start,
|
|
|
|
|
leveltok->end - leveltok->start,
|
|
|
|
|
&level)
|
|
|
|
|
|| level < notification_level)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (*last_was_progress)
|
|
|
|
|
printf("\n");
|
|
|
|
|
*last_was_progress = false;
|
|
|
|
|
message = json_get_member(buf, params, "message");
|
|
|
|
|
if (!message)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
printf("# %.*s\n",
|
|
|
|
|
message->end - message->start,
|
|
|
|
|
buf + message->start);
|
|
|
|
|
} else if (json_tok_streq(buf, method, "progress")) {
|
|
|
|
|
const jsmntok_t *num, *total, *stage;
|
|
|
|
|
u32 n, tot;
|
|
|
|
|
char bar[60 + 1];
|
|
|
|
|
char totstr[STR_MAX_CHARS(u32)];
|
|
|
|
|
|
|
|
|
|
num = json_get_member(buf, params, "num");
|
|
|
|
|
total = json_get_member(buf, params, "total");
|
|
|
|
|
if (!num || !total)
|
|
|
|
|
return true;
|
|
|
|
|
if (!json_to_u32(buf, num, &n)
|
|
|
|
|
|| !json_to_u32(buf, total, &tot))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
/* Ph3ar my gui skillz! */
|
|
|
|
|
printf("\r# ");
|
|
|
|
|
stage = json_get_member(buf, params, "stage");
|
|
|
|
|
if (stage) {
|
|
|
|
|
u32 stage_num, stage_total;
|
|
|
|
|
json_to_u32(buf, json_get_member(buf, stage, "num"),
|
|
|
|
|
&stage_num);
|
|
|
|
|
json_to_u32(buf, json_get_member(buf, stage, "total"),
|
|
|
|
|
&stage_total);
|
|
|
|
|
snprintf(totstr, sizeof(totstr), "%u", stage_total);
|
|
|
|
|
printf("Stage %*u/%s ",
|
|
|
|
|
(int)strlen(totstr), stage_num+1, totstr);
|
|
|
|
|
}
|
|
|
|
|
snprintf(totstr, sizeof(totstr), "%u", tot);
|
|
|
|
|
printf("%*u/%s ", (int)strlen(totstr), n+1, totstr);
|
|
|
|
|
memset(bar, ' ', sizeof(bar)-1);
|
|
|
|
|
memset(bar, '=', (double)strlen(bar) / (tot-1) * n);
|
|
|
|
|
bar[sizeof(bar)-1] = '\0';
|
|
|
|
|
printf("|%s|", bar);
|
|
|
|
|
/* Leave bar there if it's finished. */
|
|
|
|
|
if (n+1 == tot) {
|
|
|
|
|
printf("\n");
|
|
|
|
|
*last_was_progress = false;
|
|
|
|
|
} else {
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
*last_was_progress = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void enable_notifications(int fd)
|
|
|
|
|
{
|
|
|
|
|
const char *enable = "{ \"jsonrpc\": \"2.0\", \"method\": \"notifications\", \"id\": 0, \"params\": { \"enable\": true } }";
|
|
|
|
|
char rbuf[100];
|
|
|
|
|
|
|
|
|
|
if (!write_all(fd, enable, strlen(enable)))
|
|
|
|
|
err(ERROR_TALKING_TO_LIGHTNINGD, "Writing enable command");
|
|
|
|
|
|
|
|
|
|
/* We get a very simple response, ending in \n\n. */
|
|
|
|
|
memset(rbuf, 0, sizeof(rbuf));
|
|
|
|
|
while (!strends(rbuf, "\n\n")) {
|
|
|
|
|
size_t len = strlen(rbuf);
|
2022-01-12 21:36:56 +01:00
|
|
|
|
if (cli_read(fd, rbuf + len, sizeof(rbuf) - len) < 0)
|
2020-10-12 07:33:50 +02:00
|
|
|
|
err(ERROR_TALKING_TO_LIGHTNINGD,
|
|
|
|
|
"Reading enable response");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *opt_set_level(const char *arg, enum log_level *level)
|
|
|
|
|
{
|
|
|
|
|
if (streq(arg, "none"))
|
|
|
|
|
*level = LOG_LEVEL_MAX + 1;
|
|
|
|
|
else if (!log_level_parse(arg, strlen(arg), level))
|
|
|
|
|
return "Invalid level";
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void opt_show_level(char buf[OPT_SHOW_LEN], const enum log_level *level)
|
|
|
|
|
{
|
|
|
|
|
if (*level == LOG_LEVEL_MAX + 1)
|
|
|
|
|
strncpy(buf, "none", OPT_SHOW_LEN-1);
|
|
|
|
|
else
|
|
|
|
|
strncpy(buf, log_level_name(*level), OPT_SHOW_LEN-1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
2018-04-25 12:55:34 +02:00
|
|
|
|
setup_locale();
|
|
|
|
|
|
cli: handle OOM by directly streaming output.
We use raw malloc here, again, to handle the failure cases more easily.
I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.
diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index f840c0786..d83555a51 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
exit(0);
}
+static void *xrealloc(void *p, size_t len)
+{
+ if (len > 1000000)
+ return NULL;
+ return realloc(p, len);
+}
+#define realloc xrealloc
+
int main(int argc, char *argv[])
{
setup_locale();
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-21 09:13:24 +02:00
|
|
|
|
int fd, i;
|
|
|
|
|
size_t off;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
const char *method;
|
2019-07-25 07:35:36 +02:00
|
|
|
|
char *cmd, *resp, *idstr;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
jsmntok_t *toks;
|
|
|
|
|
const jsmntok_t *result, *error, *id;
|
|
|
|
|
const tal_t *ctx = tal(NULL, char);
|
2019-11-23 02:46:40 +01:00
|
|
|
|
char *config_filename, *lightning_dir, *net_dir, *rpc_filename;
|
2018-01-20 05:37:33 +01:00
|
|
|
|
jsmn_parser parser;
|
2018-02-05 04:23:19 +01:00
|
|
|
|
int parserr;
|
2018-01-29 01:30:15 +01:00
|
|
|
|
enum format format = DEFAULT_FORMAT;
|
|
|
|
|
enum input input = DEFAULT_INPUT;
|
2020-10-12 07:33:50 +02:00
|
|
|
|
enum log_level notification_level = LOG_INFORM;
|
|
|
|
|
bool last_was_progress = false;
|
2018-10-09 03:14:18 +02:00
|
|
|
|
char *command = NULL;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
|
|
err_set_progname(argv[0]);
|
2018-01-20 05:37:33 +01:00
|
|
|
|
jsmn_init(&parser);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
2019-07-26 04:23:44 +02:00
|
|
|
|
tal_set_backend(NULL, NULL, NULL, tal_error);
|
2019-07-25 07:35:36 +02:00
|
|
|
|
|
2019-11-23 02:45:53 +01:00
|
|
|
|
setup_option_allocators();
|
2019-07-25 07:35:36 +02:00
|
|
|
|
|
2019-11-23 02:45:53 +01:00
|
|
|
|
initial_config_opts(ctx, argc, argv,
|
2019-11-23 02:46:40 +01:00
|
|
|
|
&config_filename, &lightning_dir, &net_dir,
|
|
|
|
|
&rpc_filename);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
|
|
opt_register_noarg("--help|-h", opt_usage_and_exit,
|
2018-01-25 22:21:27 +01:00
|
|
|
|
"<command> [<params>...]", "Show this message. Use the command help (without hyphens -- \"lightning-cli help\") to get a list of all RPC commands");
|
2018-01-29 01:30:15 +01:00
|
|
|
|
opt_register_noarg("-H|--human-readable", opt_set_human, &format,
|
2020-05-11 03:11:51 +02:00
|
|
|
|
"Human-readable output");
|
2020-05-11 03:03:09 +02:00
|
|
|
|
opt_register_noarg("-F|--flat", opt_set_flat, &format,
|
|
|
|
|
"Flatten output ('x.y.x=' format)");
|
2018-01-29 01:30:15 +01:00
|
|
|
|
opt_register_noarg("-J|--json", opt_set_json, &format,
|
|
|
|
|
"JSON output (default unless 'help')");
|
2019-04-08 11:58:44 +02:00
|
|
|
|
opt_register_noarg("-R|--raw", opt_set_raw, &format,
|
|
|
|
|
"Raw, unformatted JSON output");
|
2018-01-29 01:30:15 +01:00
|
|
|
|
opt_register_noarg("-k|--keywords", opt_set_keywords, &input,
|
|
|
|
|
"Use format key=value for <params>");
|
|
|
|
|
opt_register_noarg("-o|--order", opt_set_ordered, &input,
|
|
|
|
|
"Use params in order for <params>");
|
2020-10-12 07:33:50 +02:00
|
|
|
|
opt_register_arg("-N|--notifications", opt_set_level,
|
|
|
|
|
opt_show_level, ¬ification_level,
|
|
|
|
|
"Set notification level, or none");
|
2018-01-29 01:30:15 +01:00
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
|
opt_register_version();
|
|
|
|
|
|
|
|
|
|
opt_early_parse(argc, argv, opt_log_stderr_exit);
|
|
|
|
|
opt_parse(&argc, argv, opt_log_stderr_exit);
|
|
|
|
|
|
|
|
|
|
method = argv[1];
|
2018-01-25 22:21:27 +01:00
|
|
|
|
if (!method) {
|
|
|
|
|
char *usage = opt_usage(argv[0], NULL);
|
|
|
|
|
printf("%s\n", usage);
|
|
|
|
|
tal_free(usage);
|
|
|
|
|
printf("Querying lightningd for available RPC commands (\"lightning-cli help\"):\n\n");
|
|
|
|
|
method = "help";
|
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
2018-07-03 15:48:03 +02:00
|
|
|
|
/* Launch a manpage if we have a help command with an argument. We do
|
|
|
|
|
* not need to have lightningd running in this case. */
|
2019-07-26 04:27:01 +02:00
|
|
|
|
if (streq(method, "help") && format == DEFAULT_FORMAT && argc >= 3) {
|
2018-10-09 03:14:18 +02:00
|
|
|
|
command = argv[2];
|
2019-01-11 01:38:05 +01:00
|
|
|
|
char *page = tal_fmt(ctx, "lightning-%s", command);
|
|
|
|
|
|
|
|
|
|
try_exec_man(page, NULL);
|
|
|
|
|
|
|
|
|
|
/* Try to find the page relative to this executable.
|
|
|
|
|
* This handles the common scenario where lightning-cli
|
|
|
|
|
* was built from source and hasn't been installed yet */
|
|
|
|
|
try_exec_man(page, dirname(argv[0]));
|
|
|
|
|
|
|
|
|
|
tal_free(page);
|
2018-07-03 15:48:03 +02:00
|
|
|
|
}
|
2019-12-18 00:08:30 +01:00
|
|
|
|
|
|
|
|
|
/* If an absolute path to the RPC socket is given, it takes over other
|
|
|
|
|
* configuration options. */
|
|
|
|
|
if (path_is_abs(rpc_filename)) {
|
|
|
|
|
net_dir = path_dirname(ctx, rpc_filename);
|
|
|
|
|
rpc_filename = path_basename(ctx, rpc_filename);
|
|
|
|
|
}
|
2018-07-03 15:48:03 +02:00
|
|
|
|
|
2019-11-23 02:46:40 +01:00
|
|
|
|
if (chdir(net_dir) != 0)
|
2016-01-21 21:11:48 +01:00
|
|
|
|
err(ERROR_TALKING_TO_LIGHTNINGD, "Moving into '%s'",
|
2019-11-23 02:46:40 +01:00
|
|
|
|
net_dir);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
if (strlen(rpc_filename) + 1 > sizeof(addr.sun_path))
|
|
|
|
|
errx(ERROR_USAGE, "rpc filename '%s' too long", rpc_filename);
|
|
|
|
|
strcpy(addr.sun_path, rpc_filename);
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
|
|
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0)
|
|
|
|
|
err(ERROR_TALKING_TO_LIGHTNINGD,
|
|
|
|
|
"Connecting to '%s'", rpc_filename);
|
|
|
|
|
|
|
|
|
|
idstr = tal_fmt(ctx, "lightning-cli-%i", getpid());
|
2020-10-12 07:33:50 +02:00
|
|
|
|
|
|
|
|
|
if (notification_level <= LOG_LEVEL_MAX)
|
|
|
|
|
enable_notifications(fd);
|
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
|
cmd = tal_fmt(ctx,
|
2018-12-21 07:25:31 +01:00
|
|
|
|
"{ \"jsonrpc\" : \"2.0\", \"method\" : \"%s\", \"id\" : \"%s\", \"params\" :",
|
2019-04-10 05:14:23 +02:00
|
|
|
|
json_escape(ctx, method)->s, idstr);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
2018-01-29 01:30:15 +01:00
|
|
|
|
if (input == DEFAULT_INPUT) {
|
|
|
|
|
/* Hacky autodetect; only matters if more than single arg */
|
|
|
|
|
if (argc > 2 && strchr(argv[2], '='))
|
|
|
|
|
input = KEYWORDS;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
else
|
2018-01-29 01:30:15 +01:00
|
|
|
|
input = ORDERED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input == KEYWORDS) {
|
|
|
|
|
tal_append_fmt(&cmd, "{ ");
|
|
|
|
|
for (i = 2; i < argc; i++) {
|
|
|
|
|
const char *eq = strchr(argv[i], '=');
|
|
|
|
|
|
|
|
|
|
if (!eq)
|
|
|
|
|
err(ERROR_USAGE, "Expected key=value in '%s'",
|
|
|
|
|
argv[i]);
|
|
|
|
|
|
|
|
|
|
tal_append_fmt(&cmd, "\"%.*s\" : ",
|
|
|
|
|
(int)(eq - argv[i]), argv[i]);
|
|
|
|
|
|
|
|
|
|
add_input(&cmd, eq + 1, i, argc);
|
|
|
|
|
}
|
|
|
|
|
tal_append_fmt(&cmd, "} }");
|
|
|
|
|
} else {
|
|
|
|
|
tal_append_fmt(&cmd, "[ ");
|
|
|
|
|
for (i = 2; i < argc; i++)
|
|
|
|
|
add_input(&cmd, argv[i], i, argc);
|
|
|
|
|
tal_append_fmt(&cmd, "] }");
|
2016-01-21 21:11:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 14:33:38 +01:00
|
|
|
|
toks = json_parse_simple(ctx, cmd, strlen(cmd));
|
|
|
|
|
if (toks == NULL)
|
|
|
|
|
errx(ERROR_USAGE,
|
|
|
|
|
"Some parameters are malformed, cannot create a valid "
|
|
|
|
|
"JSON-RPC request: %s",
|
|
|
|
|
cmd);
|
|
|
|
|
tal_free(toks);
|
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
|
if (!write_all(fd, cmd, strlen(cmd)))
|
|
|
|
|
err(ERROR_TALKING_TO_LIGHTNINGD, "Writing command");
|
|
|
|
|
|
2018-01-20 05:37:33 +01:00
|
|
|
|
/* Start with 1000 characters, 100 tokens. */
|
2019-07-26 04:23:44 +02:00
|
|
|
|
resp = tal_arr(ctx, char, 1000);
|
|
|
|
|
toks = tal_arr(ctx, jsmntok_t, 100);
|
2020-10-12 07:33:50 +02:00
|
|
|
|
toks[0].type = JSMN_UNDEFINED;
|
cli: handle OOM by directly streaming output.
We use raw malloc here, again, to handle the failure cases more easily.
I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.
diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index f840c0786..d83555a51 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
exit(0);
}
+static void *xrealloc(void *p, size_t len)
+{
+ if (len > 1000000)
+ return NULL;
+ return realloc(p, len);
+}
+#define realloc xrealloc
+
int main(int argc, char *argv[])
{
setup_locale();
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-21 09:13:24 +02:00
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
|
off = 0;
|
2018-01-20 05:37:33 +01:00
|
|
|
|
parserr = 0;
|
|
|
|
|
while (parserr <= 0) {
|
|
|
|
|
/* Read more if parser says, or we have 0 tokens. */
|
|
|
|
|
if (parserr == 0 || parserr == JSMN_ERROR_PART) {
|
2022-01-12 21:36:56 +01:00
|
|
|
|
ssize_t i = cli_read(fd, resp + off, tal_bytelen(resp) - 1 - off);
|
cli: handle OOM by directly streaming output.
We use raw malloc here, again, to handle the failure cases more easily.
I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.
diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index f840c0786..d83555a51 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
exit(0);
}
+static void *xrealloc(void *p, size_t len)
+{
+ if (len > 1000000)
+ return NULL;
+ return realloc(p, len);
+}
+#define realloc xrealloc
+
int main(int argc, char *argv[])
{
setup_locale();
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-21 09:13:24 +02:00
|
|
|
|
if (i == 0)
|
|
|
|
|
errx(ERROR_TALKING_TO_LIGHTNINGD,
|
|
|
|
|
"reading response: socket closed");
|
|
|
|
|
else if (i < 0)
|
2018-01-20 05:37:33 +01:00
|
|
|
|
err(ERROR_TALKING_TO_LIGHTNINGD,
|
|
|
|
|
"reading response");
|
|
|
|
|
off += i;
|
|
|
|
|
/* NUL terminate */
|
|
|
|
|
resp[off] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* (Continue) parsing */
|
2019-07-26 04:23:44 +02:00
|
|
|
|
parserr = jsmn_parse(&parser, resp, off, toks, tal_count(toks));
|
2018-01-20 05:37:33 +01:00
|
|
|
|
|
|
|
|
|
switch (parserr) {
|
|
|
|
|
case JSMN_ERROR_INVAL:
|
|
|
|
|
errx(ERROR_TALKING_TO_LIGHTNINGD,
|
|
|
|
|
"Malformed response '%s'", resp);
|
2020-10-12 07:33:50 +02:00
|
|
|
|
case JSMN_ERROR_NOMEM:
|
2018-01-20 05:37:33 +01:00
|
|
|
|
/* Need more tokens, double it */
|
2019-07-26 04:23:44 +02:00
|
|
|
|
if (!tal_resize(&toks, tal_count(toks) * 2))
|
|
|
|
|
oom_dump(fd, resp, off);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
break;
|
2018-01-20 05:37:33 +01:00
|
|
|
|
case JSMN_ERROR_PART:
|
2020-10-12 07:33:50 +02:00
|
|
|
|
/* We may actually have a complete token! */
|
|
|
|
|
if (toks[0].type == JSMN_UNDEFINED || toks[0].end == -1) {
|
|
|
|
|
/* Need more data: make room if necessary */
|
|
|
|
|
if (off == tal_bytelen(resp) - 1) {
|
|
|
|
|
if (!tal_resize(&resp, tal_count(resp) * 2))
|
|
|
|
|
oom_dump(fd, resp, off);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Otherwise fall through... */
|
|
|
|
|
default:
|
|
|
|
|
if (handle_notify(resp, toks, notification_level,
|
|
|
|
|
&last_was_progress)) {
|
|
|
|
|
/* +2 for \n\n */
|
|
|
|
|
size_t len = toks[0].end - toks[0].start + 2;
|
|
|
|
|
memmove(resp, resp + len, off - len);
|
|
|
|
|
off -= len;
|
|
|
|
|
jsmn_init(&parser);
|
|
|
|
|
toks[0].type = JSMN_UNDEFINED;
|
|
|
|
|
/* Don't force another read! */
|
|
|
|
|
parserr = JSMN_ERROR_NOMEM;
|
cli: handle OOM by directly streaming output.
We use raw malloc here, again, to handle the failure cases more easily.
I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.
diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
index f840c0786..d83555a51 100644
--- a/cli/lightning-cli.c
+++ b/cli/lightning-cli.c
@@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
exit(0);
}
+static void *xrealloc(void *p, size_t len)
+{
+ if (len > 1000000)
+ return NULL;
+ return realloc(p, len);
+}
+#define realloc xrealloc
+
int main(int argc, char *argv[])
{
setup_locale();
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-21 09:13:24 +02:00
|
|
|
|
}
|
2018-01-20 05:37:33 +01:00
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
|
if (toks->type != JSMN_OBJECT)
|
|
|
|
|
errx(ERROR_TALKING_TO_LIGHTNINGD,
|
|
|
|
|
"Non-object response '%s'", resp);
|
|
|
|
|
|
2020-10-12 07:33:50 +02:00
|
|
|
|
if (last_was_progress)
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
|
|
/* This can reallocate toks, so call before getting pointers to tokens */
|
2019-08-13 07:09:19 +02:00
|
|
|
|
format = choose_format(resp, &toks, method, command, format);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
result = json_get_member(resp, toks, "result");
|
|
|
|
|
error = json_get_member(resp, toks, "error");
|
2016-10-23 15:12:05 +02:00
|
|
|
|
if (!error && !result)
|
2016-01-21 21:11:48 +01:00
|
|
|
|
errx(ERROR_TALKING_TO_LIGHTNINGD,
|
2016-10-23 15:12:05 +02:00
|
|
|
|
"Either 'result' or 'error' must be returned in response '%s'", resp);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
id = json_get_member(resp, toks, "id");
|
|
|
|
|
if (!id)
|
|
|
|
|
errx(ERROR_TALKING_TO_LIGHTNINGD,
|
|
|
|
|
"Missing 'id' in response '%s'", resp);
|
|
|
|
|
if (!json_tok_streq(resp, id, idstr))
|
|
|
|
|
errx(ERROR_TALKING_TO_LIGHTNINGD,
|
2020-12-04 13:30:10 +01:00
|
|
|
|
"Incorrect 'id' (%.*s) in response: %.*s",
|
|
|
|
|
json_tok_full_len(id), json_tok_full(resp, id),
|
|
|
|
|
json_tok_full_len(toks), json_tok_full(resp, toks));
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
2016-10-23 15:12:05 +02:00
|
|
|
|
if (!error || json_tok_is_null(resp, error)) {
|
2019-07-26 04:27:01 +02:00
|
|
|
|
switch (format) {
|
|
|
|
|
case HELPLIST:
|
|
|
|
|
human_help(resp, result);
|
|
|
|
|
break;
|
|
|
|
|
case HUMAN:
|
|
|
|
|
human_readable(resp, result, '\n');
|
|
|
|
|
break;
|
2020-05-11 03:03:09 +02:00
|
|
|
|
case FLAT:
|
|
|
|
|
flat_json("", resp, result);
|
|
|
|
|
break;
|
2019-07-26 04:27:01 +02:00
|
|
|
|
case JSON:
|
|
|
|
|
print_json(resp, result, "");
|
|
|
|
|
printf("\n");
|
|
|
|
|
break;
|
|
|
|
|
case RAW:
|
2018-01-29 01:30:15 +01:00
|
|
|
|
printf("%.*s\n",
|
2018-12-16 05:48:06 +01:00
|
|
|
|
json_tok_full_len(result),
|
|
|
|
|
json_tok_full(resp, result));
|
2019-07-26 04:27:01 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
abort();
|
2019-04-08 11:58:44 +02:00
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
tal_free(ctx);
|
2018-01-20 05:38:30 +01:00
|
|
|
|
opt_free_table();
|
2016-01-21 21:11:48 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 11:58:44 +02:00
|
|
|
|
if (format == RAW)
|
|
|
|
|
printf("%.*s\n",
|
|
|
|
|
json_tok_full_len(error), json_tok_full(resp, error));
|
|
|
|
|
else {
|
|
|
|
|
print_json(resp, error, "");
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
tal_free(ctx);
|
2018-01-20 05:38:30 +01:00
|
|
|
|
opt_free_table();
|
2016-01-21 21:11:48 +01:00
|
|
|
|
return 1;
|
|
|
|
|
}
|