[Toybox] [PATCH] Fix pgrep -s 0 when running in session ID 0

Colin Cross ccross at google.com
Sat Oct 7 19:47:55 PDT 2023


On Fri, Oct 6, 2023 at 9:29 PM Rob Landley <rob at landley.net> wrote:
>
> On 10/6/23 17:10, Colin Cross via Toybox wrote:
> > I came across an issue when running the pgrep -s 0 test in Android's
> > CI infrastructure that uses a PID namespace, causing the test to run
> > session ID 0:
> >
> > $ sudo unshare -fp ./toybox pgrep -s 0
> > pgrep: bad -s '0'
> >
> > The attached patch fixes the argument parsing to support getsid returning 0.
>
> Ah, I read "man 2 getsid" to indicate that sid 0 was special, but it's passing
> in _pid_ 0 that's special. Which is why, right before your patch, this part
> renders what you did moot:
>
>     // For pkill, -s 0 represents pkill's session id.
>     if (pl==&TT.ss && ll[pl->len]==0) ll[pl->len] = getsid(0);
>
> Ah, no wait, that _is_ in the pkill man page:
>
>        -s, --session sid,...
>               Only match processes whose process session ID is  listed.   Ses‐
>               sion ID 0 is translated into pgrep's or pkill's own session ID.
>
> ...so you want pgrep to accept -s 0, and it'll it will coincidentally work for
> pkill if your session ID _is_ 0 then getsid() will return 0 anyway? And pkill
> should work differently from pgrep?

No, pkill and pgrep both already work with -s 0 as long as the current
process is not in session ID 0.  When passed -s 0, they replace the
parsed value (0) with the result of getsid(0).  It then falls through
to the check that the parsed value (now the getsid value) is greater
than 0, but that's incorrect because 0 is a valid return value from
getsid.  My patch splits out the > 0 check for the -s argument and
replaces it with a >= 0 check.  This is safe for the -s argument
because the case where the parsed value == 0 has already been handled
by calling getsid, so the only additional case that will be accepted
after my patch is when the parsed value was 0 and the return value
from getsid was 0.

There's no way to limit pgrep or pkill to the processes in session 0
unless it is itself running in session 0, but that's a general problem
with pgrep and pkill, and not specific to toybox.


More information about the Toybox mailing list