]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/bindings: get rid of engine_luaget()
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 5 Aug 2020 15:55:58 +0000 (17:55 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 6 Aug 2020 08:35:55 +0000 (10:35 +0200)
Lots of lines affected, but it gets slightly simpler.

daemon/bindings/cache.c
daemon/bindings/impl.h
daemon/bindings/modules.c
daemon/bindings/net.c
daemon/bindings/worker.c
daemon/engine.c
daemon/engine.h

index bf944b90fc443fd94cf5946e6135c6f52bb972b5..d2d37f24b7a0a23b50e37a6b83ba6a910952f1ba 100644 (file)
@@ -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);
index f981614dd1610159beb378fe204e46a23e9be877..9c9e8937c95192eefbf1696da1e522e765cd5197 100644 (file)
@@ -5,6 +5,7 @@
 #pragma once
 
 #include "daemon/engine.h"
+#include "daemon/worker.h" /* the_worker is often useful */
 
 #include <lua.h>
 #include <lauxlib.h>
index e16eeedd36c96ee22a4ba59526f2b4fa12015baa..5116ff33c56d49aeb15ebb74e4b32a4eecf51647 100644 (file)
@@ -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);
index c558ec561dc4ff11db27156a810d351982a2b799..f888225349b710e7c427e048e257bf68ab6204f4 100644 (file)
@@ -7,7 +7,6 @@
 #include "contrib/base64.h"
 #include "daemon/network.h"
 #include "daemon/tls.h"
-#include "daemon/worker.h"
 
 #include <stdlib.h>
 
@@ -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) {
index 720a56311a264335311620025d139bb92047022c..b28048fafafb6c95aed6abe581e0a6b5fc3ef858 100644 (file)
@@ -4,7 +4,6 @@
 
 #include "daemon/bindings/impl.h"
 
-#include "daemon/worker.h"
 
 static inline double getseconds(uv_timeval_t *tv)
 {
index f52f91e3997283b93e675443fe3cf209103613d0..c45e99010bc9cde803b463c3a5cade8b80f899b5 100644 (file)
@@ -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;
-}
index 0a05610674ab38a4278a0b66857f36380ba85e96..3de30320be93e1be65ae80c68d009eb6c31327a4 100644 (file)
@@ -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);