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 Wed Oct 2 02:04:59 2002 +++ lib/libc/net/getaddrinfo.c Wed Oct 2 02:09:01 2002 @@ -281,6 +281,15 @@ static int _hostconf_init_done; static void _hostconf_init(void); +/* 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) \ @@ -677,11 +686,15 @@ result = NULL; *res = 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; + } if (!_hostconf_init_done) _hostconf_init(); @@ -696,11 +709,13 @@ GET_PORT(cur, servname); /* canonname should be filled already */ } + 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 Fri Sep 20 03:40:48 2002 +++ lib/libc/net/name6.c Wed Oct 2 02:22:03 2002 @@ -185,6 +185,15 @@ static struct hostent *_icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp); #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); + /* * Select order host function. */ @@ -342,11 +351,15 @@ } } + THREAD_LOCK(); for (i = 0; i < MAXHOSTCONF; i++) { if (_hostconf[i].byname - && (hp = (*_hostconf[i].byname)(name, af, errp)) != NULL) + && (hp = (*_hostconf[i].byname)(name, af, errp)) != NULL) { + THREAD_UNLOCK(); return hp; + } } + THREAD_UNLOCK(); return NULL; } @@ -486,11 +499,15 @@ if (!_hostconf_init_done) _hostconf_init(); + THREAD_LOCK(); for (i = 0; i < MAXHOSTCONF; i++) { if (_hostconf[i].byaddr - && (hp = (*_hostconf[i].byaddr)(src, len, af, errp)) != NULL) + && (hp = (*_hostconf[i].byaddr)(src, len, af, errp)) != NULL) { + THREAD_UNLOCK(); return hp; + } } + THREAD_UNLOCK(); return NULL; }