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/include/resolv_private.h b/libc/dns/include/resolv_private.h
index 77b03bf..3054555 100644
--- a/libc/dns/include/resolv_private.h
+++ b/libc/dns/include/resolv_private.h
@@ -51,8 +51,7 @@
* Id: resolv.h,v 1.7.2.11.4.2 2004/06/25 00:41:05 marka Exp
*/
-#ifndef _RESOLV_PRIVATE_H_
-#define _RESOLV_PRIVATE_H_
+#pragma once
#include <sys/cdefs.h>
@@ -63,6 +62,8 @@
#include <net/if.h>
#include <time.h>
+__BEGIN_DECLS
+
// Linux defines MAXHOSTNAMELEN as 64, while the domain name limit in
// RFC 1034 and RFC 1035 is 255 octets.
#ifdef MAXHOSTNAMELEN
@@ -293,8 +294,6 @@
/* 0x00010000 */
/* Things involving an internal (static) resolver context. */
-__BEGIN_DECLS
-
__LIBC_HIDDEN__ extern struct __res_state *__res_get_state(void);
__LIBC_HIDDEN__ extern void __res_put_state(struct __res_state *);
@@ -307,8 +306,6 @@
#define _res (*__res_state())
#endif
-__END_DECLS
-
#ifndef __BIND_NOSTATIC
#define fp_nquery __fp_nquery
#define fp_query __fp_query
@@ -319,7 +316,6 @@
#define res_isourserver __res_isourserver
#define res_querydomain __res_querydomain
#define res_send __res_send
-#define res_sendsigned __res_sendsigned
#ifdef BIND_RES_POSIX3
#define dn_expand __dn_expand
@@ -329,7 +325,6 @@
#define res_mkquery __res_mkquery
#endif
-__BEGIN_DECLS
void fp_nquery(const u_char *, int, FILE *);
void fp_query(const u_char *, FILE *);
const char * hostalias(const char *);
@@ -343,8 +338,6 @@
int res_querydomain(const char *, const char *, int, int, u_char *, int);
int res_search(const char *, int, int, u_char *, int);
int res_send(const u_char *, int, u_char *, int);
-int res_sendsigned(const u_char *, int, ns_tsig_key *, u_char *, int);
-__END_DECLS
#endif
#if !defined(SHARED_LIBBIND) || defined(LIB)
@@ -424,7 +417,6 @@
#define res_send_setrhook __res_send_setrhook
#define res_servicename __res_servicename
#define res_servicenumber __res_servicenumber
-__BEGIN_DECLS
int res_hnok(const char *);
int res_ownok(const char *);
int res_mailok(const char *);
@@ -532,5 +524,3 @@
int ns_name_labels(ns_nname_ct, size_t);
__END_DECLS
-
-#endif /* !_RESOLV_PRIVATE_H_ */
diff --git a/libc/dns/include/resolv_static.h b/libc/dns/include/resolv_static.h
index 8f2a095..83a435a 100644
--- a/libc/dns/include/resolv_static.h
+++ b/libc/dns/include/resolv_static.h
@@ -1,7 +1,7 @@
-#ifndef _RESOLV_STATIC_H
-#define _RESOLV_STATIC_H
+#pragma once
#include <netdb.h>
+#include <sys/cdefs.h>
/* this structure contains all the variables that were declared
* 'static' in the original NetBSD resolver code.
@@ -15,18 +15,20 @@
#define MAXALIASES 35
#define MAXADDRS 35
-typedef struct res_static {
- char* h_addr_ptrs[MAXADDRS + 1];
- char* host_aliases[MAXALIASES];
- char hostbuf[8*1024];
- u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */
- FILE* hostf;
- int stayopen;
- const char* servent_ptr;
- struct servent servent;
- struct hostent host;
-} *res_static;
+__BEGIN_DECLS
-extern res_static __res_get_static(void);
+struct res_static {
+ char* h_addr_ptrs[MAXADDRS + 1];
+ char* host_aliases[MAXALIASES];
+ char hostbuf[8 * 1024];
+ u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */
+ FILE* hostf;
+ int stayopen;
+ const char* servent_ptr;
+ struct servent servent;
+ struct hostent host;
+};
-#endif /* _RESOLV_STATIC_H */
+struct res_static* __res_get_static(void);
+
+__END_DECLS