From 247ec9965a59df42fa4a8c11f5928abb2d54a04d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Wed, 5 Aug 2020 17:55:58 +0200 Subject: [PATCH] daemon/bindings: get rid of engine_luaget() Lots of lines affected, but it gets slightly simpler. --- daemon/bindings/cache.c | 24 ++++++--------- daemon/bindings/impl.h | 1 + daemon/bindings/modules.c | 12 +++----- daemon/bindings/net.c | 64 +++++++++++++++------------------------ daemon/bindings/worker.c | 1 - daemon/engine.c | 16 +++------- daemon/engine.h | 3 -- 7 files changed, 45 insertions(+), 76 deletions(-) diff --git a/daemon/bindings/cache.c b/daemon/bindings/cache.c index bf944b90f..d2d37f24b 100644 --- a/daemon/bindings/cache.c +++ b/daemon/bindings/cache.c @@ -5,14 +5,12 @@ #include "daemon/bindings/impl.h" #include "lib/cache/cdb_lmdb.h" -#include "daemon/worker.h" #include "daemon/zimport.h" /** @internal return cache, or throw lua error if not open */ -struct kr_cache * cache_assert_open(lua_State *L) +static struct kr_cache * cache_assert_open(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct kr_cache *cache = &engine->resolver.cache; + struct kr_cache *cache = &the_worker->engine->resolver.cache; assert(cache); if (!cache || !kr_cache_is_open(cache)) lua_error_p(L, "no cache is open yet, use cache.open() or cache.size, etc."); @@ -22,7 +20,7 @@ struct kr_cache * cache_assert_open(lua_State *L) /** Return available cached backends. */ static int cache_backends(lua_State *L) { - struct engine *engine = engine_luaget(L); + struct engine *engine = the_worker->engine; lua_newtable(L); for (unsigned i = 0; i < engine->backends.len; ++i) { @@ -176,7 +174,7 @@ static int cache_open(lua_State *L) lua_error_p(L, "expected 'open(number max_size, string config = \"\")'"); /* Select cache storage backend */ - struct engine *engine = engine_luaget(L); + struct engine *engine = the_worker->engine; lua_Integer csize_lua = lua_tointeger(L, 1); if (!(csize_lua >= 8192 && csize_lua < SIZE_MAX)) { /* min. is basically arbitrary */ @@ -223,8 +221,7 @@ static int cache_open(lua_State *L) static int cache_close(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct kr_cache *cache = &engine->resolver.cache; + struct kr_cache *cache = &the_worker->engine->resolver.cache; if (!kr_cache_is_open(cache)) { return 0; } @@ -264,10 +261,10 @@ static int cache_clear_everything(lua_State *L) lua_error_maybe(L, ret); /* Clear reputation tables */ - struct engine *engine = engine_luaget(L); - lru_reset(engine->resolver.cache_rtt); - lru_reset(engine->resolver.cache_rep); - lru_reset(engine->resolver.cache_cookie); + struct kr_context *ctx = &the_worker->engine->resolver; + lru_reset(ctx->cache_rtt); + lru_reset(ctx->cache_rep); + lru_reset(ctx->cache_cookie); lua_pushboolean(L, true); return 1; } @@ -339,8 +336,7 @@ static int cache_get(lua_State *L) * in NS elections again. */ static int cache_ns_tout(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct kr_context *ctx = &engine->resolver; + struct kr_context *ctx = &the_worker->engine->resolver; /* Check parameters */ int n = lua_gettop(L); diff --git a/daemon/bindings/impl.h b/daemon/bindings/impl.h index f981614dd..9c9e8937c 100644 --- a/daemon/bindings/impl.h +++ b/daemon/bindings/impl.h @@ -5,6 +5,7 @@ #pragma once #include "daemon/engine.h" +#include "daemon/worker.h" /* the_worker is often useful */ #include #include diff --git a/daemon/bindings/modules.c b/daemon/bindings/modules.c index e16eeedd3..5116ff33c 100644 --- a/daemon/bindings/modules.c +++ b/daemon/bindings/modules.c @@ -8,10 +8,10 @@ /** List loaded modules */ static int mod_list(lua_State *L) { - struct engine *engine = engine_luaget(L); + const module_array_t * const modules = &the_worker->engine->modules; lua_newtable(L); - for (unsigned i = 0; i < engine->modules.len; ++i) { - struct kr_module *module = engine->modules.at[i]; + for (unsigned i = 0; i < modules->len; ++i) { + struct kr_module *module = modules->at[i]; lua_pushstring(L, module->name); lua_rawseti(L, -2, i + 1); } @@ -33,8 +33,7 @@ static int mod_load(lua_State *L) const char *precedence = strtok(NULL, " "); const char *ref = strtok(NULL, " "); /* Load engine module */ - struct engine *engine = engine_luaget(L); - int ret = engine_register(engine, name, precedence, ref); + int ret = engine_register(the_worker->engine, name, precedence, ref); free(declaration); if (ret != 0) { if (ret == kr_error(EIDRM)) { @@ -56,8 +55,7 @@ static int mod_unload(lua_State *L) if (n != 1 || !lua_isstring(L, 1)) lua_error_p(L, "expected 'unload(string name)'"); /* Unload engine module */ - struct engine *engine = engine_luaget(L); - int ret = engine_unregister(engine, lua_tostring(L, 1)); + int ret = engine_unregister(the_worker->engine, lua_tostring(L, 1)); lua_error_maybe(L, ret); lua_pushboolean(L, 1); diff --git a/daemon/bindings/net.c b/daemon/bindings/net.c index c558ec561..f88822534 100644 --- a/daemon/bindings/net.c +++ b/daemon/bindings/net.c @@ -7,7 +7,6 @@ #include "contrib/base64.h" #include "daemon/network.h" #include "daemon/tls.h" -#include "daemon/worker.h" #include @@ -86,10 +85,9 @@ static int net_list_add(const char *key, void *val, void *ext) /** List active endpoints. */ static int net_list(lua_State *L) { - struct engine *engine = engine_luaget(L); lua_newtable(L); lua_pushinteger(L, 1); - map_walk(&engine->net.endpoints, net_list_add, L); + map_walk(&the_worker->engine->net.endpoints, net_list_add, L); lua_pop(L, 1); return 1; } @@ -110,21 +108,21 @@ static bool net_listen_addrs(lua_State *L, int port, bool tls, const char *kind, /* Case: string, representing a single address. */ const char *str = lua_tostring(L, -1); if (str != NULL) { - struct engine *engine = engine_luaget(L); + struct network *net = &the_worker->engine->net; int ret = 0; endpoint_flags_t flags = { .tls = tls, .freebind = freebind }; if (!kind && !flags.tls) { /* normal UDP */ flags.sock_type = SOCK_DGRAM; - ret = network_listen(&engine->net, str, port, flags); + ret = network_listen(net, str, port, flags); } if (!kind && ret == 0) { /* common for normal TCP and TLS */ flags.sock_type = SOCK_STREAM; - ret = network_listen(&engine->net, str, port, flags); + ret = network_listen(net, str, port, flags); } if (kind) { flags.kind = strdup(kind); flags.sock_type = SOCK_STREAM; /* TODO: allow to override this? */ - ret = network_listen(&engine->net, str, port, flags); + ret = network_listen(net, str, port, flags); } if (ret != 0) { if (str[0] == '/') { @@ -240,8 +238,7 @@ static int net_close(lua_State *L) if (!ok) lua_error_p(L, "expected 'close(string addr, [number port])'"); - struct network *net = &engine_luaget(L)->net; - int ret = network_close(net, addr, port); + int ret = network_close(&the_worker->engine->net, addr, port); lua_pushboolean(L, ret == 0); return 1; } @@ -301,8 +298,7 @@ static int net_interfaces(lua_State *L) /** Set UDP maximum payload size. */ static int net_bufsize(lua_State *L) { - struct engine *engine = engine_luaget(L); - knot_rrset_t *opt_rr = engine->resolver.opt_rr; + knot_rrset_t *opt_rr = the_worker->engine->resolver.opt_rr; if (!lua_isnumber(L, 1)) { lua_pushinteger(L, knot_edns_get_payload(opt_rr)); return 1; @@ -335,11 +331,7 @@ static int net_pipeline(lua_State *L) static int net_tls(lua_State *L) { - struct engine *engine = engine_luaget(L); - if (!engine) { - return 0; - } - struct network *net = &engine->net; + struct network *net = &the_worker->engine->net; if (!net) { return 0; } @@ -474,7 +466,7 @@ static int net_tls_client(lua_State *L) /* TODO idea: allow starting the lua table with *multiple* IP targets, * meaning the authentication config should be applied to each. */ - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; if (lua_gettop(L) == 0) return tls_params2lua(L, net->tls_client_params); /* Various basic sanity-checking. */ @@ -688,7 +680,7 @@ int net_tls_client_clear(lua_State *L) if (!addr) lua_error_p(L, "invalid IP address"); /* Do the actual removal. */ - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; int r = tls_client_param_remove(net->tls_client_params, addr); free_const(addr); lua_error_maybe(L, r); @@ -698,18 +690,18 @@ int net_tls_client_clear(lua_State *L) static int net_tls_padding(lua_State *L) { - struct engine *engine = engine_luaget(L); + struct kr_context *ctx = &the_worker->engine->resolver; /* Only return current padding. */ if (lua_gettop(L) == 0) { - if (engine->resolver.tls_padding < 0) { + if (ctx->tls_padding < 0) { lua_pushboolean(L, true); return 1; - } else if (engine->resolver.tls_padding == 0) { + } else if (ctx->tls_padding == 0) { lua_pushboolean(L, false); return 1; } - lua_pushinteger(L, engine->resolver.tls_padding); + lua_pushinteger(L, ctx->tls_padding); return 1; } @@ -720,15 +712,15 @@ static int net_tls_padding(lua_State *L) if (lua_isboolean(L, 1)) { bool x = lua_toboolean(L, 1); if (x) { - engine->resolver.tls_padding = -1; + ctx->tls_padding = -1; } else { - engine->resolver.tls_padding = 0; + ctx->tls_padding = 0; } } else if (lua_isnumber(L, 1)) { int padding = lua_tointeger(L, 1); if ((padding < 0) || (padding > MAX_TLS_PADDING)) lua_error_p(L, "%s", errstr); - engine->resolver.tls_padding = padding; + ctx->tls_padding = padding; } else { lua_error_p(L, "%s", errstr); } @@ -741,7 +733,7 @@ static int net_tls_padding(lua_State *L) static int net_tls_sticket_secret_string(lua_State *L) { - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; size_t secret_len; const char *secret; @@ -807,7 +799,7 @@ static int net_tls_sticket_secret_file(lua_State *L) } fclose(fp); - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; tls_session_ticket_ctx_destroy(net->tls_session_ticket_ctx); net->tls_session_ticket_ctx = @@ -896,17 +888,13 @@ static int net_update_timeout(lua_State *L, uint64_t *timeout, const char *name) static int net_tcp_in_idle(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct network *net = &engine->net; - + struct network *net = &the_worker->engine->net; return net_update_timeout(L, &net->tcp.in_idle_timeout, "net.tcp_in_idle"); } static int net_tls_handshake_timeout(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct network *net = &engine->net; - + struct network *net = &the_worker->engine->net; return net_update_timeout(L, &net->tcp.tls_handshake_timeout, "net.tls_handshake_timeout"); } @@ -919,8 +907,6 @@ static int net_bpf_set(lua_State *L) #if __linux__ - struct engine *engine = engine_luaget(L); - struct network *net = &engine->net; int progfd = lua_tointeger(L, 1); if (progfd == 0) { /* conversion error despite that fact @@ -930,7 +916,7 @@ static int net_bpf_set(lua_State *L) } lua_pop(L, 1); - if (network_set_bpf(net, progfd) == 0) { + if (network_set_bpf(&the_worker->engine->net, progfd) == 0) { lua_error_p(L, "failed to attach BPF program to some networks: %s", kr_strerror(errno)); } @@ -949,9 +935,7 @@ static int net_bpf_clear(lua_State *L) #if __linux__ - struct engine *engine = engine_luaget(L); - struct network *net = &engine->net; - network_clear_bpf(net); + network_clear_bpf(&the_worker->engine->net); lua_pushboolean(L, 1); return 1; @@ -970,7 +954,7 @@ static int net_register_endpoint_kind(lua_State *L) } size_t kind_len; const char *kind = lua_tolstring(L, 1, &kind_len); - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; /* Unregistering */ if (param_count == 1) { diff --git a/daemon/bindings/worker.c b/daemon/bindings/worker.c index 720a56311..b28048faf 100644 --- a/daemon/bindings/worker.c +++ b/daemon/bindings/worker.c @@ -4,7 +4,6 @@ #include "daemon/bindings/impl.h" -#include "daemon/worker.h" static inline double getseconds(uv_timeval_t *tv) { diff --git a/daemon/engine.c b/daemon/engine.c index f52f91e39..c45e99010 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -136,7 +136,7 @@ static int l_setuser(lua_State *L) /** Quit current executable. */ static int l_quit(lua_State *L) { - engine_stop(engine_luaget(L)); + engine_stop(the_worker->engine); return 0; } @@ -185,7 +185,7 @@ int engine_set_hostname(struct engine *engine, const char *hostname) { /** Return hostname. */ static int l_hostname(lua_State *L) { - struct engine *engine = engine_luaget(L); + struct engine *engine = the_worker->engine; if (lua_gettop(L) == 0) { lua_pushstring(L, engine_get_hostname(engine)); return 1; @@ -210,8 +210,7 @@ static int l_package_version(lua_State *L) /** Load root hints from zonefile. */ static int l_hint_root_file(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct kr_context *ctx = &engine->resolver; + struct kr_context *ctx = &the_worker->engine->resolver; const char *file = lua_tostring(L, 1); const char *err = engine_hint_root_file(ctx, file); @@ -384,7 +383,6 @@ static int l_map(lua_State *L) if (lua_gettop(L) != 1 || !lua_isstring(L, 1)) lua_error_p(L, "map('string with a lua expression')"); - struct engine *engine = engine_luaget(L); const char *cmd = lua_tostring(L, 1); uint32_t len = strlen(cmd); lua_newtable(L); @@ -395,8 +393,8 @@ static int l_map(lua_State *L) lua_settop(L, ntop + 1); /* Push only one return value to table */ lua_rawseti(L, -2, 1); - for (size_t i = 0; i < engine->ipc_set.len; ++i) { - int fd = engine->ipc_set.at[i]; + for (size_t i = 0; i < the_worker->engine->ipc_set.len; ++i) { + int fd = the_worker->engine->ipc_set.at[i]; /* Send command */ expr_checked(write(fd, &len, sizeof(len)) == sizeof(len)); expr_checked(write(fd, cmd, len) == len); @@ -857,7 +855,3 @@ int engine_unregister(struct engine *engine, const char *name) return kr_error(ENOENT); } -struct engine *engine_luaget(lua_State *L) -{ - return the_worker->engine; -} diff --git a/daemon/engine.h b/daemon/engine.h index 0a0561067..3de30320b 100644 --- a/daemon/engine.h +++ b/daemon/engine.h @@ -52,9 +52,6 @@ void engine_stop(struct engine *engine); int engine_register(struct engine *engine, const char *name, const char *precedence, const char* ref); int engine_unregister(struct engine *engine, const char *name); -/** Return engine light userdata; TODO: remove this function? */ -struct engine *engine_luaget(struct lua_State *L); - /** Set/get the per engine hostname */ char *engine_get_hostname(struct engine *engine); int engine_set_hostname(struct engine *engine, const char *hostname); -- 2.47.2