diff --git a/src/Util.c b/src/Util.c index 3b6d958..1986d43 100644 --- a/src/Util.c +++ b/src/Util.c @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -399,14 +400,13 @@ UtilGetLine(char **linePtr, size_t * n, FILE * stream) char * UtilRandomString(size_t len) { - static const char charset[] = - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + static pthread_mutex_t seedLock = PTHREAD_MUTEX_INITIALIZER; + static unsigned int seed = 0; char *str; size_t i; - unsigned int seed = UtilServerTs() * (unsigned long) pthread_self(); - if (!len) { return NULL; @@ -418,11 +418,20 @@ UtilRandomString(size_t len) return NULL; } + pthread_mutex_lock(&seedLock); + + if (!seed) + { + seed = UtilServerTs() ^ getpid() ^ (unsigned long) pthread_self(); + } + for (i = 0; i < len; i++) { str[i] = charset[rand_r(&seed) % (sizeof(charset) - 1)]; } + pthread_mutex_unlock(&seedLock); + str[len] = '\0'; return str; }