dietlibc/dnscruft: try to get dns server via command if __sys_property_get fails

- bionic libc's property_get and __sys_property_get fail for statically compiled binaries
- fallback to a default known dns server if all else fails
This commit is contained in:
Chris Renshaw 2017-07-03 03:17:06 -03:00 committed by topjohnwu
parent 4b30a324c4
commit d882a3fe44

View File

@ -61,6 +61,9 @@ void __dns_readstartfiles(void) {
#ifdef __BIONIC__ #ifdef __BIONIC__
char propvalue[PROP_VALUE_MAX]; char propvalue[PROP_VALUE_MAX];
char propname[PROP_NAME_MAX]; char propname[PROP_NAME_MAX];
char propcmd[PROP_NAME_MAX+8];
char pbuf[256];
FILE *fp;
int i; int i;
#endif #endif
@ -73,7 +76,19 @@ void __dns_readstartfiles(void) {
for(i = 1; i <= MAX_DNS_PROPERTIES; i++) { for(i = 1; i <= MAX_DNS_PROPERTIES; i++) {
snprintf(propname, sizeof(propname), "%s%d", DNS_PROP_NAME_PREFIX, i); snprintf(propname, sizeof(propname), "%s%d", DNS_PROP_NAME_PREFIX, i);
if(__system_property_get(propname, propvalue) < 1) { if(__system_property_get(propname, propvalue) < 1) {
break; /* attempt to get the property via command instead since the call to Bionic libc failed */
snprintf(propcmd, sizeof(propcmd), "getprop %s", propname);
if((fp = popen(propcmd, "r"))) {
if((fgets(pbuf, sizeof(pbuf)-1, fp) != NULL) && (strlen(pbuf) > 0)) {
if (pbuf[strlen(pbuf)-1] == '\n')
pbuf[strlen(pbuf)-1] = '\0'; /* strip trailing newline */
strncpy(propvalue, pbuf, sizeof(propvalue));
}
}
pclose(fp);
memset(&propname[0], 0, sizeof(propname)); /* clear propname to avoid unpredictable behavior */
if(strlen(propvalue) < 1)
strcpy(propvalue, "8.8.8.8"); /* use a fallback nameserver since we still fail to get the property correctly */
} }
if (parsesockaddr(propvalue,&_diet_res.nsaddr_list[_diet_res.nscount])) if (parsesockaddr(propvalue,&_diet_res.nsaddr_list[_diet_res.nscount]))