aboutsummaryrefslogtreecommitdiff
path: root/toys/lsb
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2019-01-19 11:32:54 -0800
committerRob Landley <rob@landley.net>2019-01-19 14:34:55 -0600
commit7bfdff085dca664b3bbda50371f4f67be2fbd5cf (patch)
tree86b4df8128b2766d4663a2bd56802b457f68a428 /toys/lsb
parente47144c8d0bc4fe1986403ade1e3d54a5c94f993 (diff)
downloadtoybox-7bfdff085dca664b3bbda50371f4f67be2fbd5cf.tar.gz
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
Diffstat (limited to 'toys/lsb')
-rw-r--r--toys/lsb/hostname.c17
1 files changed, 10 insertions, 7 deletions
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);
}