The problem is that g_get_host_name() caches the hostname in a
thread local variable. Therefore, it doesn't reflect any
subsequent hostname changes. While this might be acceptable for
logs where the hostname is printed exactly once when the libvirtd
starts up, it is not optimal for virGetHostnameImpl() which is
what our public virConnectGetHostname() API calls. If the
hostname at the moment of the first API invocation happens to be
"localhost" or contains a dot, then no further hostname changes
will ever be reflected.

This reverts 26d9748ff11, partially.

Signed-off-by: Michal Privoznik <mpriv...@redhat.com>
---
 src/util/virutil.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/util/virutil.c b/src/util/virutil.c
index 227997c7be..118ceec0db 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -475,11 +475,17 @@ static char *
 virGetHostnameImpl(bool quiet)
 {
     int r;
-    const char *hostname;
-    char *result = NULL;
+    char hostname[HOST_NAME_MAX+1], *result = NULL;
     struct addrinfo hints, *info;
 
-    hostname = g_get_host_name();
+    r = gethostname(hostname, sizeof(hostname));
+    if (r == -1) {
+        if (!quiet)
+            virReportSystemError(errno,
+                                 "%s", _("failed to determine host name"));
+        return NULL;
+    }
+    NUL_TERMINATE(hostname);
 
     if (STRPREFIX(hostname, "localhost") || strchr(hostname, '.')) {
         /* in this case, gethostname returned localhost (meaning we can't
-- 
2.26.2

Reply via email to