ETHER: Add support to determine host NIC address without ifconfig

- Recent Linux versions don't install ifconfig by default and now use
  the new ip command for network details.
- Avoid writing command results to a temp file and use popen instead.
This commit is contained in:
Mark Pizzolato 2021-01-31 12:56:05 -08:00
parent 11b5c1f1dd
commit e82910570f

View file

@ -1753,18 +1753,21 @@ static void eth_get_nic_hw_addr(ETH_DEV* dev, const char *devname)
FILE *f; FILE *f;
int i; int i;
const char *patterns[] = { const char *patterns[] = {
"grep [0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]", "ip link show %.*s | grep [0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]",
"egrep [0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]", "ip link show %.*s | egrep [0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]",
"ifconfig %.*s | grep [0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]",
"ifconfig %.*s | egrep [0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]",
NULL}; NULL};
memset(command, 0, sizeof(command)); memset(command, 0, sizeof(command));
/* try to force an otherwise unused interface to be turned on */ /* try to force an otherwise unused interface to be turned on */
snprintf(command, sizeof(command)-1, "ip link set dev %.*s up", (int)(sizeof(command) - 21), devname);
if (system(command)) {};
snprintf(command, sizeof(command)-1, "ifconfig %.*s up", (int)(sizeof(command) - 14), devname); snprintf(command, sizeof(command)-1, "ifconfig %.*s up", (int)(sizeof(command) - 14), devname);
if (system(command)) {}; if (system(command)) {};
for (i=0; patterns[i] && (0 == dev->have_host_nic_phy_addr); ++i) { for (i=0; patterns[i] && (0 == dev->have_host_nic_phy_addr); ++i) {
snprintf(command, sizeof(command)-1, "ifconfig %.*s | %s >NIC.hwaddr", (int)(sizeof(command) - (26 + strlen(patterns[i]))), devname, patterns[i]); snprintf(command, sizeof(command)-1, patterns[i], (int)(sizeof(command) - (2 + strlen(patterns[i]))), devname);
if (system(command)) {}; if (NULL != (f = popen(command, "r"))) {
if (NULL != (f = fopen("NIC.hwaddr", "r"))) {
while (0 == dev->have_host_nic_phy_addr) { while (0 == dev->have_host_nic_phy_addr) {
if (fgets(command, sizeof(command)-1, f)) { if (fgets(command, sizeof(command)-1, f)) {
char *p1, *p2; char *p1, *p2;
@ -1791,8 +1794,7 @@ static void eth_get_nic_hw_addr(ETH_DEV* dev, const char *devname)
else else
break; break;
} }
fclose(f); pclose(f);
(void)remove("NIC.hwaddr");
} }
} }
} }