[Toybox] Add -b and -F arguments to hostname

Rob Landley rob at landley.net
Wed May 4 02:11:05 PDT 2016


On 05/01/2016 01:56 PM, Paul Barker wrote:
> Hi all,
> 
> I've attached a patch to add -b and -F arguments to the hostname
> program within toybox. These options are used by the
> '/etc/init.d/hostname.sh' init script in an image built with
> OpenEmbedded. With these options added, the hostname is set correctly
> during boot.
> 
> I've tried to keep the new code concise and followed the style used
> elsewhere in toybox. Hopefully it won't need much cleanup.

It passed an initial sniff test and I applied it as-is, now I'm going
through and doing whitespace tweaks and such. (Put a blank line between
variable declarations and non-declaration code, etc.)

I _think_ you're trimming trailing whitespace because readfile() gets a
newline? (Elliott added chomp() to lib/lib.c to trim trailing newlines.)

I short-lived program that isn't run nofork only needs to free() data
when it's satisfying valgrind of similar, which means
if (CFG_TOYBOX_FREE) free(buf); (It's fine to leak resources the OS will
clean up on exit, the problem is when they persist or accumulate, or if
you're running in a bootloader context where there's no per-process VM.)
Also, we don't need buf, we didn't move the start of hostname so can
just free that. Don't need an error_exit() for !buf because there's
xreadfile() that does it for us.

I should probably add an XFREE() macro that assigns 0 to the thing that
was freed. It's a pretty common idiom...

Don't need to say "failed" if the actual output is
"hostname: message: Error Message" such as "Name too long". (I try to
restrict error message vocabulary for users who don't have english as
their first language.)

Posix says it's unspecified whether gethostname() null terminates
truncated names but toybuf starts zeroed so use sizeof(toybuf)-1;

Hmmm... Ok, there's a difference between this one and ubuntu's: if I do:

  hostname -bF <(echo "")

The ubuntu version produces no output (but doesn't change the
existingname either), and this version says "driftwood" (I.E. prints the
old name).

I think I prefer my behavior for that. Neither is quite... clear. (-F
said to chagne to an empty name, is that an error in the presence of -b?
Should it replace the existing name with "localhost"?)

As usual, posix is useless here. (Grrr. I don't suppose Android/Alphabet
has somebody dedicated to coming up with a new baseline OS standard that
actually COVERS all this nonsense? I'm sure Rich Felker would have his
own laundry list. No, the Posix committee is not salvageable, it's
thoroughly Jorged: http://landley.net/notes-2016.html#11-03-2016 .)

Eh, hit it with a thorough test suite. (The current hostname test is
useless, I should add a real one. Except, hmmm... How do I _clear_ the
hostname so that -b has something to do?)

> As an aside, it'd be really useful to add some instructions on how to
> contribute to toybox to the README file. I've not contributed to toybox
> before so let me know if I've done anything wrong/missed anything and
> I'll try to fix it.

I fluffed up the README a bit, let me know if I missed anything.

> Thanks,
> Paul Barker

Rob



More information about the Toybox mailing list