From 9f006fdf03381ab52bdcaa70961b748b6f1a6711 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 15 Sep 2020 20:12:12 -0500 Subject: [PATCH] memleak: 'generify' the memleak calling to code for openingd/dualopend Switch on name of subd(aemon) as to whether to call dualopend or openingd for memleak results --- lightningd/memdump.c | 2 +- lightningd/opening_common.c | 81 ++++++++++++++++++++++++++++++++++++ lightningd/opening_common.h | 7 ++++ lightningd/opening_control.c | 66 ----------------------------- lightningd/opening_control.h | 6 --- 5 files changed, 89 insertions(+), 73 deletions(-) diff --git a/lightningd/memdump.c b/lightningd/memdump.c index 4e62c9fa8..ba5cafbf8 100644 --- a/lightningd/memdump.c +++ b/lightningd/memdump.c @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/lightningd/opening_common.c b/lightningd/opening_common.c index 32ee081a5..8a4f4e4c9 100644 --- a/lightningd/opening_common.c +++ b/lightningd/opening_common.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include static void destroy_uncommitted_channel(struct uncommitted_channel *uc) @@ -144,3 +146,82 @@ void channel_config(struct lightningd *ld, /* This is filled in by lightning_openingd, for consistency. */ ours->channel_reserve = AMOUNT_SAT(UINT64_MAX); } + +#if DEVELOPER + /* Indented to avoid include ordering check */ + #include + +static void opening_died_forget_memleak(struct subd *open_daemon, + struct command *cmd) +{ + /* FIXME: We ignore the remaining opening daemons in this case. */ + opening_memleak_done(cmd, NULL); +} + +/* Mutual recursion */ +static void opening_memleak_req_next(struct command *cmd, struct peer *prev); +static void opening_memleak_req_done(struct subd *open_daemon, + const u8 *msg, const int *fds UNUSED, + struct command *cmd) +{ + bool found_leak; + struct uncommitted_channel *uc = open_daemon->channel; + + tal_del_destructor2(open_daemon, opening_died_forget_memleak, cmd); + if (!fromwire_openingd_dev_memleak_reply(msg, &found_leak) && + !fromwire_dual_open_dev_memleak_reply(msg, + &found_leak)) { + was_pending(command_fail(cmd, LIGHTNINGD, + "Bad opening_dev_memleak")); + return; + } + + if (found_leak) { + opening_memleak_done(cmd, open_daemon); + return; + } + opening_memleak_req_next(cmd, uc->peer); +} + +static void opening_memleak_req_next(struct command *cmd, struct peer *prev) +{ + struct peer *p; + u8 *msg; + + list_for_each(&cmd->ld->peers, p, list) { + struct subd *open_daemon; + + if (!p->uncommitted_channel) + continue; + if (p == prev) { + prev = NULL; + continue; + } + if (prev != NULL) + continue; + + open_daemon = p->uncommitted_channel->open_daemon; + + if (!open_daemon) + continue; + + if (streq(open_daemon->name, "dualopend")) + msg = towire_dual_open_dev_memleak(NULL); + else + msg = towire_openingd_dev_memleak(NULL); + + subd_req(p, open_daemon, take(msg), -1, 0, + opening_memleak_req_done, cmd); + /* Just in case it dies before replying! */ + tal_add_destructor2(p->uncommitted_channel->open_daemon, + opening_died_forget_memleak, cmd); + return; + } + opening_memleak_done(cmd, NULL); +} + +void opening_dev_memleak(struct command *cmd) +{ + opening_memleak_req_next(cmd, NULL); +} +#endif /* DEVELOPER */ diff --git a/lightningd/opening_common.h b/lightningd/opening_common.h index e55301f3f..88cc45b8d 100644 --- a/lightningd/opening_common.h +++ b/lightningd/opening_common.h @@ -101,4 +101,11 @@ void channel_config(struct lightningd *ld, struct channel_config *ours, u32 *max_to_self_delay, struct amount_msat *min_effective_htlc_capacity); + +#if DEVELOPER +struct command; +/* Calls report_leak_info() async. */ +void opening_dev_memleak(struct command *cmd); +#endif + #endif /* LIGHTNING_LIGHTNINGD_OPENING_COMMON_H */ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 2dbb476f6..adb31de66 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -1148,72 +1148,6 @@ static const struct json_command fund_channel_complete_command = { }; AUTODATA(json_command, &fund_channel_complete_command); -#if DEVELOPER - /* Indented to avoid include ordering check */ - #include - -static void opening_died_forget_memleak(struct subd *openingd, - struct command *cmd) -{ - /* FIXME: We ignore the remaining openingds in this case. */ - opening_memleak_done(cmd, NULL); -} - -/* Mutual recursion */ -static void opening_memleak_req_next(struct command *cmd, struct peer *prev); -static void opening_memleak_req_done(struct subd *openingd, - const u8 *msg, const int *fds UNUSED, - struct command *cmd) -{ - bool found_leak; - struct uncommitted_channel *uc = openingd->channel; - - tal_del_destructor2(openingd, opening_died_forget_memleak, cmd); - if (!fromwire_openingd_dev_memleak_reply(msg, &found_leak)) { - was_pending(command_fail(cmd, LIGHTNINGD, - "Bad opening_dev_memleak")); - return; - } - - if (found_leak) { - opening_memleak_done(cmd, openingd); - return; - } - opening_memleak_req_next(cmd, uc->peer); -} - -static void opening_memleak_req_next(struct command *cmd, struct peer *prev) -{ - struct peer *p; - - list_for_each(&cmd->ld->peers, p, list) { - if (!p->uncommitted_channel) - continue; - if (p == prev) { - prev = NULL; - continue; - } - if (prev != NULL) - continue; - - subd_req(p, - p->uncommitted_channel->open_daemon, - take(towire_openingd_dev_memleak(NULL)), - -1, 0, opening_memleak_req_done, cmd); - /* Just in case it dies before replying! */ - tal_add_destructor2(p->uncommitted_channel->open_daemon, - opening_died_forget_memleak, cmd); - return; - } - opening_memleak_done(cmd, NULL); -} - -void opening_dev_memleak(struct command *cmd) -{ - opening_memleak_req_next(cmd, NULL); -} -#endif /* DEVELOPER */ - struct subd *peer_get_owning_subd(struct peer *peer) { struct channel *channel; diff --git a/lightningd/opening_control.h b/lightningd/opening_control.h index b31f76b22..d3f0d7257 100644 --- a/lightningd/opening_control.h +++ b/lightningd/opening_control.h @@ -18,12 +18,6 @@ void peer_start_openingd(struct peer *peer, struct per_peer_state *pps, const u8 *msg); -#if DEVELOPER -struct command; -/* Calls report_leak_info() async. */ -void opening_dev_memleak(struct command *cmd); -#endif - struct subd *peer_get_owning_subd(struct peer *peer); #endif /* LIGHTNING_LIGHTNINGD_OPENING_CONTROL_H */