From c2840e90ea9aa86d5e5babd3a9418cb075e50c6d Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Tue, 9 Mar 2021 15:11:23 +0100 Subject: [PATCH] virutil: Do not use g_get_host_name() to obtain hostname MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Ján Tomko --- 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