chiark / gitweb /
umask: change default umask to 0022 just to be sure, and set it explicitly in all...
[elogind.git] / src / random-seed.c
index 91aa6c6..ee5cae3 100644 (file)
 #include "log.h"
 #include "util.h"
 
+#define POOL_SIZE_MIN 512
+
 int main(int argc, char *argv[]) {
         int seed_fd = -1, random_fd = -1;
-        int ret = 1;
-        uint8_t buf[512];
+        int ret = EXIT_FAILURE;
+        void* buf;
+        size_t buf_size = 0;
         ssize_t r;
+        FILE *f;
 
         if (argc != 2) {
                 log_error("This program requires one argument.");
-                return 1;
+                return EXIT_FAILURE;
         }
 
+        log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
         log_parse_environment();
+        log_open();
+
+        umask(0022);
+
+        /* Read pool size, if possible */
+        if ((f = fopen("/proc/sys/kernel/random/poolsize", "re"))) {
+                fscanf(f, "%zu", &buf_size);
+                fclose(f);
+        }
+
+        if (buf_size <= POOL_SIZE_MIN)
+                buf_size = POOL_SIZE_MIN;
+
+        if (!(buf = malloc(buf_size))) {
+                log_error("Failed to allocate buffer.");
+                goto finish;
+        }
+
+        if (mkdir_parents(RANDOM_SEED, 0755) < 0) {
+                log_error("Failed to create directories parents of %s: %m", RANDOM_SEED);
+                goto finish;
+        }
 
         /* When we load the seed we read it and write it to the device
          * and then immediately update the saved seed with new data,
@@ -61,12 +88,14 @@ int main(int argc, char *argv[]) {
                         }
                 }
 
-                if ((r = loop_read(seed_fd, buf, sizeof(buf), false)) != sizeof(buf))
-                        log_error("Failed to read seed file: %s", r < 0 ? strerror(errno) : "EOF");
-                else {
+                if ((r = loop_read(seed_fd, buf, buf_size, false)) <= 0) {
+
+                        if (r != 0)
+                                log_error("Failed to read seed file: %m");
+                } else {
                         lseek(seed_fd, 0, SEEK_SET);
 
-                        if ((r = loop_write(random_fd, buf, sizeof(buf), false)) != sizeof(buf))
+                        if ((r = loop_write(random_fd, buf, (size_t) r, false)) <= 0)
                                 log_error("Failed to write seed to /dev/random: %s", r < 0 ? strerror(errno) : "short write");
                 }
 
@@ -92,14 +121,14 @@ int main(int argc, char *argv[]) {
         fchmod(seed_fd, 0600);
         fchown(seed_fd, 0, 0);
 
-        if ((r = loop_read(random_fd, buf, sizeof(buf), false)) != sizeof(buf))
+        if ((r = loop_read(random_fd, buf, buf_size, false)) <= 0)
                 log_error("Failed to read new seed from /dev/urandom: %s", r < 0 ? strerror(errno) : "EOF");
         else {
-                if ((r = loop_write(seed_fd, buf, sizeof(buf), false)) != sizeof(buf))
+                if ((r = loop_write(seed_fd, buf, (size_t) r, false)) <= 0)
                         log_error("Failed to write new random seed file: %s", r < 0 ? strerror(errno) : "short write");
         }
 
-        ret = 0;
+        ret = EXIT_SUCCESS;
 
 finish:
         if (random_fd >= 0)
@@ -108,5 +137,7 @@ finish:
         if (seed_fd >= 0)
                 close_nointr_nofail(seed_fd);
 
+        free(buf);
+
         return ret;
 }