From 04c77f4853ae8e4ee0e27632ef9b6ebb0e2cd680 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 20 Sep 2018 12:37:41 +0930 Subject: [PATCH] lightningd: use hsm_get_client_fd() helper for global daemons too. We couldn't use it before because it asserted dbid was non-zero. Remove assert and save some code. Signed-off-by: Rusty Russell Header from folded patch 'fixup!_lightningd__use_hsm_get_client_fd()_helper_for_global_daemons_too.patch': fixup! lightningd: use hsm_get_client_fd() helper for global daemons too. Suggested-by: @ZmnSCPxj --- lightningd/connect_control.c | 14 ++------------ lightningd/gossip_control.c | 13 +------------ lightningd/hsm_control.c | 18 ++++++++++++++++-- lightningd/hsm_control.h | 3 +++ 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index 1c6c38489..f55b9897f 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -333,7 +334,6 @@ int connectd_init(struct lightningd *ld) int fds[2]; u8 *msg; int hsmfd; - u64 capabilities = HSM_CAP_ECDH; struct wireaddr_internal *wireaddrs = ld->proposed_wireaddr; enum addr_listen_announce *listen_announce = ld->proposed_listen_announce; bool allow_localhost = false; @@ -345,17 +345,7 @@ int connectd_init(struct lightningd *ld) if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) fatal("Could not socketpair for connectd<->gossipd"); - msg = towire_hsm_client_hsmfd(tmpctx, &ld->id, 0, capabilities); - if (!wire_sync_write(ld->hsm_fd, msg)) - fatal("Could not write to HSM: %s", strerror(errno)); - - msg = wire_sync_read(tmpctx, ld->hsm_fd); - if (!fromwire_hsm_client_hsmfd_reply(msg)) - fatal("Malformed hsmfd response: %s", tal_hex(msg, msg)); - - hsmfd = fdpass_recv(ld->hsm_fd); - if (hsmfd < 0) - fatal("Could not read fd from HSM: %s", strerror(errno)); + hsmfd = hsm_get_global_fd(ld, HSM_CAP_ECDH); ld->connectd = new_global_subd(ld, "lightning_connectd", connect_wire_type_name, connectd_msg, diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 88644231c..5ebf00b7b 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -148,19 +148,8 @@ void gossip_init(struct lightningd *ld, int connectd_fd) { u8 *msg; int hsmfd; - u64 capabilities = HSM_CAP_SIGN_GOSSIP; - msg = towire_hsm_client_hsmfd(tmpctx, &ld->id, 0, capabilities); - if (!wire_sync_write(ld->hsm_fd, msg)) - fatal("Could not write to HSM: %s", strerror(errno)); - - msg = wire_sync_read(tmpctx, ld->hsm_fd); - if (!fromwire_hsm_client_hsmfd_reply(msg)) - fatal("Malformed hsmfd response: %s", tal_hex(msg, msg)); - - hsmfd = fdpass_recv(ld->hsm_fd); - if (hsmfd < 0) - fatal("Could not read fd from HSM: %s", strerror(errno)); + hsmfd = hsm_get_global_fd(ld, HSM_CAP_SIGN_GOSSIP); ld->gossip = new_global_subd(ld, "lightning_gossipd", gossip_wire_type_name, gossip_msg, diff --git a/lightningd/hsm_control.c b/lightningd/hsm_control.c index e609de96f..030f165ea 100644 --- a/lightningd/hsm_control.c +++ b/lightningd/hsm_control.c @@ -19,7 +19,7 @@ #include #include -int hsm_get_client_fd(struct lightningd *ld, +static int hsm_get_fd(struct lightningd *ld, const struct pubkey *id, u64 dbid, int capabilities) @@ -27,7 +27,6 @@ int hsm_get_client_fd(struct lightningd *ld, int hsm_fd; u8 *msg; - assert(dbid); msg = towire_hsm_client_hsmfd(NULL, id, dbid, capabilities); if (!wire_sync_write(ld->hsm_fd, take(msg))) fatal("Could not write to HSM: %s", strerror(errno)); @@ -42,6 +41,21 @@ int hsm_get_client_fd(struct lightningd *ld, return hsm_fd; } +int hsm_get_client_fd(struct lightningd *ld, + const struct pubkey *id, + u64 dbid, + int capabilities) +{ + assert(dbid); + + return hsm_get_fd(ld, id, dbid, capabilities); +} + +int hsm_get_global_fd(struct lightningd *ld, int capabilities) +{ + return hsm_get_fd(ld, &ld->id, 0, capabilities); +} + static unsigned int hsm_msg(struct subd *hsmd, const u8 *msg, const int *fds UNUSED) { diff --git a/lightningd/hsm_control.h b/lightningd/hsm_control.h index 2a5ddd9f8..5e80a4736 100644 --- a/lightningd/hsm_control.h +++ b/lightningd/hsm_control.h @@ -15,5 +15,8 @@ int hsm_get_client_fd(struct lightningd *ld, u64 dbid, int capabilities); +/* Ask HSM for an fd for a global subdaemon to use (gossipd, connectd) */ +int hsm_get_global_fd(struct lightningd *ld, int capabilities); + void hsm_init(struct lightningd *ld); #endif /* LIGHTNING_LIGHTNINGD_HSM_CONTROL_H */