mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-08-14 15:29:56 +00:00
thread safe: rand() => rand_r()
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
This commit is contained in:
parent
21d0acc2a5
commit
280cc35f08
@ -550,14 +550,15 @@ void rand_complete_hwaddr(char *hwaddr)
|
|||||||
{
|
{
|
||||||
const char hex[] = "0123456789abcdef";
|
const char hex[] = "0123456789abcdef";
|
||||||
char *curs = hwaddr;
|
char *curs = hwaddr;
|
||||||
|
#ifdef HAVE_RAND_R
|
||||||
#ifndef HAVE_RAND_R
|
|
||||||
randseed(true);
|
|
||||||
#else
|
|
||||||
unsigned int seed;
|
unsigned int seed;
|
||||||
|
|
||||||
seed = randseed(false);
|
seed = randseed(false);
|
||||||
|
#else
|
||||||
|
|
||||||
|
(void)randseed(true);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (*curs != '\0' && *curs != '\n') {
|
while (*curs != '\0' && *curs != '\n') {
|
||||||
if (*curs == 'x' || *curs == 'X') {
|
if (*curs == 'x' || *curs == 'X') {
|
||||||
if (curs - hwaddr == 1) {
|
if (curs - hwaddr == 1) {
|
||||||
@ -635,13 +636,22 @@ void update_hwaddr(const char *line)
|
|||||||
bool new_hwaddr(char *hwaddr)
|
bool new_hwaddr(char *hwaddr)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
#ifdef HAVE_RAND_R
|
||||||
|
unsigned int seed;
|
||||||
|
|
||||||
|
seed = randseed(false);
|
||||||
|
|
||||||
|
ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand_r(&seed) % 255,
|
||||||
|
rand_r(&seed) % 255, rand_r(&seed) % 255);
|
||||||
|
#else
|
||||||
|
|
||||||
(void)randseed(true);
|
(void)randseed(true);
|
||||||
|
|
||||||
ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
|
ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
|
||||||
rand() % 255, rand() % 255);
|
rand() % 255, rand() % 255);
|
||||||
|
#endif
|
||||||
if (ret < 0 || ret >= 18) {
|
if (ret < 0 || ret >= 18) {
|
||||||
SYSERROR("Failed to call snprintf().");
|
SYSERROR("Failed to call snprintf()");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1965,12 +1965,18 @@ static const char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|||||||
char *lxc_mkifname(char *template)
|
char *lxc_mkifname(char *template)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
unsigned int seed;
|
|
||||||
FILE *urandom;
|
|
||||||
struct ifaddrs *ifa, *ifaddr;
|
struct ifaddrs *ifa, *ifaddr;
|
||||||
char name[IFNAMSIZ];
|
char name[IFNAMSIZ];
|
||||||
bool exists = false;
|
bool exists = false;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
|
#ifdef HAVE_RAND_R
|
||||||
|
unsigned int seed;
|
||||||
|
|
||||||
|
seed = randseed(false);
|
||||||
|
#else
|
||||||
|
|
||||||
|
(void)randseed(true);
|
||||||
|
#endif
|
||||||
|
|
||||||
if (strlen(template) >= IFNAMSIZ)
|
if (strlen(template) >= IFNAMSIZ)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -1982,26 +1988,13 @@ char *lxc_mkifname(char *template)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize the random number generator. */
|
|
||||||
urandom = fopen("/dev/urandom", "r");
|
|
||||||
if (urandom != NULL) {
|
|
||||||
if (fread(&seed, sizeof(seed), 1, urandom) <= 0)
|
|
||||||
seed = time(0);
|
|
||||||
fclose(urandom);
|
|
||||||
} else {
|
|
||||||
seed = time(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef HAVE_RAND_R
|
|
||||||
srand(seed);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Generate random names until we find one that doesn't exist. */
|
/* Generate random names until we find one that doesn't exist. */
|
||||||
while (true) {
|
while (true) {
|
||||||
name[0] = '\0';
|
name[0] = '\0';
|
||||||
(void)strlcpy(name, template, IFNAMSIZ);
|
(void)strlcpy(name, template, IFNAMSIZ);
|
||||||
|
|
||||||
exists = false;
|
exists = false;
|
||||||
|
|
||||||
for (i = 0; i < strlen(name); i++) {
|
for (i = 0; i < strlen(name); i++) {
|
||||||
if (name[i] == 'X') {
|
if (name[i] == 'X') {
|
||||||
#ifdef HAVE_RAND_R
|
#ifdef HAVE_RAND_R
|
||||||
|
Loading…
Reference in New Issue
Block a user