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.
From 7687d83e85d26fb60c85dbeb563ab2c8dcfed4c6 Mon Sep 17 00:00:00 2001
From: Colin Cross <ccr...@android.com>
Date: Fri, 6 Oct 2023 15:01:36 -0700
Subject: [PATCH] Fix pgrep -s 0 when running in session ID 0

Running in a PID namespace can cause getsid to return 0, which triggers
an incorrect error in pgrep:
$ sudo unshare -fp ./toybox pgrep -s 0
pgrep: bad -s '0'
               ^

This was found in Android's CI when running the tests in a sandbox that
uses a PID namespace.

Use >=0 when parsing the -s argument.
---
 toys/posix/ps.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index d0992619..c2a20fe2 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -1166,10 +1166,16 @@ static char *parse_rest(void *data, char *str, int len)
     if (pl==&TT.ss && ll[pl->len]==0) ll[pl->len] = getsid(0);
   }
 
-  if (pl==&TT.pp || pl==&TT.ss) {
+  if (pl==&TT.pp) {
     if (num && ll[pl->len]>0) {
       pl->len++;
 
+      return 0;
+    }
+  } else if (pl==&TT.ss) {
+    if (num && ll[pl->len]>=0) {
+      pl->len++;
+
       return 0;
     }
   } else if (pl==&TT.tt) {
-- 
2.42.0.609.gbb76f46606-goog
_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to