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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 12 additions & 10 deletions apisix/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
]]


Expand Down Expand Up @@ -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())))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can obtain the hostname by reading /proc/sys/kernel/hostname?
The /bin/hostname requires a binary, but as long as procfs exists, the above path will reflect the hostname, which may be more reliable. 🤔

The gethostname may behave differently on various Linux kernels and libc implementations, such as the maximum length of hostname. I noticed that you assumed it to be 256. It seems that this is not good.

end

return hostname
Expand Down
21 changes: 21 additions & 0 deletions t/core/utils.t
Original file line number Diff line number Diff line change
Expand Up @@ -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