ETHER: Proper cleanup while getting Linux system id

This commit is contained in:
Mark Pizzolato 2020-12-11 07:18:43 -08:00
parent e353b910ea
commit 2f38b5f779

View file

@ -1676,18 +1676,24 @@ static int _eth_get_system_id (char *buf, size_t buf_size)
static int _eth_get_system_id (char *buf, size_t buf_size) static int _eth_get_system_id (char *buf, size_t buf_size)
{ {
FILE *f; FILE *f;
t_bool popened = FALSE;
memset (buf, 0, buf_size); memset (buf, 0, buf_size);
if (buf_size < 37) if (buf_size < 37)
return -1; return -1;
if ((f = fopen ("/etc/machine-id", "r")) == NULL) if ((f = fopen ("/etc/machine-id", "r")) == NULL) {
f = popen ("hostname", "r"); f = popen ("hostname", "r");
popened = TRUE;
}
if (f) { if (f) {
size_t read_size; size_t read_size;
read_size = fread (buf, 1, buf_size - 1, f); read_size = fread (buf, 1, buf_size - 1, f);
buf[read_size] = '\0'; buf[read_size] = '\0';
if (popened)
pclose (f); pclose (f);
else
fclose (f);
} }
while ((strlen (buf) > 0) && sim_isspace(buf[strlen (buf) - 1])) while ((strlen (buf) > 0) && sim_isspace(buf[strlen (buf) - 1]))
buf[strlen (buf) - 1] = '\0'; buf[strlen (buf) - 1] = '\0';