From 0f97b8cf3602a80e36ad20de553d609bc7533262 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 8 Jan 2018 20:31:09 +1030 Subject: [PATCH] subdaemon.c: subdaemon_setup() routine for all daemons. Our handling of SIGPIPE was incoherent and inconsistent, and we had much cut & paste between the daemons. They should *ALL* ignore SIGPIPE, and much of the rest of the boilerplate can be shared, so should be. Reported-by: @ZmnSCPxj Fixes: #528 Signed-off-by: Rusty Russell --- channeld/Makefile | 2 +- channeld/channel.c | 15 ++------------- closingd/Makefile | 2 +- closingd/closing.c | 14 ++------------ common/Makefile | 2 +- common/debug.h | 7 ------- common/{debug.c => subdaemon.c} | 16 ++++++++++++++-- common/subdaemon.h | 7 +++++++ gossipd/Makefile | 2 +- gossipd/gossip.c | 12 ++---------- hsmd/Makefile | 2 +- hsmd/hsm.c | 12 ++---------- onchaind/Makefile | 2 +- onchaind/onchain.c | 14 ++------------ openingd/Makefile | 2 +- openingd/opening.c | 14 ++------------ 16 files changed, 40 insertions(+), 85 deletions(-) delete mode 100644 common/debug.h rename common/{debug.c => subdaemon.c} (82%) create mode 100644 common/subdaemon.h diff --git a/channeld/Makefile b/channeld/Makefile index 40ebd139a..fb0c91c69 100644 --- a/channeld/Makefile +++ b/channeld/Makefile @@ -38,7 +38,6 @@ CHANNELD_COMMON_OBJS := \ common/crypto_sync.o \ common/cryptomsg.o \ common/daemon_conn.o \ - common/debug.o \ common/derive_basepoints.o \ common/dev_disconnect.o \ common/htlc_state.o \ @@ -57,6 +56,7 @@ CHANNELD_COMMON_OBJS := \ common/pseudorand.o \ common/sphinx.o \ common/status.o \ + common/subdaemon.o \ common/timeout.o \ common/type_to_string.o \ common/utils.o \ diff --git a/channeld/channel.c b/channeld/channel.c index 694777303..b6807dd5c 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -38,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -49,7 +49,6 @@ #include #include #include -#include #include #include #include @@ -2499,17 +2498,7 @@ int main(int argc, char *argv[]) fd_set fds_in, fds_out; struct peer *peer; - if (argc == 2 && streq(argv[1], "--version")) { - printf("%s\n", version()); - exit(0); - } - - subdaemon_debug(argc, argv); - - /* We handle write returning errors! */ - signal(SIGCHLD, SIG_IGN); - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); + subdaemon_setup(argc, argv); peer = tal(NULL, struct peer); peer->num_pings_outstanding = 0; diff --git a/closingd/Makefile b/closingd/Makefile index aa58aa2d5..2cb6786b7 100644 --- a/closingd/Makefile +++ b/closingd/Makefile @@ -47,7 +47,6 @@ CLOSINGD_COMMON_OBJS := \ common/crypto_sync.o \ common/cryptomsg.o \ common/daemon_conn.o \ - common/debug.o \ common/dev_disconnect.o \ common/derive_basepoints.o \ common/htlc_wire.o \ @@ -56,6 +55,7 @@ CLOSINGD_COMMON_OBJS := \ common/peer_failed.o \ common/permute_tx.o \ common/status.o \ + common/subdaemon.o \ common/type_to_string.o \ common/utils.o \ common/version.o diff --git a/closingd/closing.c b/closingd/closing.c index ba47ded6c..0fbee2bcc 100644 --- a/closingd/closing.c +++ b/closingd/closing.c @@ -2,17 +2,16 @@ #include #include #include -#include #include #include #include #include +#include #include #include #include #include #include -#include #include #include #include @@ -170,17 +169,8 @@ int main(int argc, char *argv[]) u64 next_index[NUM_SIDES], revocations_received; u64 gossip_index; - if (argc == 2 && streq(argv[1], "--version")) { - printf("%s\n", version()); - exit(0); - } + subdaemon_setup(argc, argv); - subdaemon_debug(argc, argv); - - /* We handle write returning errors! */ - signal(SIGCHLD, SIG_IGN); - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); status_setup_sync(REQ_FD); msg = wire_sync_read(ctx, REQ_FD); diff --git a/common/Makefile b/common/Makefile index 49f6b2b7b..b254a43de 100644 --- a/common/Makefile +++ b/common/Makefile @@ -9,7 +9,6 @@ COMMON_SRC := \ common/crypto_sync.c \ common/cryptomsg.c \ common/daemon_conn.c \ - common/debug.c \ common/derive_basepoints.c \ common/dev_disconnect.c \ common/funding_tx.c \ @@ -31,6 +30,7 @@ COMMON_SRC := \ common/pseudorand.c \ common/sphinx.c \ common/status.c \ + common/subdaemon.c \ common/timeout.c \ common/type_to_string.c \ common/utils.c \ diff --git a/common/debug.h b/common/debug.h deleted file mode 100644 index cf7259a91..000000000 --- a/common/debug.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef LIGHTNING_COMMON_DEBUG_H -#define LIGHTNING_COMMON_DEBUG_H -#include "config.h" - -void subdaemon_debug(int argc, char *argv[]); - -#endif /* LIGHTNING_COMMON_DEBUG_H */ diff --git a/common/debug.c b/common/subdaemon.c similarity index 82% rename from common/debug.c rename to common/subdaemon.c index 99a2e2ba1..abf4c25cc 100644 --- a/common/debug.c +++ b/common/subdaemon.c @@ -1,9 +1,11 @@ #include #include #include -#include #include #include +#include +#include +#include #include #include #include @@ -48,17 +50,27 @@ static void crashlog_activate(void) sigaction(SIGBUS, &sa, NULL); } -void subdaemon_debug(int argc, char *argv[]) +void subdaemon_setup(int argc, char *argv[]) { #if DEVELOPER int i; bool printed = false; #endif + if (argc == 2 && streq(argv[1], "--version")) { + printf("%s\n", version()); + exit(0); + } + err_set_progname(argv[0]); backtrace_state = backtrace_create_state(argv[0], 0, NULL, NULL); crashlog_activate(); + /* We handle write returning errors! */ + signal(SIGPIPE, SIG_IGN); + secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY + | SECP256K1_CONTEXT_SIGN); + #if DEVELOPER for (i = 1; i < argc; i++) { if (strstarts(argv[i], "--dev-disconnect=")) { diff --git a/common/subdaemon.h b/common/subdaemon.h new file mode 100644 index 000000000..3722493e1 --- /dev/null +++ b/common/subdaemon.h @@ -0,0 +1,7 @@ +#ifndef LIGHTNING_COMMON_SUBDAEMON_H +#define LIGHTNING_COMMON_SUBDAEMON_H +#include "config.h" + +void subdaemon_setup(int argc, char *argv[]); + +#endif /* LIGHTNING_COMMON_SUBDAEMON_H */ diff --git a/gossipd/Makefile b/gossipd/Makefile index 55afc3100..310a60125 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -39,13 +39,13 @@ GOSSIPD_COMMON_OBJS := \ common/crypto_sync.o \ common/cryptomsg.o \ common/daemon_conn.o \ - common/debug.o \ common/dev_disconnect.o \ common/io_debug.o \ common/msg_queue.o \ common/ping.o \ common/pseudorand.o \ common/status.o \ + common/subdaemon.o \ common/timeout.o \ common/type_to_string.o \ common/utils.o \ diff --git a/gossipd/gossip.c b/gossipd/gossip.c index bc82e79bb..5799b4370 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -15,10 +15,10 @@ #include #include #include -#include #include #include #include +#include #include #include #include @@ -1625,17 +1625,9 @@ int main(int argc, char *argv[]) { struct daemon *daemon; - subdaemon_debug(argc, argv); + subdaemon_setup(argc, argv); io_poll_override(debug_poll); - if (argc == 2 && streq(argv[1], "--version")) { - printf("%s\n", version()); - exit(0); - } - - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | - SECP256K1_CONTEXT_SIGN); - daemon = tal(NULL, struct daemon); list_head_init(&daemon->peers); list_head_init(&daemon->reaching); diff --git a/hsmd/Makefile b/hsmd/Makefile index 8e15da75b..7f27b23d5 100644 --- a/hsmd/Makefile +++ b/hsmd/Makefile @@ -22,7 +22,6 @@ LIGHTNINGD_HSM_OBJS := $(LIGHTNINGD_HSM_SRC:.c=.o) HSMD_COMMON_OBJS := \ common/bip32.o \ common/daemon_conn.o \ - common/debug.o \ common/derive_basepoints.o \ common/funding_tx.o \ common/hash_u5.o \ @@ -31,6 +30,7 @@ HSMD_COMMON_OBJS := \ common/msg_queue.o \ common/permute_tx.o \ common/status.o \ + common/subdaemon.o \ common/type_to_string.o \ common/utils.o \ common/utxo.o \ diff --git a/hsmd/hsm.c b/hsmd/hsm.c index f91e53f0f..df215c616 100644 --- a/hsmd/hsm.c +++ b/hsmd/hsm.c @@ -14,13 +14,13 @@ #include #include #include -#include #include #include #include #include #include #include +#include #include #include #include @@ -833,17 +833,9 @@ int main(int argc, char *argv[]) { struct client *client; - if (argc == 2 && streq(argv[1], "--version")) { - printf("%s\n", version()); - exit(0); - } - - subdaemon_debug(argc, argv); + subdaemon_setup(argc, argv); io_poll_override(debug_poll); - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - client = new_client(NULL, NULL, HSM_CAP_MASTER | HSM_CAP_SIGN_GOSSIP, handle_client, STDIN_FILENO); /* We're our own master! */ diff --git a/onchaind/Makefile b/onchaind/Makefile index 604dea7c5..8b89bfb1d 100644 --- a/onchaind/Makefile +++ b/onchaind/Makefile @@ -48,7 +48,6 @@ $(LIGHTNINGD_ONCHAIN_OBJS): $(LIGHTNINGD_HEADERS) # Common source we use. ONCHAIND_COMMON_OBJS := \ common/daemon_conn.o \ - common/debug.o \ common/derive_basepoints.o \ common/dev_disconnect.o \ common/htlc_tx.o \ @@ -60,6 +59,7 @@ ONCHAIND_COMMON_OBJS := \ common/msg_queue.o \ common/permute_tx.o \ common/status.o \ + common/subdaemon.o \ common/type_to_string.o \ common/utils.o \ common/version.o diff --git a/onchaind/onchain.c b/onchaind/onchain.c index 24277ff73..c40312c88 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -3,13 +3,13 @@ #include #include #include -#include #include #include #include #include #include #include +#include #include #include #include @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -1976,17 +1975,8 @@ int main(int argc, char *argv[]) bool *tell_if_missing, *tell_immediately; u32 tx_blockheight; - if (argc == 2 && streq(argv[1], "--version")) { - printf("%s\n", version()); - exit(0); - } + subdaemon_setup(argc, argv); - subdaemon_debug(argc, argv); - - /* We handle write returning errors! */ - signal(SIGCHLD, SIG_IGN); - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); status_setup_sync(REQ_FD); missing_htlc_msgs = tal_arr(ctx, u8 *, 0); diff --git a/openingd/Makefile b/openingd/Makefile index 96509b106..89490bc82 100644 --- a/openingd/Makefile +++ b/openingd/Makefile @@ -41,7 +41,6 @@ OPENINGD_COMMON_OBJS := \ common/crypto_sync.o \ common/cryptomsg.o \ common/daemon_conn.o \ - common/debug.o \ common/derive_basepoints.o \ common/dev_disconnect.o \ common/funding_tx.o \ @@ -57,6 +56,7 @@ OPENINGD_COMMON_OBJS := \ common/permute_tx.o \ common/pseudorand.o \ common/status.o \ + common/subdaemon.o \ common/type_to_string.o \ common/utils.o \ common/utxo.o \ diff --git a/openingd/opening.c b/openingd/opening.c index 3f4b0c3a2..00e252a29 100644 --- a/openingd/opening.c +++ b/openingd/opening.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -16,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -761,17 +760,8 @@ int main(int argc, char *argv[]) struct ext_key bip32_base; u32 network_index; - if (argc == 2 && streq(argv[1], "--version")) { - printf("%s\n", version()); - exit(0); - } + subdaemon_setup(argc, argv); - subdaemon_debug(argc, argv); - - /* We handle write returning errors! */ - signal(SIGCHLD, SIG_IGN); - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); status_setup_sync(REQ_FD); msg = wire_sync_read(state, REQ_FD);