Make res_init() work again.
Change 75830fb836621ebbcf68155e466983eb231f9ca1 to fix _nres
initialization to be thread safe accidentally introduced a behavior
change whereby res_init() became a no-op. It also failed to remove all
direct accesses to _nres.
Move the file over to C++ so we can let RAII ensure we're always holding
a lock while using the global state, make all callers access the global
state via this class, and restore the previous behavior of res_init().
Test: atest DnsResolverTest
Bug: 166235340
Change-Id: Ib390a7eac063bc0ff5eeba755e8c74ef1383004e
diff --git a/libc/dns/net/gethnamaddr.c b/libc/dns/net/gethnamaddr.c
index f8212a2..add124f 100644
--- a/libc/dns/net/gethnamaddr.c
+++ b/libc/dns/net/gethnamaddr.c
@@ -1537,7 +1537,7 @@
gethostbyname(const char *name)
{
struct hostent *result = NULL;
- res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+ struct res_static* rs = __res_get_static();
gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
return result;
@@ -1547,7 +1547,7 @@
gethostbyname2(const char *name, int af)
{
struct hostent *result = NULL;
- res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+ struct res_static* rs = __res_get_static();
gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
return result;
@@ -1583,7 +1583,7 @@
res_state res = __res_get_state();
if (res == NULL)
return NULL;
- res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+ struct res_static* rs = __res_get_static();
hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
&h_errno, netcontext);
__res_put_state(res);
@@ -1615,7 +1615,7 @@
android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
const struct android_net_context *netcontext)
{
- res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
+ struct res_static* rs = __res_get_static();
return android_gethostbyaddrfornetcontext_proxy_internal(addr, len, af, &rs->host, rs->hostbuf,
sizeof(rs->hostbuf), &h_errno, netcontext);
}
@@ -1623,7 +1623,7 @@
struct hostent *
gethostent(void)
{
- res_static rs = __res_get_static();
+ struct res_static* rs = __res_get_static();
if (!rs->hostf) {
sethostent_r(&rs->hostf);
if (!rs->hostf) {
diff --git a/libc/dns/net/getservent.c b/libc/dns/net/getservent.c
index 03add59..6a50d23 100644
--- a/libc/dns/net/getservent.c
+++ b/libc/dns/net/getservent.c
@@ -35,7 +35,7 @@
#include "resolv_static.h"
#include "services.h"
-struct servent* getservent_r(res_static rs) {
+struct servent* getservent_r(struct res_static* rs) {
const char* p;
const char* q;
int namelen;
@@ -109,17 +109,17 @@
}
void endservent(void) {
- res_static rs = __res_get_static();
+ struct res_static* rs = __res_get_static();
if (rs) rs->servent_ptr = NULL;
}
struct servent* getservent(void) {
- res_static rs = __res_get_static();
+ struct res_static* rs = __res_get_static();
return rs ? getservent_r(rs) : NULL;
}
struct servent* getservbyname(const char* name, const char* proto) {
- res_static rs = __res_get_static();
+ struct res_static* rs = __res_get_static();
if (rs == NULL) return NULL;
const char* old_servent_ptr = rs->servent_ptr;
@@ -135,7 +135,7 @@
}
struct servent* getservbyport(int port, const char* proto) {
- res_static rs = __res_get_static();
+ struct res_static* rs = __res_get_static();
if (rs == NULL) return NULL;
const char* old_servent_ptr = rs->servent_ptr;
diff --git a/libc/dns/net/sethostent.c b/libc/dns/net/sethostent.c
index b2b0132..483105a 100644
--- a/libc/dns/net/sethostent.c
+++ b/libc/dns/net/sethostent.c
@@ -65,14 +65,14 @@
/*ARGSUSED*/
sethostent(int stayopen)
{
- res_static rs = __res_get_static();
+ struct res_static* rs = __res_get_static();
if (rs) sethostent_r(&rs->hostf);
}
void
endhostent(void)
{
- res_static rs = __res_get_static();
+ struct res_static* rs = __res_get_static();
if (rs) endhostent_r(&rs->hostf);
}