[Toybox] A ps cleanup I can't test...

enh enh at google.com
Tue Jan 14 08:36:44 PST 2025


On Sun, Jan 12, 2025 at 3:38 PM Rob Landley <rob at landley.net> wrote:
>
> Elliott, could you give this a sniff/smoke test on android? Built with
> the NDK it's giving blank space for PCY on all the processes I've got,
> which seems right according to the logic (before and after), but maybe
> not intentional?
>
>    $ cat /proc/self/cgroup
>    0::/1
>
> It's "-" if the file couldn't be opened, but "  " if the file exists but
> doesn't start with ":cpuset:/"? And then also "  " for
> "system-background"...

yeah, i noticed that last time i touched this code and left it because
i thought it was potentially non-useless given that i spend my life
remotely debugging unreproducible things and the distinction might be
a useful clue. but, no, i have no concrete use for this.

note that the strstart() part is wrong because there's an integer
before ":cpuset:/" which is why there was a strstr() rather than a
strcmp() in the original.

this modified version works correctly though:

diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index 0e95c9eb..f1d45562 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -861,17 +861,12 @@ static int get_ps(struct dirtree *new)
     sprintf(buf, "/proc/%lld/cgroup", slot[SLOT_tid]);
     if ((fp = fopen(buf, "re"))) {
       char *s, *line;
-      while ((line = xgetline(fp))) {
-        if ((s = strstr(line, ":cpuset:/"))) {
+      while ((s = line = xgetline(fp))) {
+        if ((s = strstr(s, ":cpuset:/"))) {
           s += strlen(":cpuset:/");
-          if (!*s || !strcmp(s, "foreground")) strcpy(tb->pcy, "fg");
-          else if (!strcmp(s, "system-background")) strcpy(tb->pcy, "  ");
-          else if (!strcmp(s, "background")) strcpy(tb->pcy, "bg");
-          else if (!strcmp(s, "top-app")) strcpy(tb->pcy, "ta");
-          else if (!strcmp(s, "restricted")) strcpy(tb->pcy, "rs");
-          else if (!strcmp(s, "foreground_window")) strcpy(tb->pcy, "wi");
-          else if (!strcmp(s, "camera-daemon")) strcpy(tb->pcy, "cd");
-          else strcpy(tb->pcy, "?");
+          sprintf(tb->pcy, "%.2s","? fgfg  bgtarswicd"+2*anystr(s, (char *[]){
+            "", "foreground", "system-background", "background", "top-app",
+            "restricted", "foreground_window", "camera-daemon", 0}));
         }
         free(line);
       }


> Rob
>
> (It just trying to genericize a function already used in sh.c. The idx+1
> or 0 return instead of idx or -1 is a bit different than other lib
> entries, but every user so far either cares about true/false or is using
> it for a lookup table like this that has a "not found" entry so I'd be
> adding +1 to every caller to get the logic I want. The "anystr" vs
> "strany" name is a bit of cognitive friction to remind about the
> semantic difference, still not sure whether that's a good idea or not.
> Again, "hard because the stakes are so small": as with Bill Door
> sharpening his scythe, the tricky bits were the later passes...)


More information about the Toybox mailing list