2021-02-01 03:58:50 +01:00
|
|
|
#include "config.h"
|
2018-10-25 01:46:10 +02:00
|
|
|
#include <assert.h>
|
2018-03-29 04:06:45 +02:00
|
|
|
#include <backtrace-supported.h>
|
|
|
|
#include <backtrace.h>
|
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <ccan/io/io.h>
|
2018-12-08 01:30:56 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2018-03-29 04:06:45 +02:00
|
|
|
#include <common/daemon.h>
|
2018-11-21 22:45:37 +01:00
|
|
|
#include <common/memleak.h>
|
2020-05-16 00:13:22 +02:00
|
|
|
#include <common/setup.h>
|
2018-03-29 04:06:45 +02:00
|
|
|
#include <common/utils.h>
|
|
|
|
#include <common/version.h>
|
|
|
|
#include <signal.h>
|
2020-05-15 03:20:56 +02:00
|
|
|
|
2018-03-29 04:06:45 +02:00
|
|
|
#if BACKTRACE_SUPPORTED
|
|
|
|
static void (*bt_print)(const char *fmt, ...) PRINTF_FMT(1,2);
|
|
|
|
static void (*bt_exit)(void);
|
|
|
|
|
|
|
|
static int backtrace_status(void *unused UNUSED, uintptr_t pc,
|
|
|
|
const char *filename, int lineno,
|
|
|
|
const char *function)
|
|
|
|
{
|
|
|
|
bt_print("backtrace: %s:%d (%s) %p",
|
|
|
|
filename, lineno, function, (void *)pc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-08 01:51:30 +02:00
|
|
|
void send_backtrace(const char *why)
|
2018-03-29 04:06:45 +02:00
|
|
|
{
|
|
|
|
/* We do stderr first, since it's most reliable. */
|
2019-04-08 01:51:30 +02:00
|
|
|
warnx("%s (version %s)", why, version());
|
2018-04-03 09:28:18 +02:00
|
|
|
if (backtrace_state)
|
|
|
|
backtrace_print(backtrace_state, 0, stderr);
|
2018-03-29 04:06:45 +02:00
|
|
|
|
|
|
|
/* Now send to parent. */
|
2019-04-08 01:51:30 +02:00
|
|
|
bt_print("%s (version %s)", why, version());
|
2018-04-03 09:28:18 +02:00
|
|
|
if (backtrace_state)
|
|
|
|
backtrace_full(backtrace_state, 0, backtrace_status, NULL, NULL);
|
2019-04-08 01:51:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void crashdump(int sig)
|
|
|
|
{
|
|
|
|
char why[100];
|
|
|
|
|
|
|
|
snprintf(why, 100, "FATAL SIGNAL %d", sig);
|
|
|
|
send_backtrace(why);
|
2018-03-29 04:06:45 +02:00
|
|
|
|
|
|
|
/* Probably shouldn't return. */
|
|
|
|
bt_exit();
|
|
|
|
|
|
|
|
/* This time it will kill us instantly. */
|
|
|
|
kill(getpid(), sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void crashlog_activate(void)
|
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
sa.sa_handler = crashdump;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
|
|
|
|
/* We want to fall through to default handler */
|
|
|
|
sa.sa_flags = SA_RESETHAND;
|
|
|
|
sigaction(SIGILL, &sa, NULL);
|
|
|
|
sigaction(SIGABRT, &sa, NULL);
|
|
|
|
sigaction(SIGFPE, &sa, NULL);
|
|
|
|
sigaction(SIGSEGV, &sa, NULL);
|
|
|
|
sigaction(SIGBUS, &sa, NULL);
|
|
|
|
}
|
2019-04-08 01:51:30 +02:00
|
|
|
#else
|
|
|
|
void send_backtrace(const char *why)
|
|
|
|
{
|
|
|
|
}
|
2018-03-29 04:06:45 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-03 05:40:00 +02:00
|
|
|
int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
2018-03-29 04:06:45 +02:00
|
|
|
{
|
|
|
|
const char *t;
|
|
|
|
|
|
|
|
t = taken_any();
|
|
|
|
if (t)
|
|
|
|
errx(1, "Outstanding taken pointers: %s", t);
|
|
|
|
|
2020-09-23 12:43:28 +02:00
|
|
|
if (wally_tal_ctx)
|
|
|
|
errx(1, "Outstanding tal_wally_start!");
|
2020-09-23 03:37:04 +02:00
|
|
|
|
2018-03-29 04:06:45 +02:00
|
|
|
clean_tmpctx();
|
|
|
|
|
|
|
|
return poll(fds, nfds, timeout);
|
|
|
|
}
|
|
|
|
|
2018-11-26 14:28:51 +01:00
|
|
|
#if DEVELOPER && BACKTRACE_SUPPORTED
|
2018-10-25 01:46:10 +02:00
|
|
|
static void steal_notify(tal_t *child, enum tal_notify_type n, tal_t *newparent)
|
|
|
|
{
|
|
|
|
tal_t *p = newparent;
|
|
|
|
|
|
|
|
assert(tal_parent(child) == newparent);
|
|
|
|
while ((p = tal_parent(p)) != NULL)
|
|
|
|
assert(p != child);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_steal_notifier(tal_t *parent UNUSED,
|
|
|
|
enum tal_notify_type type UNNEEDED,
|
|
|
|
void *child)
|
|
|
|
{
|
|
|
|
tal_add_notifier(child, TAL_NOTIFY_ADD_CHILD, add_steal_notifier);
|
|
|
|
tal_add_notifier(child, TAL_NOTIFY_STEAL, steal_notify);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_steal_notifiers(const tal_t *root)
|
|
|
|
{
|
|
|
|
tal_add_notifier(root, TAL_NOTIFY_ADD_CHILD, add_steal_notifier);
|
|
|
|
|
|
|
|
for (const tal_t *i = tal_first(root); i; i = tal_next(i))
|
|
|
|
add_steal_notifiers(i);
|
|
|
|
}
|
2020-12-03 02:21:35 +01:00
|
|
|
|
|
|
|
static void remove_steal_notifiers(void)
|
|
|
|
{
|
|
|
|
/* We remove this from root, assuming everything else freed. */
|
|
|
|
tal_del_notifier(NULL, add_steal_notifier);
|
|
|
|
}
|
2018-10-25 01:46:10 +02:00
|
|
|
#endif
|
|
|
|
|
2018-03-29 04:06:45 +02:00
|
|
|
void daemon_setup(const char *argv0,
|
|
|
|
void (*backtrace_print)(const char *fmt, ...),
|
|
|
|
void (*backtrace_exit)(void))
|
|
|
|
{
|
2020-05-16 00:13:22 +02:00
|
|
|
common_setup(argv0);
|
2018-04-05 01:19:31 +02:00
|
|
|
#if BACKTRACE_SUPPORTED
|
2018-03-29 04:06:45 +02:00
|
|
|
bt_print = backtrace_print;
|
|
|
|
bt_exit = backtrace_exit;
|
|
|
|
#if DEVELOPER
|
|
|
|
/* Suppresses backtrace (breaks valgrind) */
|
|
|
|
if (!getenv("LIGHTNINGD_DEV_NO_BACKTRACE"))
|
|
|
|
backtrace_state = backtrace_create_state(argv0, 0, NULL, NULL);
|
2018-10-25 01:46:10 +02:00
|
|
|
add_steal_notifiers(NULL);
|
2018-03-29 04:06:45 +02:00
|
|
|
#else
|
|
|
|
backtrace_state = backtrace_create_state(argv0, 0, NULL, NULL);
|
|
|
|
#endif
|
|
|
|
crashlog_activate();
|
|
|
|
#endif
|
|
|
|
|
2018-11-21 22:45:37 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
/* This has significant overhead, so we only enable it if told */
|
|
|
|
if (getenv("LIGHTNINGD_DEV_MEMLEAK"))
|
|
|
|
memleak_init();
|
|
|
|
#endif
|
|
|
|
|
2018-03-29 04:06:45 +02:00
|
|
|
/* We handle write returning errors! */
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2020-05-15 03:20:56 +02:00
|
|
|
|
2018-03-29 04:06:45 +02:00
|
|
|
io_poll_override(daemon_poll);
|
|
|
|
}
|
|
|
|
|
|
|
|
void daemon_shutdown(void)
|
|
|
|
{
|
2020-05-16 00:13:22 +02:00
|
|
|
common_shutdown();
|
2020-12-03 02:21:35 +01:00
|
|
|
|
|
|
|
#if DEVELOPER && BACKTRACE_SUPPORTED
|
|
|
|
remove_steal_notifiers();
|
|
|
|
#endif
|
2018-03-29 04:06:45 +02:00
|
|
|
}
|
2018-12-08 01:30:56 +01:00
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
void daemon_maybe_debug(char *argv[])
|
2018-12-08 01:30:56 +01:00
|
|
|
{
|
|
|
|
#if DEVELOPER
|
2019-01-15 05:13:27 +01:00
|
|
|
for (int i = 1; argv[i]; i++) {
|
2018-12-08 01:30:56 +01:00
|
|
|
if (!streq(argv[i], "--debugger"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Don't let this mess up stdout, so redir to /dev/null */
|
|
|
|
char *cmd = tal_fmt(NULL, "${DEBUG_TERM:-gnome-terminal --} gdb -ex 'attach %u' %s >/dev/null &", getpid(), argv[0]);
|
|
|
|
fprintf(stderr, "Running %s\n", cmd);
|
|
|
|
/* warn_unused_result is fascist bullshit.
|
|
|
|
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */
|
|
|
|
if (system(cmd))
|
|
|
|
;
|
|
|
|
/* Continue in the debugger. */
|
|
|
|
kill(getpid(), SIGSTOP);
|
|
|
|
}
|
|
|
|
#endif /* DEVELOPER */
|
|
|
|
}
|