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>
This commit is contained in:
parent
076c2552ae
commit
5e1f180f0b
@ -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_prefix[] = "SeedRNG v1 Old+New Prefix";
|
||||||
static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure";
|
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];
|
uint8_t new_seed[MAX_SEED_LEN];
|
||||||
size_t new_seed_len;
|
size_t new_seed_len;
|
||||||
bool new_seed_creditable;
|
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));
|
eerrorx("Unable to create \"%s\" directory: %s", SEED_DIR, strerror(errno));
|
||||||
|
|
||||||
lock = open(LOCK_FILE, O_WRONLY | O_CREAT, 0000);
|
lock = open(LOCK_FILE, O_WRONLY | O_CREAT, 0000);
|
||||||
if (lock < 0 || flock(lock, LOCK_EX) < 0)
|
if (lock < 0 || flock(lock, LOCK_EX) < 0) {
|
||||||
eerrorx("Unable to open lock file: %s", strerror(errno));
|
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);
|
ret = seed_from_file_if_exists(NON_CREDITABLE_SEED, false, &hash);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
@ -447,7 +450,9 @@ int main(int argc _unused, char *argv[] _unused)
|
|||||||
program_ret |= 1 << 6;
|
program_ret |= 1 << 6;
|
||||||
}
|
}
|
||||||
out:
|
out:
|
||||||
|
if (fd >= 0)
|
||||||
close(fd);
|
close(fd);
|
||||||
|
if (lock >= 0)
|
||||||
close(lock);
|
close(lock);
|
||||||
return program_ret;
|
return program_ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user