Skip to content

fix: can not get hostname in redhat #12267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions apisix/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ 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
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
local get_string_buf = base.get_string_buf
local exiting = ngx.worker.exiting
local ngx_sleep = ngx.sleep
Expand All @@ -56,6 +57,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);
]]


Expand Down Expand Up @@ -256,19 +259,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("failed to call gethostname(): ", ffi_string(C.strerror(ffi.errno())))
end

return hostname
Expand Down
40 changes: 40 additions & 0 deletions t/core/utils.t
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,43 @@ 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 ~= "")

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
GET /t
--- response_body_like
hostname: .+
hostname cached: true
hostname valid: true
system hostname: .+
hostname match: true
Loading