Skip to content

Commit

Permalink
common/daemon: enable/cleanup memleak in daemon_setup / daemon_shutdown.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Nov 22, 2018
1 parent 2252691 commit 5a81dbd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
10 changes: 10 additions & 0 deletions common/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ccan/io/io.h>
#include <ccan/str/str.h>
#include <common/daemon.h>
#include <common/memleak.h>
#include <common/status.h>
#include <common/utils.h>
#include <common/version.h>
Expand Down Expand Up @@ -126,6 +127,12 @@ void daemon_setup(const char *argv0,
crashlog_activate();
#endif

#if DEVELOPER
/* This has significant overhead, so we only enable it if told */
if (getenv("LIGHTNINGD_DEV_MEMLEAK"))
memleak_init();
#endif

/* We handle write returning errors! */
signal(SIGPIPE, SIG_IGN);
wally_init(0);
Expand All @@ -137,6 +144,9 @@ void daemon_setup(const char *argv0,

void daemon_shutdown(void)
{
#if DEVELOPER
memleak_cleanup();
#endif
tal_free(tmpctx);
wally_cleanup(0);
}
1 change: 1 addition & 0 deletions connectd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ CONNECTD_COMMON_OBJS := \
common/dev_disconnect.o \
common/features.o \
common/gen_status_wire.o \
common/memleak.o \
common/msg_queue.o \
common/pseudorand.o \
common/status.o \
Expand Down
1 change: 1 addition & 0 deletions gossipd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ GOSSIPD_COMMON_OBJS := \
common/dev_disconnect.o \
common/features.o \
common/gen_status_wire.o \
common/memleak.o \
common/msg_queue.o \
common/ping.o \
common/pseudorand.o \
Expand Down
1 change: 1 addition & 0 deletions hsmd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ HSMD_COMMON_OBJS := \
common/gen_status_wire.o \
common/hash_u5.o \
common/key_derive.o \
common/memleak.o \
common/msg_queue.o \
common/permute_tx.o \
common/status.o \
Expand Down
20 changes: 4 additions & 16 deletions lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
/*~ This is common code: routines shared by one or more executables
* (separate daemons, or the lightning-cli program). */
#include <common/daemon.h>
#include <common/memleak.h>
#include <common/timeout.h>
#include <common/utils.h>
#include <common/version.h>
Expand Down Expand Up @@ -116,15 +115,6 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
ld->dev_disconnect_fd = -1;
ld->dev_subdaemon_fail = false;
ld->dev_allow_localhost = false;

/*~ Behaving differently depending on environment variables is a hack,
* *but* hacks are allowed for dev-mode stuff. In this case, there's
* a significant overhead to the memory leak detection stuff, and we
* can't use it under valgrind (an awesome runtime memory usage
* detector for C and C++ programs), so the test harness uses this var
* to disable it in that case. */
if (getenv("LIGHTNINGD_DEV_MEMLEAK"))
memleak_init();
#endif

/*~ These are CCAN lists: an embedded double-linked list. It's not
Expand Down Expand Up @@ -374,9 +364,10 @@ static const char *find_daemon_dir(const tal_t *ctx, const char *argv0)
return find_my_pkglibexec_path(ctx, take(my_path));
}

/*~ We like to free everything on exit, so valgrind doesn't complain. In some
* ways it would be neater not to do this, but it turns out some transient
* objects still need cleaning. */
/*~ We like to free everything on exit, so valgrind doesn't complain (valgrind
* is an awesome runtime memory usage detector for C and C++ programs). In
* some ways it would be neater not to do this, but it turns out some
* transient objects still need cleaning. */
static void shutdown_subdaemons(struct lightningd *ld)
{
struct peer *p;
Expand Down Expand Up @@ -798,9 +789,6 @@ int main(int argc, char *argv[])
tal_free(ld);
opt_free_table();

#if DEVELOPER
memleak_cleanup();
#endif
daemon_shutdown();

/*~ Farewell. Next stop: hsmd/hsmd.c. */
Expand Down

0 comments on commit 5a81dbd

Please sign in to comment.