]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virutil: Do not use g_get_host_name() to obtain hostname
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 9 Mar 2021 14:11:23 +0000 (15:11 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 12 Mar 2021 07:45:04 +0000 (08:45 +0100)
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
start with "localhost" or contains a dot, then no further
hostname changes will ever be reflected.

This reverts 26d9748ff11, partially.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/virutil.c

index 227997c7be61d2d359e4694eb169c6f45c555ef1..118ceec0dbf21130520cfbb5ebda4f0530396c7b 100644 (file)
@@ -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