Index: lib/libc/net/getaddrinfo.c diff -u lib/libc/net/getaddrinfo.c.orig lib/libc/net/getaddrinfo.c --- lib/libc/net/getaddrinfo.c.orig Mon Sep 16 05:36:38 2002 +++ lib/libc/net/getaddrinfo.c Wed Oct 2 00:54:27 2002 @@ -274,6 +274,15 @@ "Unknown error", /* EAI_MAX */ }; +/* Make getaddrinfo thread-safe in libc for use with kernel threads. */ +#include "libc_private.h" +#include "spinlock.h" +spinlock_t _getaddrinfo_thread_lock = _SPINLOCK_INITIALIZER; +#define THREAD_LOCK() \ + if (__isthreaded) _SPINLOCK(&_getaddrinfo_thread_lock); +#define THREAD_UNLOCK() \ + if (__isthreaded) _SPINUNLOCK(&_getaddrinfo_thread_lock); + /* XXX macros that make external reference is BAD. */ #define GET_AI(ai, afd, addr) \ @@ -599,11 +608,15 @@ result = NULL; + THREAD_LOCK(); + /* * if the servname does not match socktype/protocol, ignore it. */ - if (get_portmatch(pai, servname) != 0) + if (get_portmatch(pai, servname) != 0) { + THREAD_UNLOCK(); return 0; + } switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", default_dns_files, hostname, pai)) { @@ -624,12 +637,14 @@ } break; } + THREAD_UNLOCK(); *res = result; return 0; free: + THREAD_UNLOCK(); if (result) freeaddrinfo(result); return error; Index: lib/libc/net/name6.c diff -u lib/libc/net/name6.c.orig lib/libc/net/name6.c --- lib/libc/net/name6.c.orig Tue Sep 24 18:23:28 2002 +++ lib/libc/net/name6.c Wed Oct 2 00:55:24 2002 @@ -191,6 +191,15 @@ static int _icmp_ghbyaddr(void *, void *, va_list); #endif /* ICMPNL */ +/* Make getipnodeby*() thread-safe in libc for use with kernel threads. */ +#include "libc_private.h" +#include "spinlock.h" +extern spinlock_t _getaddrinfo_thread_lock; +#define THREAD_LOCK() \ + if (__isthreaded) _SPINLOCK(&_getaddrinfo_thread_lock); +#define THREAD_UNLOCK() \ + if (__isthreaded) _SPINUNLOCK(&_getaddrinfo_thread_lock); + /* Host lookup order if nsswitch.conf is broken or nonexistant */ static const ns_src default_src[] = { { NSSRC_FILES, NS_SUCCESS }, @@ -277,8 +286,10 @@ } } + THREAD_LOCK(); rval = nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyname", default_src, name, af, errp); + THREAD_UNLOCK(); return (rval == NS_SUCCESS) ? hp : NULL; } @@ -422,8 +433,10 @@ return NULL; } + THREAD_LOCK(); rval = nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyaddr", default_src, src, len, af, errp); + THREAD_UNLOCK(); return (rval == NS_SUCCESS) ? hp : NULL; }