]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon: allow kr_assume() configuration
authorTomas Krizek <tomas.krizek@nic.cz>
Fri, 19 Mar 2021 15:47:34 +0000 (16:47 +0100)
committerTomas Krizek <tomas.krizek@nic.cz>
Tue, 25 May 2021 12:39:44 +0000 (14:39 +0200)
- add lua options debugging.assumption_abort and
  debugging.assumption_fork to make the behaviour configurable
- select default values based on meson buildtype:
  - debug/debugoptimized (default, e.g. used in CI) - abort and don't
    fork
  - plain/release - don't abort, but fork if abort is user-configured

.luacheckrc
daemon/engine.c
daemon/lua/kres-gen.lua
daemon/lua/kres-gen.sh
daemon/lua/sandbox.lua.in
lib/utils.c
lib/utils.h
meson.build

index 67bc18f6b1916c4155eec4b9d02680b2c686f0b5..885b29e7894a832e5e0682a6aa497840dafdf8cc 100644 (file)
@@ -38,6 +38,7 @@ new_read_globals = {
        'reorder_RR',
        'option',
        'env',
+       'debugging',
        'kres',
        'libknot_SONAME',
        'libzscanner_SONAME',
index 9ac8445b19d44baed711bac22892a7ad7373bc46..c63ea2a04286b8dc770d4d4b5e0bfc7e70485783 100644 (file)
@@ -85,6 +85,7 @@ static int l_help(lua_State *L)
                "modules\n    modules configuration\n"
                "kres\n    resolver services\n"
                "trust_anchors\n    configure trust anchors\n"
+               "debugging\n    debugging configuration\n"
                ;
        lua_pushstring(L, help_str);
        return 1;
index 679d028cb0d646b1bf35d5daae90ef55575b867b..e520790f1d5936ba4fdc287cc361d3452a16e4bb 100644 (file)
@@ -299,7 +299,11 @@ struct kr_server_selection {
        void (*error)(struct kr_query *, const struct kr_transport *, enum kr_selection_error);
        struct local_state *local_state;
 };
+
 kr_layer_t kr_layer_t_static;
+_Bool kr_dbg_assumption_abort;
+_Bool kr_dbg_assumption_fork;
+
 typedef int32_t (*kr_stale_cb)(int32_t ttl, const knot_dname_t *owner, uint16_t type,
                                const struct kr_query *qry);
 
index 52539a5ed1389e88369385c7ba2db7f3bfccd51b..9263d4ee42b66e5e32a3ee63a4adbca91ee5ecdf 100755 (executable)
@@ -131,8 +131,12 @@ ${CDEFS} ${LIBKRES} types <<-EOF
        struct kr_server_selection
 EOF
 
-# a static variable; the line might not be simple to generate
-printf "kr_layer_t kr_layer_t_static;"
+# static variables; these lines might not be simple to generate
+printf "
+kr_layer_t kr_layer_t_static;
+_Bool kr_dbg_assumption_abort;
+_Bool kr_dbg_assumption_fork;
+"
 
 printf "
 typedef int32_t (*kr_stale_cb)(int32_t ttl, const knot_dname_t *owner, uint16_t type,
index 993f36d5e26dbc65b1d44fd2d4061075f723116c..0c86c42425cf0c9e0be0cdb970533579754bcd8d 100644 (file)
@@ -129,6 +129,22 @@ setmetatable(env, {
        __index = function (_, k) return os.getenv(k) end
 })
 
+debugging = {}
+setmetatable(debugging, {
+       __index = function(_, k)
+               if k == 'assumption_abort' then return ffi.C.kr_dbg_assumption_abort
+               elseif k == 'assumption_fork' then return ffi.C.kr_dbg_assumption_fork
+               else panic('invalid debugging option: ' .. tostring(k))
+               end
+       end,
+       __newindex = function(_, k, v)
+               if k == 'assumption_abort' then ffi.C.kr_dbg_assumption_abort = v
+               elseif k == 'assumption_fork' then ffi.C.kr_dbg_assumption_fork = v
+               else panic('invalid debugging option: ' .. tostring(k))
+               end
+       end
+})
+
 -- Quick access to interfaces
 -- `net.<iface>` => `net.interfaces()[iface]`
 -- `net = {addr1, ..}` => `net.listen(name, addr1)`
@@ -406,7 +422,10 @@ setmetatable(cache, {
 
 -- Make sandboxed environment
 local function make_sandbox(defined)
-       local __protected = { worker = true, env = true, modules = true, cache = true, net = true, trust_anchors = true }
+       local __protected = {
+               worker = true, env = true, debugging = true, modules = true,
+               cache = true, net = true, trust_anchors = true
+       }
 
        -- Compute and export the list of top-level names (hidden otherwise)
        local nl = ""
index f61ec40fd7a60649b7784229e4253c9bbebe81f7..85c92993277af574c18c1d96e2e80d00c23a31d4 100644 (file)
@@ -38,7 +38,8 @@
 
 /* Logging & debugging */
 bool kr_verbose_status = false;
-bool kr_debug_assumption = true;
+bool kr_dbg_assumption_abort = DBG_ASSUMPTION_ABORT;
+bool kr_dbg_assumption_fork = DBG_ASSUMPTION_FORK;
 
 void kr_fail(bool is_fatal, const char *expr, const char *func, const char *file, int line)
 {
@@ -47,8 +48,10 @@ void kr_fail(bool is_fatal, const char *expr, const char *func, const char *file
        else
                kr_log_error("assumption \"%s\" failed in %s@%s:%d\n", expr, func, file, line);
 
-       if (is_fatal || (kr_debug_assumption && fork() == 0))
+       if (is_fatal || (kr_dbg_assumption_abort && !kr_dbg_assumption_fork))
                abort();
+       else if (kr_dbg_assumption_abort && kr_dbg_assumption_fork)
+               fork() == 0 ? abort() : (void)0;
 }
 
 /*
index 8316a5bcd884b972bdef8fb330bcb499b660b100..5884050004960efe0fec6089aa3bfbb1f33230ec 100644 (file)
@@ -61,8 +61,16 @@ typedef void (*trace_log_f)(const struct kr_request *request, const char *msg);
 #define kr_assume(expression) kr_assume_func((expression), #expression,       \
                                             __func__, __FILE__, __LINE__)
 
-/** Whether kr_assume() checks should result fork and abort. */
-KR_EXPORT extern bool kr_debug_assumption;
+/** Whether kr_assume() checks should abort. */
+KR_EXPORT extern bool kr_dbg_assumption_abort;
+
+/** Whether kr_assume() should fork the process before issuing abort (if configured).
+ *
+ * This can be useful for debugging rare edge-cases in production. When both
+ * kr_debug_assumption_abort and kr_debug_assumption_fork are set to true, it is
+ * possible to both obtain a coredump (from forked child) and recover from the
+ * non-fatal error in the parent process. */
+KR_EXPORT extern bool kr_dbg_assumption_fork;
 
 /** Use kr_require() and kr_assume() instead of directly this function. */
 KR_EXPORT KR_COLD void kr_fail(bool is_fatal, const char* expr, const char *func,
index 52c7edf7247c999d6f38a6aee73b821eb3ac8b4b..809fecc278cdc0c65bf79f21be8ab2d071272026 100644 (file)
@@ -178,6 +178,8 @@ conf_data.set('ENABLE_SENDMMSG', sendmmsg.to_int())
 conf_data.set('ENABLE_XDP', xdp.to_int())
 conf_data.set('ENABLE_CAP_NG', capng.found().to_int())
 conf_data.set('ENABLE_DOH2', nghttp2.found().to_int())
+conf_data.set('DBG_ASSUMPTION_ABORT', get_option('debug').to_int())
+conf_data.set('DBG_ASSUMPTION_FORK', (not get_option('debug')).to_int())
 
 kresconfig = configure_file(
   output: 'kresconfig.h',