LADI
/
spa
1
Fork 0

utils: handle random_r fallback

musl does not seem to have it so fall back to rand() and srand().
This commit is contained in:
Wim Taymans 2023-02-22 16:46:31 +01:00
parent 4e298f2fe7
commit 78e5c2f3e6
2 changed files with 11 additions and 0 deletions

View File

@ -403,6 +403,7 @@ check_functions = [
['gettid', '#include <unistd.h>', ['-D_GNU_SOURCE'], []],
['memfd_create', '#include <sys/mman.h>', ['-D_GNU_SOURCE'], []],
['getrandom', '#include <stddef.h>\n#include <sys/random.h>', ['-D_GNU_SOURCE'], []],
['random_r', '#include <stdlib.h>', ['-D_GNU_SOURCE'], []],
['reallocarray', '#include <stdlib.h>', ['-D_GNU_SOURCE'], []],
['sigabbrev_np', '#include <string.h>', ['-D_GNU_SOURCE'], []],
['XSetIOErrorExitHandler', '#include <X11/Xlib.h>', [], [x11_dep]],

View File

@ -197,8 +197,10 @@ ssize_t pw_getrandom(void *buf, size_t buflen, unsigned int flags)
return res;
}
#ifdef HAVE_RANDOM_R
static char statebuf[256];
static struct random_data random_state;
#endif
/** Fill a buffer with random data
* \param buf a buffer to fill
@ -215,7 +217,11 @@ void pw_random(void *buf, size_t buflen)
uint8_t *p = buf;
while (buflen-- > 0) {
int32_t val;
#ifdef HAVE_RANDOM_R
random_r(&random_state, &val);
#else
val = rand();
#endif
*p++ = (uint8_t) val;;
}
}
@ -229,7 +235,11 @@ void pw_random_init()
clock_gettime(CLOCK_REALTIME, &ts);
seed = (unsigned int) SPA_TIMESPEC_TO_NSEC(&ts);
}
#ifdef HAVE_RANDOM_R
initstate_r(seed, statebuf, sizeof(statebuf), &random_state);
#else
srand(seed);
#endif
}
SPA_EXPORT