From 68acbb7c3ff2a3ed5cecdb36442ecad63cf19904 Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Tue, 10 Feb 2015 16:54:58 -0800 Subject: [PATCH] rand_key: fix non-randomness in rand_key() If rand_key() is called twive within the same second it would return the same key both times. Not very random. Redo how rand_key() works to make it more likely to be random and more likely that two consequtive calls do not yield the same result. Reported-by: CyberLoiter Signed-off-by: Ronnie Sahlberg --- test-tool/iscsi-support.h | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/test-tool/iscsi-support.h b/test-tool/iscsi-support.h index cf6ae7c..45f017b 100644 --- a/test-tool/iscsi-support.h +++ b/test-tool/iscsi-support.h @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -220,17 +221,20 @@ struct scsi_command_descriptor *get_command_descriptor(int opcode, int sa); static inline long rand_key(void) { - time_t t; - pid_t p; - unsigned int s; - long l; + static int seed = 0; - (void)time(&t); - p = getpid(); - s = ((int)p * (t & 0xffff)); - srandom(s); - l = random(); - return l; + if (!seed) { + struct timeval tv; + pid_t p; + unsigned int s; + + gettimeofday(&tv, NULL); + p = getpid(); + s = p ^ tv.tv_sec ^ tv.tv_usec; + srandom(s); + } + seed = 1; + return random(); } static inline int pr_type_is_all_registrants(