"Daniel P. Berrange" <berrange(a)redhat.com> wrote:
On Wed, Dec 10, 2008 at 07:33:17PM -0800, David Lutterkort wrote:
> 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(a)redhat.com>
> 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)
> +
> +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;
Actually, gethostbyname should never be used in any modern code as it is
not thread safe. Instead use getaddrinfo() and the AI_CANONNAME flag
Good point.
Yet another reason not to use gethostbyname.
I think it's time to make the prohibition official.
Here's a patch to do that:
From 5105125b7a91d701ce2dc9f549814a8fa4e352d7 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering(a)redhat.com>
Date: Thu, 11 Dec 2008 11:49:40 +0100
Subject: [PATCH] syntax-check: prohibit all new uses of gethostby* functions
* Makefile.maint (sc_prohibit_gethostby): New rule.
* .x-sc_prohibit_gethostby: Exempt the few existing uses.
---
.x-sc_prohibit_gethostby | 3 +++
Makefile.maint | 6 ++++++
2 files changed, 9 insertions(+), 0 deletions(-)
create mode 100644 .x-sc_prohibit_gethostby
diff --git a/.x-sc_prohibit_gethostby b/.x-sc_prohibit_gethostby
new file mode 100644
index 0000000..d74ea40
--- /dev/null
+++ b/.x-sc_prohibit_gethostby
@@ -0,0 +1,3 @@
+gnulib/lib/getaddrinfo.c
+gnulib/m4/getaddrinfo.m4
+src/xend_internal.c
diff --git a/Makefile.maint b/Makefile.maint
index 10d481b..051e4c0 100644
--- a/Makefile.maint
+++ b/Makefile.maint
@@ -390,6 +390,12 @@ sc_prohibit_virBufferAdd_with_string_literal:
{ echo '$(ME): use virBufferAddLit, not virBufferAdd,' \
'with a string literal' 1>&2; exit 1; } || :
+# Not only do they fail to deal well with ipv6, but the gethostby*
+# functions are also not thread-safe.
+sc_prohibit_gethostby:
+ @grep -nE '\<gethostby(addr|name2?) *\(' $$($(VC_LIST_EXCEPT)) && \
+ { echo '$(ME): use getaddrinfo, not gethostby*' 1>&2; exit 1; } || :
+
# Avoid useless parentheses like those in this example:
# #if defined (SYMBOL) || defined (SYM2)
sc_useless_cpp_parens:
--
1.6.0.4.1044.g77718