From: Otto Moerbeek Date: Mon, 14 Oct 2024 13:22:25 +0000 (+0200) Subject: Make Lua startup script customizable X-Git-Tag: rec-5.2.0-alpha1~12^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34bdffee068a7822a1690457018c23f667ed9bce;p=thirdparty%2Fpdns.git Make Lua startup script customizable --- diff --git a/pdns/recursordist/docs/lua-scripting/functions.rst b/pdns/recursordist/docs/lua-scripting/functions.rst index 1e3c503f3c..404337966e 100644 --- a/pdns/recursordist/docs/lua-scripting/functions.rst +++ b/pdns/recursordist/docs/lua-scripting/functions.rst @@ -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. diff --git a/pdns/recursordist/lua-recursor4.cc b/pdns/recursordist/lua-recursor4.cc index 7f6d8bb87e..0349d204bb 100644 --- a/pdns/recursordist/lua-recursor4.cc +++ b/pdns/recursordist/lua-recursor4.cc @@ -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(); + lua->loadFile(scriptName); + log->info(Logr::Notice, "Lua thread exiting"); + }); + thread.detach(); + }); + + if (!d_include_path.empty()) { includePath(d_include_path); } diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index fae9f7aa16..a638ef8ea0 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -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> parseCPUMap(Logr::log_t log) { std::map> 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(); - 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 doConfig(Logr::log_t startupLog, const string& configname LockGuarded 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(); + 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 diff --git a/pdns/recursordist/recursor_cache.cc b/pdns/recursordist/recursor_cache.cc index b0b8dfd274..2e988d642d 100644 --- a/pdns/recursordist/recursor_cache.cc +++ b/pdns/recursordist/recursor_cache.cc @@ -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 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); diff --git a/pdns/recursordist/settings/table.py b/pdns/recursordist/settings/table.py index 8f0733ac47..cebd18355f 100644 --- a/pdns/recursordist/settings/table.py +++ b/pdns/recursordist/settings/table.py @@ -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', + }, ]