]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Make Lua startup script customizable
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 14 Oct 2024 13:22:25 +0000 (15:22 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Thu, 24 Oct 2024 09:23:06 +0000 (11:23 +0200)
pdns/recursordist/docs/lua-scripting/functions.rst
pdns/recursordist/lua-recursor4.cc
pdns/recursordist/rec-main.cc
pdns/recursordist/recursor_cache.cc
pdns/recursordist/settings/table.py

index 1e3c503f3cc8f7909a228d82f9ca23e357ed362e..404337966ea1ca413795c1eefeb58d13728dd7e7 100644 (file)
@@ -21,3 +21,11 @@ These are some functions that don't really have a place in one of the other cate
   Get a random number.
 
   :param int upper_bound: The upper bound. You will get a random number below this upper bound.
+
+.. function:: spawnThread(script)
+
+   .. versionadded:: 5.2.0
+
+   Spawn a thread.
+
+   :param str script: The pathname of the Lua script to run.
index 7f6d8bb87e27377ef0e0b95613801bee6a747a71..0349d204bb1e6a6a16a8be98b54d7dc660044c67 100644 (file)
@@ -504,6 +504,18 @@ void RecursorLua4::postPrepareContext() // NOLINT(readability-function-cognitive
     g_recCache->putRecords(data);
   });
 
+  d_lw->writeFunction("spawnThread", [](const string& scriptName) {
+    auto log = g_slog->withName("lua")->withValues("name", Logging::Loggable(scriptName));
+    log->info(Logr::Info, "Starting Lua thread");
+    std::thread thread([=]() {
+      auto lua = std::make_shared<RecursorLua4>();
+      lua->loadFile(scriptName);
+      log->info(Logr::Notice, "Lua thread exiting");
+    });
+    thread.detach();
+  });
+
+
   if (!d_include_path.empty()) {
     includePath(d_include_path);
   }
index fae9f7aa16202ce5b7f99995dca74ea69c9d7dc1..a638ef8ea0880feecfd2757c1405c6b8fe0359a8 100644 (file)
@@ -147,6 +147,8 @@ thread_local unsigned int RecThreadInfo::t_id;
 
 pdns::RateLimitedLog g_rateLimitedLogger;
 
+static void runCustomLua(Logr::log_t log);
+
 static std::map<unsigned int, std::set<int>> parseCPUMap(Logr::log_t log)
 {
   std::map<unsigned int, std::set<int>> result;
@@ -807,17 +809,6 @@ static void checkSocketDir(Logr::log_t log)
   _exit(1);
 }
 
-static void setupNatsThread(Logr::log_t log)
-{
-  log->info(Logr::Info, "Starting nats thread");
-  std::thread thread([tid = std::this_thread::get_id(), log]() {
-    auto lua = std::make_shared<RecursorLua4>();
-    lua->loadFile("tmp/natsimpl.lua");
-    log->info(Logr::Info, "Nats thread exited");
-  });
-  thread.detach();
-}
-
 #ifdef NOD_ENABLED
 static void setupNODThread(Logr::log_t log)
 {
@@ -2387,7 +2378,7 @@ static int serviceMain(Logr::log_t log)
   setupNODThread(log);
 #endif /* NOD_ENABLED */
 
-  setupNatsThread(log);
+  runCustomLua(log);
   
   return RecThreadInfo::runThreads(log);
 }
@@ -3050,6 +3041,22 @@ static pair<int, bool> doConfig(Logr::log_t startupLog, const string& configname
 
 LockGuarded<pdns::rust::settings::rec::Recursorsettings> g_yamlStruct;
 
+static void runCustomLua(Logr::log_t log)
+{
+  auto settings = g_yamlStruct.lock();
+  const auto& script = settings->recursor.lua_startup_script;
+  if (script.empty()) {
+    return;
+  }
+  log->info(Logr::Info, "Starting Custom Lua", "script", Logging::Loggable(script));
+  //std::thread thread([=]() {
+    auto lua = std::make_shared<RecursorLua4>();
+    lua->loadFile(std::string(script));
+    log->info(Logr::Info, "Custom Lua done");
+    //});
+    //thread.detach();
+}
+
 static void handleRuntimeDefaults(Logr::log_t log)
 {
 #ifdef HAVE_FIBER_SANITIZER
index b0b8dfd2742d629362c17d80e73fd82c4078e2cd..2e988d642d5455ef8f0b2a8e368b8da574de15b9 100644 (file)
@@ -991,7 +991,6 @@ void MemRecursorCache::getRecords(size_t howmany, size_t maxSize, std::string& r
     string buf;
     for (const auto& recordSet : sidx) {
       protozero::pbf_builder<PBCacheEntry> message(full, PBCacheDump::repeated_message_cacheEntry);
-      cerr << "S " << count << ' ' << recordSet.d_qname << ' ' << QType(recordSet.d_qtype) << ' ' << recordSet.d_auth << ' ' << recordSet.d_records.size() << endl;
       // Two fields below must come before the other fields
       message.add_bytes(PBCacheEntry::required_bytes_name, recordSet.d_qname.toString());
       message.add_uint32(PBCacheEntry::required_uint32_qtype, recordSet.d_qtype);
index 8f0733ac477ae5d85ed7bd77b2f3399b0289ccc9..cebd18355fa804a6967c387203cb7995ec088061 100644 (file)
@@ -3510,4 +3510,16 @@ Sequence of ProxyMapping
         'skip-old' : 'Equivalent Lua config in :doc:`lua-config/proxymapping`',
         'versionadded': '5.1.0',
     },
+    {
+        'name' : 'lua_startup_script',
+        'section' : 'recursor',
+        'type' : LType.String,
+        'default' : '',
+        'help' : 'Custom Lua script to run on startup',
+        'doc' : '''
+Run this Lua script on startup in a separate thread.
+        ''',
+        'skip-old' : 'No equivalent old-style setting',
+        'versionadded': '5.2.0',
+    },
 ]