2017-09-12 06:56:59 +02:00
|
|
|
#include <backtrace.h>
|
|
|
|
#include <ccan/err/err.h>
|
2017-02-24 06:52:56 +01:00
|
|
|
#include <ccan/str/str.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/dev_disconnect.h>
|
2017-09-12 06:56:59 +02:00
|
|
|
#include <common/status.h>
|
2018-01-08 11:01:09 +01:00
|
|
|
#include <common/subdaemon.h>
|
|
|
|
#include <common/utils.h>
|
|
|
|
#include <common/version.h>
|
2017-09-12 06:56:59 +02:00
|
|
|
#include <signal.h>
|
2017-02-24 06:52:56 +01:00
|
|
|
#include <stdio.h>
|
2017-05-24 12:10:16 +02:00
|
|
|
#include <stdlib.h>
|
2017-02-24 06:52:56 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-09-12 06:56:59 +02:00
|
|
|
static struct backtrace_state *backtrace_state;
|
|
|
|
|
2018-02-21 16:06:07 +01:00
|
|
|
static int backtrace_status(void *unused UNUSED, uintptr_t pc,
|
2017-09-12 06:56:59 +02:00
|
|
|
const char *filename, int lineno,
|
|
|
|
const char *function)
|
|
|
|
{
|
2018-03-02 09:59:17 +01:00
|
|
|
fprintf(stderr, "backtrace: %s:%u (%s) %p\n",
|
|
|
|
filename, lineno, function, (void *)pc);
|
2017-09-12 06:56:59 +02:00
|
|
|
status_trace("backtrace: %s:%u (%s) %p",
|
|
|
|
filename, lineno, function, (void *)pc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void crashdump(int sig)
|
|
|
|
{
|
|
|
|
/* We do stderr first, since it's most reliable. */
|
|
|
|
warnx("Fatal signal %u", sig);
|
|
|
|
backtrace_print(backtrace_state, 0, stderr);
|
|
|
|
|
|
|
|
/* Now send to parent. */
|
|
|
|
backtrace_full(backtrace_state, 0, backtrace_status, NULL, NULL);
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR, "FATAL SIGNAL %u", 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);
|
|
|
|
}
|
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2018-01-15 10:36:17 +01:00
|
|
|
extern volatile bool debugger_connected;
|
|
|
|
volatile bool debugger_connected;
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2017-02-24 06:52:56 +01:00
|
|
|
|
2018-01-15 10:36:17 +01:00
|
|
|
void subdaemon_setup(int argc, char *argv[])
|
|
|
|
{
|
2018-01-08 11:01:09 +01:00
|
|
|
if (argc == 2 && streq(argv[1], "--version")) {
|
|
|
|
printf("%s\n", version());
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2017-09-12 06:56:59 +02:00
|
|
|
err_set_progname(argv[0]);
|
|
|
|
backtrace_state = backtrace_create_state(argv[0], 0, NULL, NULL);
|
|
|
|
crashlog_activate();
|
|
|
|
|
2018-01-08 11:01:09 +01:00
|
|
|
/* We handle write returning errors! */
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
|
|
|
|
| SECP256K1_CONTEXT_SIGN);
|
|
|
|
|
2018-03-15 05:30:37 +01:00
|
|
|
setup_tmpctx();
|
|
|
|
|
2018-01-15 10:36:17 +01:00
|
|
|
for (int i = 1; i < argc; i++) {
|
2018-02-05 05:09:28 +01:00
|
|
|
if (streq(argv[i], "--log-io"))
|
|
|
|
logging_io = true;
|
2017-05-24 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 05:09:28 +01:00
|
|
|
#if DEVELOPER
|
2018-01-15 10:36:17 +01:00
|
|
|
/* From debugger, set debugger_spin to 0. */
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
if (streq(argv[i], "--debugger")) {
|
|
|
|
fprintf(stderr, "gdb -ex 'attach %u' -ex 'p debugger_connected=1' %s\n",
|
|
|
|
getpid(), argv[0]);
|
|
|
|
while (!debugger_connected)
|
|
|
|
usleep(10000);
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|
2018-02-05 05:09:28 +01:00
|
|
|
if (strstarts(argv[i], "--dev-disconnect=")) {
|
|
|
|
dev_disconnect_init(atoi(argv[i]
|
|
|
|
+ strlen("--dev-disconnect=")));
|
|
|
|
}
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|