aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorPaul Barker <paul@paulbarker.me.uk>2016-05-01 15:42:57 +0100
committerRob Landley <rob@landley.net>2016-05-03 22:05:21 -0500
commitc451397f16ecfbe05d41ac76e3d5f84dc39a6bfc (patch)
treeba474591d60329bda44efd89432ffda226757860 /toys
parente8bd47bc5eed21da79afb52b4fc83f63f4fc63e2 (diff)
downloadtoybox-c451397f16ecfbe05d41ac76e3d5f84dc39a6bfc.tar.gz
Add -b and -F arguments to hostname
These arguments are required to correctly set the hostname at boot time. They are used by the '/etc/init.d/hostname.sh' init script in an OpenEmbedded system.
Diffstat (limited to 'toys')
-rw-r--r--toys/lsb/hostname.c42
1 files changed, 40 insertions, 2 deletions
diff --git a/toys/lsb/hostname.c b/toys/lsb/hostname.c
index 23467fb3..ebfc803c 100644
--- a/toys/lsb/hostname.c
+++ b/toys/lsb/hostname.c
@@ -4,23 +4,61 @@
*
* http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/hostname.html
-USE_HOSTNAME(NEWTOY(hostname, NULL, TOYFLAG_BIN))
+USE_HOSTNAME(NEWTOY(hostname, "bF:", TOYFLAG_BIN))
config HOSTNAME
bool "hostname"
default y
help
- usage: hostname [newname]
+ usage: hostname [-b] [-F FILENAME] [newname]
Get/Set the current hostname
+
+ -b Set hostname to 'localhost' if otherwise unset
+ -F Set hostname to contents of FILENAME
*/
#define FOR_hostname
#include "toys.h"
+GLOBALS(
+ char *fname;
+)
+
void hostname_main(void)
{
const char *hostname = toys.optargs[0];
+
+ if (toys.optflags & FLAG_F) {
+ char *buf;
+ if ((hostname = buf = readfile(TT.fname, 0, 0))) {
+ size_t len = strlen(hostname);
+ char *end = buf + len - 1;
+
+ /* Trim trailing whitespace. */
+ while (len && isspace(*end)) {
+ *end-- = '\0';
+ len--;
+ }
+ if (!len) {
+ free(buf);
+ hostname = NULL;
+ if (!(toys.optflags & FLAG_b))
+ error_exit("empty file '%s'", TT.fname);
+ }
+ } else if (!(toys.optflags & FLAG_b))
+ error_exit("failed to read '%s'", TT.fname);
+ }
+
+ if (!hostname && toys.optflags & FLAG_b) {
+ /* Do nothing if hostname already set. */
+ if (gethostname(toybuf, sizeof(toybuf))) perror_exit("get failed");
+ if (strnlen(toybuf, sizeof(toybuf))) exit(0);
+
+ /* Else set hostname to localhost. */
+ hostname = "localhost";
+ }
+
if (hostname) {
if (sethostname(hostname, strlen(hostname)))
perror_exit("set failed '%s'", hostname);