engine.c 7.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 <http://www.gnu.org/licenses/>.
 */

#include <uv.h>
#include <unistd.h>
#include <libknot/internal/mem.h>

#include "daemon/engine.h"
22
#include "daemon/bindings.h"
23 24 25 26 27 28 29
#include "lib/cache.h"
#include "lib/defines.h"

/*
 * Global bindings.
 */

30 31 32 33 34 35 36
/** Register module callback into Lua world. */
#define REGISTER_MODULE_CALL(L, module, cb, name) \
	lua_pushlightuserdata((L), (module)); \
	lua_pushlightuserdata((L), (cb)); \
	lua_pushcclosure((L), l_trampoline, 2); \
	lua_setfield((L), -2, (name))

37 38 39 40 41 42
/** Print help and available commands. */
static int l_help(lua_State *L)
{
	static const char *help_str = 
		"help()\n    show this help\n"
		"quit()\n    quit\n"
Marek Vavruša's avatar
Marek Vavruša committed
43 44 45 46 47
		"modules.list()\n    list modules\n"
		"modules.load()\n    load module\n"
		"modules.unload()\n    unload module\n"
		"cache.open(path, max_size)\n    open cache\n"
		"cache.close()\n    close cache\n"
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
		;
	puts(help_str);
	/* No results */
	return 0;
}

/** Quit current executable. */
static int l_quit(lua_State *L)
{
	/* Stop engine */
	engine_stop(engine_luaget(L));
	/* No results */
	return 0;
}

63
/** Trampoline function for module properties. */
64 65
static int l_trampoline(lua_State *L)
{
66 67
	struct kr_module *module = lua_touserdata(L, lua_upvalueindex(1));
	void* callback = lua_touserdata(L, lua_upvalueindex(2));
68 69
	struct engine *engine = engine_luaget(L);

70 71 72 73 74 75 76 77 78 79 80
	/* Now we only have property callback or config,
	 * if we expand the callables, we might need a callback_type.
	 */
	if (callback == module->config) {
		const char *param = lua_tostring(L, 1);
		module->config(module, param);
	} else {
		kr_prop_cb *prop = (kr_prop_cb *)callback;
		auto_free char *ret = prop(engine, module, lua_tostring(L, 1));
		lua_pushstring(L, ret);
		return 1;
81
	}
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	/* No results */
	return 0;
}

/*
 * Engine API.
 */

static int init_resolver(struct engine *engine)
{
	/* Open resolution context */
	engine->resolver.modules = &engine->modules;

	/* Load basic modules */
	engine_register(engine, "iterate");
	engine_register(engine, "itercache");

	return kr_ok();
}

static int init_state(struct engine *engine)
{
	/* Initialize Lua state */
	engine->L = luaL_newstate();
	if (engine->L == NULL) {
		return kr_error(ENOMEM);
	}
	/* Initialize used libraries. */
	luaL_openlibs(engine->L);
	/* Global functions */
	lua_pushcfunction(engine->L, l_help);
	lua_setglobal(engine->L, "help");
	lua_pushcfunction(engine->L, l_quit);
	lua_setglobal(engine->L, "quit");
	lua_pushlightuserdata(engine->L, engine);
	lua_setglobal(engine->L, "__engine");
	return kr_ok();
}

int engine_init(struct engine *engine, mm_ctx_t *pool)
{
	if (engine == NULL) {
		return kr_error(EINVAL);
	}

	memset(engine, 0, sizeof(*engine));
	engine->pool = pool;

	/* Initialize state */
	int ret = init_state(engine);
	if (ret != 0) {
		engine_deinit(engine);
	}
	/* Initialize resolver */
	ret = init_resolver(engine);
	if (ret != 0) {
		return ret;
	}
141 142
	/* Initialize network */
	network_init(&engine->net, uv_default_loop());
143 144 145 146 147 148 149 150 151 152

	return ret;
}

void engine_deinit(struct engine *engine)
{
	if (engine == NULL) {
		return;
	}

153 154
	network_deinit(&engine->net);

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	/* Unload modules. */
	for (size_t i = 0; i < engine->modules.len; ++i) {
		kr_module_unload(&engine->modules.at[i]);
	}
	array_clear(engine->modules);

	if (engine->L) {
		lua_close(engine->L);
	}

	kr_cache_close(engine->resolver.cache);
}

int engine_cmd(struct engine *engine, const char *str)
{
	if (engine == NULL || engine->L == NULL) {
		return kr_error(ENOEXEC);
	}

	/* Evaluate results */
	int ret = luaL_dostring(engine->L, str);

	/* Print results. */
	int nres = lua_gettop(engine->L);
	for (int i = 0; i < nres; ++i) {
		const char *out = lua_tostring(engine->L, -1);
		if (out != NULL) {
			printf("%s\n", out);
		}
		lua_pop(engine->L, 1);
	}

	/* Check result. */
	if (ret != 0) {
		return kr_error(EINVAL);
	}

	return kr_ok();
}

static int engine_loadconf(struct engine *engine)
{
197 198 199 200 201
	/* Init environment */
	static const char l_init[] = {
		#include "daemon/lua/init.inc"
	};
	if (luaL_dostring(engine->L, l_init) != 0) {
202 203
		fprintf(stderr, "[system] error %s\n", lua_tostring(engine->L, -1));
		lua_pop(engine->L, 1);
204 205 206
		return kr_error(ENOEXEC);
	}

207 208 209 210 211 212 213
	/* Load config file */
	int ret = 0;
	if(access("config", F_OK ) != -1 ) {
		ret = luaL_dofile(engine->L, "config");
	} else {
		/* Load defaults */
		static const char config_init[] = {
214
			#include "daemon/lua/config.inc"
215 216 217 218 219 220
		};
		ret = luaL_dostring(engine->L, config_init);
	}

	/* Evaluate */
	if (ret != 0) {
221
		fprintf(stderr, "[system] error %s\n", lua_tostring(engine->L, -1));
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
		lua_pop(engine->L, 1);
		return kr_error(EINVAL);
	}

	return kr_ok();
}

int engine_start(struct engine *engine)
{
	/* Load configuration. */
	int ret = engine_loadconf(engine);
	if (ret != 0) {
		return ret;
	}

	return uv_run(uv_default_loop(), UV_RUN_DEFAULT);	
}

void engine_stop(struct engine *engine)
{
	uv_stop(uv_default_loop());
}

int engine_register(struct engine *engine, const char *name)
{
	if (engine == NULL || name == NULL) {
		return kr_error(EINVAL);
	}

	/* Load module */
	size_t next = engine->modules.len;
	array_reserve(engine->modules, next + 1);
	struct kr_module *module = &engine->modules.at[next];
	int ret = kr_module_load(module, name, NULL);
	if (ret != 0) {
		return ret;
	} else {
		engine->modules.len += 1;
	}

	/* Register properties */
	if (module->props) {
		lua_newtable(engine->L);
265 266 267
		if (module->config != NULL) {
			REGISTER_MODULE_CALL(engine->L, module, module->config, "config");
		}
268
		for (struct kr_prop *p = module->props; p->name; ++p) {
269 270 271
			if (p->cb != NULL && p->name != NULL) {
				REGISTER_MODULE_CALL(engine->L, module, p->cb, p->name);
			}
272 273 274 275 276 277 278 279 280 281
		}
		lua_setglobal(engine->L, module->name);
	}

	return kr_ok();
}

int engine_unregister(struct engine *engine, const char *name)
{
	/* Find matching module. */
282
	module_array_t *mod_list = &engine->modules;
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	size_t found = mod_list->len;
	for (size_t i = 0; i < mod_list->len; ++i) {
		if (strcmp(mod_list->at[i].name, name) == 0) {
			found = i;
			break;
		}
	}

	/* Unregister module */
	if (found < mod_list->len) {
		kr_module_unload(&mod_list->at[found]);
		array_del(*mod_list, found);
		return kr_ok();
	}

	return kr_error(ENOENT);
}

void engine_lualib(struct engine *engine, const char *name, lua_CFunction lib_cb)
{
	if (engine != NULL) {
#if LUA_VERSION_NUM < 502
		lib_cb(engine->L);
#else
		luaL_requiref(engine->L, name, lib_cb, 1);
		lua_pop(engine->L, 1);
#endif
	}
}

struct engine *engine_luaget(lua_State *L)
{
 	lua_getglobal(L, "__engine");
	struct engine *engine = lua_touserdata(L, -1);
	lua_pop(engine->L, 1);
	return engine;
}