]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Move the Lua mutex to LockGuarded
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 16 Apr 2021 13:40:34 +0000 (15:40 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 17 Aug 2021 12:04:45 +0000 (14:04 +0200)
pdns/dnsdist-console.cc
pdns/dnsdist-lua-actions.cc
pdns/dnsdist.cc
pdns/dnsdist.hh
pdns/dnsdistdist/dnsdist-healthchecks.cc
pdns/dnsdistdist/dnsdist-lbpolicies.cc
pdns/dnsdistdist/dnsdist-rules.hh
pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc

index 50679039d73a3f86305716f239d7ee8f9a21cd1d..2ab26821c5194df4ffcb18517d7116a6ab881874 100644 (file)
@@ -321,10 +321,10 @@ void doConsole()
       bool withReturn=true;
     retry:;
       try {
-        std::lock_guard<std::mutex> lock(g_luamutex);
+        auto lua = g_lua.lock();
         g_outputBuffer.clear();
         resetLuaSideEffect();
-        auto ret=g_lua.executeCode<
+        auto ret = lua->executeCode<
           boost::optional<
             boost::variant<
               string, 
@@ -779,11 +779,11 @@ static void controlClientThread(ConsoleConnection&& conn)
         bool withReturn=true;
       retry:;
         try {
-          std::lock_guard<std::mutex> lock(g_luamutex);
+          auto lua = g_lua.lock();
         
           g_outputBuffer.clear();
           resetLuaSideEffect();
-          auto ret=g_lua.executeCode<
+          auto ret = lua->executeCode<
             boost::optional<
               boost::variant<
                 string, 
index 95a4d539131e8c6ef31b7cb9f349283737838011..236bd28fee201b00d733ed43394024d09be3d14d 100644 (file)
@@ -381,7 +381,7 @@ public:
 
   DNSAction::Action operator()(DNSQuestion* dq, std::string* ruleresult) const override
   {
-    std::lock_guard<std::mutex> lock(g_luamutex);
+    auto lock = g_lua.lock();
     try {
       auto ret = d_func(dq);
       if (ruleresult) {
@@ -418,7 +418,7 @@ public:
   {}
   DNSResponseAction::Action operator()(DNSResponse* dr, std::string* ruleresult) const override
   {
-    std::lock_guard<std::mutex> lock(g_luamutex);
+    auto lock = g_lua.lock();
     try {
       auto ret = d_func(dr);
       if (ruleresult) {
@@ -460,8 +460,7 @@ public:
   {
     dnsdist_ffi_dnsquestion_t dqffi(dq);
     try {
-      std::lock_guard<std::mutex> lock(g_luamutex);
-
+      auto lock = g_lua.lock();
       auto ret = d_func(&dqffi);
       if (ruleresult) {
         if (dqffi.result) {
@@ -576,8 +575,7 @@ public:
 
     dnsdist_ffi_dnsquestion_t dqffi(dq);
     try {
-      std::lock_guard<std::mutex> lock(g_luamutex);
-
+      auto lock = g_lua.lock();
       auto ret = d_func(&dqffi);
       if (ruleresult) {
         if (dqffi.result) {
@@ -1269,7 +1267,7 @@ public:
     DnstapMessage message(data, !dq->getHeader()->qr ? DnstapMessage::MessageType::client_query : DnstapMessage::MessageType::client_response, d_identity, dq->remote, dq->local, protocol, reinterpret_cast<const char*>(dq->getData().data()), dq->getData().size(), dq->queryTime, nullptr);
     {
       if (d_alterFunc) {
-        std::lock_guard<std::mutex> lock(g_luamutex);
+        auto lock = g_lua.lock();
         (*d_alterFunc)(dq, &message);
       }
     }
@@ -1314,7 +1312,7 @@ public:
 #endif /* HAVE_LIBCRYPTO */
 
     if (d_alterFunc) {
-      std::lock_guard<std::mutex> lock(g_luamutex);
+      auto lock = g_lua.lock();
       (*d_alterFunc)(dq, &message);
     }
 
@@ -1403,7 +1401,7 @@ public:
     DnstapMessage message(data, DnstapMessage::MessageType::client_response, d_identity, dr->remote, dr->local, protocol, reinterpret_cast<const char*>(dr->getData().data()), dr->getData().size(), dr->queryTime, &now);
     {
       if (d_alterFunc) {
-        std::lock_guard<std::mutex> lock(g_luamutex);
+        auto lock = g_lua.lock();
         (*d_alterFunc)(dr, &message);
       }
     }
@@ -1448,7 +1446,7 @@ public:
 #endif /* HAVE_LIBCRYPTO */
 
     if (d_alterFunc) {
-      std::lock_guard<std::mutex> lock(g_luamutex);
+      auto lock = g_lua.lock();
       (*d_alterFunc)(dr, &message);
     }
 
index 920fc78bb8e48954e61c680e5926800933b2ade5..ba25be0765631d887797127a27f47636f0151aa0 100644 (file)
@@ -728,8 +728,7 @@ catch (...)
 }
 }
 
-std::mutex g_luamutex;
-LuaContext g_lua;
+LockGuarded<LuaContext> g_lua{LuaContext()};
 ComboAddress g_serverControl{"127.0.0.1:5199"};
 
 
@@ -853,7 +852,7 @@ static bool applyRulesToQuery(LocalHolders& holders, DNSQuestion& dq, const stru
     string qname = (*dq.qname).toLogString();
     bool countQuery{true};
     if(g_qcount.filter) {
-      std::lock_guard<std::mutex> lock(g_luamutex);
+      auto lock = g_lua.lock();
       std::tie (countQuery, qname) = g_qcount.filter(&dq);
     }
 
@@ -1603,8 +1602,8 @@ static void maintThread()
     sleep(interval);
 
     {
-      std::lock_guard<std::mutex> lock(g_luamutex);
-      auto f = g_lua.readVariable<boost::optional<std::function<void()> > >("maintenance");
+      auto lua = g_lua.lock();
+      auto f = lua->readVariable<boost::optional<std::function<void()> > >("maintenance");
       if (f) {
         try {
           (*f)();
@@ -2217,7 +2216,7 @@ int main(int argc, char** argv)
 
     g_policy.setState(leastOutstandingPol);
     if(g_cmdLine.beClient || !g_cmdLine.command.empty()) {
-      setupLua(g_lua, true, false, g_cmdLine.config);
+      setupLua(*(g_lua.lock()), true, false, g_cmdLine.config);
       if (clientAddress != ComboAddress())
         g_serverControl = clientAddress;
       doClient(g_serverControl, g_cmdLine.command);
@@ -2243,7 +2242,7 @@ int main(int argc, char** argv)
     registerBuiltInWebHandlers();
 
     if (g_cmdLine.checkConfig) {
-      setupLua(g_lua, false, true, g_cmdLine.config);
+      setupLua(*(g_lua.lock()), false, true, g_cmdLine.config);
       // No exception was thrown
       infolog("Configuration '%s' OK!", g_cmdLine.config);
 #ifdef COVERAGE
@@ -2253,7 +2252,7 @@ int main(int argc, char** argv)
 #endif
     }
 
-    auto todo = setupLua(g_lua, false, false, g_cmdLine.config);
+    auto todo = setupLua(*(g_lua.lock()), false, false, g_cmdLine.config);
 
     auto localPools = g_pools.getCopy();
     {
index c9ef384522743b65c7974172892fb801e2793dde..74b0647cff9967e81bef7d431c892c94579f6dd0 100644 (file)
@@ -1080,8 +1080,7 @@ private:
 using servers_t =vector<std::shared_ptr<DownstreamState>>;
 
 void responderThread(std::shared_ptr<DownstreamState> state);
-extern std::mutex g_luamutex;
-extern LuaContext g_lua;
+extern LockGuarded<LuaContext> g_lua;
 extern std::string g_outputBuffer; // locking for this is ok, as locked by g_luamutex
 
 class DNSRule
index 4b1dd9057e456424c7ff5be190056c352c5d29de..44629e7ba17e4f35ca5c601909449a289aee45b6 100644 (file)
@@ -195,7 +195,7 @@ bool queueHealthCheck(std::shared_ptr<FDMultiplexer>& mplexer, const std::shared
     }
 
     if (ds->checkFunction) {
-      std::lock_guard<std::mutex> lock(g_luamutex);
+      auto lock = g_lua.lock();
       auto ret = ds->checkFunction(checkName, checkType, checkClass, &checkHeader);
       checkName = std::get<0>(ret);
       checkType = std::get<1>(ret);
index d01ccd69acaceb0eb5a585a8cad8894a42d7b80e..7138782774c035a6563114644210f8a0d07c8c1f 100644 (file)
@@ -329,7 +329,7 @@ std::shared_ptr<DownstreamState> ServerPolicy::getSelectedBackend(const ServerPo
 
   if (d_isLua) {
     if (!d_isFFI) {
-      std::lock_guard<std::mutex> lock(g_luamutex);
+      auto lock = g_lua.lock();
       selectedBackend = d_policy(servers, &dq);
     }
     else {
@@ -338,7 +338,7 @@ std::shared_ptr<DownstreamState> ServerPolicy::getSelectedBackend(const ServerPo
       unsigned int selected = 0;
 
       if (!d_isPerThread) {
-        std::lock_guard<std::mutex> lock(g_luamutex);
+        auto lock = g_lua.lock();
         selected = d_ffipolicy(&serversList, &dnsq);
       }
       else {
index 0b42866341afaa37efc0804f22bed02a235f56dd..c40519363c2ea4dbe85b706c5f0e6b0ff31e49fe 100644 (file)
@@ -1161,7 +1161,7 @@ public:
   bool matches(const DNSQuestion* dq) const override
   {
     try {
-      std::lock_guard<std::mutex> lock(g_luamutex);
+      auto lock = g_lua.lock();
       return d_func(dq);
     } catch (const std::exception &e) {
       warnlog("LuaRule failed inside Lua: %s", e.what());
@@ -1190,7 +1190,7 @@ public:
   {
     dnsdist_ffi_dnsquestion_t dqffi(const_cast<DNSQuestion*>(dq));
     try {
-      std::lock_guard<std::mutex> lock(g_luamutex);
+      auto lock = g_lua.lock();
       return d_func(&dqffi);
     } catch (const std::exception &e) {
       warnlog("LuaFFIRule failed inside Lua: %s", e.what());
index 29f297f72ef0381b3e4cd13a2639125dfe91a8b3..b1a31886e3048608d9057126c89993a2ea376f91 100644 (file)
@@ -11,9 +11,8 @@
 
 uint16_t g_maxOutstanding{std::numeric_limits<uint16_t>::max()};
 
-std::mutex g_luamutex;
 #include "ext/luawrapper/include/LuaContext.hpp"
-LuaContext g_lua;
+LockGuarded<LuaContext> g_lua{LuaContext()};
 
 bool g_snmpEnabled{false};
 bool g_snmpTrapsEnabled{false};
@@ -148,7 +147,7 @@ static void resetLuaContext()
   /* we need to reset this before cleaning the Lua state because the server policy might holds
      a reference to a Lua function (Lua policies) */
   g_policy.setState(ServerPolicy("leastOutstanding", leastOutstanding, false));
-  g_lua = LuaContext();
+  *(g_lua.lock()) = LuaContext();
 }
 
 BOOST_AUTO_TEST_SUITE(dnsdistlbpolicies)
@@ -571,10 +570,10 @@ BOOST_AUTO_TEST_CASE(test_lua) {
     setServerPolicyLua("luaroundrobin", luaroundrobin)
   )foo";
   resetLuaContext();
-  g_lua.writeFunction("setServerPolicyLua", [](string name, ServerPolicy::policyfunc_t policy) {
+  g_lua.lock()->writeFunction("setServerPolicyLua", [](string name, ServerPolicy::policyfunc_t policy) {
       g_policy.setState(ServerPolicy{name, policy, true});
     });
-  g_lua.executeCode(policySetupStr);
+  g_lua.lock()->executeCode(policySetupStr);
 
   {
     ServerPolicy pol = g_policy.getCopy();
@@ -630,11 +629,11 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_rr) {
     setServerPolicyLuaFFI("FFI round-robin", ffilb)
   )foo";
   resetLuaContext();
-  g_lua.executeCode(getLuaFFIWrappers());
-  g_lua.writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) {
+  g_lua.lock()->executeCode(getLuaFFIWrappers());
+  g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) {
       g_policy.setState(ServerPolicy(name, policy));
     });
-  g_lua.executeCode(policySetupStr);
+  g_lua.lock()->executeCode(policySetupStr);
 
   {
     ServerPolicy pol = g_policy.getCopy();
@@ -687,11 +686,11 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_hashed) {
     setServerPolicyLuaFFI("FFI hashed", ffilb)
   )foo";
   resetLuaContext();
-  g_lua.executeCode(getLuaFFIWrappers());
-  g_lua.writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) {
+  g_lua.lock()->executeCode(getLuaFFIWrappers());
+  g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) {
       g_policy.setState(ServerPolicy(name, policy));
     });
-  g_lua.executeCode(policySetupStr);
+  g_lua.lock()->executeCode(policySetupStr);
 
   {
     ServerPolicy pol = g_policy.getCopy();
@@ -742,11 +741,11 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_whashed) {
     setServerPolicyLuaFFI("FFI whashed", ffilb)
   )foo";
   resetLuaContext();
-  g_lua.executeCode(getLuaFFIWrappers());
-  g_lua.writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) {
+  g_lua.lock()->executeCode(getLuaFFIWrappers());
+  g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) {
       g_policy.setState(ServerPolicy(name, policy));
     });
-  g_lua.executeCode(policySetupStr);
+  g_lua.lock()->executeCode(policySetupStr);
 
   {
     ServerPolicy pol = g_policy.getCopy();
@@ -800,11 +799,11 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_chashed) {
     setServerPolicyLuaFFI("FFI chashed", ffilb)
   )foo";
   resetLuaContext();
-  g_lua.executeCode(getLuaFFIWrappers());
-  g_lua.writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) {
+  g_lua.lock()->executeCode(getLuaFFIWrappers());
+  g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) {
       g_policy.setState(ServerPolicy(name, policy));
     });
-  g_lua.executeCode(policySetupStr);
+  g_lua.lock()->executeCode(policySetupStr);
 
   {
     ServerPolicy pol = g_policy.getCopy();