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 <yanming.xiao@gmail.com> Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user