]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
random-seed: move pool size determination to random-util.[ch]
authorLennart Poettering <lennart@poettering.net>
Fri, 19 Jul 2019 17:34:10 +0000 (19:34 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 25 Jul 2019 16:16:46 +0000 (18:16 +0200)
That way we can reuse it elsewhere.

src/basic/random-util.c
src/basic/random-util.h
src/random-seed/random-seed.c

index 3af6f271f0e38eba268378052ee965c5828778d4..b6a9ad4060fddce11e2314303c1422d5422b0d31 100644 (file)
 
 #include "alloc-util.h"
 #include "fd-util.h"
+#include "fileio.h"
 #include "io-util.h"
 #include "missing.h"
+#include "parse-util.h"
 #include "random-util.h"
 #include "siphash24.h"
 #include "time-util.h"
@@ -389,3 +391,26 @@ void random_bytes(void *p, size_t n) {
         /* If for some reason some user made /dev/urandom unavailable to us, or the kernel has no entropy, use a PRNG instead. */
         pseudo_random_bytes(p, n);
 }
+
+size_t random_pool_size(void) {
+        _cleanup_free_ char *s = NULL;
+        int r;
+
+        /* Read pool size, if possible */
+        r = read_one_line_file("/proc/sys/kernel/random/poolsize", &s);
+        if (r < 0)
+                log_debug_errno(r, "Failed to read pool size from kernel: %m");
+        else {
+                unsigned sz;
+
+                r = safe_atou(s, &sz);
+                if (r < 0)
+                        log_debug_errno(r, "Failed to parse pool size: %s", s);
+                else
+                        /* poolsize is in bits on 2.6, but we want bytes */
+                        return CLAMP(sz / 8, RANDOM_POOL_SIZE_MIN, RANDOM_POOL_SIZE_MAX);
+        }
+
+        /* Use the minimum as default, if we can't retrieve the correct value */
+        return RANDOM_POOL_SIZE_MIN;
+}
index 148b6c7813ddd7ae845917f6f8dc9dc95ec50605..facc11b976fbdba8376920af2966cb3f6cb4de21 100644 (file)
@@ -31,3 +31,9 @@ static inline uint32_t random_u32(void) {
 }
 
 int rdrand(unsigned long *ret);
+
+/* Some limits on the pool sizes when we deal with the kernel random pool */
+#define RANDOM_POOL_SIZE_MIN 512U
+#define RANDOM_POOL_SIZE_MAX (10U*1024U*1024U)
+
+size_t random_pool_size(void);
index 510a2715f2a018a864087f60180fc6991c70e34c..12f14f2888ea3e7733405dc703ca1730cc721743 100644 (file)
 #include "log.h"
 #include "main-func.h"
 #include "mkdir.h"
+#include "random-util.h"
 #include "string-util.h"
 #include "util.h"
 
-#define POOL_SIZE_MIN 512
-#define POOL_SIZE_MAX (10*1024*1024)
-
 static int run(int argc, char *argv[]) {
         _cleanup_close_ int seed_fd = -1, random_fd = -1;
         bool read_seed_file, write_seed_file;
         _cleanup_free_ void* buf = NULL;
-        size_t buf_size = 0;
+        size_t buf_size;
         struct stat st;
         ssize_t k;
-        FILE *f;
         int r;
 
         log_setup_service();
@@ -39,18 +36,7 @@ static int run(int argc, char *argv[]) {
 
         umask(0022);
 
-        /* Read pool size, if possible */
-        f = fopen("/proc/sys/kernel/random/poolsize", "re");
-        if (f) {
-                if (fscanf(f, "%zu", &buf_size) > 0)
-                        /* poolsize is in bits on 2.6, but we want bytes */
-                        buf_size /= 8;
-
-                fclose(f);
-        }
-
-        if (buf_size < POOL_SIZE_MIN)
-                buf_size = POOL_SIZE_MIN;
+        buf_size = random_pool_size();
 
         r = mkdir_parents(RANDOM_SEED, 0755);
         if (r < 0)
@@ -113,7 +99,7 @@ static int run(int argc, char *argv[]) {
 
         /* If the seed file is larger than what we expect, then honour the existing size and save/restore as much as it says */
         if ((uint64_t) st.st_size > buf_size)
-                buf_size = MIN(st.st_size, POOL_SIZE_MAX);
+                buf_size = MIN(st.st_size, RANDOM_POOL_SIZE_MAX);
 
         buf = malloc(buf_size);
         if (!buf)