]> granicus.if.org Git - pdns/commitdiff
move all the lua configuration items to a struct that is RCU for easy reloading
authorbert hubert <bert.hubert@netherlabs.nl>
Sun, 29 Nov 2015 12:41:41 +0000 (13:41 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Sun, 29 Nov 2015 12:41:41 +0000 (13:41 +0100)
pdns/pdns_recursor.cc
pdns/rec-lua-conf.cc

index 8c1e09d376423432c0df48e4a578ad2a9141083d..8da6b6a677de68d5abc8895e99acc37d3693f162 100644 (file)
@@ -80,11 +80,13 @@ extern SortList g_sortlist;
 #include "filterpo.hh"
 #include "rpzloader.hh"
 #include "validate-recursor.hh"
+#include "rec-lua-conf.hh"
+
 #ifndef RECURSOR
 #include "statbag.hh"
 StatBag S;
 #endif
-void loadRecursorLuaConfig(const std::string& fname);
+
 __thread FDMultiplexer* t_fdm;
 __thread unsigned int t_id;
 unsigned int g_maxTCPPerClient;
@@ -819,7 +821,7 @@ void startDoResolve(void *p)
 
       if(ret.size()) {
         orderAndShuffle(ret);
-       if(auto sl = g_sortlist.getOrderCmp(dc->d_remote)) {
+       if(auto sl = g_luaconfs.getCopy().sortlist.getOrderCmp(dc->d_remote)) {
          sort(ret.begin(), ret.end(), *sl);
          variableAnswer=true;
        }
index 1dabe6e7c034fd65743b034d1e20ae116d5aa331..7471413c6dae44cfd1d5de93ddeae3099cf88d1a 100644 (file)
@@ -2,11 +2,32 @@
 #include <fstream>
 #include "namespaces.hh"
 #include "logger.hh"
+#include "rec-lua-conf.hh"
 #include "sortlist.hh"
 
-SortList g_sortlist;
+GlobalStateHolder<LuaConfigItems> g_luaconfs;
+
+/* SO HOW DOES THIS WORK! AND PLEASE PAY ATTENTION!
+   This function can be called at any time. It is expected to overwrite all the contents
+   of LuaConfigItems, which is held in a GlobalStateHolder for RCU properties.
+
+   This function can be called again at a later date, so you must make sure that anything you
+   allow to be configured from here lives in g_luaconfs AND NOWHERE ELSE.
+
+   If someone loads an empty Lua file, the default LuaConfigItems struct MUST MAKE SENSE.
+
+   To make this easy on you, here is a LuaConfigItems constructor where you
+   can set sane defaults:
+*/
+
+LuaConfigItems::LuaConfigItems()
+{
+}
+
 void loadRecursorLuaConfig(const std::string& fname)
 {
+  LuaConfigItems lci;
+
   LuaContext Lua;
   if(fname.empty())
     return;
@@ -15,7 +36,7 @@ void loadRecursorLuaConfig(const std::string& fname)
     theL()<<"Unable to read configuration file from '"<<fname<<"': "<<strerror(errno)<<endl;
     return;
   }
-  Lua.writeFunction("clearSortlist", []() { g_sortlist.clear(); });
+  Lua.writeFunction("clearSortlist", [&lci]() { lci.sortlist.clear(); });
   
   /* we can get: "1.2.3.4"
                  {"1.2.3.4", "4.5.6.7"}
@@ -24,26 +45,26 @@ void loadRecursorLuaConfig(const std::string& fname)
 
   typedef vector<pair<int,boost::variant<string, vector<pair<int, string> > > > > argvec_t;
   Lua.writeFunction("addSortList", 
-                   [](const std::string& formask_, 
+                   [&lci](const std::string& formask_, 
                       const boost::variant<string, argvec_t>& masks,
                       boost::optional<int> order_) 
                    {
                      try {
                        Netmask formask(formask_);
-                       int order = order_ ? (*order_) : g_sortlist.getMaxOrder(formask)+1;
+                       int order = order_ ? (*order_) : lci.sortlist.getMaxOrder(formask)+1;
                        if(auto str = boost::get<string>(&masks)) 
-                         g_sortlist.addEntry(formask, Netmask(*str), order);
+                         lci.sortlist.addEntry(formask, Netmask(*str), order);
                        else {
        
                          auto vec = boost::get<argvec_t>(&masks);
                          for(const auto& e : *vec) {
                            if(auto s = boost::get<string>(&e.second)) {
-                             g_sortlist.addEntry(formask, Netmask(*s), order);
+                             lci.sortlist.addEntry(formask, Netmask(*s), order);
                            }
                            else {
                              const auto& v =boost::get<vector<pair<int, string> > >(e.second);
                              for(const auto& e : v)
-                               g_sortlist.addEntry(formask, Netmask(e.second), order);
+                               lci.sortlist.addEntry(formask, Netmask(e.second), order);
                            }
                            ++order;
                          }
@@ -54,5 +75,5 @@ void loadRecursorLuaConfig(const std::string& fname)
                      }
                    });
   Lua.executeCode(ifs);
-  
+  g_luaconfs.setState(lci);
 }