ETHER: Proper cleanup while getting Linux system id
This commit is contained in:
parent
e353b910ea
commit
2f38b5f779
1 changed files with 8 additions and 2 deletions
10
sim_ether.c
10
sim_ether.c
|
@ -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';
|
||||||
pclose (f);
|
if (popened)
|
||||||
|
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';
|
||||||
|
|
Loading…
Add table
Reference in a new issue