Unverified Commit 8bbca3cc by Christian Brauner Committed by GitHub

Merge pull request #3487 from samboyles1/master

Improve efficiency of lxc_ifname_alnum_case_sensitive
parents 19be19a3 4810a7a3
...@@ -2754,9 +2754,7 @@ static const char padchar[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM ...@@ -2754,9 +2754,7 @@ static const char padchar[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM
char *lxc_ifname_alnum_case_sensitive(char *template) char *lxc_ifname_alnum_case_sensitive(char *template)
{ {
int ret; int ret;
struct netns_ifaddrs *ifa, *ifaddr;
char name[IFNAMSIZ]; char name[IFNAMSIZ];
bool exists = false;
size_t i = 0; size_t i = 0;
#ifdef HAVE_RAND_R #ifdef HAVE_RAND_R
unsigned int seed; unsigned int seed;
...@@ -2770,18 +2768,11 @@ char *lxc_ifname_alnum_case_sensitive(char *template) ...@@ -2770,18 +2768,11 @@ char *lxc_ifname_alnum_case_sensitive(char *template)
if (strlen(template) >= IFNAMSIZ) if (strlen(template) >= IFNAMSIZ)
return NULL; return NULL;
/* Get all the network interfaces. */
ret = netns_getifaddrs(&ifaddr, -1, &(bool){false});
if (ret < 0)
return log_error_errno(NULL, errno, "Failed to get network interfaces");
/* Generate random names until we find one that doesn't exist. */ /* Generate random names until we find one that doesn't exist. */
for (;;) { for (;;) {
name[0] = '\0'; name[0] = '\0';
(void)strlcpy(name, template, IFNAMSIZ); (void)strlcpy(name, template, IFNAMSIZ);
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
...@@ -2792,18 +2783,11 @@ char *lxc_ifname_alnum_case_sensitive(char *template) ...@@ -2792,18 +2783,11 @@ char *lxc_ifname_alnum_case_sensitive(char *template)
} }
} }
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (if_nametoindex(name) == 0) {
if (!strcmp(ifa->ifa_name, name)) {
exists = true;
break; break;
} }
} }
if (!exists)
break;
}
netns_freeifaddrs(ifaddr);
(void)strlcpy(template, name, strlen(template) + 1); (void)strlcpy(template, name, strlen(template) + 1);
return template; return template;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment