The attached patch makes virConnectGetHostname try a little harder to get a FQDN on systems where gethostname only returns a short name without a domain (which is pretty useless). The behavior is equivalent to 'hostname --fqdn'. > From 2ae57d0c8c68c453b3f9715fcc9f83af0ebe84a0 Mon Sep 17 00:00:00 2001 From: David Lutterkort <lutter@xxxxxxxxxx> Date: Wed, 10 Dec 2008 18:34:39 -0800 Subject: [PATCH] virConnectGetHostname: return a fully qualified hostname Instead of doing the equivalent of 'hostname', do 'hostname --fqdn', i.e. try to qualify the local host name by an additional call to gethostbyname(3) --- src/libvirt_sym.version.in | 1 + src/qemu_driver.c | 16 ++++------------ src/test.c | 16 +++++----------- src/uml_driver.c | 15 ++++----------- src/util.c | 17 +++++++++++++++++ src/util.h | 2 ++ src/xen_unified.c | 15 +++++---------- 7 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/libvirt_sym.version.in b/src/libvirt_sym.version.in index de0bc4a..f02d9e0 100644 --- a/src/libvirt_sym.version.in +++ b/src/libvirt_sym.version.in @@ -594,6 +594,7 @@ LIBVIRT_PRIVATE_@VERSION@ { virFileReadLimFD; virFileReadPid; virFileLinkPointsTo; + virGetHostname; virParseNumber; virRun; virSkipSpaces; diff --git a/src/qemu_driver.c b/src/qemu_driver.c index 5f6fbd1..d6b7515 100644 --- a/src/qemu_driver.c +++ b/src/qemu_driver.c @@ -1562,23 +1562,16 @@ cleanup: static char * qemudGetHostname (virConnectPtr conn) { - int r; - char hostname[HOST_NAME_MAX+1], *str; + char *result; - r = gethostname (hostname, HOST_NAME_MAX+1); - if (r == -1) { + result = virGetHostname(); + if (result == NULL) { qemudReportError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, "%s", strerror (errno)); return NULL; } /* Caller frees this string. */ - str = strdup (hostname); - if (str == NULL) { - qemudReportError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, - "%s", strerror (errno)); - return NULL; - } - return str; + return result; } static int qemudListDomains(virConnectPtr conn, int *ids, int nids) { @@ -4249,4 +4242,3 @@ int qemuRegister(void) { virRegisterStateDriver(&qemuStateDriver); return 0; } - diff --git a/src/test.c b/src/test.c index 257fc8a..cb623ed 100644 --- a/src/test.c +++ b/src/test.c @@ -651,22 +651,16 @@ static int testGetVersion(virConnectPtr conn ATTRIBUTE_UNUSED, static char *testGetHostname (virConnectPtr conn) { - int r; - char hostname [HOST_NAME_MAX+1], *str; + char *result; - r = gethostname (hostname, HOST_NAME_MAX+1); - if (r == -1) { + result = virGetHostname(); + if (result == NULL) { testError (conn, VIR_ERR_SYSTEM_ERROR, "%s", strerror (errno)); return NULL; } - str = strdup (hostname); - if (str == NULL) { - testError (conn, VIR_ERR_SYSTEM_ERROR, "%s", - strerror (errno)); - return NULL; - } - return str; + /* Caller frees this string. */ + return result; } static int testGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED, diff --git a/src/uml_driver.c b/src/uml_driver.c index 408096e..52a4e5b 100644 --- a/src/uml_driver.c +++ b/src/uml_driver.c @@ -1150,23 +1150,16 @@ cleanup: static char * umlGetHostname (virConnectPtr conn) { - int r; - char hostname[HOST_NAME_MAX+1], *str; + char *result; - r = gethostname (hostname, HOST_NAME_MAX+1); - if (r == -1) { + result = virGetHostname(); + if (result == NULL) { umlReportError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, "%s", strerror (errno)); return NULL; } /* Caller frees this string. */ - str = strdup (hostname); - if (str == NULL) { - umlReportError (conn, NULL, NULL, VIR_ERR_SYSTEM_ERROR, - "%s", strerror (errno)); - return NULL; - } - return str; + return result; } static int umlListDomains(virConnectPtr conn, int *ids, int nids) { diff --git a/src/util.c b/src/util.c index da26009..7188239 100644 --- a/src/util.c +++ b/src/util.c @@ -47,6 +47,7 @@ #ifdef HAVE_PATHS_H #include <paths.h> #endif +#include <netdb.h> #include "virterror_internal.h" #include "logging.h" @@ -1306,3 +1307,19 @@ int virDiskNameToIndex(const char *name) { return idx; } + +char *virGetHostname(void) +{ + int r; + char hostname[HOST_NAME_MAX+1], *str; + struct hostent *he; + + r = gethostname (hostname, HOST_NAME_MAX+1); + if (r == -1) + return NULL; + if (!(he = gethostbyname(hostname))) + return NULL; + + /* Caller frees this string. */ + return strdup (he->h_name); +} diff --git a/src/util.h b/src/util.h index 0748cbf..f85a61e 100644 --- a/src/util.h +++ b/src/util.h @@ -161,4 +161,6 @@ static inline int getuid (void) { return 0; } static inline int getgid (void) { return 0; } #endif +char *virGetHostname(void); + #endif /* __VIR_UTIL_H__ */ diff --git a/src/xen_unified.c b/src/xen_unified.c index a60bc79..dbda3b5 100644 --- a/src/xen_unified.c +++ b/src/xen_unified.c @@ -447,20 +447,15 @@ xenUnifiedGetVersion (virConnectPtr conn, unsigned long *hvVer) static char * xenUnifiedGetHostname (virConnectPtr conn) { - int r; - char hostname [HOST_NAME_MAX+1], *str; + char *result; - r = gethostname (hostname, HOST_NAME_MAX+1); - if (r == -1) { - xenUnifiedError (conn, VIR_ERR_SYSTEM_ERROR, "%s", strerror(errno)); - return NULL; - } - str = strdup (hostname); - if (str == NULL) { + result = virGetHostname(); + if (result == NULL) { xenUnifiedError (conn, VIR_ERR_SYSTEM_ERROR, "%s", strerror(errno)); return NULL; } - return str; + /* Caller frees this string. */ + return result; } static int -- 1.6.0.4 -- Libvir-list mailing list Libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list