diff --git a/plugins/bkpr/db.c b/plugins/bkpr/db.c index 0b44de609..9d89c17b9 100644 --- a/plugins/bkpr/db.c +++ b/plugins/bkpr/db.c @@ -13,6 +13,8 @@ struct migration { void (*func)(struct plugin *p, struct db *db); }; +static struct plugin *plugin; + /* Do not reorder or remove elements from this array. * It is used to migrate existing databases from a prevoius state, based on * string indicies */ @@ -136,21 +138,21 @@ static bool db_migrate(struct plugin *p, struct db *db) void db_fatal(const char *fmt, ...) { va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); + plugin_errv(plugin, fmt, ap); + /* Won't actually exit, but va_end() required to balance va_start in standard. */ va_end(ap); - - exit(1); } #endif /* DB_FATAL */ struct db *db_setup(const tal_t *ctx, struct plugin *p, char *db_dsn) { bool migrated; - struct db *db = db_open(ctx, db_dsn); + struct db *db; + /* Set global for db_fatal */ + plugin = p; + db = db_open(ctx, db_dsn); db->report_changes_fn = NULL; db_begin_transaction(db); diff --git a/plugins/libplugin.c b/plugins/libplugin.c index 9098506ae..900b1cc2f 100644 --- a/plugins/libplugin.c +++ b/plugins/libplugin.c @@ -1288,18 +1288,26 @@ void NORETURN plugin_exit(struct plugin *p, int exitcode) exit(exitcode); } +void NORETURN plugin_errv(struct plugin *p, const char *fmt, va_list ap) +{ + va_list ap2; + + /* In case it gets consumed, make a copy. */ + va_copy(ap2, ap); + + plugin_logv(p, LOG_BROKEN, fmt, ap); + vfprintf(stderr, fmt, ap2); + plugin_exit(p, 1); + va_end(ap2); +} + void NORETURN plugin_err(struct plugin *p, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - plugin_logv(p, LOG_BROKEN, fmt, ap); + plugin_errv(p, fmt, ap); va_end(ap); - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); - va_end(ap); - plugin_exit(p, 1); } void plugin_log(struct plugin *p, enum log_level l, const char *fmt, ...) diff --git a/plugins/libplugin.h b/plugins/libplugin.h index 6342460cd..d0e9336f5 100644 --- a/plugins/libplugin.h +++ b/plugins/libplugin.h @@ -17,6 +17,7 @@ #include #include #include +#include struct json_out; struct plugin; @@ -226,6 +227,9 @@ struct command_result *command_param_failed(void); /* Call this on fatal error. */ void NORETURN plugin_err(struct plugin *p, const char *fmt, ...); +/* Call this on fatal error. */ +void NORETURN plugin_errv(struct plugin *p, const char *fmt, va_list ap); + /* Normal exit (makes sure to flush output!). */ void NORETURN plugin_exit(struct plugin *p, int exitcode);