]> www.infradead.org Git - mtd-utils.git/commitdiff
ubinize/ubiformat: improve random number seeding
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Mon, 28 Sep 2009 08:50:51 +0000 (11:50 +0300)
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Mon, 28 Sep 2009 08:50:51 +0000 (11:50 +0300)
Add current time to the PID to improve the pseudo-random number
generator seeding. Also, use 'rand()' instead of 'random()', because
'srand()' is for 'rand()'.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
ubi-utils/src/common.c
ubi-utils/src/common.h
ubi-utils/src/ubiformat.c
ubi-utils/src/ubinize.c

index 3fd470b6c011adf3279db5b9b75f492f8f4370cb..da5156dd94ce0f4462ee6648a6c9338df7c673e9 100644 (file)
  *          Adrian Hunter
  */
 
+#include <sys/time.h>
+#include <sys/types.h>
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include "common.h"
 
 /**
@@ -175,3 +178,32 @@ void ubiutils_print_text(FILE *stream, const char *text, int width)
                        ++p;
        }
 }
+
+/**
+ * ubiutils_srand - randomly seed the standard pseudo-random generator.
+ *
+ * This helper function seeds the standard libc pseudo-random generator with a
+ * more or less random value to make sure the 'rand()' call does not return the
+ * same sequence every time UBI utilities run. Returns zero in case of success
+ * and a %-1 in case of error.
+ */
+int ubiutils_srand(void)
+{
+       struct timeval tv;
+       struct timezone tz;
+       unsigned int seed;
+
+       /*
+        * Just assume that a combination of the PID + current time is a
+        * reasonably random number.
+        */
+       if (gettimeofday(&tv, &tz))
+               return -1;
+
+       seed = (unsigned int)tv.tv_sec;
+       seed += (unsigned int)tv.tv_usec;
+       seed *= getpid();
+       seed %= RAND_MAX;
+       srand(seed);
+       return 0;
+}
index 955c50fba8f305af85b66b27f4a0fa6b3743015e..d14cb4831001db75db415aafcc9956aad9908703 100644 (file)
@@ -78,6 +78,7 @@ static inline int is_power_of_2(unsigned long long n)
 long long ubiutils_get_bytes(const char *str);
 void ubiutils_print_bytes(long long bytes, int bracket);
 void ubiutils_print_text(FILE *stream, const char *txt, int len);
+int ubiutils_srand(void);
 
 #ifdef __cplusplus
 }
index 54363d785a6dfb731f7038bb62563d80363fcaf5..62375a679f487bf44bc3a1ef4e4ab79a034b770b 100644 (file)
@@ -29,7 +29,6 @@
  */
 #define MAX_CONSECUTIVE_BAD_BLOCKS 4
 
-#include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <stdint.h>
@@ -130,8 +129,8 @@ static const struct option long_options[] = {
 
 static int parse_opt(int argc, char * const argv[])
 {
-       srand(getpid());
-       args.image_seq = random();
+       ubiutils_srand();
+       args.image_seq = rand();
 
        while (1) {
                int key;
index 74ddc0fe8af5726a592c82a213802d6ecf83bddd..ab5b0b8e66f6fe759677e50d5ff37d74037b1d5b 100644 (file)
  *          Oliver Lohmann
  */
 
-#include <sys/stat.h>
-#include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <getopt.h>
 #include <string.h>
-#include <unistd.h>
 #include <fcntl.h>
 
 #include <mtd/ubi-media.h>
@@ -157,8 +154,8 @@ static struct args args = {
 
 static int parse_opt(int argc, char * const argv[])
 {
-       srand(getpid());
-       args.image_seq = random();
+       ubiutils_srand();
+       args.image_seq = rand();
 
        while (1) {
                int key;