[libvirt] [PATCH] virConnectGetHostname: return a fully qualified hostname

Daniel P. Berrange berrange at redhat.com
Thu Dec 11 10:29:26 UTC 2008


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'.
> 
> +
> +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;

The original code for this was broken too - potentially having
an unterminated string

      "In case the null-terminated hostname does not fit, no error
       is returned, but the hostname  is  truncated.   It  is 
       unspecified whether the truncated hostname will be null-
       terminated."

So for safety we need to add a line following gethostname() to
always do

    NUL_TERMINATE(hostname);

(that's a convenient macro we have)


ACK to the rest of the patch - particularly the removal of all those
duplicated impls !

Daniel
-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list