From 75d97ca45b74eccc01b18f44040f946d9d5df9f5 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Mon, 28 Sep 2009 11:50:51 +0300 Subject: ubinize/ubiformat: improve random number seeding 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 --- ubi-utils/src/common.c | 32 ++++++++++++++++++++++++++++++++ ubi-utils/src/common.h | 1 + ubi-utils/src/ubiformat.c | 5 ++--- ubi-utils/src/ubinize.c | 7 ++----- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/ubi-utils/src/common.c b/ubi-utils/src/common.c index 3fd470b..da5156d 100644 --- a/ubi-utils/src/common.c +++ b/ubi-utils/src/common.c @@ -23,10 +23,13 @@ * Adrian Hunter */ +#include +#include #include #include #include #include +#include #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; +} diff --git a/ubi-utils/src/common.h b/ubi-utils/src/common.h index 955c50f..d14cb48 100644 --- a/ubi-utils/src/common.h +++ b/ubi-utils/src/common.h @@ -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 } diff --git a/ubi-utils/src/ubiformat.c b/ubi-utils/src/ubiformat.c index 54363d7..62375a6 100644 --- a/ubi-utils/src/ubiformat.c +++ b/ubi-utils/src/ubiformat.c @@ -29,7 +29,6 @@ */ #define MAX_CONSECUTIVE_BAD_BLOCKS 4 -#include #include #include #include @@ -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; diff --git a/ubi-utils/src/ubinize.c b/ubi-utils/src/ubinize.c index 74ddc0f..ab5b0b8 100644 --- a/ubi-utils/src/ubinize.c +++ b/ubi-utils/src/ubinize.c @@ -24,13 +24,10 @@ * Oliver Lohmann */ -#include -#include #include #include #include #include -#include #include #include @@ -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; -- cgit v1.2.3