]> granicus.if.org Git - pdns/commitdiff
auth: Initialize cURL before starting any thread
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 31 May 2019 08:16:00 +0000 (10:16 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 31 May 2019 08:16:00 +0000 (10:16 +0200)
If `curl_global_init()` was not called prior to any call to
`curl_easy_init()`, it will be automatically called.
The documentation states that:

> This may be lethal in multi-threaded cases, since
> curl_global_init is not thread-safe, and it may result in
> resource problems because there is no corresponding cleanup.

pdns/minicurl.cc
pdns/minicurl.hh
pdns/receiver.cc

index 915eeecb133d7d58553c142cdd82897ee0b01129..e8ba8e9edad4b13f5f4b5203bf64d933a1cebfb9 100644 (file)
 #include <curl/curl.h>
 #include <stdexcept>
 
+void MiniCurl::init()
+{
+  static std::atomic_flag s_init = ATOMIC_FLAG_INIT;
+
+  if (s_init.test_and_set())
+    return;
+
+  CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
+  if (code != 0) {
+    throw std::runtime_error("Error initializing libcurl");
+  }
+}
+
 MiniCurl::MiniCurl(const string& useragent)
 {
   d_curl = curl_easy_init();
-  if (d_curl != nullptr) {
-    curl_easy_setopt(d_curl, CURLOPT_USERAGENT, useragent.c_str());
+  if (d_curl == nullptr) {
+    throw std::runtime_error("Error creating a MiniCurl session");
   }
+  curl_easy_setopt(d_curl, CURLOPT_USERAGENT, useragent.c_str());
 }
 
 MiniCurl::~MiniCurl()
@@ -101,6 +115,7 @@ void MiniCurl::setupURL(const std::string& str, const ComboAddress* rem, const C
 
   d_data.clear();
 }
+
 std::string MiniCurl::getURL(const std::string& str, const ComboAddress* rem, const ComboAddress* src)
 {
   setupURL(str, rem, src);
index 7e913a897de2b16f00e5e109b6b17d0756b3bb7d..80ee3d04feb5a8153f7e233c887202ef9b69bd5b 100644 (file)
@@ -31,6 +31,8 @@
 class MiniCurl
 {
 public:
+  static void init();
+
   MiniCurl(const string& useragent="MiniCurl/0.0");
   ~MiniCurl();
   MiniCurl& operator=(const MiniCurl&) = delete;
index 0c86d515be4f71bf91fbc585f516894e73071e85..84e7a218959d0458d28110be3c57bd4b78c4d696 100644 (file)
 #include "dnsrecords.hh"
 #include "version.hh"
 
+#ifdef HAVE_LUA_RECORDS
+#include "minicurl.hh"
+#endif /* HAVE_LUA_RECORDS */
+
 time_t s_starttime;
 
 string s_programname="pdns"; // used in packethandler.cc
@@ -468,6 +472,10 @@ int main(int argc, char **argv)
     /* setup rng */
     dns_random_init();
 
+#ifdef HAVE_LUA_RECORDS
+    MiniCurl::init();
+#endif /* HAVE_LUA_RECORDS */
+
     if(!::arg()["load-modules"].empty()) {
       vector<string> modules;