From 5a73380e640aab64a0beace04184627c203c9d0e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 7 Mar 2017 11:39:03 +1030 Subject: [PATCH] lightningd/subdaemon: keep peer pointer for one-per-peer daemons. This is really useful to map daemon back to peer. Signed-off-by: Rusty Russell --- lightningd/gossip_control.c | 2 +- lightningd/hsm_control.c | 2 +- lightningd/peer_control.c | 22 +++++++--------------- lightningd/subdaemon.c | 2 ++ lightningd/subdaemon.h | 7 +++++++ 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 3417c3fe5..6c3d898fb 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -132,7 +132,7 @@ static enum subdaemon_status gossip_status(struct subdaemon *gossip, void gossip_init(struct lightningd *ld) { - ld->gossip = new_subdaemon(ld, ld, "lightningd_gossip", + ld->gossip = new_subdaemon(ld, ld, "lightningd_gossip", NULL, gossip_status_wire_type_name, gossip_control_wire_type_name, gossip_status, gossip_finished, -1); diff --git a/lightningd/hsm_control.c b/lightningd/hsm_control.c index a715e1ab9..a6e62dff9 100644 --- a/lightningd/hsm_control.c +++ b/lightningd/hsm_control.c @@ -77,7 +77,7 @@ void hsm_init(struct lightningd *ld, bool newdir) { bool create; - ld->hsm = new_subdaemon(ld, ld, "lightningd_hsm", + ld->hsm = new_subdaemon(ld, ld, "lightningd_hsm", NULL, hsm_status_wire_type_name, hsm_control_wire_type_name, hsm_status, hsm_finished, -1); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 182656529..502701a75 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -162,8 +162,8 @@ static void peer_got_handshake_hsmfd(struct subdaemon *hsm, const u8 *msg, } /* Give handshake daemon the hsm fd. */ - peer->owner = new_subdaemon(peer, peer->ld, - "lightningd_handshake", + peer->owner = new_subdaemon(peer->ld, peer->ld, + "lightningd_handshake", peer, handshake_status_wire_type_name, handshake_control_wire_type_name, NULL, NULL, @@ -178,10 +178,6 @@ static void peer_got_handshake_hsmfd(struct subdaemon *hsm, const u8 *msg, /* Peer struct longer owns fd. */ peer->fd = -1; - /* Now handshake owns peer: until it succeeds, peer vanishes - * when it does. */ - tal_steal(peer->owner, peer); - if (peer->id) { req = towire_handshake_initiator_req(peer, &peer->ld->dstate.id, peer->id); @@ -767,8 +763,8 @@ void peer_accept_open(struct peer *peer, } peer_set_condition(peer, "Starting opening daemon"); - peer->owner = new_subdaemon(peer, ld, - "lightningd_opening", + peer->owner = new_subdaemon(ld, ld, + "lightningd_opening", peer, opening_status_wire_type_name, opening_control_wire_type_name, NULL, NULL, @@ -780,7 +776,6 @@ void peer_accept_open(struct peer *peer, tal_free(peer); return; } - tal_steal(peer->owner, peer); /* We handed off peer fd */ peer->fd = -1; @@ -831,8 +826,8 @@ static void gossip_peer_released(struct subdaemon *gossip, id, fc->peer->unique_id); peer_set_condition(fc->peer, "Starting opening daemon"); - fc->peer->owner = new_subdaemon(fc->peer, ld, - "lightningd_opening", + fc->peer->owner = new_subdaemon(fc->peer->ld, ld, + "lightningd_opening", fc->peer, opening_status_wire_type_name, opening_control_wire_type_name, NULL, NULL, @@ -841,15 +836,12 @@ static void gossip_peer_released(struct subdaemon *gossip, log_unusual(ld->log, "Could not subdaemon opening: %s", strerror(errno)); peer_set_condition(fc->peer, "Failed to subdaemon opening"); - tal_free(fc); + tal_free(fc->peer); return; } /* They took our fd. */ fc->peer->fd = -1; - /* fc only lasts as long as this daemon does, for now. */ - tal_steal(fc->peer->owner, fc); - channel_config(ld, &ours, &max_to_self_delay, &max_minimum_depth, &min_effective_htlc_capacity_msat); diff --git a/lightningd/subdaemon.c b/lightningd/subdaemon.c index 541965f1e..dffc2804e 100644 --- a/lightningd/subdaemon.c +++ b/lightningd/subdaemon.c @@ -269,6 +269,7 @@ static void destroy_subdaemon(struct subdaemon *sd) struct subdaemon *new_subdaemon(const tal_t *ctx, struct lightningd *ld, const char *name, + struct peer *peer, const char *(*statusname)(int status), const char *(*reqname)(int req), enum subdaemon_status (*statuscb) @@ -312,6 +313,7 @@ struct subdaemon *new_subdaemon(const tal_t *ctx, log_info(sd->log, "pid %u, statusfd %i, reqfd %i", sd->pid, status_fd, req_fd); + sd->peer = tal_steal(sd, peer); return sd; } diff --git a/lightningd/subdaemon.h b/lightningd/subdaemon.h index 73b0837b8..c1de98460 100644 --- a/lightningd/subdaemon.h +++ b/lightningd/subdaemon.h @@ -26,6 +26,9 @@ struct subdaemon { /* Connection for requests if any (write, then read) */ struct io_conn *req_conn; + /* If we are associated with a single peer, this points to it. */ + struct peer *peer; + /* For logging */ struct log *log; @@ -48,6 +51,7 @@ struct subdaemon { * @ctx: context to allocate from * @ld: global state * @name: basename of daemon + * @peer: peer to take ownership of if non-NULL; * @statusname: function to get name from status messages * @reqname: function to get name from request messages, or NULL if no requests. * @statuscb: function to call when status message received (or NULL) @@ -57,10 +61,13 @@ struct subdaemon { * @statuscb is called with fd == -1 when a status message is * received; if it returns STATUS_NEED_FD, we read an fd from the * daemon and call it again with that as the third arg. + * + * If this succeeds subdaemon owns @peer. */ struct subdaemon *new_subdaemon(const tal_t *ctx, struct lightningd *ld, const char *name, + struct peer *peer, const char *(*statusname)(int status), const char *(*reqname)(int req), enum subdaemon_status (*statuscb)