xuruidong commented on code in PR #12267:
URL: https://github.com/apache/apisix/pull/12267#discussion_r2133530962


##########
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:
   I think `gethostname()`  is more reliable than procfs, as procfs may not be 
available. The hostname length is typically 64, though some systems use 255.



-- 
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

Reply via email to