[git commit] seedrng: remove some global variables
Bernhard Reutner-Fischer
rep.dot.nop at gmail.com
Wed Apr 20 13:43:00 UTC 2022
commit: https://git.busybox.net/busybox/commit/?id=3c60711f836b151b8f361098475c3b0cd162dd17
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master
- Remove global variables and pass dfd by value, opened once instead of
twice, which shaves off some more bytes.
function old new delta
seedrng_main 1086 1088 +2
seed_dir 8 - -8
non_creditable_seed 8 - -8
lock_file 8 - -8
creditable_seed 8 - -8
seed_from_file_if_exists 456 426 -30
------------------------------------------------------------------------------
(add/remove: 0/4 grow/shrink: 1/1 up/down: 2/-62) Total: -60 bytes
text data bss dec hex filename
976236 4227 1848 982311 efd27 busybox_old
976208 4227 1816 982251 efceb busybox_unstripped
Signed-off-by: Jason A. Donenfeld <Jason at zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop at gmail.com>
---
util-linux/seedrng.c | 32 ++++++++++++++++----------------
1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/util-linux/seedrng.c b/util-linux/seedrng.c
index 1bceae405..53be5048a 100644
--- a/util-linux/seedrng.c
+++ b/util-linux/seedrng.c
@@ -21,7 +21,7 @@
*/
//config:config SEEDRNG
-//config: bool "seedrng (2.5 kb)"
+//config: bool "seedrng (2.4 kb)"
//config: default y
//config: help
//config: Seed the kernel RNG from seed files, meant to be called
@@ -76,8 +76,6 @@
#define CREDITABLE_SEED_NAME "seed.credit"
#define NON_CREDITABLE_SEED_NAME "seed.no-credit"
-static char *seed_dir, *lock_file, *creditable_seed, *non_creditable_seed;
-
enum seedrng_lengths {
MIN_SEED_LEN = SHA256_OUTSIZE,
MAX_SEED_LEN = 512
@@ -153,18 +151,12 @@ static int seed_rng(uint8_t *seed, size_t len, bool credit)
return ret ? -1 : 0;
}
-static int seed_from_file_if_exists(const char *filename, bool credit, sha256_ctx_t *hash)
+static int seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash)
{
uint8_t seed[MAX_SEED_LEN];
ssize_t seed_len;
- int dfd = -1, ret = 0;
+ int ret = 0;
- dfd = open(seed_dir, O_DIRECTORY | O_RDONLY);
- if (dfd < 0) {
- ret = -errno;
- bb_simple_perror_msg("unable to open seed directory");
- goto out;
- }
seed_len = open_read_close(filename, seed, sizeof(seed));
if (seed_len < 0) {
if (errno != ENOENT) {
@@ -189,8 +181,6 @@ static int seed_from_file_if_exists(const char *filename, bool credit, sha256_ct
if (ret < 0)
bb_simple_perror_msg("unable to seed");
out:
- if (ENABLE_FEATURE_CLEAN_UP && dfd >= 0)
- close(dfd);
errno = -ret;
return ret ? -1 : 0;
}
@@ -200,7 +190,8 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
{
static const char seedrng_prefix[] = "SeedRNG v1 Old+New Prefix";
static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure";
- int ret, fd = -1, lock, program_ret = 0;
+ char *seed_dir, *lock_file, *creditable_seed, *non_creditable_seed;
+ int ret, fd = -1, dfd = -1, lock, program_ret = 0;
uint8_t new_seed[MAX_SEED_LEN];
size_t new_seed_len;
bool new_seed_creditable;
@@ -245,6 +236,13 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
goto out;
}
+ dfd = open(seed_dir, O_DIRECTORY | O_RDONLY);
+ if (dfd < 0) {
+ bb_simple_perror_msg("unable to open seed directory");
+ program_ret = 1;
+ goto out;
+ }
+
sha256_begin(&hash);
sha256_hash(&hash, seedrng_prefix, strlen(seedrng_prefix));
clock_gettime(CLOCK_REALTIME, ×tamp);
@@ -252,10 +250,10 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
clock_gettime(CLOCK_BOOTTIME, ×tamp);
sha256_hash(&hash, ×tamp, sizeof(timestamp));
- ret = seed_from_file_if_exists(non_creditable_seed, false, &hash);
+ ret = seed_from_file_if_exists(non_creditable_seed, dfd, false, &hash);
if (ret < 0)
program_ret |= 1 << 1;
- ret = seed_from_file_if_exists(creditable_seed, !skip_credit, &hash);
+ ret = seed_from_file_if_exists(creditable_seed, dfd, !skip_credit, &hash);
if (ret < 0)
program_ret |= 1 << 2;
@@ -290,6 +288,8 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
out:
if (ENABLE_FEATURE_CLEAN_UP && fd >= 0)
close(fd);
+ if (ENABLE_FEATURE_CLEAN_UP && dfd >= 0)
+ close(dfd);
if (ENABLE_FEATURE_CLEAN_UP && lock >= 0)
close(lock);
return program_ret;
More information about the busybox-cvs
mailing list