Remove the name filtering from toolbox ps.
Regular ps doesn't have this, and at least in our tree everyone
seems to actually pipe to grep instead of using this anyway. But
since the switch to a more full-featured ps is going to be a nightmare
anyway, let's dip a toe in the water by removing something we don't plan
on supporting.
Change-Id: Id220ae0cfdad8368341730f643a2bfbaad3c9716
diff --git a/toolbox/ps.c b/toolbox/ps.c
index cf3f05a..4d84b19 100644
--- a/toolbox/ps.c
+++ b/toolbox/ps.c
@@ -41,14 +41,14 @@
static void print_exe_abi(int pid);
-static int ps_line(int pid, int tid, char *namefilter)
+static int ps_line(int pid, int tid)
{
char statline[1024];
char cmdline[1024];
char macline[1024];
char user[32];
struct stat stats;
- int fd, r;
+ int r;
char *ptr, *name, *state;
int ppid;
unsigned rss, vss;
@@ -68,7 +68,7 @@
sprintf(statline, "/proc/%d/stat", pid);
sprintf(cmdline, "/proc/%d/cmdline", pid);
snprintf(macline, sizeof(macline), "/proc/%d/attr/current", pid);
- fd = open(cmdline, O_RDONLY);
+ int fd = open(cmdline, O_RDONLY);
if(fd == 0) {
r = 0;
} else {
@@ -79,7 +79,7 @@
cmdline[r] = 0;
}
- fd = open(statline, O_RDONLY);
+ int fd = open(statline, O_RDONLY);
if(fd == 0) return -1;
r = read(fd, statline, 1023);
close(fd);
@@ -158,51 +158,49 @@
return 0;
}
- if(!namefilter || !strncmp(cmdline[0] ? cmdline : name, namefilter, strlen(namefilter))) {
- if (display_flags & SHOW_MACLABEL) {
- fd = open(macline, O_RDONLY);
- strcpy(macline, "-");
- if (fd >= 0) {
- r = read(fd, macline, sizeof(macline)-1);
- close(fd);
- if (r > 0)
- macline[r] = 0;
- }
- printf("%-30s %-9s %-5d %-5d %s\n", macline, user, pid, ppid, cmdline[0] ? cmdline : name);
- return 0;
+ if (display_flags & SHOW_MACLABEL) {
+ fd = open(macline, O_RDONLY);
+ strcpy(macline, "-");
+ if (fd >= 0) {
+ r = read(fd, macline, sizeof(macline)-1);
+ close(fd);
+ if (r > 0)
+ macline[r] = 0;
}
-
- printf("%-9s %-5d %-5d %-6d %-5d", user, pid, ppid, vss / 1024, rss * 4);
- if (display_flags & SHOW_CPU)
- printf(" %-2d", psr);
- if (display_flags & SHOW_PRIO)
- printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched);
- if (display_flags & SHOW_POLICY) {
- SchedPolicy p;
- if (get_sched_policy(pid, &p) < 0)
- printf(" un ");
- else
- printf(" %.2s ", get_sched_policy_name(p));
- }
- char path[PATH_MAX];
- snprintf(path, sizeof(path), "/proc/%d/wchan", pid);
- char wchan[10];
- int fd = open(path, O_RDONLY);
- ssize_t wchan_len = read(fd, wchan, sizeof(wchan));
- if (wchan_len == -1) {
- wchan[wchan_len = 0] = '\0';
- }
- close(fd);
- printf(" %10.*s %0*" PRIxPTR " %s ", (int) wchan_len, wchan, (int) PC_WIDTH, eip, state);
- if (display_flags & SHOW_ABI) {
- print_exe_abi(pid);
- }
- printf("%s", cmdline[0] ? cmdline : name);
- if(display_flags&SHOW_TIME)
- printf(" (u:%d, s:%d)", utime, stime);
-
- printf("\n");
+ printf("%-30s %-9s %-5d %-5d %s\n", macline, user, pid, ppid, cmdline[0] ? cmdline : name);
+ return 0;
}
+
+ printf("%-9s %-5d %-5d %-6d %-5d", user, pid, ppid, vss / 1024, rss * 4);
+ if (display_flags & SHOW_CPU)
+ printf(" %-2d", psr);
+ if (display_flags & SHOW_PRIO)
+ printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched);
+ if (display_flags & SHOW_POLICY) {
+ SchedPolicy p;
+ if (get_sched_policy(pid, &p) < 0)
+ printf(" un ");
+ else
+ printf(" %.2s ", get_sched_policy_name(p));
+ }
+ char path[PATH_MAX];
+ snprintf(path, sizeof(path), "/proc/%d/wchan", pid);
+ char wchan[10];
+ fd = open(path, O_RDONLY);
+ ssize_t wchan_len = read(fd, wchan, sizeof(wchan));
+ if (wchan_len == -1) {
+ wchan[wchan_len = 0] = '\0';
+ }
+ close(fd);
+ printf(" %10.*s %0*" PRIxPTR " %s ", (int) wchan_len, wchan, (int) PC_WIDTH, eip, state);
+ if (display_flags & SHOW_ABI) {
+ print_exe_abi(pid);
+ }
+ printf("%s", cmdline[0] ? cmdline : name);
+ if(display_flags&SHOW_TIME)
+ printf(" (u:%d, s:%d)", utime, stime);
+
+ printf("\n");
return 0;
}
@@ -240,7 +238,7 @@
}
}
-void ps_threads(int pid, char *namefilter)
+void ps_threads(int pid)
{
char tmp[128];
DIR *d;
@@ -254,7 +252,7 @@
if(isdigit(de->d_name[0])){
int tid = atoi(de->d_name);
if(tid == pid) continue;
- ps_line(pid, tid, namefilter);
+ ps_line(pid, tid);
}
}
closedir(d);
@@ -264,7 +262,6 @@
{
DIR *d;
struct dirent *de;
- char *namefilter = 0;
int pidfilter = 0;
int threads = 0;
@@ -290,12 +287,18 @@
display_flags |= SHOW_ABI;
} else if(!strcmp(argv[1],"--ppid")) {
ppid_filter = atoi(argv[2]);
+ if (ppid_filter == 0) {
+ fprintf(stderr, "bad ppid '%s'\n", argv[2]);
+ return 1;
+ }
argc--;
argv++;
- } else if(isdigit(argv[1][0])){
- pidfilter = atoi(argv[1]);
} else {
- namefilter = argv[1];
+ pidfilter = atoi(argv[1]);
+ if (pidfilter == 0) {
+ fprintf(stderr, "bad pid '%s'\n", argv[1]);
+ return 1;
+ }
}
argc--;
argv++;
@@ -315,8 +318,8 @@
if(isdigit(de->d_name[0])){
int pid = atoi(de->d_name);
if(!pidfilter || (pidfilter == pid)) {
- ps_line(pid, 0, namefilter);
- if(threads) ps_threads(pid, namefilter);
+ ps_line(pid, 0);
+ if(threads) ps_threads(pid);
}
}
}