1 /* Copyright (C) 2015-2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 */
4
5 #pragma once
6
7 #include "daemon/engine.h"
8 #include "daemon/worker.h" /* the_worker is often useful */
9
10 #include <lua.h>
11 #include <lauxlib.h>
12 /* It may happen that include files are messed up and we're hitting a header
13 * e.g. from vanilla Lua. Even 5.1 won't work due to missing luaL_traceback() in <lauxlib.h>. */
14 #if (LUA_VERSION_NUM) != 501 || !defined(LUA_LJDIR)
15 #error "Incorrect Lua version in #include <lua.h> - LuaJIT compatible with Lua 5.1 is required"
16 #endif
17
18
19 /** Useful to stringify macros into error strings. */
20 #define STR(s) STRINGIFY_TOKEN(s)
21 #define STRINGIFY_TOKEN(s) #s
22
23
24 /** Check lua table at the top of the stack for allowed keys.
25 * \param keys NULL-terminated array of 0-terminated strings
26 * \return NULL if passed or the offending string (pushed on top of lua stack)
27 * \note Future work: if non-NULL is returned, there's extra stuff on the lua stack.
28 * \note Brute-force complexity: table length * summed length of keys.
29 */
30 const char * lua_table_checkindices(lua_State *L, const char *keys[]);
31
32 /** If the value at the top of the stack isn't a table, make it a single-element list. */
lua_listify(lua_State * L)33 static inline void lua_listify(lua_State *L)
34 {
35 if (lua_istable(L, -1))
36 return;
37 lua_createtable(L, 1, 0);
38 lua_insert(L, lua_gettop(L) - 1); /* swap the top two stack elements */
39 lua_pushinteger(L, 1);
40 lua_insert(L, lua_gettop(L) - 1); /* swap the top two stack elements */
41 lua_settable(L, -3);
42 }
43
44
45 /** Throw a formatted lua error.
46 *
47 * The message will get prefixed by "ERROR: " and supplemented by stack trace.
48 * \return never! It calls lua_error().
49 *
50 * Example:
51 ERROR: not a valid pin_sha256: 'a1Z/3ek=', raw length 5 instead of 32
52 stack traceback:
53 [C]: in function 'tls_client'
54 /PathToPREFIX/lib/kdns_modules/policy.lua:175: in function 'TLS_FORWARD'
55 /PathToConfig.lua:46: in main chunk
56 */
57 KR_PRINTF(2) KR_NORETURN KR_COLD
58 void lua_error_p(lua_State *L, const char *fmt, ...);
59 /** @internal Annotate for static checkers. */
60 KR_NORETURN int lua_error(lua_State *L);
61
62 /** Shortcut for common case. */
lua_error_maybe(lua_State * L,int err)63 static inline void lua_error_maybe(lua_State *L, int err)
64 {
65 if (err) lua_error_p(L, "%s", kr_strerror(err));
66 }
67
execute_callback(lua_State * L,int argc)68 static inline int execute_callback(lua_State *L, int argc)
69 {
70 int ret = engine_pcall(L, argc);
71 if (ret != 0) {
72 kr_log_error(SYSTEM, "error: %s\n", lua_tostring(L, -1));
73 }
74 /* Clear the stack, there may be event a/o anything returned */
75 lua_settop(L, 0);
76 return ret;
77 }
78
79 /** Push a pointer as heavy/full userdata.
80 *
81 * It's useful as a replacement of lua_pushlightuserdata(),
82 * but note that it behaves differently in lua (converts to pointer-to-pointer).
83 */
lua_pushpointer(lua_State * L,void * p)84 static inline void lua_pushpointer(lua_State *L, void *p)
85 {
86 void *addr = lua_newuserdata(L, sizeof(void *));
87 kr_require(addr);
88 memcpy(addr, &p, sizeof(void *));
89 }
90
91