bzp2010 commented on code in PR #12267: URL: https://github.com/apache/apisix/pull/12267#discussion_r2117596298
########## apisix/core/utils.lua: ########## @@ -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()))) Review Comment: 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org