diff options
author | Ray Lai <ray@cvs.openbsd.org> | 2007-02-15 04:25:36 +0000 |
---|---|---|
committer | Ray Lai <ray@cvs.openbsd.org> | 2007-02-15 04:25:36 +0000 |
commit | 6eaf61be172d046c0072644b64be7977d34bb4e2 (patch) | |
tree | aa93a0e3ec5cc0ea9192dbf28bc459423c502b6c | |
parent | 1ab33abacee957aabcb8a207a5f718af3a4b98e8 (diff) |
Remove two mutexes by replacing getservbyname() and getservbyport()
calls with their reentrant versions.
OK millert@.
-rw-r--r-- | lib/libc/net/getaddrinfo.c | 17 | ||||
-rw-r--r-- | lib/libc/net/getnameinfo.c | 44 |
2 files changed, 29 insertions, 32 deletions
diff --git a/lib/libc/net/getaddrinfo.c b/lib/libc/net/getaddrinfo.c index 01cc3a292b2..f462b2a2c92 100644 --- a/lib/libc/net/getaddrinfo.c +++ b/lib/libc/net/getaddrinfo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getaddrinfo.c,v 1.58 2007/02/14 05:48:46 ray Exp $ */ +/* $OpenBSD: getaddrinfo.c,v 1.59 2007/02/15 04:25:35 ray Exp $ */ /* $KAME: getaddrinfo.c,v 1.31 2000/08/31 17:36:43 itojun Exp $ */ /* @@ -826,11 +826,8 @@ static int get_port(struct addrinfo *ai, const char *servname, int matchonly) { const char *errstr, *proto; - struct servent *sp; int port; int allownumeric; - /* mutex is defined in getnameinfo.c */ - extern void *__THREAD_NAME(serv_mutex); if (servname == NULL) return 0; @@ -864,6 +861,9 @@ get_port(struct addrinfo *ai, const char *servname, int matchonly) return EAI_SERVICE; port = htons(port); } else { + struct servent sp; + struct servent_data sd; + if (errno == ERANGE) return EAI_SERVICE; if (ai->ai_flags & AI_NUMERICSERV) @@ -881,12 +881,11 @@ get_port(struct addrinfo *ai, const char *servname, int matchonly) break; } - _THREAD_PRIVATE_MUTEX_LOCK(serv_mutex); - sp = getservbyname(servname, proto); - _THREAD_PRIVATE_MUTEX_UNLOCK(serv_mutex); - if (sp == NULL) + (void)memset(&sd, 0, sizeof(sd)); + if (getservbyname_r(servname, proto, &sp, &sd) == -1) return EAI_SERVICE; - port = sp->s_port; + port = sp.s_port; + endservent_r(&sd); } if (!matchonly) { diff --git a/lib/libc/net/getnameinfo.c b/lib/libc/net/getnameinfo.c index 57e0ddf3bea..7041a4ee48a 100644 --- a/lib/libc/net/getnameinfo.c +++ b/lib/libc/net/getnameinfo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getnameinfo.c,v 1.32 2006/11/17 01:11:23 itojun Exp $ */ +/* $OpenBSD: getnameinfo.c,v 1.33 2007/02/15 04:25:35 ray Exp $ */ /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */ /* @@ -88,9 +88,6 @@ static int ip6_parsenumeric(const struct sockaddr *, const char *, char *, static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int); #endif -/* - * this mutex is also used by get_port in getaddrinfo.c - */ void *__THREAD_NAME(serv_mutex); int @@ -98,7 +95,6 @@ getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) { const struct afd *afd; - struct servent *sp; struct hostent *hp; u_short port; int family, i; @@ -133,25 +129,27 @@ getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, * "||" here: rfc2553bis-03 says that serv == NULL OR * servlen == 0 means that the caller does not want the result. */ - } else { - if (flags & NI_NUMERICSERV) - sp = NULL; - else { - _THREAD_PRIVATE_MUTEX_LOCK(serv_mutex); - sp = getservbyport(port, - (flags & NI_DGRAM) ? "udp" : "tcp"); - _THREAD_PRIVATE_MUTEX_UNLOCK(serv_mutex); - } - if (sp) { - if (strlen(sp->s_name) + 1 > servlen) - return EAI_MEMORY; - strlcpy(serv, sp->s_name, servlen); - } else { - snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); - if (strlen(numserv) + 1 > servlen) - return EAI_MEMORY; - strlcpy(serv, numserv, servlen); + } else if (!(flags & NI_NUMERICSERV)) { + struct servent sp; + struct servent_data sd; + + (void)memset(&sd, 0, sizeof(sd)); + if (getservbyport_r(port, + (flags & NI_DGRAM) ? "udp" : "tcp", &sp, &sd) == -1) + goto numeric; + + if (strlen(sp.s_name) + 1 > servlen) { + endservent_r(&sd); + return EAI_MEMORY; } + strlcpy(serv, sp.s_name, servlen); + endservent_r(&sd); + } else { + numeric: + i = snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); + if (i < 0 || i >= servlen || i >= sizeof(numserv)) + return EAI_MEMORY; + strlcpy(serv, numserv, servlen); } switch (sa->sa_family) { |