From 7bfdff085dca664b3bbda50371f4f67be2fbd5cf Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Sat, 19 Jan 2019 11:32:54 -0800 Subject: hostname: fix behavior when in jail. Only -d and -f should cause a DNS lookup. The rest should just act directly on the result of gethostname(3). Encountered with the AOSP buildbots' use of nsjail, but tested with both the Debian hostname and toybox hostname thus: ``` unshare -Uunr sh hostname android-build hostname hostname -s hostname -d hostname -f ``` (Not sure how to add that to the tests.) Also fix a SEGV with -s if the hostname doesn't contain a '.'. Also switch to the FLAG() macro. Also add the missing -s to the synopsis. Bug: http://b/123123255 --- toys/lsb/hostname.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'toys/lsb') diff --git a/toys/lsb/hostname.c b/toys/lsb/hostname.c index c9fafa54..ac7f9163 100644 --- a/toys/lsb/hostname.c +++ b/toys/lsb/hostname.c @@ -10,7 +10,7 @@ config HOSTNAME bool "hostname" default y help - usage: hostname [-bsf] [-F FILENAME] [newname] + usage: hostname [-bdsf] [-F FILENAME] [newname] Get/set the current hostname. @@ -36,13 +36,13 @@ void hostname_main(void) if (TT.F && (hostname = xreadfile(TT.F, 0, 0))) { if (!*chomp(hostname)) { if (CFG_TOYBOX_FREE) free(hostname); - if (!(toys.optflags&FLAG_b)) error_exit("empty '%s'", TT.F); + if (!FLAG(b)) error_exit("empty '%s'", TT.F); hostname = 0; } } // Implement -b. - if (!hostname && (toys.optflags&FLAG_b)) + if (!hostname && FLAG(b)) if (gethostname(toybuf, sizeof(toybuf)-1) || !*toybuf) hostname = "localhost"; @@ -55,9 +55,12 @@ void hostname_main(void) // Get the hostname. if (gethostname(toybuf, sizeof(toybuf)-1)) perror_exit("gethostname"); - if (!(h = gethostbyname(toybuf))) perror_exit("gethostbyname"); - snprintf(toybuf, sizeof(toybuf), "%s", h->h_name); + // We only do the DNS lookup for -d and -f. + if (FLAG(d) || FLAG(f)) { + if (!(h = gethostbyname(toybuf))) perror_exit("gethostbyname"); + snprintf(toybuf, sizeof(toybuf), "%s", h->h_name); + } dot = strchr(toybuf, '.'); - if (toys.optflags&FLAG_s) *dot = '\0'; - xputs(toys.optflags&FLAG_d ? dot+1 : toybuf); + if (FLAG(s) && dot) *dot = '\0'; + xputs(FLAG(d) ? dot+1 : toybuf); } -- cgit v1.2.3