hostname: do not use gethostbyname() for "hostname -s"

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@tls.msk.ru>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Michael Tokarev 2013-12-17 19:13:45 +01:00 committed by Denys Vlasenko
parent 4967a41ba1
commit e9355c4326

View File

@ -106,7 +106,7 @@ int hostname_main(int argc UNUSED_PARAM, char **argv)
OPT_i = 0x4, OPT_i = 0x4,
OPT_s = 0x8, OPT_s = 0x8,
OPT_F = 0x10, OPT_F = 0x10,
OPT_dfis = 0xf, OPT_dfi = 0x7,
}; };
unsigned opts; unsigned opts;
@ -134,7 +134,7 @@ int hostname_main(int argc UNUSED_PARAM, char **argv)
if (applet_name[0] == 'd') /* dnsdomainname? */ if (applet_name[0] == 'd') /* dnsdomainname? */
opts = OPT_d; opts = OPT_d;
if (opts & OPT_dfis) { if (opts & OPT_dfi) {
/* Cases when we need full hostname (or its part) */ /* Cases when we need full hostname (or its part) */
struct hostent *hp; struct hostent *hp;
char *p; char *p;
@ -159,6 +159,9 @@ int hostname_main(int argc UNUSED_PARAM, char **argv)
bb_putchar('\n'); bb_putchar('\n');
} }
} }
} else if (opts & OPT_s) {
strchrnul(buf, '.')[0] = '\0';
puts(buf);
} else if (opts & OPT_F) { } else if (opts & OPT_F) {
/* Set the hostname */ /* Set the hostname */
do_sethostname(hostname_str, 1); do_sethostname(hostname_str, 1);