diff --git a/common/test/run-gossmap_local.c b/common/test/run-gossmap_local.c index e2a198f54..20b08d562 100644 --- a/common/test/run-gossmap_local.c +++ b/common/test/run-gossmap_local.c @@ -7,6 +7,7 @@ #include #include #include +#include #include /* 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); diff --git a/common/test/run-route-specific.c b/common/test/run-route-specific.c index 4451e51ae..33ab55dce 100644 --- a/common/test/run-route-specific.c +++ b/common/test/run-route-specific.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -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)); diff --git a/common/test/run-route.c b/common/test/run-route.c index 07dd66e3d..0db4d77d9 100644 --- a/common/test/run-route.c +++ b/common/test/run-route.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -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); diff --git a/common/utils.c b/common/utils.c index 311de3e0a..57bc8f02e 100644 --- a/common/utils.c +++ b/common/utils.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -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; +} diff --git a/common/utils.h b/common/utils.h index 0282f5a53..66ca58a4d 100644 --- a/common/utils.h +++ b/common/utils.h @@ -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 */ diff --git a/plugins/test/run-route-overlong.c b/plugins/test/run-route-overlong.c index c4abb47e5..ce67a15a9 100644 --- a/plugins/test/run-route-overlong.c +++ b/plugins/test/run-route-overlong.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -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)); diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index c6123e9ab..9f7e17d6a 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -13,6 +13,7 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const s #include "test_utils.h" #include +#include #include #include @@ -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); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index e5b170ccd..43425ab05 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -18,6 +18,7 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const s #include "wallet/db.c" #include +#include #include 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;