chiark / gitweb /
util: unify how we see srand()
authorLennart Poettering <lennart@poettering.net>
Thu, 30 Oct 2014 14:35:37 +0000 (15:35 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 30 Oct 2014 14:35:37 +0000 (15:35 +0100)
src/libsystemd-terminal/modeset.c
src/shared/util.c
src/shared/util.h
src/udev/cdrom_id/cdrom_id.c
src/udev/scsi_id/scsi_serial.c

index f5be38e4fa1e49d927707b74cc15485db7f71e03..f0508d7a05416ebd1f24a44de6ebe332ad2ad923 100644 (file)
@@ -480,7 +480,7 @@ int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
 
         log_parse_environment();
         log_open();
 
-        srand(time(NULL));
+        initialize_srand();
 
         r = parse_argv(argc, argv);
         if (r <= 0)
 
         r = parse_argv(argc, argv);
         if (r <= 0)
index 1d67abe58f4834a1f730dd71eca9265ed654167b..0f44eb5afe7a8c57c8985a8397747339d4a11c75 100644 (file)
@@ -2521,8 +2521,36 @@ int dev_urandom(void *p, size_t n) {
         return 0;
 }
 
         return 0;
 }
 
-void random_bytes(void *p, size_t n) {
+void initialize_srand(void) {
         static bool srand_called = false;
         static bool srand_called = false;
+        unsigned x;
+#ifdef HAVE_SYS_AUXV_H
+        void *auxv;
+#endif
+
+        if (srand_called)
+                return;
+
+        x = 0;
+
+#ifdef HAVE_SYS_AUXV_H
+        /* The kernel provides us with a bit of entropy in auxv, so
+         * let's try to make use of that to seed the pseudo-random
+         * generator. It's better than nothing... */
+
+        auxv = (void*) getauxval(AT_RANDOM);
+        if (auxv)
+                x ^= *(unsigned*) auxv;
+#endif
+
+        x ^= (unsigned) now(CLOCK_REALTIME);
+        x ^= (unsigned) gettid();
+
+        srand(x);
+        srand_called = true;
+}
+
+void random_bytes(void *p, size_t n) {
         uint8_t *q;
         int r;
 
         uint8_t *q;
         int r;
 
@@ -2533,28 +2561,7 @@ void random_bytes(void *p, size_t n) {
         /* If some idiot made /dev/urandom unavailable to us, he'll
          * get a PRNG instead. */
 
         /* If some idiot made /dev/urandom unavailable to us, he'll
          * get a PRNG instead. */
 
-        if (!srand_called) {
-                unsigned x = 0;
-
-#ifdef HAVE_SYS_AUXV_H
-                /* The kernel provides us with a bit of entropy in
-                 * auxv, so let's try to make use of that to seed the
-                 * pseudo-random generator. It's better than
-                 * nothing... */
-
-                void *auxv;
-
-                auxv = (void*) getauxval(AT_RANDOM);
-                if (auxv)
-                        x ^= *(unsigned*) auxv;
-#endif
-
-                x ^= (unsigned) now(CLOCK_REALTIME);
-                x ^= (unsigned) gettid();
-
-                srand(x);
-                srand_called = true;
-        }
+        initialize_srand();
 
         for (q = p; q < (uint8_t*) p + n; q ++)
                 *q = rand();
 
         for (q = p; q < (uint8_t*) p + n; q ++)
                 *q = rand();
index 35584467c15df5b87eac8b6ad15bd45065281979..aad9eb7fcbc07f51edc4a95dd4107eea2d310e99 100644 (file)
@@ -321,6 +321,7 @@ int make_console_stdio(void);
 
 int dev_urandom(void *p, size_t n);
 void random_bytes(void *p, size_t n);
 
 int dev_urandom(void *p, size_t n);
 void random_bytes(void *p, size_t n);
+void initialize_srand(void);
 
 static inline uint64_t random_u64(void) {
         uint64_t u;
 
 static inline uint64_t random_u64(void) {
         uint64_t u;
index 7a4b98726cb03b1c666ae675e0ee141eebb80693..5fe3fb535b40bf56405e6671509dc9baba606455 100644 (file)
@@ -922,7 +922,7 @@ int main(int argc, char *argv[])
                 goto exit;
         }
 
                 goto exit;
         }
 
-        srand((unsigned int)getpid());
+        initialize_srand();
         for (cnt = 20; cnt > 0; cnt--) {
                 struct timespec duration;
 
         for (cnt = 20; cnt > 0; cnt--) {
                 struct timespec duration;
 
index b3d20a3c2db3d931a77b32436efe76ae7d1a72a1..933867159118162287138c9ea1ea2c1be3d6d50c 100644 (file)
@@ -861,7 +861,7 @@ int scsi_get_serial(struct udev *udev,
         int retval;
 
         memzero(dev_scsi->serial, len);
         int retval;
 
         memzero(dev_scsi->serial, len);
-        srand((unsigned int)getpid());
+        initialize_srand();
         for (cnt = 20; cnt > 0; cnt--) {
                 struct timespec duration;
 
         for (cnt = 20; cnt > 0; cnt--) {
                 struct timespec duration;