[Toybox] ps and top (and Android)

Rob Landley rob at landley.net
Sun Apr 24 18:28:54 PDT 2016



On 04/24/2016 06:55 PM, Andy Chu wrote:
>>> if that was the intention, it's useless anyway, given the
>>> posix-specified -- for all argument parsing.
>>
>> About that:
>>
>> $ echo -- hello
>> -- hello
>> $ /bin/echo -- hello
>> -- hello
>>
>> _I'm_ doing it, but the gnu tools aren't, and nobody seems to have cared.
>>
>> Posix does not seem to be aware that it's been losing traction for a
>> while now. My own last attempt to engage with the posix mailinglist made
>> it as far as:
> 
> I'm pretty sure that's for backward compatibility, not because the
> bash or GNU coreutils maintainers are ignorant of POSIX or don't care.
> 
> There is an in-depth discussion of echo in the book "Portable Shell
> Scripting" I referred to:
> 
> https://books.google.com/books?id=3ETiIJG1UB4C&pg=PA183&lpg=PA183&dq=portable+shell+scripting+public+enemy+1+echo&source=bl&ots=1Ps1NAaM-1&sig=_KN0uPhyVvceuZwojK5ysJtDyng&hl=en&sa=X&ved=0ahUKEwjjgvzCuajMAhWruoMKHYNrAOwQ6AEIHDAA#v=onepage&q=portable%20shell%20scripting%20public%20enemy%201%20echo&f=false
> 
> Hopefully that is readable, but the gist is that sometimes -n is a
> flag and sometimes it is echo'd;

Oh yeah, toybox does that already, and there's a test for it in
tests/echo.test (echo -nex: note that has to DROP -n and -e when it hits
the -x, "echo -ne" eats the -ne as an argument, "echo -nex" leaves -nex
as a literal _after_ looking at the -n and the -e which by themselves
would be valid. lib/args.c handles this case. Commit 1a221d9b4f05 back
in 2008 did that.)

In fact I should add another test: "echo -ne -x" should print -x without
a newline. (And does.)

> same thing with -- I guess.

Nope, the first -- gets eaten in toybox. It does not get eaten in gnu.

In both gnu and toybox, the echo arguments have to come first: once you
hit a non-option argument, argument processing is disabled. This is a
common pattern: xargs and nice and so on do that too. But with toybox
echo I can say echo -- "$BLAH" and it doesn't matter if $BLAH resolves
to -n it'll still print it. But with gnu echo you can't do that, if you
add -- it's not parsed, it's a non-option argument that disables laster
argument processing the exact same way "echo xx $BLAH" would. In toybox
-- will make the rest of the line literal without modifying the rest of
the line. In gnu, there's no way to do that: in fact they say "don't use
echo, use printf %s for that".

Sigh...

> Apparently "echo" is one of the most un-portable builtins in practice.
> I think he suggests using printf instead of echo somewhere.

Yes, see above. It's because gnu is broken and nonstandard.

Technically, -n and -e are nonstandard, -n is explicitly NOT recognized
on "xsi conformant" systems, and the backslashes are _always_ parsed (so
no -e to enable that). So I'm following MOSTLY gnu syntax here, with the
-- behavior from posix's throat-clearing section but largely ignoring
their actual "echo" page.

Again: posix is increasingly irrelevant, thanks largely to Jorg I
suspect. (Solaris being the one true unix and Linux being a persitent
abberation? No really. In 2016. I don't think they've noticed the
existence of Android yet. Given them a few more years. I could tell you
how many if I had actuarial tables and looked up Jorg's age.)

> This book actually goes into some detail about historical bugs in
> different shells...

On the one hand, yay comptuer history?
(http://landley.net/history/mirror is a hobby of mine.) On the other: I
don't care as far a toybox is concerned.

In 1991 Linus Torvalds created a term program that booted from a floppy
because Minix's microkernel architecture couldn't keep up with his 2400
baud modem without dropping characters, and he wanted to dial in to his
university's microvax to read comp.os.minix on usenet (without leaving
his dorm room in the middle of the Finnish winter). He then taught it to
read and write the minix filesystem on his hard drive so he could upload
and download stuff. He then taught it to handle the system calls bash
made so he could ls/mv/rm to free up space without rebooting into minix.
That's where Linux came from.

This means bash was the default shell of Linux before 0.0.1. It
continued to be the default shell of Linux until Cannonical decided
Ubuntu 6.06 took too long to boot that the fix was to run its init
scripts using a slimmer shell that launched faster, but changing the
#!/bin/sh line at the start of each init script was too intrusive a
change so it redirected /bin/sh.

No really. That's why they did that:

https://wiki.ubuntu.com/DashAsBinSh

And yes, it broke all sorts of stuff, for years, including the kernel build:

https://administratosphere.wordpress.com/2011/07/20/why-doesnt-my-binsh-script-run-under-ubuntu/

And of course it didn't fix the problem, booting was still slow, so they
created "upstart" to run init scripts in parallel, but they NEVER
REVERTED the /bin/sh redirection because that would be admitting a
mistake, which they don't do. (They blamed debian for the idea, but
debian didn't change its default for YEARS after Ubuntu did this, ala
https://lwn.net/Articles/343924/ .)

Seriously, the first program Linux ever ran was Bash.  The default shell
of linux for the first 15 years was bash. There's a reason I'm aiming
specifically for a _bash_ replacement in toybox.

> Andy

Rob



More information about the Toybox mailing list