diff options
Diffstat (limited to 'lib/libc/net/getaddrinfo.c')
-rw-r--r-- | lib/libc/net/getaddrinfo.c | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/lib/libc/net/getaddrinfo.c b/lib/libc/net/getaddrinfo.c index 847f162..92c85e8 100644 --- a/lib/libc/net/getaddrinfo.c +++ b/lib/libc/net/getaddrinfo.c @@ -274,6 +274,21 @@ static char *ai_errlist[] = { "Unknown error", /* EAI_MAX */ }; +/* Make getaddrinfo() thread-safe in libc for use with kernel threads. */ +#include "libc_private.h" +#include "spinlock.h" +/* + * XXX: Our res_*() is not thread-safe. So, we share lock between + * getaddrinfo() and getipnodeby*(). Still, we cannot use + * getaddrinfo() and getipnodeby*() in conjunction with other + * functions which call res_*(). + */ +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 +614,15 @@ explore_fqdn(pai, hostname, servname, res) 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 +643,14 @@ explore_fqdn(pai, hostname, servname, res) } break; } + THREAD_UNLOCK(); *res = result; return 0; free: + THREAD_UNLOCK(); if (result) freeaddrinfo(result); return error; |