diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2022-03-29 17:21:28 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-29 16:21:28 -0500 |
commit | 5e1f180f0b8e972dc8f91b0143f1ae7fd61609e7 (patch) | |
tree | 8fdddce897a4e7ca145843c48eb27df80f1ed716 | |
parent | 076c2552aeff88a27fe275dfaae61dedf4bb4bd5 (diff) |
seedrng: clean up fds and avoid -1 close on exit (#509)
This cleans up the exit path a little bit.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | src/rc/seedrng.c | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/rc/seedrng.c b/src/rc/seedrng.c index c1f94145..65a09b35 100644 --- a/src/rc/seedrng.c +++ b/src/rc/seedrng.c @@ -385,7 +385,7 @@ int main(int argc _unused, char *argv[] _unused) { static const char seedrng_prefix[] = "SeedRNG v1 Old+New Prefix"; static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure"; - int ret, fd, lock, program_ret = 0; + int ret, fd = -1, lock, program_ret = 0; uint8_t new_seed[MAX_SEED_LEN]; size_t new_seed_len; bool new_seed_creditable; @@ -408,8 +408,11 @@ int main(int argc _unused, char *argv[] _unused) eerrorx("Unable to create \"%s\" directory: %s", SEED_DIR, strerror(errno)); lock = open(LOCK_FILE, O_WRONLY | O_CREAT, 0000); - if (lock < 0 || flock(lock, LOCK_EX) < 0) - eerrorx("Unable to open lock file: %s", strerror(errno)); + if (lock < 0 || flock(lock, LOCK_EX) < 0) { + eerror("Unable to open lock file: %s", strerror(errno)); + program_ret = 1; + goto out; + } ret = seed_from_file_if_exists(NON_CREDITABLE_SEED, false, &hash); if (ret < 0) @@ -447,7 +450,9 @@ int main(int argc _unused, char *argv[] _unused) program_ret |= 1 << 6; } out: - close(fd); - close(lock); + if (fd >= 0) + close(fd); + if (lock >= 0) + close(lock); return program_ret; } |