diff options
author | Rob Landley <rob@landley.net> | 2016-12-08 21:29:00 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2016-12-08 21:29:00 -0600 |
commit | a975952d885c47aeca013e76194142a47e96d49a (patch) | |
tree | dad2f585a2d9a338fdf721f492bff57d4e68e8c4 | |
parent | e82d6d14fcc272041e578e8d0a2e342ee65365b3 (diff) | |
download | toybox-a975952d885c47aeca013e76194142a47e96d49a.tar.gz |
Add DIRTREE_PROC to skip non-numeric entries and make ps/top etc use it.
-rw-r--r-- | lib/dirtree.c | 1 | ||||
-rw-r--r-- | lib/lib.h | 2 | ||||
-rw-r--r-- | toys/posix/ps.c | 13 |
3 files changed, 9 insertions, 7 deletions
diff --git a/lib/dirtree.c b/lib/dirtree.c index 07c1cdc7..f9524a5d 100644 --- a/lib/dirtree.c +++ b/lib/dirtree.c @@ -151,6 +151,7 @@ int dirtree_recurse(struct dirtree *node, // The extra parentheses are to shut the stupid compiler up. while ((entry = readdir(dir))) { + if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue; if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue; new = dirtree_handle_callback(new, callback); if (new == DIRTREE_ABORTVAL) break; @@ -73,6 +73,8 @@ void get_optflags(void); #define DIRTREE_SHUTUP 16 // Breadth first traversal, conserves filehandles at the expense of memory #define DIRTREE_BREADTH 32 +// skip non-numeric entries +#define DIRTREE_PROC 64 // Don't look at any more files in this directory. #define DIRTREE_ABORT 256 diff --git a/toys/posix/ps.c b/toys/posix/ps.c index 77d777bd..3a3b8c11 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -620,7 +620,7 @@ static int get_ps(struct dirtree *new) |(DIRTREE_SAVE*(TT.threadparent||!TT.show_process)); memset(slot, 0, sizeof(tb->slot)); - if (!(tb->slot[SLOT_tid] = *slot = atol(new->name))) return 0; + tb->slot[SLOT_tid] = *slot = atol(new->name); if (TT.threadparent && TT.threadparent->extra) if (*slot == *(((struct carveup *)TT.threadparent->extra)->slot)) return 0; fd = dirtree_parentfd(new); @@ -870,8 +870,7 @@ static int get_threads(struct dirtree *new) unsigned pid, kcount; if (!new->parent) return get_ps(new); - - if (!(pid = atol(new->name))) return 0; + pid = atol(new->name); TT.threadparent = new; if (!get_ps(new)) { @@ -884,7 +883,7 @@ static int get_threads(struct dirtree *new) // Disable show_process at least until we can calculate tcount kcount = TT.kcount; sprintf(toybuf, "/proc/%u/task", pid); - new->child = dirtree_flagread(toybuf, DIRTREE_SHUTUP, get_ps); + new->child = dirtree_flagread(toybuf, DIRTREE_SHUTUP|DIRTREE_PROC, get_ps); TT.threadparent = 0; kcount = TT.kcount-kcount+1; tb = (void *)new->extra; @@ -1218,7 +1217,7 @@ void ps_main(void) if (!(toys.optflags&FLAG_M)) printf("%.*s\n", TT.width, toybuf); if (!(toys.optflags&(FLAG_k|FLAG_M))) TT.show_process = show_ps; TT.match_process = ps_match_process; - dt = dirtree_read("/proc", + dt = dirtree_flagread("/proc", DIRTREE_SHUTUP|DIRTREE_PROC, ((toys.optflags&FLAG_T) || (TT.bits&(_PS_TID|_PS_TCNT))) ? get_threads : get_ps); @@ -1348,7 +1347,7 @@ static void top_common( plold = plist+(tock++&1); plnew = plist+(tock&1); plnew->whence = millitime(); - dt = dirtree_read("/proc", + dt = dirtree_flagread("/proc", DIRTREE_SHUTUP|DIRTREE_PROC, ((toys.optflags&FLAG_H) || (TT.bits&(_PS_TID|_PS_TCNT))) ? get_threads : get_ps); plnew->tb = collate(plnew->count = TT.kcount, dt); @@ -1751,7 +1750,7 @@ void pgrep_main(void) // pgrep should return failure if there are no matches. toys.exitval = 1; - dirtree_read("/proc", get_ps); + dirtree_flagread("/proc", DIRTREE_SHUTUP|DIRTREE_PROC, get_ps); if (toys.optflags&FLAG_c) printf("%d\n", TT.sortpos); if (TT.pgrep.snapshot) { do_pgk(TT.pgrep.snapshot); |