change net_info to work if /etc/resolv.conf does not exist
This commit is contained in:
parent
a2572d9510
commit
e91faa5dbc
47
src/sigar.c
47
src/sigar.c
|
@ -1220,37 +1220,34 @@ int sigar_net_info_get(sigar_t *sigar,
|
||||||
char buffer[BUFSIZ], *ptr;
|
char buffer[BUFSIZ], *ptr;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
if (!(fp = fopen("/etc/resolv.conf", "r"))) {
|
|
||||||
return errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
SIGAR_ZERO(netinfo);
|
SIGAR_ZERO(netinfo);
|
||||||
|
|
||||||
while ((ptr = fgets(buffer, sizeof(buffer), fp))) {
|
if ((fp = fopen("/etc/resolv.conf", "r"))) {
|
||||||
int len;
|
while ((ptr = fgets(buffer, sizeof(buffer), fp))) {
|
||||||
|
int len;
|
||||||
|
|
||||||
SIGAR_SKIP_SPACE(ptr);
|
SIGAR_SKIP_SPACE(ptr);
|
||||||
if (!(ptr = strstr(ptr, "nameserver"))) {
|
if (!(ptr = strstr(ptr, "nameserver"))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ptr += 10;
|
ptr += 10;
|
||||||
SIGAR_SKIP_SPACE(ptr);
|
SIGAR_SKIP_SPACE(ptr);
|
||||||
|
|
||||||
len = strlen(ptr);
|
len = strlen(ptr);
|
||||||
ptr[len-1] = '\0'; /* chop \n */
|
ptr[len-1] = '\0'; /* chop \n */
|
||||||
|
|
||||||
if (!netinfo->primary_dns[0]) {
|
if (!netinfo->primary_dns[0]) {
|
||||||
SIGAR_SSTRCPY(netinfo->primary_dns, ptr);
|
SIGAR_SSTRCPY(netinfo->primary_dns, ptr);
|
||||||
|
}
|
||||||
|
else if (!netinfo->secondary_dns[0]) {
|
||||||
|
SIGAR_SSTRCPY(netinfo->secondary_dns, ptr);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (!netinfo->secondary_dns[0]) {
|
fclose(fp);
|
||||||
SIGAR_SSTRCPY(netinfo->secondary_dns, ptr);
|
} /* else /etc/resolv.conf may not exist if unplugged (MacOSX) */
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
size = sizeof(netinfo->host_name)-1;
|
size = sizeof(netinfo->host_name)-1;
|
||||||
if (gethostname(netinfo->host_name, size) == 0) {
|
if (gethostname(netinfo->host_name, size) == 0) {
|
||||||
|
|
Loading…
Reference in New Issue