[Toybox] [PATCH] Fix ps infinite loop on Android.

Rob Landley rob at landley.net
Fri Oct 23 08:05:02 PDT 2015


On 10/21/2015 11:47 PM, enh wrote:
> I pasted this into a thread "smack support" on 2015-09-19, but it
> looks like it got lost.
> 
> diff --git a/toys/pending/ps.c b/toys/pending/ps.c
> index 29111d5..2bd7bcb 100644
> --- a/toys/pending/ps.c
> +++ b/toys/pending/ps.c
> @@ -238,11 +238,12 @@ void ps_main(void)
>    TT.width--;
> 
>    // find controlling tty, falling back to /dev/tty if none
> -  for (i = fd = 0; i < 4; i++) {
> +  for (i = 0; i < 4; i++) {
>      struct stat st;
> 
> -    if (i != 3 || -1 != (i = fd = open("/dev/tty", O_RDONLY))) {
> -      if (isatty(i) && !fstat(i, &st)) {
> +    fd = i;
> +    if (i != 3 || -1 != (fd = open("/dev/tty", O_RDONLY))) {
> +      if (isatty(fd) && !fstat(fd, &st)) {
>          TT.tty = st.st_rdev;
>          break;
> 

Infinite loop... If you haven't got a /dev/tty it would return -1 and
reset the loop. Right. My fault, sorry. But a few lines down there's an
if (fd!=-1) close(fd) that probably shouldn't trigger on stdin/out/err,
so I still need to fix...

Query: if you haven't got a controlling tty on stdin, stdout, stderr, or
/dev/tty, what should the default behavior of ps _be_? In theory it
lists processes attached to the current tty, but tty 0 is reserved
(lanana.org says it's the null device, but not /dev/null because that's
1:3).

I guess with no tty, act like -A maybe? (That seems to be what toolbox
ps did...)

Hmmm, I installed a terminal on my phone and toolbox ps is going:

USER PID PPID VSIZE RSS WCHAN PC NAME

Except between C and NAME it's got "S" which I'm guessing is STAT elided
from the title field for space. This doesn't quite map to the default,
-l, or -f outputs, and "-o name" isn't recognized by psutils (I'm
guessing cmd?)

Lemme finish implementing the posix+obvious bits of psutils behavior
before worrying about that...

Rob

 1445612702.0


More information about the Toybox mailing list