Skip to content

Commit e90570a

Browse files
authored
fix: can not get hostname in redhat (#12267)
1 parent a5d06b3 commit e90570a

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

apisix/core/utils.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ local base = require("resty.core.base")
3333
local open = io.open
3434
local sub_str = string.sub
3535
local str_byte = string.byte
36+
local str_gsub = string.gsub
3637
local tonumber = tonumber
3738
local tostring = tostring
3839
local re_gsub = ngx.re.gsub
3940
local re_match = ngx.re.match
4041
local re_gmatch = ngx.re.gmatch
4142
local type = type
42-
local io_popen = io.popen
4343
local C = ffi.C
4444
local ffi_string = ffi.string
45+
local ffi_new = ffi.new
4546
local get_string_buf = base.get_string_buf
4647
local exiting = ngx.worker.exiting
4748
local ngx_sleep = ngx.sleep
@@ -56,6 +57,8 @@ local max_sleep_interval = 1
5657
ffi.cdef[[
5758
int ngx_escape_uri(char *dst, const char *src,
5859
size_t size, int type);
60+
int gethostname(char *name, size_t len);
61+
char *strerror(int errnum);
5962
]]
6063

6164

@@ -256,19 +259,17 @@ function _M.gethostname()
256259
return hostname
257260
end
258261

259-
local hd = io_popen("/bin/hostname")
260-
local data, err = hd:read("*a")
261-
if err == nil then
262-
hostname = data
263-
if string.has_suffix(hostname, "\r\n") then
264-
hostname = sub_str(hostname, 1, -3)
265-
elseif string.has_suffix(hostname, "\n") then
266-
hostname = sub_str(hostname, 1, -2)
267-
end
262+
local size = 256
263+
local buf = ffi_new("unsigned char[?]", size)
264+
265+
local res = C.gethostname(buf, size)
266+
if res == 0 then
267+
local data = ffi_string(buf, size)
268+
hostname = str_gsub(data, "%z+$", "")
268269

269270
else
270271
hostname = "unknown"
271-
log.error("failed to read output of \"/bin/hostname\": ", err)
272+
log.error("failed to call gethostname(): ", ffi_string(C.strerror(ffi.errno())))
272273
end
273274

274275
return hostname

t/core/utils.t

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,43 @@ res:nil
393393
res:5
394394
res:12
395395
res:7
396+
397+
398+
399+
=== TEST 13: gethostname
400+
--- config
401+
location /t {
402+
content_by_lua_block {
403+
local core = require("apisix.core")
404+
local hostname = core.utils.gethostname()
405+
ngx.say("hostname: ", hostname)
406+
local hostname2 = core.utils.gethostname()
407+
ngx.say("hostname cached: ", hostname == hostname2)
408+
ngx.say("hostname valid: ", hostname ~= "")
409+
410+
local handle = io.popen("/bin/hostname")
411+
if handle then
412+
local system_hostname = handle:read("*a")
413+
handle:close()
414+
if system_hostname then
415+
system_hostname = string.gsub(system_hostname, "\n$", "")
416+
ngx.say("system hostname: ", system_hostname)
417+
ngx.say("hostname match: ", hostname == system_hostname)
418+
else
419+
ngx.say("system hostname: failed to read")
420+
ngx.say("hostname match: unable to verify")
421+
end
422+
else
423+
ngx.say("system hostname: failed to execute /bin/hostname")
424+
ngx.say("hostname match: unable to verify")
425+
end
426+
}
427+
}
428+
--- request
429+
GET /t
430+
--- response_body_like
431+
hostname: .+
432+
hostname cached: true
433+
hostname valid: true
434+
system hostname: .+
435+
hostname match: true

0 commit comments

Comments
 (0)