diff options
author | Vladimír Čunát <vladimir.cunat@nic.cz> | 2019-04-03 19:51:28 +0200 |
---|---|---|
committer | Tomas Krizek <tomas.krizek@nic.cz> | 2019-04-17 18:44:29 +0200 |
commit | 1ad8c45e27ed3235bc18e3792b1ec4edecf75f09 (patch) | |
tree | c19d5cfb57893da1fbe308e54fbb0a9985fff48a /daemon/ffimodule.c | |
parent | module API+ABI: remove one level of indirection (diff) | |
download | knot-resolver-1ad8c45e27ed3235bc18e3792b1ec4edecf75f09.tar.xz knot-resolver-1ad8c45e27ed3235bc18e3792b1ec4edecf75f09.zip |
lua module layers: avoid incorrect returns on errors
The layer functions are supposed to return the new state.
We've been lucky that errors are negative numbers and thus
they typically passed the (status & KR_STATE_FAIL) test.
Diffstat (limited to 'daemon/ffimodule.c')
-rw-r--r-- | daemon/ffimodule.c | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/daemon/ffimodule.c b/daemon/ffimodule.c index 496be6fa..79ebf8c5 100644 --- a/daemon/ffimodule.c +++ b/daemon/ffimodule.c @@ -81,12 +81,12 @@ static int l_ffi_defer(lua_State *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) +/** @internal Helper for calling the entrypoint, for kr_module functions. */ +static inline int l_ffi_call_mod(lua_State *L, int argc) { int status = lua_pcall(L, argc, 1, 0); if (status != 0) { - fprintf(stderr, "error: %s\n", lua_tostring(L, -1)); + kr_log_error("error: %s\n", lua_tostring(L, -1)); lua_pop(L, 1); return kr_error(EIO); } @@ -99,13 +99,21 @@ static inline int l_ffi_call(lua_State *L, int argc) return status; } +/** @internal Helper for calling the entrypoint, for kr_layer_api functions. */ +static inline int l_ffi_call_layer(lua_State *L, int argc) +{ + int ret = l_ffi_call_mod(L, argc); + /* The return codes are mixed at this point. We need to return KR_STATE_* */ + return ret < 0 ? KR_STATE_FAIL : ret; +} + static int l_ffi_init(struct kr_module *module) { lua_State *L = l_ffi_preface(module, "init"); if (!L) { return 0; } - return l_ffi_call(L, 1); + return l_ffi_call_mod(L, 1); } static int l_ffi_deinit(struct kr_module *module) @@ -114,7 +122,7 @@ static int l_ffi_deinit(struct kr_module *module) int ret = 0; lua_State *L = module->lib; if (l_ffi_preface(module, "deinit")) { - ret = l_ffi_call(L, 1); + ret = l_ffi_call_mod(L, 1); } module->lib = NULL; /* Free the layer API wrapper (unconst it) */ @@ -147,14 +155,14 @@ static int l_ffi_layer_begin(kr_layer_t *ctx) { LAYER_FFI_CALL(ctx, begin); lua_pushlightuserdata(L, ctx->req); - return l_ffi_call(L, 2); + return l_ffi_call_layer(L, 2); } static int l_ffi_layer_reset(kr_layer_t *ctx) { LAYER_FFI_CALL(ctx, reset); lua_pushlightuserdata(L, ctx->req); - return l_ffi_call(L, 2); + return l_ffi_call_layer(L, 2); } static int l_ffi_layer_finish(kr_layer_t *ctx) @@ -163,7 +171,7 @@ static int l_ffi_layer_finish(kr_layer_t *ctx) LAYER_FFI_CALL(ctx, finish); lua_pushlightuserdata(L, req); lua_pushlightuserdata(L, req->answer); - return l_ffi_call(L, 3); + return l_ffi_call_layer(L, 3); } static int l_ffi_layer_consume(kr_layer_t *ctx, knot_pkt_t *pkt) @@ -174,7 +182,7 @@ static int l_ffi_layer_consume(kr_layer_t *ctx, knot_pkt_t *pkt) LAYER_FFI_CALL(ctx, consume); lua_pushlightuserdata(L, ctx->req); lua_pushlightuserdata(L, pkt); - return l_ffi_call(L, 3); + return l_ffi_call_layer(L, 3); } static int l_ffi_layer_produce(kr_layer_t *ctx, knot_pkt_t *pkt) @@ -185,7 +193,7 @@ static int l_ffi_layer_produce(kr_layer_t *ctx, knot_pkt_t *pkt) LAYER_FFI_CALL(ctx, produce); lua_pushlightuserdata(L, ctx->req); lua_pushlightuserdata(L, pkt); - return l_ffi_call(L, 3); + return l_ffi_call_layer(L, 3); } static int l_ffi_layer_checkout(kr_layer_t *ctx, knot_pkt_t *pkt, struct sockaddr *dst, int type) @@ -198,14 +206,14 @@ static int l_ffi_layer_checkout(kr_layer_t *ctx, knot_pkt_t *pkt, struct sockadd lua_pushlightuserdata(L, pkt); lua_pushlightuserdata(L, dst); lua_pushboolean(L, type == SOCK_STREAM); - return l_ffi_call(L, 5); + return l_ffi_call_layer(L, 5); } static int l_ffi_layer_answer_finalize(kr_layer_t *ctx) { LAYER_FFI_CALL(ctx, answer_finalize); lua_pushlightuserdata(L, ctx->req); - return l_ffi_call(L, 2); + return l_ffi_call_layer(L, 2); } #undef LAYER_FFI_CALL @@ -255,7 +263,7 @@ int ffimodule_register_lua(struct engine *engine, struct kr_module *module, cons lua_getglobal(L, "require"); lua_pushfstring(L, "kres_modules.%s", name); if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) { - fprintf(stderr, "error: %s\n", lua_tostring(L, -1)); + kr_log_error("error: %s\n", lua_tostring(L, -1)); lua_pop(L, 1); return kr_error(ENOENT); } |