Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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())))
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))
Copy link
Contributor

Choose a reason for hiding this comment

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

(hostname ~= "unknown" or true) always evaluates to true.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @xuruidong, please have a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Baoyuantop ,please review this. Thank you.

}
}
--- request
GET /t
--- response_body_like
hostname: .+
hostname cached: true
hostname valid: true
Loading