[PATCH] do not use gethostbyname() for hostname -s
Tito
farmatito at tiscali.it
Mon Dec 9 21:40:22 UTC 2013
On Monday 09 December 2013 18:56:43 Michael Tokarev wrote:
> There's no reason to call gethostbyname() on the value returned
> by uname() when asked just for a short name of a host. This may
> also be wrong, when uname is set to one value, but in /etc/hosts
> (or elsewhere) the "canonical" name is different. This is often
> the case for localhost entry in /etc/hosts:
>
> 127.0.0.1 localhost myname
>
> With this content of /etc/hosts, and uname being set to myname,
> busybox hostname -s will return localhost, while regular
> hostname utility returns myname.
>
> Fix this by not calling gethostbyname() for the simple `hostname -s'
> use.
>
> Signed-off-by: Michael Tokarev <mjt at tls.msk.ru>
>
> diff --git a/networking/hostname.c b/networking/hostname.c
> index d2516b5..1e68116 100644
> --- a/networking/hostname.c
> +++ b/networking/hostname.c
> @@ -106,12 +106,13 @@ int hostname_main(int argc UNUSED_PARAM, char **argv)
> OPT_i = 0x4,
> OPT_s = 0x8,
> OPT_F = 0x10,
> - OPT_dfis = 0xf,
> + OPT_dfi = 0x7,
> };
>
> unsigned opts;
> char *buf;
> char *hostname_str;
> + char *p;
>
> #if ENABLE_LONG_OPTS
> applet_long_options =
> @@ -134,10 +135,9 @@ int hostname_main(int argc UNUSED_PARAM, char **argv)
> if (applet_name[0] == 'd') /* dnsdomainname? */
> opts = OPT_d;
>
> - if (opts & OPT_dfis) {
> + if (opts & OPT_dfi) {
> /* Cases when we need full hostname (or its part) */
> struct hostent *hp;
> - char *p;
>
> hp = xgethostbyname(buf);
> p = strchrnul(hp->h_name, '.');
> @@ -159,6 +159,10 @@ int hostname_main(int argc UNUSED_PARAM, char **argv)
> bb_putchar('\n');
> }
> }
> + } else if (opts & OPT_s) {
> + p = strchrnul(buf, '.');
> + *p = '\0';
> + puts(buf);
> } else if (opts & OPT_F) {
> /* Set the hostname */
> do_sethostname(hostname_str, 1);
Hi,
I cannot reproduce the case you show with busybox hostname
and /etc/hosts on debian 7:
$ echo $HOSTNAME
debian
$ cat /etc/hostname
debian
$ cat /etc/hosts
127.0.0.1 localhost debian
$ hostname
debian
$ hostname -s
debian
$ ./busybox hostname
debian
$ ./busybox hostname -s
debian
Even modifying /etc/hosts makes no difference:
$ echo $HOSTNAME
debian
$ cat /etc/hostname
debian
$ cat /etc/hosts
127.0.0.1 localhost hosts.file.debian
$ hostname
debian
$ hostname -s
debian
$ ./busybox hostname
debian
$ ./busybox hostname -s
debian
So now I'm wondering where "(or elsewhere)" could be?
One interesting difference between busybox's hostname and
real hostname pops up if we change hostname on the fly:
su
hostname prova
debian:/home/tito# hostname
prova
debian:/home/tito# hostname -s
prova
debian:/home/tito# ./busybox hostname
prova
debian:/home/tito# ./busybox hostname -s
hostname: prova: Unknown host
This in fact is fixed by your patch:
debian:~/Desktop/SourceCode/busybox$ ./busybox hostname
prova
debian:~/Desktop/SourceCode/busybox$ ./busybox hostname -s
prova
BTW.: It is better if you attach patches as my or your email client
ate the tabs and the patch doesn't apply cleanly.
Ciao,
Tito
More information about the busybox
mailing list