diff options
author | Daniel P. Berrangé <berrange@redhat.com> | 2019-12-23 14:55:45 +0000 |
---|---|---|
committer | Daniel P. Berrangé <berrange@redhat.com> | 2020-01-03 15:42:13 +0000 |
commit | 26d9748ff114a060ee751959d108d062f737f5d9 (patch) | |
tree | eb8cc7763a7566d91e9e674f906ad533a813628f /src | |
parent | b4d601ba87ade7fa1a3a4f9c0c268659c15a35c3 (diff) | |
download | libvirt-26d9748ff114a060ee751959d108d062f737f5d9.tar.gz |
util: replace gethostname() with g_get_hostname()
Note the glib function returns a const string because it
caches the hostname using a one time thread initializer
function.
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/util/virlog.c | 17 | ||||
-rw-r--r-- | src/util/virutil.c | 12 |
2 files changed, 7 insertions, 22 deletions
diff --git a/src/util/virlog.c b/src/util/virlog.c index aa98024e1c..8a9fb34161 100644 --- a/src/util/virlog.c +++ b/src/util/virlog.c @@ -60,7 +60,6 @@ VIR_LOG_INIT("util.log"); static GRegex *virLogRegex; -static char virLogHostname[HOST_NAME_MAX+1]; #define VIR_LOG_DATE_REGEX "[0-9]{4}-[0-9]{2}-[0-9]{2}" @@ -253,8 +252,6 @@ virLogPriorityString(virLogPriority lvl) static int virLogOnceInit(void) { - int r; - if (virMutexInit(&virLogMutex) < 0) return -1; @@ -263,19 +260,13 @@ virLogOnceInit(void) virLogRegex = g_regex_new(VIR_LOG_REGEX, G_REGEX_OPTIMIZE, 0, NULL); - /* We get and remember the hostname early, because at later time + /* GLib caches the hostname using a one time thread initializer. + * We want to prime this cache early though, because at later time * it might not be possible to load NSS modules via getaddrinfo() * (e.g. at container startup the host filesystem will not be * accessible anymore. - * Must not use virGetHostname though as that causes re-entrancy - * problems if it triggers logging codepaths */ - r = gethostname(virLogHostname, sizeof(virLogHostname)); - if (r == -1) { - ignore_value(virStrcpyStatic(virLogHostname, "(unknown)")); - } else { - NUL_TERMINATE(virLogHostname); - } + ignore_value(g_get_host_name()); virLogUnlock(); return 0; @@ -471,7 +462,7 @@ virLogHostnameString(char **rawmsg, { char *hoststr; - hoststr = g_strdup_printf("hostname: %s", virLogHostname); + hoststr = g_strdup_printf("hostname: %s", g_get_host_name()); virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, hoststr); *rawmsg = hoststr; diff --git a/src/util/virutil.c b/src/util/virutil.c index 9620ff204d..a0fd7618ee 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -503,17 +503,11 @@ static char * virGetHostnameImpl(bool quiet) { int r; - char hostname[HOST_NAME_MAX+1], *result = NULL; + const char *hostname; + char *result = NULL; struct addrinfo hints, *info; - r = gethostname(hostname, sizeof(hostname)); - if (r == -1) { - if (!quiet) - virReportSystemError(errno, - "%s", _("failed to determine host name")); - return NULL; - } - NUL_TERMINATE(hostname); + hostname = g_get_host_name(); if (STRPREFIX(hostname, "localhost") || strchr(hostname, '.')) { /* in this case, gethostname returned localhost (meaning we can't |