Skip to content

Commit 2bf03cb

Browse files
plugins/bcli: added rpcwait
Changelog-None Signed-off-by: Nishant Bansal <[email protected]>
1 parent 48f3ed8 commit 2bf03cb

File tree

2 files changed

+50
-84
lines changed

2 files changed

+50
-84
lines changed

lightningd/log.c

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "config.h"
22
#include <ccan/err/err.h>
33
#include <ccan/io/io.h>
4-
#include <ccan/json_escape/json_escape.h>
54
#include <ccan/read_write_all/read_write_all.h>
65
#include <ccan/str/hex/hex.h>
76
#include <ccan/tal/link/link.h>
@@ -558,17 +557,23 @@ static void maybe_notify_log(struct logger *log,
558557
notify_log(log->log_book->ld, l);
559558
}
560559

561-
static void add_one_log_entry(struct logger *log, enum log_level level,
562-
const struct node_id *node_id, bool call_notifier,
563-
const char *log_msg)
560+
void logv(struct logger *log, enum log_level level,
561+
const struct node_id *node_id,
562+
bool call_notifier,
563+
const char *fmt, va_list ap)
564564
{
565+
int save_errno = errno;
565566
struct log_entry *l = new_log_entry(log, level, node_id);
566567

567-
l->log = strdup(log_msg);
568+
/* This is WARN_UNUSED_RESULT, because everyone should somehow deal
569+
* with OOM, even though nobody does. */
570+
if (vasprintf(&l->log, fmt, ap) == -1)
571+
abort();
568572

569-
/* Sanitize any non-printable characters, and replace with '?'
570-
*/
571-
for (size_t i = 0; log_msg[i] != '\0'; i++)
573+
size_t log_len = strlen(l->log);
574+
575+
/* Sanitize any non-printable characters, and replace with '?' */
576+
for (size_t i=0; i<log_len; i++)
572577
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
573578
l->log[i] = '?';
574579

@@ -579,39 +584,6 @@ static void add_one_log_entry(struct logger *log, enum log_level level,
579584

580585
if (call_notifier)
581586
notify_warning(log->log_book->ld, l);
582-
}
583-
584-
void logv(struct logger *log, enum log_level level,
585-
const struct node_id *node_id,
586-
bool call_notifier,
587-
const char *fmt, va_list ap)
588-
{
589-
int save_errno = errno;
590-
char *log_msg = NULL;
591-
const char *unescaped_log;
592-
593-
/* This is WARN_UNUSED_RESULT, because everyone should somehow deal
594-
* with OOM, even though nobody does. */
595-
if (vasprintf(&log_msg, fmt, ap) == -1)
596-
abort();
597-
598-
/* Nothing to escape: simple copy */
599-
if (!strchr(log_msg, '\\'))
600-
add_one_log_entry(log, level, node_id, call_notifier, log_msg);
601-
/* If it's weird, unescaping can fail */
602-
else if ((unescaped_log = json_escape_unescape(
603-
tmpctx, (struct json_escape *)log_msg)) == NULL)
604-
add_one_log_entry(log, level, node_id, call_notifier, log_msg);
605-
else {
606-
char **lines = tal_strsplit(unescaped_log, unescaped_log, "\n",
607-
STR_EMPTY_OK);
608-
/* Split to lines and log them separately. */
609-
for (size_t i = 0; lines[i]; i++)
610-
add_one_log_entry(log, level, node_id, call_notifier,
611-
lines[i]);
612-
}
613-
614-
free(log_msg);
615587

616588
errno = save_errno;
617589
}
@@ -1206,4 +1178,4 @@ static const struct json_command getlog_command = {
12061178
"getlog",
12071179
json_getlog,
12081180
};
1209-
AUTODATA(json_command, &getlog_command);
1181+
AUTODATA(json_command, &getlog_command);

plugins/bcli.c

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static const char **gather_argsv(const tal_t *ctx, const char *cmd, va_list ap)
101101
const char *arg;
102102

103103
args[0] = bitcoind->cli ? bitcoind->cli : chainparams->cli;
104+
add_arg(&args, "-rpcwait");
104105
if (chainparams->cli_args)
105106
add_arg(&args, chainparams->cli_args);
106107
if (bitcoind->datadir)
@@ -129,6 +130,7 @@ static const char **gather_argsv(const tal_t *ctx, const char *cmd, va_list ap)
129130
// system processes.
130131
add_arg(&args, "-stdinrpcpass");
131132

133+
132134
add_arg(&args, cmd);
133135
while ((arg = va_arg(ap, char *)) != NULL)
134136
add_arg(&args, arg);
@@ -1038,56 +1040,48 @@ static void wait_and_check_bitcoind(struct plugin *p)
10381040
int in, from, status, ret;
10391041
pid_t child;
10401042
const char **cmd = gather_args(bitcoind, "getnetworkinfo", NULL);
1041-
bool printed = false;
10421043
char *output = NULL;
10431044

1044-
for (;;) {
1045-
tal_free(output);
1045+
plugin_log(p, LOG_UNUSUAL, "Waiting for bitcoind to warm up...");
10461046

1047-
child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd));
1047+
child = pipecmdarr(&in, &from, &from, cast_const2(char **, cmd));
10481048

1049-
if (bitcoind->rpcpass)
1050-
write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass));
1049+
if (bitcoind->rpcpass)
1050+
write_all(in, bitcoind->rpcpass, strlen(bitcoind->rpcpass));
10511051

1052-
close(in);
1052+
close(in);
10531053

1054-
if (child < 0) {
1055-
if (errno == ENOENT)
1056-
bitcoind_failure(p, "bitcoin-cli not found. Is bitcoin-cli "
1057-
"(part of Bitcoin Core) available in your PATH?");
1058-
plugin_err(p, "%s exec failed: %s", cmd[0], strerror(errno));
1059-
}
1054+
if (child < 0) {
1055+
if (errno == ENOENT)
1056+
bitcoind_failure(
1057+
p,
1058+
"bitcoin-cli not found. Is bitcoin-cli "
1059+
"(part of Bitcoin Core) available in your PATH?");
1060+
plugin_err(p, "%s exec failed: %s", cmd[0], strerror(errno));
1061+
}
10601062

1061-
output = grab_fd(cmd, from);
1062-
1063-
while ((ret = waitpid(child, &status, 0)) < 0 && errno == EINTR);
1064-
if (ret != child)
1065-
bitcoind_failure(p, tal_fmt(bitcoind, "Waiting for %s: %s",
1066-
cmd[0], strerror(errno)));
1067-
if (!WIFEXITED(status))
1068-
bitcoind_failure(p, tal_fmt(bitcoind, "Death of %s: signal %i",
1069-
cmd[0], WTERMSIG(status)));
1070-
1071-
if (WEXITSTATUS(status) == 0)
1072-
break;
1073-
1074-
/* bitcoin/src/rpc/protocol.h:
1075-
* RPC_IN_WARMUP = -28, //!< Client still warming up
1076-
*/
1077-
if (WEXITSTATUS(status) != 28) {
1078-
if (WEXITSTATUS(status) == 1)
1079-
bitcoind_failure(p, "Could not connect to bitcoind using"
1080-
" bitcoin-cli. Is bitcoind running?");
1081-
bitcoind_failure(p, tal_fmt(bitcoind, "%s exited with code %i: %s",
1082-
cmd[0], WEXITSTATUS(status), output));
1083-
}
1063+
output = grab_fd(cmd, from);
10841064

1085-
if (!printed) {
1086-
plugin_log(p, LOG_UNUSUAL,
1087-
"Waiting for bitcoind to warm up...");
1088-
printed = true;
1089-
}
1090-
sleep(1);
1065+
while ((ret = waitpid(child, &status, 0)) < 0 && errno == EINTR)
1066+
;
1067+
if (ret != child)
1068+
bitcoind_failure(p, tal_fmt(bitcoind, "Waiting for %s: %s",
1069+
cmd[0], strerror(errno)));
1070+
if (!WIFEXITED(status))
1071+
bitcoind_failure(p, tal_fmt(bitcoind, "Death of %s: signal %i",
1072+
cmd[0], WTERMSIG(status)));
1073+
1074+
/* bitcoin/src/rpc/protocol.h:
1075+
* RPC_IN_WARMUP = -28, //!< Client still warming up
1076+
*/
1077+
if (WEXITSTATUS(status) != 28) {
1078+
if (WEXITSTATUS(status) == 1)
1079+
bitcoind_failure(p,
1080+
"Could not connect to bitcoind using"
1081+
" bitcoin-cli. Is bitcoind running?");
1082+
bitcoind_failure(p,
1083+
tal_fmt(bitcoind, "%s exited with code %i: %s",
1084+
cmd[0], WEXITSTATUS(status), output));
10911085
}
10921086

10931087
parse_getnetworkinfo_result(p, output);

0 commit comments

Comments
 (0)