From fbba4423e921270f7721b146db2ae9a52d2b0cc4 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Wed, 7 Jan 2009 10:43:16 +0000 Subject: [PATCH] * src/libvirt_private.syms src/qemu_driver.c src/test.c src/uml_driver.c src/util.c src/util.h src/xen_unified.c: unify hostname lookup using virGetHostname convenience function, patch by David Lutterkort daniel --- ChangeLog | 7 +++++++ src/libvirt_private.syms | 1 + src/qemu_driver.c | 16 ++++------------ src/test.c | 16 +++++----------- src/uml_driver.c | 15 ++++----------- src/util.c | 33 +++++++++++++++++++++++++++++++++ src/util.h | 2 ++ src/xen_unified.c | 15 +++++---------- 8 files changed, 61 insertions(+), 44 deletions(-) diff --git a/ChangeLog b/ChangeLog index fdf589451a..36b497588e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Wed Jan 7 11:38:04 CET 2009 Daniel Veillard + + * src/libvirt_private.syms src/qemu_driver.c src/test.c + src/uml_driver.c src/util.c src/util.h src/xen_unified.c: + unify hostname lookup using virGetHostname convenience function, + patch by David Lutterkort + Tue Jan 6 20:38:23 +0100 2009 Jim Meyering update from gnulib; use its time_r module for localtime_r on mingw diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index f502e12e7e..6b48ea4f39 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -277,6 +277,7 @@ virEventAddHandle; virEventRemoveHandle; virExec; virFormatMacAddr; +virGetHostname; virParseMacAddr; virFileDeletePid; virFileExists; diff --git a/src/qemu_driver.c b/src/qemu_driver.c index b38ecd62a7..7307be5686 100644 --- a/src/qemu_driver.c +++ b/src/qemu_driver.c @@ -1636,23 +1636,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) { @@ -4326,4 +4319,3 @@ int qemuRegister(void) { virRegisterStateDriver(&qemuStateDriver); return 0; } - diff --git a/src/test.c b/src/test.c index e91eed91a0..5d28cced6c 100644 --- a/src/test.c +++ b/src/test.c @@ -656,22 +656,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 1562042e7c..77e19f6758 100644 --- a/src/uml_driver.c +++ b/src/uml_driver.c @@ -1149,23 +1149,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 dd18522635..2a9ea6481f 100644 --- a/src/util.c +++ b/src/util.c @@ -47,6 +47,7 @@ #ifdef HAVE_PATHS_H #include #endif +#include #include "virterror_internal.h" #include "logging.h" @@ -1338,6 +1339,38 @@ int virDiskNameToIndex(const char *name) { return idx; } +#ifndef AI_CANONIDN +#define AI_CANONIDN 0 +#endif + +char *virGetHostname(void) +{ + int r; + char hostname[HOST_NAME_MAX+1], *result; + struct addrinfo hints, *info; + + r = gethostname (hostname, sizeof(hostname)); + if (r == -1) + return NULL; + NUL_TERMINATE(hostname); + + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_CANONNAME|AI_CANONIDN; + hints.ai_family = AF_UNSPEC; + r = getaddrinfo(hostname, NULL, &hints, &info); + if (r != 0) + return NULL; + if (info->ai_canonname == NULL) { + freeaddrinfo(info); + return NULL; + } + + /* Caller frees this string. */ + result = strdup (info->ai_canonname); + freeaddrinfo(info); + return result; +} + /* send signal to a single process */ int virKillProcess(pid_t pid, int sig) { diff --git a/src/util.h b/src/util.h index f622bad06c..4fad6df8a1 100644 --- a/src/util.h +++ b/src/util.h @@ -166,6 +166,8 @@ static inline int getuid (void) { return 0; } static inline int getgid (void) { return 0; } #endif +char *virGetHostname(void); + int virKillProcess(pid_t pid, int sig); #endif /* __VIR_UTIL_H__ */ diff --git a/src/xen_unified.c b/src/xen_unified.c index 4be9b17d05..a4e8d32cee 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) { + result = virGetHostname(); + if (result == NULL) { xenUnifiedError (conn, VIR_ERR_SYSTEM_ERROR, "%s", strerror(errno)); return NULL; } - str = strdup (hostname); - if (str == NULL) { - xenUnifiedError (conn, VIR_ERR_SYSTEM_ERROR, "%s", strerror(errno)); - return NULL; - } - return str; + /* Caller frees this string. */ + return result; } static int