diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index d8a3212ae..1fc62e2a6 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -169,6 +169,17 @@ err: return false; } +/* When a per-peer subdaemon exits, see if we need to do anything. */ +static void peer_owner_finished(struct subd *subd, int status) +{ + /* If peer has moved on, do nothing. */ + if (subd->peer->owner != subd) + return; + + subd->peer->owner = NULL; + peer_fail(subd->peer, "Owning subdaemon %s died", subd->name); +} + static bool peer_got_handshake_hsmfd(struct subd *hsm, const u8 *msg, const int *fds, struct peer *peer) @@ -183,11 +194,10 @@ static bool peer_got_handshake_hsmfd(struct subd *hsm, const u8 *msg, } /* Give handshake daemon the hsm fd. */ - /* FIXME! */ peer->owner = new_subd(peer->ld, peer->ld, "lightningd_handshake", peer, handshake_wire_type_name, - NULL, NULL, + NULL, peer_owner_finished, fds[0], peer->fd, -1); if (!peer->owner) { peer_fail(peer, "Could not subdaemon handshake: %s", @@ -1389,7 +1399,8 @@ static bool peer_start_channeld_hsmfd(struct subd *hsm, const u8 *resp, cds->peer->owner = new_subd(cds->peer->ld, cds->peer->ld, "lightningd_channel", cds->peer, channel_wire_type_name, - channel_msg, NULL, + channel_msg, + peer_owner_finished, cds->peer->fd, cds->peer->gossip_client_fd, fds[0], -1); if (!cds->peer->owner) { @@ -1585,6 +1596,8 @@ static bool opening_accept_finish_response(struct subd *opening, return false; } + peer_set_condition(peer, OPENING_AWAITING_LOCKIN); + /* On to normal operation! */ peer_start_channeld(peer, &their_config, &crypto_state, &first_commit_sig, &remote_fundingkey, &theirbase, @@ -1605,7 +1618,6 @@ static bool opening_accept_reply(struct subd *opening, const u8 *reply, return false; } - peer_set_condition(peer, OPENING_AWAITING_LOCKIN); log_debug(peer->log, "Watching funding tx %s", type_to_string(reply, struct sha256_double, peer->funding_txid)); @@ -1684,7 +1696,7 @@ void peer_accept_open(struct peer *peer, peer_set_condition(peer, OPENING_NOT_LOCKED); peer->owner = new_subd(ld, ld, "lightningd_opening", peer, opening_wire_type_name, - NULL, NULL, + NULL, peer_owner_finished, peer->fd, -1); if (!peer->owner) { peer_fail(peer, "Failed to subdaemon opening: %s", @@ -1757,7 +1769,7 @@ static bool gossip_peer_released(struct subd *gossip, opening = new_subd(fc->peer->ld, ld, "lightningd_opening", fc->peer, opening_wire_type_name, - NULL, NULL, + NULL, peer_owner_finished, fc->peer->fd, -1); if (!opening) { peer_fail(fc->peer, "Failed to subdaemon opening: %s", diff --git a/lightningd/subd.c b/lightningd/subd.c index cb58f5884..660f7cb53 100644 --- a/lightningd/subd.c +++ b/lightningd/subd.c @@ -393,6 +393,7 @@ struct subd *new_subd(const tal_t *ctx, msg_queue_init(&sd->outq, sd); tal_add_destructor(sd, destroy_subd); list_head_init(&sd->reqs); + sd->peer = peer; /* conn actually owns daemon: we die when it does. */ sd->conn = io_new_conn(ctx, msg_fd, msg_setup, sd); @@ -400,7 +401,6 @@ struct subd *new_subd(const tal_t *ctx, log_info(sd->log, "pid %u, msgfd %i", sd->pid, msg_fd); - sd->peer = tal_steal(sd, peer); return sd; } diff --git a/lightningd/subd.h b/lightningd/subd.h index b1c717686..93adc75e6 100644 --- a/lightningd/subd.h +++ b/lightningd/subd.h @@ -53,7 +53,7 @@ struct subd { * @ctx: context to allocate from * @ld: global state * @name: basename of daemon - * @peer: peer to take ownership of if non-NULL + * @peer: peer to associate (if any). * @msgname: function to get name from messages * @msgcb: function to call when non-fatal message received (or NULL) * @finished: function to call when it's finished (with exit status). @@ -62,8 +62,6 @@ struct subd { * @msgcb gets called with @fds set to NULL: if it returns a positive number, * that many @fds are received before calling again. If it returns -1, the * subdaemon is shutdown. - * - * If this succeeds subd owns @peer. */ struct subd *new_subd(const tal_t *ctx, struct lightningd *ld,