summary refs log tree commit diff
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2022-03-29 01:06:13 -0400
committerMichael Aldridge <aldridge.mac@gmail.com>2022-03-29 00:53:59 -0500
commitad47d4f190175a5690f275175f3b3cd4ed35a095 (patch)
tree0b08f22df33e860e3c734ee05e55c130024105a0
parent5edfa9543b7911467468db09c4717cb878c76871 (diff)
downloadrunit-void-ad47d4f190175a5690f275175f3b3cd4ed35a095.tar.gz
runit-void-ad47d4f190175a5690f275175f3b3cd4ed35a095.tar.xz
runit-void-ad47d4f190175a5690f275175f3b3cd4ed35a095.zip
seedrng: fix up exit path and style
A few days of cooking have yielded a few trivial cleanups.
-rw-r--r--seedrng.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/seedrng.c b/seedrng.c
index 5b8533e..2bc7521 100644
--- a/seedrng.c
+++ b/seedrng.c
@@ -251,8 +251,7 @@ static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable)
 	if (ret == (ssize_t)len) {
 		*is_creditable = true;
 		return 0;
-	}
-	if (ret == -1 && errno == ENOSYS) {
+	} else if (ret < 0 && errno == ENOSYS) {
 		struct pollfd random_fd = {
 			.fd = open("/dev/random", O_RDONLY),
 			.events = POLLIN
@@ -363,7 +362,7 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((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;
@@ -391,7 +390,8 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
 	lock = open(LOCK_FILE, O_WRONLY | O_CREAT, 0000);
 	if (lock < 0 || flock(lock, LOCK_EX) < 0) {
 		fprintf(stderr, "ERROR: Unable to open lock file: %s\n", strerror(errno));
-		return 1;
+		program_ret = 1;
+		goto out;
 	}
 
 	ret = seed_from_file_if_exists(NON_CREDITABLE_SEED, false, &hash);
@@ -430,7 +430,9 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
 		program_ret |= 1 << 6;
 	}
 out:
-	close(fd);
-	close(lock);
+	if (fd >= 0)
+		close(fd);
+	if (lock >= 0)
+		close(lock);
 	return program_ret;
 }