[Toybox] ps is cheating.

Rob Landley rob at landley.net
Fri Oct 16 20:48:00 PDT 2015


So I'm going through the -o fields and testing each one (in the order posix mentions 'em to get coverage), and I've made it to "addr" which is specified in posix but not actually implemented in ubuntu. (Or in busybox, which hasn't got addr or pri so far.) It _recognizes_ the option, but the output's always '-'.

But that's not the cheating part, the cheating part is this:

$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  7730 11472  0  80   0 -  7820 wait   pts/58   00:00:00 bash
4 R  1000 24509  7730  0  80   0 -  4302 -      pts/58   00:00:00 ps

Notice how the length of the "addr" field is _smaller_ than the length of the title, and in fact the "sz" field eats that extra space during -l? That's the cheating part.

I have the problem that if I'm going to do addr, I want to make it mean something. Posix says it's "the address of the process", which isn't very helpful. On an elf system with mmu the start address and load address and such are the same for every darn program, so I was thinking of using the instruction pointer address (slot 27). Problem: on x86_64 that's something like 7fdcfbe95b4c and that's in _hex_, it's even longer in decimal (which all the other addresses and sizes are for no obvious reason).

My initial code defaulted addr to 4 bytes (because that's the length of its title), which produces the following alignment:

$ ./ps -l
F S   UID   PID  PPID C  PRI  NI ADDR    SZ WCHAN  TTY          TIME CMD                        
0 S  1000  7730 11472 -   19   0 1405  7820 do_wai pts/58   00:00:00 bash
4 R  1000 24504  7730 -   19   0 1402  1091 0      pts/58   00:00:00 ./ps -l

And that already only leaves 11 characters for cmd. (the double space between addr and sz is because sz has 5 byres reserved, which is also an issue when I ask it to show thunderbird and it only shows the first 5 digits of a size digit sz value. Yes it's right justified yet trimming _at_ the right, but eh, no correct way to truncate a numeric field.)

The ubuntu ps is cheating even more for this one:

$ ps -l -p 10406
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000 10406 10111  2  80   0 - 512395 poll_s ?       02:04:37 thunderbird

(Which actually gets us back to the vmstat size adjustment logic, I _have_ code to pull that trick just not genericized into the library because it's kinda hard to genericize. Obviously I need to work it out anyway.)

Anyway, my problem is that posix's "ps -l" only really fit in 80 colums back on 16 bit machines. On 32 bit cmd is squished down to 4 characters on an 80 column display (and that's _with_ sz limited to 5 digits), and on 64 bit machines cmd is bumped entirely off the right edge).

I am attempting to implement a spec so stale it can't be implemented as is in a sane way.

I note that ps has a "-y" option for use with -l that makes the fields saneish by dropping the "F" column from the front and swapping "addr" out for "rss". I'm tempted to switch that to be the default output, although it's still cheaing extensively on the alignment to fit everything in, ala:

$ ps -ly -p 10406,24513
S   UID   PID  PPID  C PRI  NI   RSS    SZ WCHAN  TTY          TIME CMD
S  1000 10406 10111  2  80   0 640716 512539 poll_s ?      02:04:56 thunderbird
S  1000 24513 11472  0  80   0  6900  7821 wait   pts/63   00:00:00 bash

But again, vmstat.c has code for that I can chip off and put in lib/lib.c.

But what _that_ does is yank "addr" from standard output formats entirely, and while ubuntu recognizes it via -o it doesn't list it as an option in their "man ps" output. And busybox doesn't implement it, even in current -git. So rather than "fix" this, what I probably want to do is document a deviation from posix and then maybe somebody who can stand the idea of potentially interacting with Jorg "Solaris Solaris Uber Alles" Schilling could email the austin group and go "hey, wanna bring your spec into the late 1990's"?)

(And the whole "I'm not implementing -n because /proc/self/wchan exists", and the bit where posix says default output should have field named "TTY" but if you "-o tty" the same field should be called "TT" which is _INSANE_ and I'm not doing it. It also says that -o "args" and "comm" should behave differently but use the same title, which is not the same title as the default output. Really, posix is being posix again.)

Rob

 1445053680.0


More information about the Toybox mailing list