]> git.ipfire.org Git - thirdparty/rng-tools.git/commitdiff
rngd_linux: Modify write_wakeup_threshold to the fill threshold
authorH. Peter Anvin <hpa@linux.intel.com>
Mon, 30 Jul 2012 21:42:36 +0000 (14:42 -0700)
committerJeff Garzik <jgarzik@redhat.com>
Tue, 31 Jul 2012 19:32:58 +0000 (15:32 -0400)
The kernel.random.write_wakeup_threshold sysctl needs to be set to the
point where we want poll() on the random device to wake up.  This
replaces the level check in ioctl() used during polling.

Set it by default to 3/4 to the value of kernel.random.poolsize.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Signed-off-by: Jeff Garzik <jgarzik@redhat.com>
rngd.c
rngd_linux.c
rngd_linux.h

diff --git a/rngd.c b/rngd.c
index 9819ed5301c4ba2871b7f55c6ae0b06a056918e6..f6ee8a34b2dcbc001c975c7e1e66f2f54f792346 100644 (file)
--- a/rngd.c
+++ b/rngd.c
@@ -112,7 +112,6 @@ static struct arguments default_arguments = {
        .random_name    = "/dev/random",
        .pid_file       = "/var/run/rngd.pid",
        .random_step    = 64,
-       .fill_watermark = 2048,
        .daemon         = true,
        .enable_drng    = true,
        .enable_tpm     = true,
@@ -292,6 +291,9 @@ int main(int argc, char **argv)
 
        openlog("rngd", 0, LOG_DAEMON);
 
+       /* Get the default watermark level for this platform */
+       arguments->fill_watermark = default_watermark();
+
        /* Parsing of commandline parameters */
        argp_parse(&argp, argc, argv, 0, 0, arguments);
 
index 6400adfad41436d2b7fd9c44114d5a81a5d60a50..3556fd05d38ff7610cda9cf69aaf3f8ac0715c40 100644 (file)
@@ -53,6 +53,37 @@ extern struct rng *rng_list;
 /* Kernel output device */
 static int random_fd;
 
+/*
+ * Get the default watermark
+ */
+int default_watermark(void)
+{
+       char psbuf[64], *p;
+       unsigned long ps;
+       FILE *f;
+       size_t l;
+       unsigned int wm = 2048; /* Default guess */
+
+       f = fopen("/proc/sys/kernel/random/poolsize", "r");
+       if (!f)
+               goto err;
+       l = fread(psbuf, 1, sizeof psbuf, f);
+       if (ferror(f) || !feof(f) || l == 0)
+               goto err;
+       if (psbuf[l-1] != '\n')
+               goto err;
+       psbuf[l-1] = '\0';
+       ps = strtoul(psbuf, &p, 0);
+       if (*p)
+               goto err;
+
+       wm = ps*3/4;
+
+err:
+       if (f)
+               fclose(f);
+       return wm;
+}
 
 /*
  * Initialize the interface to the Linux Kernel
@@ -62,12 +93,20 @@ static int random_fd;
  */
 void init_kernel_rng(const char* randomdev)
 {
+       FILE *f;
+
        random_fd = open(randomdev, O_RDWR);
        if (random_fd == -1) {
                message(LOG_DAEMON|LOG_ERR, "can't open %s: %s",
                        randomdev, strerror(errno));
                exit(EXIT_USAGE);
        }
+
+       f = fopen("/proc/sys/kernel/random/write_wakeup_threshold", "w");
+       if (f) {
+               fprintf(f, "%u\n", arguments->fill_watermark);
+               fclose(f);
+       }
 }
 
 void random_add_entropy(void *buf, size_t size)
index 187e17c429c6a8c63edbe44eab5f78dbc4de85a8..029584bad48344ba85c491d9ecf1c2891708a2cc 100644 (file)
@@ -26,6 +26,9 @@
 #include <unistd.h>
 #include <stdint.h>
 
+/* The default watermark level for this platform */
+extern int default_watermark(void);
+
 /*
  * Initialize the interface to the Linux Kernel
  * entropy pool (through /dev/random)