lightningd/subd: don't take ownership of peer.

Use callback which fails the peer if subd dies: that will later allow
reconnect.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-05-22 20:55:01 +09:30
parent 34e6e56471
commit fe1ff33419
3 changed files with 20 additions and 10 deletions

View File

@ -169,6 +169,17 @@ err:
return false; 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, static bool peer_got_handshake_hsmfd(struct subd *hsm, const u8 *msg,
const int *fds, const int *fds,
struct peer *peer) 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. */ /* Give handshake daemon the hsm fd. */
/* FIXME! */
peer->owner = new_subd(peer->ld, peer->ld, peer->owner = new_subd(peer->ld, peer->ld,
"lightningd_handshake", peer, "lightningd_handshake", peer,
handshake_wire_type_name, handshake_wire_type_name,
NULL, NULL, NULL, peer_owner_finished,
fds[0], peer->fd, -1); fds[0], peer->fd, -1);
if (!peer->owner) { if (!peer->owner) {
peer_fail(peer, "Could not subdaemon handshake: %s", 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, cds->peer->owner = new_subd(cds->peer->ld, cds->peer->ld,
"lightningd_channel", cds->peer, "lightningd_channel", cds->peer,
channel_wire_type_name, channel_wire_type_name,
channel_msg, NULL, channel_msg,
peer_owner_finished,
cds->peer->fd, cds->peer->fd,
cds->peer->gossip_client_fd, fds[0], -1); cds->peer->gossip_client_fd, fds[0], -1);
if (!cds->peer->owner) { if (!cds->peer->owner) {
@ -1585,6 +1596,8 @@ static bool opening_accept_finish_response(struct subd *opening,
return false; return false;
} }
peer_set_condition(peer, OPENING_AWAITING_LOCKIN);
/* On to normal operation! */ /* On to normal operation! */
peer_start_channeld(peer, &their_config, &crypto_state, peer_start_channeld(peer, &their_config, &crypto_state,
&first_commit_sig, &remote_fundingkey, &theirbase, &first_commit_sig, &remote_fundingkey, &theirbase,
@ -1605,7 +1618,6 @@ static bool opening_accept_reply(struct subd *opening, const u8 *reply,
return false; return false;
} }
peer_set_condition(peer, OPENING_AWAITING_LOCKIN);
log_debug(peer->log, "Watching funding tx %s", log_debug(peer->log, "Watching funding tx %s",
type_to_string(reply, struct sha256_double, type_to_string(reply, struct sha256_double,
peer->funding_txid)); peer->funding_txid));
@ -1684,7 +1696,7 @@ void peer_accept_open(struct peer *peer,
peer_set_condition(peer, OPENING_NOT_LOCKED); peer_set_condition(peer, OPENING_NOT_LOCKED);
peer->owner = new_subd(ld, ld, "lightningd_opening", peer, peer->owner = new_subd(ld, ld, "lightningd_opening", peer,
opening_wire_type_name, opening_wire_type_name,
NULL, NULL, NULL, peer_owner_finished,
peer->fd, -1); peer->fd, -1);
if (!peer->owner) { if (!peer->owner) {
peer_fail(peer, "Failed to subdaemon opening: %s", 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, opening = new_subd(fc->peer->ld, ld,
"lightningd_opening", fc->peer, "lightningd_opening", fc->peer,
opening_wire_type_name, opening_wire_type_name,
NULL, NULL, NULL, peer_owner_finished,
fc->peer->fd, -1); fc->peer->fd, -1);
if (!opening) { if (!opening) {
peer_fail(fc->peer, "Failed to subdaemon opening: %s", peer_fail(fc->peer, "Failed to subdaemon opening: %s",

View File

@ -393,6 +393,7 @@ struct subd *new_subd(const tal_t *ctx,
msg_queue_init(&sd->outq, sd); msg_queue_init(&sd->outq, sd);
tal_add_destructor(sd, destroy_subd); tal_add_destructor(sd, destroy_subd);
list_head_init(&sd->reqs); list_head_init(&sd->reqs);
sd->peer = peer;
/* conn actually owns daemon: we die when it does. */ /* conn actually owns daemon: we die when it does. */
sd->conn = io_new_conn(ctx, msg_fd, msg_setup, sd); 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); log_info(sd->log, "pid %u, msgfd %i", sd->pid, msg_fd);
sd->peer = tal_steal(sd, peer);
return sd; return sd;
} }

View File

@ -53,7 +53,7 @@ struct subd {
* @ctx: context to allocate from * @ctx: context to allocate from
* @ld: global state * @ld: global state
* @name: basename of daemon * @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 * @msgname: function to get name from messages
* @msgcb: function to call when non-fatal message received (or NULL) * @msgcb: function to call when non-fatal message received (or NULL)
* @finished: function to call when it's finished (with exit status). * @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, * @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 * that many @fds are received before calling again. If it returns -1, the
* subdaemon is shutdown. * subdaemon is shutdown.
*
* If this succeeds subd owns @peer.
*/ */
struct subd *new_subd(const tal_t *ctx, struct subd *new_subd(const tal_t *ctx,
struct lightningd *ld, struct lightningd *ld,