From: Tomas Krizek Date: Tue, 12 Feb 2019 11:44:54 +0000 (+0100) Subject: meson: remove moduledir X-Git-Tag: v4.0.0~24^2~140 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a871bc4f0dfe0879587eb60779f3d612650cd331;p=thirdparty%2Fknot-resolver.git meson: remove moduledir --- diff --git a/.luacheckrc b/.luacheckrc index 26f72e476..1729bd4b5 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -4,7 +4,6 @@ new_read_globals = { 'quit', 'hostname', 'package_version', - 'moduledir', 'user', 'verbose', 'resolve', diff --git a/daemon/README.rst b/daemon/README.rst index 3243b5841..8df003d97 100644 --- a/daemon/README.rst +++ b/daemon/README.rst @@ -255,14 +255,6 @@ Environment This affects ephemeral certificates for kresd serving DNS over TLS. -.. function:: moduledir([dir]) - - :return: Modules directory. - - If called with a parameter, it will change kresd's directory for - looking up the dynamic modules. If called without a parameter, it - will return kresd's modules directory. - .. function:: verbose(true | false) :return: Toggle verbose logging. diff --git a/daemon/engine.c b/daemon/engine.c index 344ab24b6..2eb57684e 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -230,63 +230,6 @@ static int l_package_version(lua_State *L) return 1; } -char *engine_get_moduledir(struct engine *engine) { - return engine->moduledir; -} - -int engine_set_moduledir(struct engine *engine, const char *moduledir) { - if (!engine || !moduledir) { - return kr_error(EINVAL); - } - - char *new_moduledir = strdup(moduledir); - if (!new_moduledir) { - return kr_error(ENOMEM); - } - if (engine->moduledir) { - free(engine->moduledir); - } - engine->moduledir = new_moduledir; - - /* Use module path for including Lua scripts */ - char l_paths[MAXPATHLEN] = { 0 }; - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wformat" /* %1$ is not in C standard */ - /* Save original package.path to package._path */ - snprintf(l_paths, MAXPATHLEN - 1, - "if package._path == nil then package._path = package.path end\n" - "package.path = '%1$s/?.lua;%1$s/?/init.lua;'..package._path\n" - "if package._cpath == nil then package._cpath = package.cpath end\n" - "package.cpath = '%1$s/?%2$s;'..package._cpath\n", - new_moduledir, LIBEXT); - #pragma GCC diagnostic pop - - int ret = l_dobytecode(engine->L, l_paths, strlen(l_paths), ""); - if (ret != 0) { - lua_pop(engine->L, 1); - return ret; - } - return 0; -} - -/** Return hostname. */ -static int l_moduledir(lua_State *L) -{ - struct engine *engine = engine_luaget(L); - if (lua_gettop(L) == 0) { - lua_pushstring(L, engine_get_moduledir(engine)); - return 1; - } - if ((lua_gettop(L) != 1) || !lua_isstring(L, 1)) - lua_error_p(L, "moduledir takes at most one parameter: (\"directory\")"); - - if (engine_set_moduledir(engine, lua_tostring(L, 1)) != 0) - lua_error_p(L, "setting moduledir failed"); - - lua_pushstring(L, engine_get_moduledir(engine)); - return 1; -} - /** Load root hints from zonefile. */ static int l_hint_root_file(lua_State *L) { @@ -608,8 +551,6 @@ static int init_state(struct engine *engine) lua_setglobal(engine->L, "hostname"); lua_pushcfunction(engine->L, l_package_version); lua_setglobal(engine->L, "package_version"); - lua_pushcfunction(engine->L, l_moduledir); - lua_setglobal(engine->L, "moduledir"); lua_pushcfunction(engine->L, l_verbose); lua_setglobal(engine->L, "verbose"); lua_pushcfunction(engine->L, l_setuser); @@ -659,6 +600,33 @@ static void init_measurement(struct engine *engine) free(snippet); } +int init_lua(struct engine *engine) { + if (!engine) { + return kr_error(EINVAL); + } + + /* Use libdir path for including Lua scripts */ + char l_paths[MAXPATHLEN] = { 0 }; + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wformat" /* %1$ is not in C standard */ + /* Save original package.path to package._path */ + snprintf(l_paths, MAXPATHLEN - 1, + "if package._path == nil then package._path = package.path end\n" + "package.path = '%1$s/?.lua;%1$s/?/init.lua;'..package._path\n" + "if package._cpath == nil then package._cpath = package.cpath end\n" + "package.cpath = '%1$s/?%2$s;'..package._cpath\n", + LIBDIR, LIBEXT); + #pragma GCC diagnostic pop + + int ret = l_dobytecode(engine->L, l_paths, strlen(l_paths), ""); + if (ret != 0) { + lua_pop(engine->L, 1); + return ret; + } + return 0; +} + + int engine_init(struct engine *engine, knot_mm_t *pool) { if (engine == NULL) { @@ -684,6 +652,13 @@ int engine_init(struct engine *engine, knot_mm_t *pool) /* Initialize network */ network_init(&engine->net, uv_default_loop(), TCP_BACKLOG_DEFAULT); + /* Initialize lua */ + ret = init_lua(engine); + if (ret != 0) { + engine_deinit(engine); + return ret; + } + return ret; } @@ -737,7 +712,6 @@ void engine_deinit(struct engine *engine) kr_ta_clear(&engine->resolver.trust_anchors); kr_ta_clear(&engine->resolver.negative_anchors); free(engine->hostname); - free(engine->moduledir); } int engine_pcall(lua_State *L, int argc) diff --git a/daemon/engine.h b/daemon/engine.h index b8d2d11e9..d968aff86 100644 --- a/daemon/engine.h +++ b/daemon/engine.h @@ -37,7 +37,6 @@ struct engine { knot_mm_t *pool; char *hostname; struct lua_State *L; - char *moduledir; }; int engine_init(struct engine *engine, knot_mm_t *pool); @@ -73,10 +72,6 @@ struct engine *engine_luaget(struct lua_State *L); char *engine_get_hostname(struct engine *engine); int engine_set_hostname(struct engine *engine, const char *hostname); -/** Set/get the per engine moduledir */ -char *engine_get_moduledir(struct engine *engine); -int engine_set_moduledir(struct engine *engine, const char *moduledir); - /** Load root hints from a zonefile (or config-time default if NULL). * * @return error message or NULL (statically allocated) diff --git a/daemon/main.c b/daemon/main.c index 4f3b453fe..f3d02d05a 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -781,8 +781,6 @@ int main(int argc, char **argv) } /* Start the scripting engine */ - engine_set_moduledir(&engine, LIBDIR); - if (engine_load_sandbox(&engine) != 0) { ret = EXIT_FAILURE; goto cleanup; diff --git a/modules/http/http.lua b/modules/http/http.lua.in similarity index 98% rename from modules/http/http.lua rename to modules/http/http.lua.in index 5384c1a74..88e56bab9 100644 --- a/modules/http/http.lua +++ b/modules/http/http.lua.in @@ -30,10 +30,9 @@ local mime_types = { -- Preload static contents, nothing on runtime will touch the disk local function pgload(relpath, modname) if not modname then modname = 'http' end - local mdir = moduledir() - local fp, err = io.open(string.format('%s/%s/%s', mdir, modname, relpath), 'r') + local fp, err = io.open(string.format('@modules_dir@/%s/%s', modname, relpath), 'r') if not fp then - fp, err = io.open(string.format('%s/%s/static/%s', mdir, modname, relpath), 'r') + fp, err = io.open(string.format('@modules_dir@/%s/static/%s', modname, relpath), 'r') end if not fp then error(err) end local data = fp:read('*all') diff --git a/modules/http/meson.build b/modules/http/meson.build index 44bf0eda3..8a4ac23c7 100644 --- a/modules/http/meson.build +++ b/modules/http/meson.build @@ -1,7 +1,16 @@ # LUA module: http +lua_http_config = configuration_data() +lua_http_config.set('modules_dir', modules_dir) + +lua_http = configure_file( + input: 'http.lua.in', + output: 'http.lua', + configuration: lua_http_config, +) + lua_mod_src += [ - files('http.lua'), + lua_http, files('http_trace.lua'), files('prometheus.lua'), ]