summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <julian.klode@canonical.com>2019-08-22 13:11:00 +0200
committerJulian Andres Klode <julian.klode@canonical.com>2019-08-22 13:11:00 +0200
commitb9127ca07c37288f16b1fdc267d3f106721ed301 (patch)
tree345c647a8776531245d50482e69c2c1f72978ef4
parent489da40d56075efaa28bfdcfb7b02b3bcc222323 (diff)
srvrec: Use re-entrant resolver functions
This should probably make those functions thread-safe, which might be useful for some external users.
-rw-r--r--CMakeLists.txt2
-rw-r--r--apt-pkg/contrib/srvrec.cc9
2 files changed, 8 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0487429c6..160f7f913 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -186,7 +186,7 @@ if (NOT HAVE_SIGHANDLER_T)
endif()
# Handle resolving
-check_function_exists(res_init HAVE_LIBC_RESOLV)
+check_function_exists(res_ninit HAVE_LIBC_RESOLV)
if(HAVE_LIBC_RESOLV)
set(RESOLV_LIBRARIES)
else()
diff --git a/apt-pkg/contrib/srvrec.cc b/apt-pkg/contrib/srvrec.cc
index a97d9c615..7d9bf116e 100644
--- a/apt-pkg/contrib/srvrec.cc
+++ b/apt-pkg/contrib/srvrec.cc
@@ -17,6 +17,7 @@
#include <time.h>
#include <algorithm>
+#include <memory>
#include <tuple>
#include <apt-pkg/configuration.h>
@@ -61,11 +62,15 @@ bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result)
unsigned char answer[PACKETSZ];
int answer_len, compressed_name_len;
int answer_count;
+ struct __res_state res;
- if (res_init() != 0)
+ if (res_ninit(&res) != 0)
return _error->Errno("res_init", "Failed to init resolver");
- answer_len = res_query(name.c_str(), C_IN, T_SRV, answer, sizeof(answer));
+ // Close on return
+ std::shared_ptr<void> guard(&res, res_nclose);
+
+ answer_len = res_nquery(&res, name.c_str(), C_IN, T_SRV, answer, sizeof(answer));
if (answer_len == -1)
return false;
if (answer_len < (int)sizeof(HEADER))