Subject: Re: gethostbyname & gethostbyaddr in libc thread-safe yet?
To: Jun-ichiro itojun Hagino <itojun@itojun.org>
From: Love <lha@stacken.kth.se>
List: tech-net
Date: 05/26/2004 08:20:45
--=-=-=


itojun@itojun.org (Jun-ichiro itojun Hagino) writes:

>> The manual page of getaddrinfo is out of date (it is thread-safe in
>> NetBSD-current). I would suggest to look into linking with the lwres_
>> library that comes with bind9. Other than that, you can use mutexes
>> to protect the current gethostbyfoo(), but that will make it really
>> slow. Another approach is to fork, which is worse.
>
> 	no, getaddrinfo is not thread-safe yet in netbsd-current.
>
> 	getadddrinfo: _yp_getaddrinfo, see variable __ypcurrent

Its still not threadsafe after fixing that, the yp-library is still not
threadsafe. If I add _yp_check_r and yp_get_default_domain_r it seems it
can be made safe enough.

Love

Index: getaddrinfo.c
===================================================================
RCS file: /sources/netbsd/NetBSD-cvs/src/lib/libc/net/getaddrinfo.c,v
retrieving revision 1.71
diff -u -u -w -r1.71 getaddrinfo.c
--- getaddrinfo.c	23 May 2004 16:54:12 -0000	1.71
+++ getaddrinfo.c	26 May 2004 06:16:39 -0000
@@ -1543,8 +1543,8 @@
 {
 	struct addrinfo sentinel, *cur;
 	struct addrinfo *ai = NULL;
-	static char *__ypcurrent;
-	int __ypcurrentlen, r;
+	char *ypbuf;
+	int ypbuflen, r;
 	const char *name;
 	const struct addrinfo *pai;
 
@@ -1558,38 +1558,34 @@
 		if (_yp_check(&__ypdomain) == 0)
 			return NS_UNAVAIL;
 	}
-	if (__ypcurrent)
-		free(__ypcurrent);
-	__ypcurrent = NULL;
 
 	/* hosts.byname is only for IPv4 (Solaris8) */
 	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
 		r = yp_match(__ypdomain, "hosts.byname", name,
-			(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
+			(int)strlen(name), &ypbuf, &ypbuflen);
 		if (r == 0) {
 			struct addrinfo ai4;
 
 			ai4 = *pai;
 			ai4.ai_family = AF_INET;
-			ai = _yphostent(__ypcurrent, &ai4);
+			ai = _yphostent(ypbuf, &ai4);
 			if (ai) {
 				cur->ai_next = ai;
 				while (cur && cur->ai_next)
 					cur = cur->ai_next;
 			}
+			free(ypbuf);
 		}
 	}
 
 	/* ipnodes.byname can hold both IPv4/v6 */
 	r = yp_match(__ypdomain, "ipnodes.byname", name,
-		(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
+		(int)strlen(name), &ypbuf, &ypbuflen);
 	if (r == 0) {
-		ai = _yphostent(__ypcurrent, pai);
-		if (ai) {
+		ai = _yphostent(ypbuf, pai);
+		if (ai)
 			cur->ai_next = ai;
-			while (cur && cur->ai_next)
-				cur = cur->ai_next;
-		}
+		free(ypbuf);
 	}
 
 	if (sentinel.ai_next == NULL) {

--=-=-=
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (NetBSD)

iQEVAwUAQLQ3P3W+NPVfDpmCAQI4bQgAoWBY7OThtjdyPApP+7qOqXHCsY+eST3A
M4lR9SVgVybELoDgmMYzGYln1/rxAINZczSL26rVIpa7o9cH8QRO7Gthb3znbvoc
BP0K+OmCbucXeDh0u61TUfcOtmBCNdqacLRcJkyut7wWOC+49U+Yi0dEWvxPEf9B
w7YUdHZBQO38dduTKZKFkbw/V9R6YMQZHvB6Bh7b0wGKuuOh1L4ROL5YNV6MYcT6
W73OkP39xXIFVjmgbvHoBprqx3p1PNcjaDhQ1vLsvi80lgK4rjufJLoPTfYwGGmh
GJj5bQHE75Gx5lA0SbAPBJG/MA+mJYedAte4ITUYfinHNbMgTTkwHA==
=Kg4T
-----END PGP SIGNATURE-----
--=-=-=--