From: Marek VavruĊĦa Date: Fri, 27 Mar 2015 12:35:32 +0000 (+0100) Subject: daemon: added basic bindings for modules, config and cache X-Git-Tag: v1.0.0-beta1~278^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc5a4eaacd362ee8c63f562fa3f10c3847766903;p=thirdparty%2Fknot-resolver.git daemon: added basic bindings for modules, config and cache --- diff --git a/daemon/bindings.c b/daemon/bindings.c new file mode 100644 index 000000000..1ba5e05c2 --- /dev/null +++ b/daemon/bindings.c @@ -0,0 +1,130 @@ +/* Copyright (C) 2015 CZ.NIC, z.s.p.o. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#include "lib/cache.h" +#include "daemon/bindings.h" + +/** @internal Compatibility wrapper for Lua 5.0 - 5.2 */ +#if LUA_VERSION_NUM < 502 +#define register_lib(L, name, lib) \ + luaL_openlib((L), (name), (lib), 0) +#else +#define register_lib(L, name, lib) \ + luaL_newlib((L), (lib)) +#endif + +/** List loaded modules */ +static int mod_list(lua_State *L) +{ + struct engine *engine = engine_luaget(L); + for (unsigned i = 0; i < engine->modules.len; ++i) { + struct kr_module *module = &engine->modules.at[i]; + lua_pushstring(L, module->name); + } + return engine->modules.len; +} + +/** Load module. */ +static int mod_load(lua_State *L) +{ + /* Check parameters */ + int n = lua_gettop(L); + if (n != 1 || !lua_isstring(L, 1)) { + lua_pushstring(L, "expected module name"); + lua_error(L); + } + /* Load engine module */ + struct engine *engine = engine_luaget(L); + int ret = engine_register(engine, lua_tostring(L, 1)); + if (ret != 0) { + lua_pushstring(L, kr_strerror(ret)); + lua_error(L); + } + return 0; +} + +/** Unload module. */ +static int mod_unload(lua_State *L) +{ + lua_pushstring(L, "not implemented"); + lua_error(L); + return 0; +} + +int lib_modules(lua_State *L) +{ + static const luaL_Reg lib[] = { + { "list", mod_list }, + { "load", mod_load }, + { "unload", mod_unload }, + { NULL, NULL } + }; + + register_lib(L, "modules", lib); + return 1; +} + +int lib_config(lua_State *L) +{ + return 0; +} + +/** Open cache */ +static int cache_open(lua_State *L) +{ + /* Check parameters */ + int n = lua_gettop(L); + if (n < 2) { + lua_pushstring(L, "expected (string path, int size)"); + lua_error(L); + } + + /* Open resolution context cache */ + struct engine *engine = engine_luaget(L); + engine->resolver.cache = kr_cache_open(lua_tostring(L, 1), engine->pool, lua_tointeger(L, 2)); + if (engine->resolver.cache == NULL) { + lua_pushstring(L, "invalid cache directory: "); + lua_pushstring(L, lua_tostring(L, 1)); + lua_concat(L, 2); + lua_error(L); + } + + lua_pushboolean(L, 1); + return 1; +} + +static int cache_close(lua_State *L) +{ + struct engine *engine = engine_luaget(L); + if (engine->resolver.cache != NULL) { + kr_cache_close(engine->resolver.cache); + engine->resolver.cache = NULL; + } + + return 0; +} + +int lib_cache(lua_State *L) +{ + static const luaL_Reg lib[] = { + { "open", cache_open }, + { "close", cache_close }, + { NULL, NULL } + }; + + register_lib(L, "cache", lib); + return 0; +} \ No newline at end of file diff --git a/daemon/bindings.h b/daemon/bindings.h new file mode 100644 index 000000000..bb2fc1fe3 --- /dev/null +++ b/daemon/bindings.h @@ -0,0 +1,43 @@ +/* Copyright (C) 2015 CZ.NIC, z.s.p.o. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +/** + * Bindings to engine services, see \a http://www.lua.org/manual/5.2/manual.html#luaL_newlib for the reference. + */ +#pragma once + +#include "daemon/engine.h" + +/** + * Load 'modules' package. + * @param L scriptable + * @return number of packages to load + */ +int lib_modules(lua_State *L); + +/** + * Load 'config' package. + * @param L scriptable + * @return number of packages to load + */ +int lib_config(lua_State *L); + +/** + * Load 'cache' package. + * @param L scriptable + * @return number of packages to load + */ +int lib_cache(lua_State *L); \ No newline at end of file diff --git a/daemon/daemon.mk b/daemon/daemon.mk index ccb127ba2..70218f4e4 100644 --- a/daemon/daemon.mk +++ b/daemon/daemon.mk @@ -4,6 +4,7 @@ kresolved_SOURCES := \ daemon/tcp.c \ daemon/engine.c \ daemon/worker.c \ + daemon/bindings.c \ daemon/main.c # Embed resources diff --git a/daemon/main.c b/daemon/main.c index b79122328..396bdaba7 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -25,6 +25,7 @@ #include "daemon/udp.h" #include "daemon/tcp.h" #include "daemon/engine.h" +#include "daemon/bindings.h" static void tty_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { @@ -141,6 +142,10 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + /* Load bindings */ + engine_lualib(&engine, "modules", lib_modules); + engine_lualib(&engine, "config", lib_config); + engine_lualib(&engine, "cache", lib_cache); /* Create main worker. */ struct worker_ctx worker = {