From 2eebda163d0a8c3af12cb0f9ab399a254bd57363 Mon Sep 17 00:00:00 2001 From: Rune Magnussen Date: Fri, 1 Dec 2017 16:18:29 +0100 Subject: [PATCH] Only force new entropy once a minute when backgrounded Make the poll-timeout one minute if the option --daemon is used. This saves a lot og CPU-time. --- software/README | 4 +++- software/infnoise.c | 2 +- software/infnoise.h | 2 +- software/writeentropy.c | 4 +++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/software/README b/software/README index 81d9dbb..88ecb68 100644 --- a/software/README +++ b/software/README @@ -73,7 +73,9 @@ Options are: --raw - do not whiten the output --multiplier - write 256 bits * value for each 512 bits written to the Keccak sponge --no-output - do not write random output data - --daemon - run in the background. Output should be redirected to a file or the options should be used with --dev-random + --daemon - run in the background. Output should be redirected to a file or + the options should be used with --dev-random. To reduce CPU-usage addition + af entropy is only forced after a minute rather than a second. --pidfile - write the process ID to a file. If --daemon is used, it is the ID of the background process. --serial - use Infinite Noise TRNG/FT240 with the given serial number (see --list-devices) --list-devices - list available devices diff --git a/software/infnoise.c b/software/infnoise.c index ffba534..e87ae0f 100644 --- a/software/infnoise.c +++ b/software/infnoise.c @@ -56,7 +56,7 @@ static void outputBytes(uint8_t *bytes, uint32_t length, uint32_t entropy, struc exit(1); } } else { - inmWaitForPoolToHaveRoom(); + inmWaitForPoolToHaveRoom(opts); inmWriteEntropyToPool(bytes, length, entropy); } } diff --git a/software/infnoise.h b/software/infnoise.h index fd58ce7..40715d5 100644 --- a/software/infnoise.h +++ b/software/infnoise.h @@ -54,7 +54,7 @@ void inmClearEntropyLevel(void); bool inmEntropyOnTarget(uint32_t entropy, uint32_t bits); void inmWriteEntropyStart(uint32_t bufLen, struct opt_struct *opts); void inmWriteEntropyToPool(uint8_t *bytes, uint32_t length, uint32_t entropy); -void inmWaitForPoolToHaveRoom(void); +void inmWaitForPoolToHaveRoom(struct opt_struct *opts); void inmDumpStats(void); void startDaemon(struct opt_struct *opts); bool isSuperUser(void); diff --git a/software/writeentropy.c b/software/writeentropy.c index e0a8f1a..e62db92 100644 --- a/software/writeentropy.c +++ b/software/writeentropy.c @@ -61,7 +61,7 @@ void inmWriteEntropyStart(uint32_t bufLen, struct opt_struct* opts) { } // Block until either the entropy pool has room, or 1 second has passed. -void inmWaitForPoolToHaveRoom(void) { +void inmWaitForPoolToHaveRoom(struct opt_struct *opts) { int ent_count; struct pollfd pfd = { .fd = inmDevRandomFD, @@ -72,6 +72,8 @@ void inmWaitForPoolToHaveRoom(void) { return; } timeout_msec = 1000u; // One second + if (opts->daemon) + timeout_msec *= 60u; // Do not poll aggressively when in the background poll(&pfd, 1, timeout_msec); }