common/utils: introduce tmpdir_mkstemp; use it

Various unit tests were creating temporary files unconditionally in /tmp
and were not cleaning up after themselves. Introduce a new variant of
mkstemp(3p) that respects the TMPDIR environment variable, and use it in
the offending unit tests. This allows each test run to use a dedicated
TMPDIR that can be cleaned up after the run.

Changelog-None

Signed-off-by: Matt Whitlock <c-lightning@mattwhitlock.name>
This commit is contained in:
Matt Whitlock 2021-12-05 02:47:38 -05:00 committed by Rusty Russell
parent 5284ee4dae
commit 1f79aad830
8 changed files with 38 additions and 12 deletions

View File

@ -7,6 +7,7 @@
#include <ccan/read_write_all/read_write_all.h>
#include <common/channel_type.h>
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>
/* AUTOGENERATED MOCKS START */
@ -292,7 +293,7 @@ static void check_nannounce(const u8 *nannounce,
int main(int argc, char *argv[])
{
int fd;
char gossfile[] = "/tmp/run-gossip_local.XXXXXX";
char *gossfile;
struct gossmap *map;
struct node_id l1, l2, l3, l4;
struct short_channel_id scid23, scid12, scid_local;
@ -306,7 +307,7 @@ int main(int argc, char *argv[])
common_setup(argv[0]);
fd = mkstemp(gossfile);
fd = tmpdir_mkstemp(tmpctx, "run-gossip_local.XXXXXX", &gossfile);
assert(write_all(fd, canned_map, sizeof(canned_map)));
map = gossmap_load(tmpctx, gossfile, NULL);

View File

@ -13,6 +13,7 @@
#include <common/route.h>
#include <common/setup.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <bitcoin/chainparams.h>
#include <stdio.h>
#include <wire/peer_wiregen.h>
@ -185,7 +186,7 @@ int main(int argc, char *argv[])
struct gossmap *gossmap;
const double riskfactor = 1.0;
char gossip_version = GOSSIP_STORE_VERSION;
char gossipfilename[] = "/tmp/run-route-specific-gossipstore.XXXXXX";
char *gossipfilename;
common_setup(argv[0]);
node_id_from_hexstr("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf",
@ -203,7 +204,7 @@ int main(int argc, char *argv[])
chainparams = chainparams_for_network("regtest");
store_fd = mkstemp(gossipfilename);
store_fd = tmpdir_mkstemp(tmpctx, "run-route-specific-gossipstore.XXXXXX", &gossipfilename);
assert(write(store_fd, &gossip_version, sizeof(gossip_version))
== sizeof(gossip_version));

View File

@ -6,6 +6,7 @@
#include <common/route.h>
#include <common/setup.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <bitcoin/chainparams.h>
#include <stdio.h>
#include <wire/peer_wiregen.h>
@ -182,11 +183,11 @@ int main(int argc, char *argv[])
struct gossmap *gossmap;
const double riskfactor = 1.0;
char gossip_version = GOSSIP_STORE_VERSION;
char gossipfilename[] = "/tmp/run-route-gossipstore.XXXXXX";
char *gossipfilename;
chainparams = chainparams_for_network("regtest");
store_fd = mkstemp(gossipfilename);
store_fd = tmpdir_mkstemp(tmpctx, "run-route-gossipstore.XXXXXX", &gossipfilename);
assert(write(store_fd, &gossip_version, sizeof(gossip_version))
== sizeof(gossip_version));
gossmap = gossmap_load(tmpctx, gossipfilename, NULL);

View File

@ -2,6 +2,7 @@
#include <bitcoin/chainparams.h>
#include <ccan/list/list.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/path/path.h>
#include <ccan/utf8/utf8.h>
#include <errno.h>
#include <locale.h>
@ -217,3 +218,17 @@ char *utf8_str(const tal_t *ctx, const u8 *buf TAKES, size_t buflen)
ret[buflen] = '\0';
return ret;
}
int tmpdir_mkstemp(const tal_t *ctx, const char *template TAKES, char **created)
{
char *tmpdir = getenv("TMPDIR");
char *path = path_join(ctx, tmpdir ?: "/tmp", template);
int fd = mkstemp(path);
if (fd >= 0)
*created = path;
else
tal_free(path);
return fd;
}

View File

@ -152,4 +152,8 @@ STRUCTEQ_DEF(ripemd160, 0, u);
/* Context which all wally allocations use (see common/setup.c) */
extern const tal_t *wally_tal_ctx;
/* Like mkstemp but resolves template relative to $TMPDIR (or /tmp if unset).
* Returns created temporary path name at *created if successful. */
int tmpdir_mkstemp(const tal_t *ctx, const char *template TAKES, char **created);
#endif /* LIGHTNING_COMMON_UTILS_H */

View File

@ -2,6 +2,7 @@
#include <bitcoin/chainparams.h>
#include <common/gossip_store.h>
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>
#include <unistd.h>
@ -332,11 +333,11 @@ int main(int argc, char *argv[])
struct payment *p;
struct payment_modifier **mods;
char gossip_version = GOSSIP_STORE_VERSION;
char gossipfilename[] = "/tmp/run-route-overlong.XXXXXX";
char *gossipfilename;
common_setup(argv[0]);
chainparams = chainparams_for_network("regtest");
store_fd = mkstemp(gossipfilename);
store_fd = tmpdir_mkstemp(tmpctx, "run-route-overlong.XXXXXX", &gossipfilename);
assert(write(store_fd, &gossip_version, sizeof(gossip_version))
== sizeof(gossip_version));

View File

@ -13,6 +13,7 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const s
#include "test_utils.h"
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>
#include <unistd.h>
@ -76,14 +77,15 @@ void plugin_hook_db_sync(struct db *db UNNEEDED)
static struct db *create_test_db(void)
{
struct db *db;
char *dsn, filename[] = "/tmp/ldb-XXXXXX";
char *dsn, *filename;
int fd = mkstemp(filename);
int fd = tmpdir_mkstemp(tmpctx, "ldb-XXXXXX", &filename);
if (fd == -1)
return NULL;
close(fd);
dsn = tal_fmt(NULL, "sqlite3://%s", filename);
tal_free(filename);
db = db_open(NULL, dsn);
db->data_version = 0;
tal_free(dsn);

View File

@ -18,6 +18,7 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const s
#include "wallet/db.c"
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>
bool deprecated_apis = true;
@ -917,8 +918,8 @@ static void cleanup_test_wallet(struct wallet *w, char *filename)
static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx)
{
char *dsn, *filename = tal_fmt(ctx, "/tmp/ldb-XXXXXX");
int fd = mkstemp(filename);
char *dsn, *filename;
int fd = tmpdir_mkstemp(ctx, "ldb-XXXXXX", &filename);
struct wallet *w = tal(ctx, struct wallet);
static unsigned char badseed[BIP32_ENTROPY_LEN_128];
const struct ext_key *bip32_base = NULL;