Subject: Re: re-reading /etc/resolv.conf on change
To: Jason Thorpe <thorpej@wasabisystems.com>
From: Jason Thorpe <thorpej@wasabisystems.com>
List: tech-userlevel
Date: 01/06/2004 16:11:13
--Apple-Mail-20-637792885
Content-Type: multipart/mixed; boundary=Apple-Mail-19-637792880


--Apple-Mail-19-637792880
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed


On Jan 6, 2004, at 3:49 PM, Jason Thorpe wrote:

> ...and replace all of the implicit calls to res_init() that include 
> checks for RES_INIT with a call to res_reinit().

...like this.

         -- Jason R. Thorpe <thorpej@wasabisystems.com>

--Apple-Mail-19-637792880
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	x-unix-mode=0644;
	name="resolv-patch.txt"
Content-Disposition: attachment;
	filename=resolv-patch.txt

Index: include/resolv.h
===================================================================
RCS file: /cvsroot/src/include/resolv.h,v
retrieving revision 1.22
diff -u -r1.22 resolv.h
--- include/resolv.h	7 Aug 2003 09:44:11 -0000	1.22
+++ include/resolv.h	7 Jan 2004 00:07:11 -0000
@@ -320,6 +320,7 @@
 int		dn_expand __P((const u_char *, const u_char *, const u_char *,
 			       char *, int));
 int		res_init __P((void));
+int		res_reinit __P((void));
 u_int		res_randomid __P((void));
 int		res_query __P((const char *, int, int, u_char *, int));
 int		res_search __P((const char *, int, int, u_char *, int));
Index: lib/libc/include/namespace.h
===================================================================
RCS file: /cvsroot/src/lib/libc/include/namespace.h,v
retrieving revision 1.82
diff -u -r1.82 namespace.h
--- lib/libc/include/namespace.h	18 Sep 2003 09:25:20 -0000	1.82
+++ lib/libc/include/namespace.h	7 Jan 2004 00:07:14 -0000
@@ -390,6 +390,7 @@
 #define regfree			_regfree
 #define registerrpc		_registerrpc
 #define res_init		_res_init
+#define res_reinit		_res_reinit
 #define res_mkquery		_res_mkquery
 #define res_query		_res_query
 #define res_search		_res_search
Index: lib/libc/net/getaddrinfo.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/getaddrinfo.c,v
retrieving revision 1.66
diff -u -r1.66 getaddrinfo.c
--- lib/libc/net/getaddrinfo.c	17 May 2003 01:36:03 -0000	1.66
+++ lib/libc/net/getaddrinfo.c	7 Jan 2004 00:07:16 -0000
@@ -1724,7 +1724,7 @@
 	rcode = NOERROR;
 	ancount = 0;
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (-1);
 	}
@@ -1833,7 +1833,7 @@
 
 	hp = (HEADER *)(void *)target->answer;	/*XXX*/
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (-1);
 	}
@@ -1973,7 +1973,7 @@
 	_DIAGASSERT(name != NULL);
 	/* XXX: target may be NULL??? */
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (-1);
 	}
Index: lib/libc/net/gethnamaddr.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/gethnamaddr.c,v
retrieving revision 1.58
diff -u -r1.58 gethnamaddr.c
--- lib/libc/net/gethnamaddr.c	11 Oct 2003 03:35:42 -0000	1.58
+++ lib/libc/net/gethnamaddr.c	7 Jan 2004 00:07:17 -0000
@@ -515,7 +515,7 @@
 
 	_DIAGASSERT(name != NULL);
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (NULL);
 	}
Index: lib/libc/net/getnetnamadr.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/getnetnamadr.c,v
retrieving revision 1.27
diff -u -r1.27 getnetnamadr.c
--- lib/libc/net/getnetnamadr.c	7 Aug 2003 16:43:09 -0000	1.27
+++ lib/libc/net/getnetnamadr.c	7 Jan 2004 00:07:17 -0000
@@ -365,7 +365,7 @@
 		{ 0 }
 	};
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (NULL);
 	}
@@ -467,7 +467,7 @@
 
 	_DIAGASSERT(net != NULL);
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (NULL);
 	}
Index: lib/libc/net/hesiod.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/hesiod.c,v
retrieving revision 1.20
diff -u -r1.20 hesiod.c
--- lib/libc/net/hesiod.c	11 Nov 2002 17:56:11 -0000	1.20
+++ lib/libc/net/hesiod.c	7 Jan 2004 00:07:17 -0000
@@ -430,7 +430,7 @@
 	_DIAGASSERT(name != NULL);
 
 	/* Make sure the resolver is initialized. */
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
+	if (res_reinit() == -1)
 		return NULL;
 
 	/* Construct the query. */
Index: lib/libc/net/res_debug.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/res_debug.c,v
retrieving revision 1.35
diff -u -r1.35 res_debug.c
--- lib/libc/net/res_debug.c	7 Aug 2003 16:43:13 -0000	1.35
+++ lib/libc/net/res_debug.c	7 Jan 2004 00:07:18 -0000
@@ -286,7 +286,7 @@
 	_DIAGASSERT(msg != NULL);
 	_DIAGASSERT(file != NULL);
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
+	if (res_reinit() == -1)
 		return;
 
 #define TruncTest(x) if (x > endMark) goto trunc
@@ -522,7 +522,7 @@
 	_DIAGASSERT(msg != NULL);
 	_DIAGASSERT(file != NULL);
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (NULL);
 	}
Index: lib/libc/net/res_init.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/res_init.c,v
retrieving revision 1.45
diff -u -r1.45 res_init.c
--- lib/libc/net/res_init.c	9 Sep 2003 22:16:58 -0000	1.45
+++ lib/libc/net/res_init.c	7 Jan 2004 00:07:19 -0000
@@ -80,6 +80,7 @@
 
 #if defined(_LIBC) && defined(__weak_alias)
 __weak_alias(res_init,_res_init)
+__weak_alias(res_reinit,_res_reinit)
 #endif
 
 static void res_setoptions __P((char *, char *));
@@ -106,6 +107,19 @@
 #endif /* INET6 */
 
 /*
+ * (Maybe) Re-initialize the resolver (or initialize it for the first time).
+ */
+int
+res_reinit(void)
+{
+
+	if ((_res.options & RES_INIT) == 0)
+		return (res_init());
+	else
+		return (0);
+}
+
+/*
  * Set up default settings.  If the configuration file exist, the values
  * there will have precedence.  Otherwise, the server address is set to
  * INADDR_ANY and the default domain name comes from the gethostname().
Index: lib/libc/net/res_mkquery.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/res_mkquery.c,v
retrieving revision 1.25
diff -u -r1.25 res_mkquery.c
--- lib/libc/net/res_mkquery.c	9 Sep 2003 22:16:58 -0000	1.25
+++ lib/libc/net/res_mkquery.c	7 Jan 2004 00:07:19 -0000
@@ -112,7 +112,7 @@
 		}
 	}
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (-1);
 	}
Index: lib/libc/net/res_query.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/res_query.c,v
retrieving revision 1.37
diff -u -r1.37 res_query.c
--- lib/libc/net/res_query.c	7 Aug 2003 16:43:14 -0000	1.37
+++ lib/libc/net/res_query.c	7 Jan 2004 00:07:19 -0000
@@ -116,7 +116,7 @@
 	_DIAGASSERT(name != NULL);
 	_DIAGASSERT(answer != NULL);
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (-1);
 	}
@@ -199,7 +199,7 @@
 	_DIAGASSERT(name != NULL);
 	_DIAGASSERT(answer != NULL);
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (-1);
 	}
@@ -343,7 +343,7 @@
 	/* domain may be NULL */
 	_DIAGASSERT(answer != NULL);
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		h_errno = NETDB_INTERNAL;
 		return (-1);
 	}
Index: lib/libc/net/res_send.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/res_send.c,v
retrieving revision 1.37
diff -u -r1.37 res_send.c
--- lib/libc/net/res_send.c	7 Aug 2003 16:43:14 -0000	1.37
+++ lib/libc/net/res_send.c	7 Jan 2004 00:07:21 -0000
@@ -389,7 +389,7 @@
 	_DIAGASSERT(buf != NULL);
 	_DIAGASSERT(ans != NULL);
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
+	if (res_reinit() == -1) {
 		/* errno should have been set by res_init() in this case. */
 		return (-1);
 	}
Index: lib/libc/net/sethostent.c
===================================================================
RCS file: /cvsroot/src/lib/libc/net/sethostent.c,v
retrieving revision 1.12
diff -u -r1.12 sethostent.c
--- lib/libc/net/sethostent.c	7 Aug 2003 16:43:15 -0000	1.12
+++ lib/libc/net/sethostent.c	7 Jan 2004 00:07:21 -0000
@@ -58,7 +58,7 @@
 	int stayopen;
 {
 
-	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
+	if (res_reinit() == -1)
 		return;
 	if (stayopen)
 		_res.options |= RES_STAYOPEN | RES_USEVC;

--Apple-Mail-19-637792880--

--Apple-Mail-20-637792885
content-type: application/pgp-signature; x-mac-type=70674453;
	name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (Darwin)

iD8DBQE/+06iOpVKkaBm8XkRAoGcAKCdDvvM8TQOGQGvpxBipfTDOT2lCQCeJhEr
G5Q9cTH37p31DB5GdNeE1Ec=
=CSNE
-----END PGP SIGNATURE-----

--Apple-Mail-20-637792885--