]> granicus.if.org Git - pdns/commitdiff
Make sure we use libmysqlclient in a thread-safe manner. This fixes a long-standing...
authorPeter van Dijk <peter.van.dijk@netherlabs.nl>
Sun, 29 Apr 2012 14:47:17 +0000 (14:47 +0000)
committerPeter van Dijk <peter.van.dijk@netherlabs.nl>
Sun, 29 Apr 2012 14:47:17 +0000 (14:47 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2591 d19b8d6e-7fed-0310-83ef-9ca221ded41b

modules/gmysqlbackend/smysql.cc
modules/gmysqlbackend/smysql.hh

index cc58eab56a771956115e1a3bf49d3e5314286c74..42b6180cee4f7f60911e6a8c8ea0da7961924379 100644 (file)
@@ -8,37 +8,42 @@
 #include "pdns/logger.hh"
 #include "pdns/dns.hh"
 #include "pdns/namespaces.hh"
+#include "pdns/lock.hh"
 
 bool SMySQL::s_dolog;
+pthread_mutex_t SMySQL::s_myinitlock = PTHREAD_MUTEX_INITIALIZER;
 
 SMySQL::SMySQL(const string &database, const string &host, uint16_t port, const string &msocket, const string &user, 
                const string &password)
 {
-  mysql_init(&d_db);
-  mysql_options(&d_db, MYSQL_READ_DEFAULT_GROUP, "client");
+  {
+    Lock l(&s_myinitlock);
+    mysql_init(&d_db);
+    mysql_options(&d_db, MYSQL_READ_DEFAULT_GROUP, "client");
   my_bool reconnect = 1;
 
-#if MYSQL_VERSION_ID >= 50013
-  mysql_options(&d_db, MYSQL_OPT_RECONNECT, &reconnect);
-#endif
+  #if MYSQL_VERSION_ID >= 50013
+    mysql_options(&d_db, MYSQL_OPT_RECONNECT, &reconnect);
+  #endif
 
-#if MYSQL_VERSION_ID > 51000
-  unsigned int timeout = 10;
-  mysql_options(&d_db, MYSQL_OPT_READ_TIMEOUT, &timeout);
-  mysql_options(&d_db, MYSQL_OPT_WRITE_TIMEOUT, &timeout);
-#endif
-  
-  if (!mysql_real_connect(&d_db, host.empty() ? 0 : host.c_str(), 
-                         user.empty() ? 0 : user.c_str(), 
-                         password.empty() ? 0 : password.c_str(),
-                         database.c_str(), port,
-                         msocket.empty() ? 0 : msocket.c_str(),
-                         CLIENT_MULTI_RESULTS)) {
-
-    throw sPerrorException("Unable to connect to database");
-  }
+  #if MYSQL_VERSION_ID > 51000
+    unsigned int timeout = 10;
+    mysql_options(&d_db, MYSQL_OPT_READ_TIMEOUT, &timeout);
+    mysql_options(&d_db, MYSQL_OPT_WRITE_TIMEOUT, &timeout);
+  #endif
+    
+    if (!mysql_real_connect(&d_db, host.empty() ? 0 : host.c_str(), 
+                         user.empty() ? 0 : user.c_str(), 
+                         password.empty() ? 0 : password.c_str(),
+                         database.c_str(), port,
+                         msocket.empty() ? 0 : msocket.c_str(),
+                         CLIENT_MULTI_RESULTS)) {
+
+      throw sPerrorException("Unable to connect to database");
+    }
 
-  d_rres=0;
+    d_rres=0;
+  }
 }
 
 void SMySQL::setLog(bool state)
index 55974a3659064986ae8eadee195e00f1012a9581..edb36456bdf69c6039c7d1ca7a894b45cba5f6a5 100644 (file)
@@ -28,6 +28,7 @@ private:
   MYSQL d_db;
   MYSQL_RES *d_rres;
   static bool s_dolog;
+  static pthread_mutex_t s_myinitlock;
 };
       
 #endif /* SSMYSQL_HH */