From cceacd60d5f6cbf2ab40b3e4d1a543f65a2cf4f0 Mon Sep 17 00:00:00 2001 From: Fred Morcos Date: Wed, 3 Aug 2022 13:09:38 +0200 Subject: [PATCH] Handle file-related errors when loading Lua scripts --- pdns/lua-base4.cc | 14 +++++++------- pdns/lua-base4.hh | 2 +- pdns/recursordist/rec-main.cc | 8 +++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pdns/lua-base4.cc b/pdns/lua-base4.cc index a2da41bb75..c82c93a047 100644 --- a/pdns/lua-base4.cc +++ b/pdns/lua-base4.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -17,18 +18,17 @@ BaseLua4::BaseLua4() { } -int BaseLua4::loadFile(const std::string& fname) +void BaseLua4::loadFile(const std::string& fname) { - int ret = 0; std::ifstream ifs(fname); if (!ifs) { - ret = errno; - SLOG(g_log << Logger::Error << "Unable to read configuration file from '" << fname << "': " << stringerror(ret) << endl, - g_slog->withName("lua")->error(Logr::Error, ret, "Unable to read configuration file", "file", Logging::Loggable(fname))); - return ret; + auto ret = errno; + auto msg = stringerror(ret); + SLOG(g_log << Logger::Error << "Unable to read configuration file from '" << fname << "': " << msg << endl, + g_slog->withName("lua")->error(Logr::Error, ret, "Unable to read configuration file", "file", Logging::Loggable(fname), "msg", Logging::Loggable(msg))); + throw std::runtime_error(msg); } loadStream(ifs); - return 0; }; void BaseLua4::loadString(const std::string &script) { diff --git a/pdns/lua-base4.hh b/pdns/lua-base4.hh index 9394de4f84..1dbf62eecb 100644 --- a/pdns/lua-base4.hh +++ b/pdns/lua-base4.hh @@ -13,7 +13,7 @@ protected: public: BaseLua4(); - int loadFile(const std::string &fname); + void loadFile(const std::string &fname); void loadString(const std::string &script); void loadStream(std::istream &is); virtual ~BaseLua4(); // this is so unique_ptr works with an incomplete type diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index 48685818dc..c248fcfc8b 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -2957,9 +2957,11 @@ static RecursorControlChannel::Answer* doReloadLuaScript() } else { t_pdl = std::make_shared(); - int err = t_pdl->loadFile(fname); - if (err != 0) { - string msg = std::to_string(RecThreadInfo::id()) + " Retaining current script, could not read '" + fname + "': " + stringerror(err); + try { + t_pdl->loadFile(fname); + } + catch (std::runtime_error& ex) { + string msg = std::to_string(RecThreadInfo::id()) + " Retaining current script, could not read '" + fname + "': " + ex.what(); g_log << Logger::Error << msg << endl; return new RecursorControlChannel::Answer{1, msg + "\n"}; } -- 2.47.3