From e36a65a1893191db81788d8a35ceea88854e4f5a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 1 Apr 2017 21:28:30 +1030 Subject: [PATCH] lightningd/subd: msgcb return -1 to close channel. They can't free it while we're using it, but they can return a value to close it. Signed-off-by: Rusty Russell --- lightningd/gossip_control.c | 2 +- lightningd/hsm_control.c | 2 +- lightningd/peer_control.c | 6 ++---- lightningd/subd.c | 8 +++++--- lightningd/subd.h | 7 ++++--- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 6d26d1bb8..7e7c393af 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -128,7 +128,7 @@ static void peer_ready(struct subd *gossip, const u8 *msg) peer_set_condition(peer, "Exchanging gossip"); } -static size_t gossip_msg(struct subd *gossip, const u8 *msg, const int *fds) +static int gossip_msg(struct subd *gossip, const u8 *msg, const int *fds) { enum gossip_wire_type t = fromwire_peektype(msg); diff --git a/lightningd/hsm_control.c b/lightningd/hsm_control.c index 02d9135e2..ff5b417f4 100644 --- a/lightningd/hsm_control.c +++ b/lightningd/hsm_control.c @@ -38,7 +38,7 @@ static void hsm_finished(struct subd *hsm, int status) errx(1, "HSM failed (signal %u), exiting.", WTERMSIG(status)); } -static size_t hsm_msg(struct subd *hsm, const u8 *msg, const int *fds) +static int hsm_msg(struct subd *hsm, const u8 *msg, const int *fds) { enum hsm_wire_type t = fromwire_peektype(msg); u8 *badmsg; diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 01da2e210..1a5f77dbd 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -616,9 +616,7 @@ static bool opening_got_hsm_funding_sig(struct subd *hsm, const u8 *resp, return true; } -static size_t update_channel_status(struct subd *sd, - const u8 *msg, - const int *unused) +static int channel_msg(struct subd *sd, const u8 *msg, const int *unused) { enum channel_wire_type t = fromwire_peektype(msg); @@ -674,7 +672,7 @@ static void peer_start_channeld(struct peer *peer, bool am_funder, peer->owner = new_subd(peer->ld, peer->ld, "lightningd_channel", peer, channel_wire_type_name, - update_channel_status, NULL, + channel_msg, NULL, peer->fd, peer->gossip_client_fd, -1); if (!peer->owner) { log_unusual(peer->log, "Could not subdaemon channel: %s", diff --git a/lightningd/subd.c b/lightningd/subd.c index 5de19bfa5..b1b373c2c 100644 --- a/lightningd/subd.c +++ b/lightningd/subd.c @@ -262,7 +262,9 @@ static struct io_plan *sd_msg_read(struct io_conn *conn, struct subd *sd) log_info(sd->log, "UPDATE %s", sd->msgname(type)); if (sd->msgcb) { - size_t i = sd->msgcb(sd, sd->msg_in, sd->fds_in); + int i = sd->msgcb(sd, sd->msg_in, sd->fds_in); + if (i < 0) + return io_close(conn); if (i != 0) { /* Don't ask for fds twice! */ assert(!sd->fds_in); @@ -328,8 +330,8 @@ struct subd *new_subd(const tal_t *ctx, const char *name, struct peer *peer, const char *(*msgname)(int msgtype), - size_t (*msgcb)(struct subd *, const u8 *, - const int *fds), + int (*msgcb)(struct subd *, const u8 *, + const int *fds), void (*finished)(struct subd *, int), ...) { diff --git a/lightningd/subd.h b/lightningd/subd.h index 802156027..00f1db9ab 100644 --- a/lightningd/subd.h +++ b/lightningd/subd.h @@ -30,7 +30,7 @@ struct subd { struct log *log; /* Callback when non-reply message comes in. */ - size_t (*msgcb)(struct subd *, const u8 *, const int *); + int (*msgcb)(struct subd *, const u8 *, const int *); const char *(*msgname)(int msgtype); void (*finished)(struct subd *sd, int status); @@ -60,7 +60,8 @@ struct subd { * @...: the fds to hand as fd 3, 4... terminated with -1. * * @msgcb gets called with @fds set to NULL: if it returns a positive number, - * that many @fds are received before calling again. + * that many @fds are received before calling again. If it returns -1, the + * subdaemon is shutdown. * * If this succeeds subd owns @peer. */ @@ -69,7 +70,7 @@ struct subd *new_subd(const tal_t *ctx, const char *name, struct peer *peer, const char *(*msgname)(int msgtype), - size_t (*msgcb)(struct subd *, const u8 *, const int *fds), + int (*msgcb)(struct subd *, const u8 *, const int *fds), void (*finished)(struct subd *, int), ...); /**