From f2287a3834de5170c46429239ee9766f0bed10ea Mon Sep 17 00:00:00 2001 From: xuruidong Date: Thu, 29 May 2025 18:03:48 +0800 Subject: [PATCH 1/5] fix: can not get hostname in redhat --- apisix/core/utils.lua | 22 ++++++++++++---------- t/core/utils.t | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index cfea756542bd..9251dc40d23b 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -33,6 +33,7 @@ local base = require("resty.core.base") local open = io.open local sub_str = string.sub local str_byte = string.byte +local str_gsub = string.gsub local tonumber = tonumber local tostring = tostring local re_gsub = ngx.re.gsub @@ -42,6 +43,7 @@ local type = type local io_popen = io.popen local C = ffi.C local ffi_string = ffi.string +local ffi_new = ffi.new local get_string_buf = base.get_string_buf local exiting = ngx.worker.exiting local ngx_sleep = ngx.sleep @@ -56,6 +58,8 @@ local max_sleep_interval = 1 ffi.cdef[[ int ngx_escape_uri(char *dst, const char *src, size_t size, int type); + int gethostname(char *name, size_t len); + char *strerror(int errnum); ]] @@ -256,19 +260,17 @@ function _M.gethostname() return hostname end - local hd = io_popen("/bin/hostname") - local data, err = hd:read("*a") - if err == nil then - hostname = data - if string.has_suffix(hostname, "\r\n") then - hostname = sub_str(hostname, 1, -3) - elseif string.has_suffix(hostname, "\n") then - hostname = sub_str(hostname, 1, -2) - end + local size = 256 + local buf = ffi_new("unsigned char[?]", size) + + local res = C.gethostname(buf, size) + if res == 0 then + local data = ffi_string(buf, size) + hostname = str_gsub(data, "%z+$", "") else hostname = "unknown" - log.error("failed to read output of \"/bin/hostname\": ", err) + log.error("gethostname error:", ffi_string(C.strerror(ffi.errno()))) end return hostname diff --git a/t/core/utils.t b/t/core/utils.t index 9faa545e17e1..bdf9fe5df05a 100644 --- a/t/core/utils.t +++ b/t/core/utils.t @@ -393,3 +393,24 @@ res:nil res:5 res:12 res:7 + + + +=== TEST 13: gethostname +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local hostname = core.utils.gethostname() + ngx.say("hostname: ", hostname) + local hostname2 = core.utils.gethostname() + ngx.say("hostname cached: ", hostname == hostname2) + ngx.say("hostname valid: ", hostname ~= "" and (hostname ~= "unknown" or true)) + } + } +--- request +GET /t +--- response_body_like +hostname: .+ +hostname cached: true +hostname valid: true From c42db6fb1d36878505b33ab4fd78ba6e7bf3da5c Mon Sep 17 00:00:00 2001 From: xuruidong Date: Wed, 18 Jun 2025 13:10:34 +0800 Subject: [PATCH 2/5] refactor: remove unused variable in utils.lua --- apisix/core/utils.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index 9251dc40d23b..0e380e5e60c6 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -40,7 +40,6 @@ local re_gsub = ngx.re.gsub local re_match = ngx.re.match local re_gmatch = ngx.re.gmatch local type = type -local io_popen = io.popen local C = ffi.C local ffi_string = ffi.string local ffi_new = ffi.new From 7c07f937215b547cd2581d0dc8bca27ef35d0f03 Mon Sep 17 00:00:00 2001 From: xuruidong Date: Wed, 18 Jun 2025 14:03:04 +0800 Subject: [PATCH 3/5] test: update gethostname --- t/core/utils.t | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/t/core/utils.t b/t/core/utils.t index bdf9fe5df05a..42a7f1cd1645 100644 --- a/t/core/utils.t +++ b/t/core/utils.t @@ -406,6 +406,23 @@ res:7 local hostname2 = core.utils.gethostname() ngx.say("hostname cached: ", hostname == hostname2) ngx.say("hostname valid: ", hostname ~= "" and (hostname ~= "unknown" or true)) + + local handle = io.popen("/bin/hostname") + if handle then + local system_hostname = handle:read("*a") + handle:close() + if system_hostname then + system_hostname = string.gsub(system_hostname, "\n$", "") + ngx.say("system hostname: ", system_hostname) + ngx.say("hostname match: ", hostname == system_hostname) + else + ngx.say("system hostname: failed to read") + ngx.say("hostname match: unable to verify") + end + else + ngx.say("system hostname: failed to execute /bin/hostname") + ngx.say("hostname match: unable to verify") + end } } --- request @@ -414,3 +431,5 @@ GET /t hostname: .+ hostname cached: true hostname valid: true +system hostname: .+ +hostname match: true From 2c4c444aca6e485db261ecf0f67bbbdb49fb70e8 Mon Sep 17 00:00:00 2001 From: xuruidong Date: Tue, 29 Jul 2025 10:12:37 +0800 Subject: [PATCH 4/5] test: update test Signed-off-by: xuruidong --- t/core/utils.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/core/utils.t b/t/core/utils.t index 42a7f1cd1645..28fa537f3674 100644 --- a/t/core/utils.t +++ b/t/core/utils.t @@ -405,7 +405,7 @@ res:7 ngx.say("hostname: ", hostname) local hostname2 = core.utils.gethostname() ngx.say("hostname cached: ", hostname == hostname2) - ngx.say("hostname valid: ", hostname ~= "" and (hostname ~= "unknown" or true)) + ngx.say("hostname valid: ", hostname ~= "") local handle = io.popen("/bin/hostname") if handle then From ac6504b00a924718cb1cae1374484c72666beabc Mon Sep 17 00:00:00 2001 From: xuruidong Date: Fri, 1 Aug 2025 11:28:28 +0800 Subject: [PATCH 5/5] refactor: improve clarity of log message --- apisix/core/utils.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index 0e380e5e60c6..d9da984f32c8 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -269,7 +269,7 @@ function _M.gethostname() else hostname = "unknown" - log.error("gethostname error:", ffi_string(C.strerror(ffi.errno()))) + log.error("failed to call gethostname(): ", ffi_string(C.strerror(ffi.errno()))) end return hostname