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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-03-07 11:39:03 +10:30
parent a845b07ada
commit 5a73380e64
5 changed files with 18 additions and 17 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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)