db: decouple fatal reliance, have as impl defined function

`fatal` is defined in lightningd and has logfile dependencies etc.

Make it more generic by allowing declaration in the use file (wallet.c)
This commit is contained in:
niftynei 2022-03-01 11:22:15 -06:00 committed by Rusty Russell
parent b0829fc52a
commit 03c950bae8
6 changed files with 54 additions and 37 deletions

View file

@ -809,22 +809,25 @@ void log_backtrace_exit(void)
}
}
void fatal_vfmt(const char *fmt, va_list ap)
{
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
if (!crashlog)
exit(1);
logv(crashlog, LOG_BROKEN, NULL, true, fmt, ap);
abort();
}
void fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
fatal_vfmt(fmt, ap);
va_end(ap);
if (!crashlog)
exit(1);
va_start(ap, fmt);
logv(crashlog, LOG_BROKEN, NULL, true, fmt, ap);
va_end(ap);
abort();
}
struct log_info {

View file

@ -53,6 +53,7 @@ char *arg_log_to_file(const char *arg, struct lightningd *ld);
/* Once this is set, we dump fatal with a backtrace to this log */
extern struct log *crashlog;
void NORETURN PRINTF_FMT(1,2) fatal(const char *fmt, ...);
void NORETURN fatal_vfmt(const char *fmt, va_list ap);
void log_backtrace_print(const char *fmt, ...);
void log_backtrace_exit(void);

View file

@ -6,11 +6,6 @@
#include <ccan/strset/strset.h>
#include <common/autodata.h>
/* For testing, we want to catch fatal messages. */
#ifndef db_fatal
#define db_fatal fatal
#endif
struct db {
char *filename;
const char *in_transaction;
@ -163,6 +158,9 @@ struct db_config {
/* Provide a way for DB backends to register themselves */
AUTODATA_TYPE(db_backends, struct db_config);
void db_fatal(const char *fmt, ...)
PRINTF_FMT(1, 2);
/**
* Report a statement that changes the wallet
*

View file

@ -1,9 +1,6 @@
#include "config.h"
#include <lightningd/log.h>
static void db_test_fatal(const char *fmt, ...);
#define db_fatal db_test_fatal
static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const struct node_id *node_id UNUSED, bool call_notifier UNUSED, const char *fmt UNUSED, ...)
{
}
@ -59,7 +56,7 @@ bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES UNNEEDED)
/* AUTOGENERATED MOCKS END */
static char *db_err;
static void db_test_fatal(const char *fmt, ...)
void db_fatal(const char *fmt, ...)
{
va_list ap;

View file

@ -1,15 +1,34 @@
#include "config.h"
#include <lightningd/log.h>
static void wallet_test_fatal(const char *fmt, ...);
#define db_fatal wallet_test_fatal
#include "test_utils.h"
#include <ccan/tal/str/str.h>
#include <wallet/db_common.h>
static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const struct node_id *node_id UNUSED, bool call_notifier UNUSED, const char *fmt UNUSED, ...)
{
}
#define log_ db_log_
#ifndef DB_FATAL
#define DB_FATAL
static char *wallet_err;
void db_fatal(const char *fmt, ...)
{
va_list ap;
/* Fail hard if we're complaining about not being in transaction */
assert(!strstarts(fmt, "No longer in transaction"));
/* Fail hard if we're complaining about not being in transaction */
assert(!strstarts(fmt, "No longer in transaction"));
va_start(ap, fmt);
wallet_err = tal_vfmt(NULL, fmt, ap);
va_end(ap);
}
#endif /* DB_FATAL */
#include "wallet/wallet.c"
#include "lightningd/htlc_end.c"
#include "lightningd/peer_control.c"
@ -837,22 +856,6 @@ bool fromwire_hsmd_get_channel_basepoints_reply(const void *p UNNEEDED,
return true;
}
static char *wallet_err;
static void wallet_test_fatal(const char *fmt, ...)
{
va_list ap;
/* Fail hard if we're complaining about not being in transaction */
assert(!strstarts(fmt, "No longer in transaction"));
/* Fail hard if we're complaining about not being in transaction */
assert(!strstarts(fmt, "No longer in transaction"));
va_start(ap, fmt);
wallet_err = tal_vfmt(NULL, fmt, ap);
va_end(ap);
}
#define transaction_wrap(db, ...) \
(db_begin_transaction(db), __VA_ARGS__, db_commit_transaction(db), wallet_err == NULL)

View file

@ -45,6 +45,21 @@ struct channel_state_param {
const enum channel_state_bucket state;
};
/* Implement db_fatal, as a wrapper around fatal.
* We use a ifndef block so that it can get be
* implemented in a test file first, if necessary */
#ifndef DB_FATAL
#define DB_FATAL
void db_fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fatal_vfmt(fmt, ap);
va_end(ap);
}
#endif /* DB_FATAL */
static void outpointfilters_init(struct wallet *w)
{
struct db_stmt *stmt;