Skip to content

Commit

Permalink
common/daemon: common routines for all daemons.
Browse files Browse the repository at this point in the history
In particular, the main daemon and subdaemons share the backtrace code,
with hooks for logging.

The daemon hook inserts the io_poll override, which means we no longer
need io_debug.[ch].  Though most daemons don't need it, they still link
against ccan/io, so it's harmess (suggested by @ZmnSCPxj).

This was tested manually to make sure we get backtraces still.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Apr 3, 2018
1 parent 8975fc2 commit 1a4a59d
Show file tree
Hide file tree
Showing 24 changed files with 220 additions and 212 deletions.
2 changes: 1 addition & 1 deletion channeld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ CHANNELD_COMMON_OBJS := \
common/crypto_state.o \
common/crypto_sync.o \
common/cryptomsg.o \
common/daemon.o \
common/daemon_conn.o \
common/derive_basepoints.o \
common/dev_disconnect.o \
Expand All @@ -48,7 +49,6 @@ CHANNELD_COMMON_OBJS := \
common/htlc_wire.o \
common/initial_channel.o \
common/initial_commit_tx.o \
common/io_debug.o \
common/keyset.o \
common/key_derive.o \
common/memleak.o \
Expand Down
3 changes: 1 addition & 2 deletions channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <common/derive_basepoints.h>
#include <common/dev_disconnect.h>
#include <common/htlc_tx.h>
#include <common/io_debug.h>
#include <common/key_derive.h>
#include <common/msg_queue.h>
#include <common/peer_billboard.h>
Expand Down Expand Up @@ -2753,6 +2752,6 @@ int main(int argc, char *argv[])
/* We only exit when shutdown is complete. */
assert(shutdown_complete(peer));
send_shutdown_complete(peer);
subdaemon_shutdown();
daemon_shutdown();
return 0;
}
1 change: 1 addition & 0 deletions closingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ CLOSINGD_COMMON_OBJS := \
common/crypto_state.o \
common/crypto_sync.o \
common/cryptomsg.o \
common/daemon.o \
common/daemon_conn.o \
common/dev_disconnect.o \
common/derive_basepoints.o \
Expand Down
2 changes: 1 addition & 1 deletion closingd/closing.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ int main(int argc, char *argv[])
wire_sync_write(REQ_FD,
take(towire_closing_complete(NULL, gossip_index)));
tal_free(ctx);
subdaemon_shutdown();
daemon_shutdown();

return 0;
}
2 changes: 1 addition & 1 deletion common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ COMMON_SRC_NOGEN := \
common/crypto_state.c \
common/crypto_sync.c \
common/cryptomsg.c \
common/daemon.c \
common/daemon_conn.c \
common/derive_basepoints.c \
common/dev_disconnect.c \
Expand All @@ -19,7 +20,6 @@ COMMON_SRC_NOGEN := \
common/htlc_wire.c \
common/initial_channel.c \
common/initial_commit_tx.c \
common/io_debug.c \
common/json.c \
common/json_escaped.c \
common/key_derive.c \
Expand Down
111 changes: 111 additions & 0 deletions common/daemon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <backtrace-supported.h>
#include <backtrace.h>
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/str/str.h>
#include <common/daemon.h>
#include <common/status.h>
#include <common/utils.h>
#include <common/version.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <wally_core.h>

#if BACKTRACE_SUPPORTED
static struct backtrace_state *backtrace_state;
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;
}

static void crashdump(int sig)
{
/* We do stderr first, since it's most reliable. */
warnx("Fatal signal %d", sig);
backtrace_print(backtrace_state, 0, stderr);

/* Now send to parent. */
bt_print("FATAL SIGNAL %d", sig);
backtrace_full(backtrace_state, 0, backtrace_status, NULL, NULL);

/* 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);
}
#endif

static int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout)
{
const char *t;

t = taken_any();
if (t)
errx(1, "Outstanding taken pointers: %s", t);

clean_tmpctx();

return poll(fds, nfds, timeout);
}

void daemon_setup(const char *argv0,
void (*backtrace_print)(const char *fmt, ...),
void (*backtrace_exit)(void))
{
err_set_progname(argv0);

bt_print = backtrace_print;
bt_exit = backtrace_exit;

#if BACKTRACE_SUPPORTED
#if DEVELOPER
/* Suppresses backtrace (breaks valgrind) */
if (!getenv("LIGHTNINGD_DEV_NO_BACKTRACE"))
backtrace_state = backtrace_create_state(argv0, 0, NULL, NULL);
#else
backtrace_state = backtrace_create_state(argv0, 0, NULL, NULL);
#endif
crashlog_activate();
#endif

/* We handle write returning errors! */
signal(SIGPIPE, SIG_IGN);
secp256k1_ctx = wally_get_secp_context();

setup_tmpctx();
io_poll_override(daemon_poll);
}

void daemon_shutdown(void)
{
tal_free(tmpctx);
wally_cleanup(0);
}
13 changes: 13 additions & 0 deletions common/daemon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef LIGHTNING_COMMON_DAEMON_H
#define LIGHTNING_COMMON_DAEMON_H
#include "config.h"

/* Common setup for all daemons */
void daemon_setup(const char *argv0,
void (*backtrace_print)(const char *fmt, ...),
void (*backtrace_exit)(void));

/* Shutdown for a valgrind-clean exit (frees everything) */
void daemon_shutdown(void);

#endif /* LIGHTNING_COMMON_DAEMON_H */
17 changes: 0 additions & 17 deletions common/io_debug.c

This file was deleted.

9 changes: 0 additions & 9 deletions common/io_debug.h

This file was deleted.

65 changes: 9 additions & 56 deletions common/subdaemon.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#include <backtrace-supported.h>
#include <backtrace.h>
#include <ccan/err/err.h>
#include <ccan/str/str.h>
#include <common/dev_disconnect.h>
#include <common/status.h>
#include <common/subdaemon.h>
Expand All @@ -14,47 +10,19 @@
#include <unistd.h>
#include <wally_core.h>

#if BACKTRACE_SUPPORTED
static struct backtrace_state *backtrace_state;

static int backtrace_status(void *unused UNUSED, uintptr_t pc,
const char *filename, int lineno,
const char *function)
{
fprintf(stderr, "backtrace: %s:%d (%s) %p\n",
filename, lineno, function, (void *)pc);
status_trace("backtrace: %s:%d (%s) %p",
filename, lineno, function, (void *)pc);
return 0;
}

static void crashdump(int sig)
static void status_backtrace_print(const char *fmt, ...)
{
/* We do stderr first, since it's most reliable. */
warnx("Fatal signal %d", sig);
backtrace_print(backtrace_state, 0, stderr);
va_list ap;

/* Now send to parent. */
backtrace_full(backtrace_state, 0, backtrace_status, NULL, NULL);
status_failed(STATUS_FAIL_INTERNAL_ERROR, "FATAL SIGNAL %d", sig);
va_start(ap, fmt);
status_vfmt(LOG_BROKEN, fmt, ap);
va_end(ap);
}

static void crashlog_activate(void)
static void status_backtrace_exit(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);
status_failed(STATUS_FAIL_INTERNAL_ERROR, "FATAL SIGNAL");
}
#endif

#if DEVELOPER
extern volatile bool debugger_connected;
Expand All @@ -68,18 +36,6 @@ void subdaemon_setup(int argc, char *argv[])
exit(0);
}

err_set_progname(argv[0]);
#if BACKTRACE_SUPPORTED
backtrace_state = backtrace_create_state(argv[0], 0, NULL, NULL);
crashlog_activate();
#endif

/* We handle write returning errors! */
signal(SIGPIPE, SIG_IGN);
secp256k1_ctx = wally_get_secp_context();

setup_tmpctx();

for (int i = 1; i < argc; i++) {
if (streq(argv[i], "--log-io"))
logging_io = true;
Expand All @@ -100,10 +56,7 @@ void subdaemon_setup(int argc, char *argv[])
}
}
#endif
}

void subdaemon_shutdown(void)
{
tal_free(tmpctx);
wally_cleanup(0);
daemon_setup(argv[0], status_backtrace_print, status_backtrace_exit);
}

5 changes: 2 additions & 3 deletions common/subdaemon.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#ifndef LIGHTNING_COMMON_SUBDAEMON_H
#define LIGHTNING_COMMON_SUBDAEMON_H
#include "config.h"
#include <common/daemon.h>

/* daemon_setup, but for subdaemons */
void subdaemon_setup(int argc, char *argv[]);

/* Shutdown for a valgrind-clean exit (frees everything) */
void subdaemon_shutdown(void);

#endif /* LIGHTNING_COMMON_SUBDAEMON_H */
2 changes: 1 addition & 1 deletion gossipd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ GOSSIPD_COMMON_OBJS := \
common/crypto_state.o \
common/crypto_sync.o \
common/cryptomsg.o \
common/daemon.o \
common/daemon_conn.o \
common/dev_disconnect.o \
common/features.o \
common/gen_status_wire.o \
common/io_debug.o \
common/msg_queue.o \
common/ping.o \
common/pseudorand.o \
Expand Down
4 changes: 1 addition & 3 deletions gossipd/gossip.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <common/cryptomsg.h>
#include <common/daemon_conn.h>
#include <common/features.h>
#include <common/io_debug.h>
#include <common/ping.h>
#include <common/status.h>
#include <common/subdaemon.h>
Expand Down Expand Up @@ -2118,7 +2117,6 @@ int main(int argc, char *argv[])
struct daemon *daemon;

subdaemon_setup(argc, argv);
io_poll_override(debug_poll);

daemon = tal(NULL, struct daemon);
list_head_init(&daemon->peers);
Expand Down Expand Up @@ -2147,7 +2145,7 @@ int main(int argc, char *argv[])
timer_expired(daemon, expired);
}
}
subdaemon_shutdown();
daemon_shutdown();
return 0;
}
#endif
2 changes: 1 addition & 1 deletion hsmd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ LIGHTNINGD_HSM_OBJS := $(LIGHTNINGD_HSM_SRC:.c=.o)
# Common source we use.
HSMD_COMMON_OBJS := \
common/bip32.o \
common/daemon.o \
common/daemon_conn.o \
common/derive_basepoints.o \
common/funding_tx.o \
common/gen_status_wire.o \
common/hash_u5.o \
common/io_debug.o \
common/key_derive.o \
common/msg_queue.o \
common/permute_tx.o \
Expand Down
Loading

0 comments on commit 1a4a59d

Please sign in to comment.