ffimodule.c 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*  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>

19
#include "daemon/engine.h"
20 21 22 23 24
#include "daemon/bindings.h"
#include "daemon/ffimodule.h"
#include "lib/module.h"
#include "lib/layer.h"

25 26 27 28 29 30
#if LUA_VERSION_NUM >= 502
#define l_resume(L, argc) lua_resume((L), NULL, (argc))
#else
#define l_resume(L, argc) lua_resume((L), (argc))
#endif

31 32 33 34 35
/** @internal Helper for retrieving the right function entrypoint. */
static inline lua_State *l_ffi_preface(struct kr_module *module, const char *call) {
	lua_State *L = module->lib;
	lua_rawgeti(L, LUA_REGISTRYINDEX, (intptr_t)module->data);
	lua_getfield(L, -1, call);
36
	lua_remove(L, -2);
37
	if (lua_isnil(L, -1)) {
38
		lua_pop(L, 1);
39 40
		return NULL;
	}
41 42 43 44 45 46 47 48
	lua_pushlightuserdata(L, module);
	return L;
}

/** @internal Continue with coroutine. */
static void l_ffi_resume_cb(uv_idle_t *check)
{
	lua_State *L = check->data;
49 50
	int status = l_resume(L, 0);
	if (status != LUA_YIELD) {
51 52 53
		uv_idle_stop(check); /* Stop coroutine */
		uv_close((uv_handle_t *)check, (uv_close_cb)free);
	}
54
	lua_pop(L, lua_gettop(L));
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
}

/** @internal Schedule deferred continuation. */
static int l_ffi_defer(lua_State *L)
{
	uv_idle_t *check = malloc(sizeof(*check));
	if (!check) {
		return kr_error(ENOMEM);
	}
	uv_idle_init(uv_default_loop(), check);
	check->data = L;
	return uv_idle_start(check, l_ffi_resume_cb);
}

/** @internal Helper for calling the entrypoint. */
static inline int l_ffi_call(lua_State *L, int argc)
{
72
	int status = lua_pcall(L, argc, 1, 0);
73
	if (status != 0) {
74
		fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
75 76 77 78 79 80 81 82 83 84 85
		lua_pop(L, 1);
		return kr_error(EIO);
	}

	int n = lua_gettop(L);
	if (n > 0) {
		if (lua_isthread(L, -1)) { /* Continuations */
			status = l_ffi_defer(lua_tothread(L, -1));
		} else if (lua_isnumber(L, -1)) { /* Return code */
			status = lua_tonumber(L, 1);
		}
86
		lua_pop(L, 1);
87 88 89 90 91 92 93
	}
	return status;
}

static int l_ffi_init(struct kr_module *module)
{
	lua_State *L = l_ffi_preface(module, "init");
94 95 96
	if (!L) {
		return 0;
	}
97 98 99 100 101 102
	return l_ffi_call(L, 1);
}

static int l_ffi_deinit(struct kr_module *module)
{
	lua_State *L = l_ffi_preface(module, "deinit");
103 104 105
	if (!L) {
		return 0;
	}
106
	int ret = l_ffi_call(L, 1);
107 108 109 110
	/* Free the layer API wrapper */
	lua_rawgeti(L, LUA_REGISTRYINDEX, (intptr_t)module->data);
	lua_getfield(L, -1, "_layercdata");
	free(lua_touserdata(L, -1));
111
	lua_pop(L, 2);
112 113 114 115 116 117 118 119 120 121 122 123 124 125
	/* Unref module and unset 'lib', so the module
	 * interface doesn't attempt to close it.
	 */
	luaL_unref(L, LUA_REGISTRYINDEX, (intptr_t)module->data);
	module->lib = NULL;
	return ret;
}

/** @internal Helper for retrieving layer Lua function by name. */
#define LAYER_FFI_CALL(ctx, name) \
	struct kr_module *module = (ctx)->api->data; \
	lua_State *L = module->lib; \
	lua_rawgeti(L, LUA_REGISTRYINDEX, (intptr_t)module->data); \
	lua_getfield(L, -1, "layer"); \
126
	lua_remove(L, -2); \
127
	lua_getfield(L, -1, (name)); \
128
	lua_remove(L, -2); \
129
	if (lua_isnil(L, -1)) { \
130
		lua_pop(L, 1); \
131 132 133 134 135 136 137 138 139
		return ctx->state; \
	} \
	lua_pushnumber(L, ctx->state);

static int l_ffi_layer_begin(knot_layer_t *ctx, void *module_param)
{
	LAYER_FFI_CALL(ctx, "begin");
	lua_pushlightuserdata(L, module_param);
	ctx->data = module_param;
140
	return l_ffi_call(L, 2);
141 142 143 144 145 146
}

static int l_ffi_layer_reset(knot_layer_t *ctx)
{
	LAYER_FFI_CALL(ctx, "reset");
	lua_pushlightuserdata(L, ctx->data);
147
	return l_ffi_call(L, 2);
148 149 150 151 152 153
}

static int l_ffi_layer_finish(knot_layer_t *ctx)
{
	LAYER_FFI_CALL(ctx, "finish");
	lua_pushlightuserdata(L, ctx->data);
154
	return l_ffi_call(L, 2);
155 156 157 158 159 160 161
}

static int l_ffi_layer_consume(knot_layer_t *ctx, knot_pkt_t *pkt)
{
	LAYER_FFI_CALL(ctx, "consume");
	lua_pushlightuserdata(L, ctx->data);
	lua_pushlightuserdata(L, pkt);
162
	return l_ffi_call(L, 3);
163 164 165 166 167 168 169
}

static int l_ffi_layer_produce(knot_layer_t *ctx, knot_pkt_t *pkt)
{
	LAYER_FFI_CALL(ctx, "produce");
	lua_pushlightuserdata(L, ctx->data);
	lua_pushlightuserdata(L, pkt);
170
	return l_ffi_call(L, 3);
171 172 173 174 175 176 177
}

static int l_ffi_layer_fail(knot_layer_t *ctx, knot_pkt_t *pkt)
{
	LAYER_FFI_CALL(ctx, "fail");
	lua_pushlightuserdata(L, ctx->data);
	lua_pushlightuserdata(L, pkt);
178
	return l_ffi_call(L, 3);
179 180 181 182 183 184 185 186
}

/** @internal Retrieve C layer api wrapper. */
static const knot_layer_api_t* l_ffi_layer(struct kr_module *module)
{
	lua_State *L = module->lib;
	lua_rawgeti(L, LUA_REGISTRYINDEX, (intptr_t)module->data);
	lua_getfield(L, -1, "_layer_capi");
187 188 189
	knot_layer_api_t *api = lua_touserdata(L, -1);
	lua_pop(L, 1);
	if (!api) {
190
		/* Fabricate layer API wrapping the Lua functions */
191
		api = malloc(sizeof(*api));
192 193 194 195 196 197 198 199
		if (api) {
			api->begin = l_ffi_layer_begin;
			api->finish = l_ffi_layer_finish;
			api->consume = l_ffi_layer_consume;
			api->produce = l_ffi_layer_produce;
			api->reset = l_ffi_layer_reset;
			api->fail = l_ffi_layer_fail;
			api->data = module;
200 201 202 203 204
		}
		/* Store the api in the registry. */
		lua_pushlightuserdata(L, api);
		lua_setfield(L, -2, "_layer_capi");
	}
205 206
	lua_pop(L, 1); /* Clear module table */
	return api;
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
}

#undef LAYER_FFI_CALL

/** @internal Helper macro for function presence check. */
#define REGISTER_FFI_CALL(L, attr, name, cb) do { \
	lua_getfield((L), -1, (name)); \
	if (!lua_isnil((L), -1)) { attr = cb; } \
	lua_pop((L), 1); \
	} while (0)

int ffimodule_register_lua(struct engine *engine, struct kr_module *module, const char *name)
{
	/* Register module in Lua */
	lua_State *L = engine->L;
	lua_getglobal(L, "require");
	lua_pushstring(L, name);
	if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) {
		fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
		lua_pop(L, 1);
		return kr_error(ENOENT);
	}
	lua_setglobal(L, name);
	lua_getglobal(L, name);

	/* Create FFI module with trampolined functions. */
	memset(module, 0, sizeof(*module));
	module->name = strdup(name);
	REGISTER_FFI_CALL(L, module->init,   "init",   &l_ffi_init);
	REGISTER_FFI_CALL(L, module->deinit, "deinit", &l_ffi_deinit);
	REGISTER_FFI_CALL(L, module->layer,  "layer",  &l_ffi_layer);
	module->data = (void *)(intptr_t)luaL_ref(L, LUA_REGISTRYINDEX);
239
	module->lib = L;
240
	lua_pop(L, 1); /* Clear the module global */
241 242 243 244
	return module->init(module);
}

#undef REGISTER_FFI_CALL