From 499c450d36955e53a9c822a45c137329c7484743 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Fri, 1 Apr 2022 12:05:22 +0800 Subject: [PATCH] libc/netdb: Hold dns lock when operating with resolv.conf Signed-off-by: Xiang Xiao --- libs/libc/netdb/lib_dnsaddserver.c | 10 +++++++++- libs/libc/netdb/lib_dnsforeach.c | 8 +++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/libs/libc/netdb/lib_dnsaddserver.c b/libs/libc/netdb/lib_dnsaddserver.c index e0f6adc843..f642e7bcb3 100644 --- a/libs/libc/netdb/lib_dnsaddserver.c +++ b/libs/libc/netdb/lib_dnsaddserver.c @@ -81,6 +81,8 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen) return ret; } + dns_semtake(); + #ifdef CONFIG_NET_IPv4 /* Check for an IPv4 address */ @@ -183,11 +185,17 @@ int dns_add_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen) goto errout; } - dns_notify_nameserver(addr, addrlen); ret = OK; errout: + dns_semgive(); fclose(stream); + + if (ret == OK) + { + dns_notify_nameserver(addr, addrlen); + } + return ret; } diff --git a/libs/libc/netdb/lib_dnsforeach.c b/libs/libc/netdb/lib_dnsforeach.c index d93c323e65..2857fd6ad5 100644 --- a/libs/libc/netdb/lib_dnsforeach.c +++ b/libs/libc/netdb/lib_dnsforeach.c @@ -96,6 +96,8 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR void *arg) return ret; } + dns_semtake(); + keylen = strlen(NETDB_DNS_KEYWORD); while (fgets(line, DNS_MAX_LINE, stream) != NULL) { @@ -218,14 +220,14 @@ int dns_foreach_nameserver(dns_callback_t callback, FAR void *arg) if (ret != OK) { - fclose(stream); - return ret; + break; } } } + dns_semgive(); fclose(stream); - return OK; + return ret; } #else /* CONFIG_NETDB_RESOLVCONF */