[Toybox] ps: only stat() /proc/<pid>

Mark Salyzyn salyzyn at android.com
Wed Oct 26 13:14:26 PDT 2016


>From the AOSP gerrit fixing internal bug 32399196
( https://android-review.googlesource.com/#/c/295732 )

Change subject: dirtree: add DIRTREE_NOSTAT flag

Callback from top /proc/ node tells dirtree to hold off performing
a fstatat() call until after we hare satisfied with the name being
numerical.  Affects ps and pgrep superfluous calls.

Test: ps, logcat -b events -d -s auditd and check for
      selinux getattr for /proc/iomem or /proc/sysrq-trigger.
Change-Id: Ib7d64601ed9db90ff877a4ad0856f20e9fa15398
---
 toys/posix/ps.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index 15acc84..25b06c4 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -615,7 +615,7 @@ static int get_ps(struct dirtree *new)
 
   // Recurse one level into /proc children, skip non-numeric entries
   if (!new->parent)
-    return DIRTREE_RECURSE|DIRTREE_SHUTUP
+    return DIRTREE_RECURSE|DIRTREE_SHUTUP|DIRTREE_NOSTAT
       |(DIRTREE_SAVE*(TT.threadparent||!TT.show_process));
 
   memset(slot, 0, sizeof(tb->slot));
@@ -623,6 +623,8 @@ static int get_ps(struct dirtree *new)
   if (TT.threadparent && TT.threadparent->extra)
     if (*slot == *(((struct carveup *)TT.threadparent->extra)->slot)) return 0;
   fd = dirtree_parentfd(new);
+  // Implied policy is DIRTREE_SHUTUP, no need for error checking.
+  fstatat(fd, new->name, &new->st, AT_SYMLINK_NOFOLLOW);
 
   len = 2048;
   sprintf(buf, "%lld/stat", *slot);




More information about the Toybox mailing list