From: Vladimír Čunát Date: Sun, 25 May 2025 08:17:44 +0000 (+0200) Subject: daemon/lua: introduce kres.strerror(errcode) X-Git-Tag: v6.0.13~3^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=638bf76f5ab96853abb2651a81d9e4cee288b634;p=thirdparty%2Fknot-resolver.git daemon/lua: introduce kres.strerror(errcode) As seen in the grandparent comit, the previous style was error-prone and a bit verbose. --- diff --git a/daemon/lua/kres-gen-33.lua b/daemon/lua/kres-gen-33.lua index 8147af88f..3e7313c4b 100644 --- a/daemon/lua/kres-gen-33.lua +++ b/daemon/lua/kres-gen-33.lua @@ -488,6 +488,7 @@ void lru_free_items_impl(struct lru *); struct lru *lru_create_impl(unsigned int, unsigned int, knot_mm_t *, knot_mm_t *); void *lru_get_impl(struct lru *, const char *, unsigned int, unsigned int, _Bool, _Bool *); void *mm_realloc(knot_mm_t *, void *, size_t, size_t); +const char *kr_strerror(int); knot_rrset_t *kr_ta_get(trie_t *, const knot_dname_t *); int kr_ta_add(trie_t *, const knot_dname_t *, uint16_t, uint32_t, const uint8_t *, uint16_t); int kr_ta_del(trie_t *, const knot_dname_t *); diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh index 003886c52..69011adf1 100755 --- a/daemon/lua/kres-gen.sh +++ b/daemon/lua/kres-gen.sh @@ -285,6 +285,7 @@ ${CDEFS} ${LIBKRES} functions <<-EOF lru_create_impl lru_get_impl mm_realloc + kr_strerror # Trust anchors kr_ta_get kr_ta_add diff --git a/daemon/lua/kres.lua b/daemon/lua/kres.lua index 473d0828a..3ce6191b3 100644 --- a/daemon/lua/kres.lua +++ b/daemon/lua/kres.lua @@ -1143,6 +1143,10 @@ kres = { context = function () return ffi.C.the_resolver end, knot_pkt_rr = knot_pkt_rr, + -- Convenience shorthand + strerror = function (errcode) + return ffi.string(ffi.C.kr_strerror(errcode)) + end, } return kres diff --git a/daemon/lua/sandbox.lua.in b/daemon/lua/sandbox.lua.in index 116489f87..898667265 100644 --- a/daemon/lua/sandbox.lua.in +++ b/daemon/lua/sandbox.lua.in @@ -355,7 +355,7 @@ cache.fssize = function () end local size = tonumber(ffi.C.kr_fssize(path)) if size < 0 then - panic('cache.fssize(): %s', ffi.string(ffi.C.knot_strerror(size))) + panic('cache.fssize(): %s', kres.strerror(size)) else return size end @@ -386,7 +386,7 @@ cache.clear = function (name, exact_name, rr_type, chunk_size, callback, prev_st local apex_array = ffi.new('knot_dname_t *[1]') -- C: dname **apex_array local ret = ffi.C.kr_cache_closest_apex(cach, dname, false, apex_array) if ret < 0 then - error(ffi.string(ffi.C.knot_strerror(ret))) end + error(kres.strerror(ret)) end if not ffi.C.knot_dname_is_equal(apex_array[0], dname) then local apex_str = kres.dname2str(apex_array[0]) rettable.not_apex = 'to clear proofs of non-existence call ' @@ -403,7 +403,7 @@ cache.clear = function (name, exact_name, rr_type, chunk_size, callback, prev_st if chunk_size or callback then error('cache.clear(): chunk_size and callback parameters not supported with rr_type') end local ret = ffi.C.kr_cache_remove(cach, dname, rr_type) - if ret < 0 then error(ffi.string(ffi.C.knot_strerror(ret))) end + if ret < 0 then error(kres.strerror(ret)) end return {count = 1} end @@ -424,7 +424,7 @@ cache.clear = function (name, exact_name, rr_type, chunk_size, callback, prev_st -- Default callback function: repeat after 1ms if callback == nil then callback = function (cbname, cbexact_name, cbrr_type, cbchunk_size, cbself, cbprev_state, cbrettable) - if cbrettable.count < 0 then error(ffi.string(ffi.C.knot_strerror(cbrettable.count))) end + if cbrettable.count < 0 then error(kres.strerror(cbrettable.count)) end if cbprev_state == nil then cbprev_state = { round = 0 } end if type(cbprev_state) ~= 'table' then error('cache.clear() callback: incorrect prev_state passed') end diff --git a/daemon/lua/trust_anchors.lua.in b/daemon/lua/trust_anchors.lua.in index 13e815690..46c901198 100644 --- a/daemon/lua/trust_anchors.lua.in +++ b/daemon/lua/trust_anchors.lua.in @@ -263,7 +263,7 @@ local function keyset_read(path, str) local ta_keytag = C.kr_dnssec_key_tag(ta.type, ta.rdata, #ta.rdata) if not (ta_keytag >= 0 and ta_keytag <= 65535) then return nil, string.format('invalid key: "%s": %s', - kres.rr2str(ta), ffi.string(C.knot_strerror(ta_keytag))) + kres.rr2str(ta), kres.strerror(ta_keytag)) end ta.key_tag = ta_keytag end diff --git a/modules/ta_update/ta_update.lua b/modules/ta_update/ta_update.lua index 3c059f85e..c5ad46b5f 100644 --- a/modules/ta_update/ta_update.lua +++ b/modules/ta_update/ta_update.lua @@ -17,7 +17,7 @@ local function ta_find(keyset, rr) local rr_tag = C.kr_dnssec_key_tag(rr.type, rr.rdata, #rr.rdata) if rr_tag < 0 or rr_tag > 65535 then log_warn(ffi.C.LOG_GRP_TAUPDATE, string.format('ignoring invalid or unsupported RR: %s: %s', - kres.rr2str(rr), ffi.string(C.knot_strerror(rr_tag)))) + kres.rr2str(rr), kres.strerror(rr_tag))) return nil end for i, ta in ipairs(keyset) do @@ -25,7 +25,7 @@ local function ta_find(keyset, rr) local ta_tag = C.kr_dnssec_key_tag(ta.type, ta.rdata, #ta.rdata) if ta_tag < 0 or ta_tag > 65535 then log_warn(ffi.C.LOG_GRP_TAUPDATE, string.format('[ta_update] ignoring invalid or unsupported RR: %s: %s', - kres.rr2str(ta), ffi.string(C.knot_strerror(ta_tag)))) + kres.rr2str(ta), kres.strerror(ta_tag))) else if ta.owner == rr.owner then if ta.type == rr.type then @@ -65,7 +65,7 @@ local function ta_present(keyset, rr, hold_down_time) local key_tag = C.kr_dnssec_key_tag(rr.type, rr.rdata, #rr.rdata) if key_tag < 0 or key_tag > 65535 then log_warn(ffi.C.LOG_GRP_TAUPDATE, string.format('[ta_update] ignoring invalid or unsupported RR: %s: %s', - kres.rr2str(rr), ffi.string(C.knot_strerror(key_tag)))) + kres.rr2str(rr), kres.strerror(key_tag))) return false end -- Find the key in current key set and check its status @@ -115,7 +115,7 @@ local function ta_missing(ta, hold_down_time) local key_tag = C.kr_dnssec_key_tag(ta.type, ta.rdata, #ta.rdata) if key_tag < 0 or key_tag > 65535 then log_warn(ffi.C.LOG_GRP_TAUPDATE, string.format('[ta_update] ignoring invalid or unsupported RR: %s: %s', - kres.rr2str(ta), ffi.string(C.knot_strerror(key_tag)))) + kres.rr2str(ta), kres.strerror(key_tag))) key_tag = '' end if ta.state == key_state.Valid then diff --git a/modules/watchdog/watchdog.lua b/modules/watchdog/watchdog.lua index 6d50be22c..f13c08ede 100644 --- a/modules/watchdog/watchdog.lua +++ b/modules/watchdog/watchdog.lua @@ -102,7 +102,7 @@ function watchdog.init() end private.ok_callback = sd_signal_ok if ret < 0 then - error('[watchdog] %s', ffi.string(ffi.C.knot_strerror(math.abs(ret)))) + error('[watchdog] %s', kres.strerror(ret)) return elseif ret == 0 then log_info(ffi.C.LOG_GRP_WATCHDOG, 'disabled in systemd (WatchdogSec= not specified)')