summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-06-30 08:03:26 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-06-30 08:03:26 +0000
commitf7d07b1723c15ee818f0c1f5cce96c55274024a6 (patch)
treeb7f847c25ce3705d315ce4f0ac1b7976fae58cc6
parent42ee26d00cc9c9a6cf1f652a5351b30ac221fa34 (diff)
downloadbusybox-w32-f7d07b1723c15ee818f0c1f5cce96c55274024a6.tar.gz
busybox-w32-f7d07b1723c15ee818f0c1f5cce96c55274024a6.tar.bz2
busybox-w32-f7d07b1723c15ee818f0c1f5cce96c55274024a6.zip
killall, pidof: use argv0 for process matching too
top: show cmdline, not comm field (fixes problems with re-execed applets showing as processes with name "exe", and not being found by pidof/killall by applet name) function old new delta find_pid_by_name 98 156 +58 procps_scan 692 732 +40 top_main 2724 2762 +38 find_pair 164 180 +16 collect_int 114 123 +9 cmp_main 547 555 +8 collect_fork 112 119 +7 collect_ctx 112 119 +7 read_package_field 253 257 +4 passwd_main 1983 1985 +2 process_stdin 435 433 -2 xstrtoul_range_sfx 229 226 -3 get_next_block 1852 1849 -3 arith 2042 2033 -9 sv_main 1236 1226 -10 singlemount 4690 4672 -18 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 10/6 up/down: 189/-45) Total: 144 bytes text data bss dec hex filename 734789 3028 14400 752217 b7a59 busybox_old 734933 3028 14400 752361 b7ae9 busybox_unstripped
-rw-r--r--include/libbb.h18
-rw-r--r--libbb/find_pid_by_name.c20
-rw-r--r--libbb/procps.c37
-rw-r--r--procps/top.c12
4 files changed, 60 insertions, 27 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 124b11fa8..e80e76403 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -836,6 +836,8 @@ typedef struct {
836 DIR *dir; 836 DIR *dir;
837/* Fields are set to 0/NULL if failed to determine (or not requested) */ 837/* Fields are set to 0/NULL if failed to determine (or not requested) */
838 char *cmd; 838 char *cmd;
839 char *argv0;
840 /*char *exe;*/
839 USE_SELINUX(char *context;) 841 USE_SELINUX(char *context;)
840 /* Everything below must contain no ptrs to malloc'ed data: 842 /* Everything below must contain no ptrs to malloc'ed data:
841 * it is memset(0) for each process in procps_scan() */ 843 * it is memset(0) for each process in procps_scan() */
@@ -861,13 +863,15 @@ enum {
861 PSSCAN_UIDGID = 1 << 4, 863 PSSCAN_UIDGID = 1 << 4,
862 PSSCAN_COMM = 1 << 5, 864 PSSCAN_COMM = 1 << 5,
863 PSSCAN_CMD = 1 << 6, 865 PSSCAN_CMD = 1 << 6,
864 PSSCAN_STATE = 1 << 7, 866 PSSCAN_ARGV0 = 1 << 7,
865 PSSCAN_VSZ = 1 << 8, 867 PSSCAN_EXE = 1 << 8, /* not implemented yet */
866 PSSCAN_RSS = 1 << 9, 868 PSSCAN_STATE = 1 << 9,
867 PSSCAN_STIME = 1 << 10, 869 PSSCAN_VSZ = 1 << 10,
868 PSSCAN_UTIME = 1 << 11, 870 PSSCAN_RSS = 1 << 11,
869 PSSCAN_TTY = 1 << 12, 871 PSSCAN_STIME = 1 << 12,
870 USE_SELINUX(PSSCAN_CONTEXT = 1 << 13,) 872 PSSCAN_UTIME = 1 << 13,
873 PSSCAN_TTY = 1 << 14,
874 USE_SELINUX(PSSCAN_CONTEXT = 1 << 15,)
871 /* These are all retrieved from proc/NN/stat in one go: */ 875 /* These are all retrieved from proc/NN/stat in one go: */
872 PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID 876 PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
873 | PSSCAN_COMM | PSSCAN_STATE 877 | PSSCAN_COMM | PSSCAN_STATE
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
index 13ccb545d..cfc5b3468 100644
--- a/libbb/find_pid_by_name.c
+++ b/libbb/find_pid_by_name.c
@@ -38,6 +38,14 @@ execXXX("/proc/self/exe", applet_name, params....)
38and therefore comm field contains "exe". 38and therefore comm field contains "exe".
39*/ 39*/
40 40
41static const char *bb_basename(const char *name)
42{
43 const char *cp = strrchr(name, '/');
44 if (cp)
45 return cp + 1;
46 return name;
47}
48
41/* find_pid_by_name() 49/* find_pid_by_name()
42 * 50 *
43 * Modified by Vladimir Oleynik for use with libbb/procps.c 51 * Modified by Vladimir Oleynik for use with libbb/procps.c
@@ -55,8 +63,16 @@ pid_t* find_pid_by_name(const char* procName)
55 procps_status_t* p = NULL; 63 procps_status_t* p = NULL;
56 64
57 pidList = xmalloc(sizeof(*pidList)); 65 pidList = xmalloc(sizeof(*pidList));
58 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM))) { 66 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0))) {
59 if (strncmp(p->comm, procName, sizeof(p->comm)-1) == 0) { 67 if (
68 /* we require comm to match and to not be truncated */
69 /* in Linux, if comm is 15 chars, it may be a truncated
70 * name, so we don't allow that to match */
71 (!p->comm[sizeof(p->comm)-2] && strcmp(p->comm, procName) == 0)
72 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
73 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
74 /* TOOD: we can also try exe, do we want that? */
75 ) {
60 pidList = xrealloc(pidList, sizeof(*pidList) * (i+2)); 76 pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
61 pidList[i++] = p->pid; 77 pidList[i++] = p->pid;
62 } 78 }
diff --git a/libbb/procps.c b/libbb/procps.c
index 5924d60a8..8413ce8a1 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -92,7 +92,7 @@ static int read_to_buf(const char *filename, void *buf)
92 return ret; 92 return ret;
93} 93}
94 94
95procps_status_t* alloc_procps_scan(int flags) 95procps_status_t *alloc_procps_scan(int flags)
96{ 96{
97 procps_status_t* sp = xzalloc(sizeof(procps_status_t)); 97 procps_status_t* sp = xzalloc(sizeof(procps_status_t));
98 sp->dir = xopendir("/proc"); 98 sp->dir = xopendir("/proc");
@@ -133,7 +133,7 @@ static char *skip_fields(char *str, int count)
133#endif 133#endif
134 134
135void BUG_comm_size(void); 135void BUG_comm_size(void);
136procps_status_t* procps_scan(procps_status_t* sp, int flags) 136procps_status_t *procps_scan(procps_status_t* sp, int flags)
137{ 137{
138 struct dirent *entry; 138 struct dirent *entry;
139 char buf[PROCPS_BUFSIZE]; 139 char buf[PROCPS_BUFSIZE];
@@ -266,24 +266,31 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
266 266
267 } 267 }
268 268
269 if (flags & PSSCAN_CMD) { 269 if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
270 free(sp->cmd); 270 if (sp->argv0) {
271 sp->cmd = NULL; 271 free(sp->argv0);
272 sp->argv0 = NULL;
273 }
274 if (sp->cmd) {
275 free(sp->cmd);
276 sp->cmd = NULL;
277 }
272 strcpy(filename_tail, "/cmdline"); 278 strcpy(filename_tail, "/cmdline");
279 /* TODO: to get rid of size limits, read into malloc buf,
280 * then realloc it down to real size. */
273 n = read_to_buf(filename, buf); 281 n = read_to_buf(filename, buf);
274 if (n <= 0) 282 if (n <= 0)
275 break; 283 break;
276 if (buf[n-1] == '\n') { 284 if (flags & PSSCAN_ARGV0)
277 if (!--n) 285 sp->argv0 = xstrdup(buf);
278 break; 286 if (flags & PSSCAN_CMD) {
279 buf[n] = '\0'; 287 do {
288 n--;
289 if ((unsigned char)(buf[n]) < ' ')
290 buf[n] = ' ';
291 } while (n);
292 sp->cmd = xstrdup(buf);
280 } 293 }
281 do {
282 n--;
283 if ((unsigned char)(buf[n]) < ' ')
284 buf[n] = ' ';
285 } while (n);
286 sp->cmd = xstrdup(buf);
287 } 294 }
288 break; 295 break;
289 } 296 }
diff --git a/procps/top.c b/procps/top.c
index c6efe2d77..136f1404a 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -40,7 +40,8 @@ typedef struct top_status_t {
40 unsigned pid, ppid; 40 unsigned pid, ppid;
41 unsigned uid; 41 unsigned uid;
42 char state[4]; 42 char state[4];
43 char comm[COMM_LEN]; 43/* TODO: read /proc/$PID/cmdline only for processes which are displayed */
44 char cmd[64];
44} top_status_t; 45} top_status_t;
45 46
46typedef struct jiffy_counts_t{ 47typedef struct jiffy_counts_t{
@@ -444,7 +445,7 @@ static void display_status(int count, int scr_width)
444#endif 445#endif
445 ); 446 );
446 if (col > 0) 447 if (col > 0)
447 printf("%.*s", col, s->comm); 448 printf("%.*s", col, s->cmd);
448 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, 449 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
449 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */ 450 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
450 s++; 451 s++;
@@ -559,6 +560,7 @@ int top_main(int argc, char **argv)
559 | PSSCAN_UTIME 560 | PSSCAN_UTIME
560 | PSSCAN_STATE 561 | PSSCAN_STATE
561 | PSSCAN_COMM 562 | PSSCAN_COMM
563 | PSSCAN_CMD
562 | PSSCAN_SID 564 | PSSCAN_SID
563 | PSSCAN_UIDGID 565 | PSSCAN_UIDGID
564 ))) { 566 ))) {
@@ -572,7 +574,10 @@ int top_main(int argc, char **argv)
572#endif 574#endif
573 top[n].uid = p->uid; 575 top[n].uid = p->uid;
574 strcpy(top[n].state, p->state); 576 strcpy(top[n].state, p->state);
575 strcpy(top[n].comm, p->comm); 577 if (p->cmd)
578 safe_strncpy(top[n].cmd, p->cmd, sizeof(top[n].cmd));
579 else /* mimic ps */
580 sprintf(top[n].cmd, "[%s]", p->comm);
576 } 581 }
577 if (ntop == 0) { 582 if (ntop == 0) {
578 bb_error_msg_and_die("no process info in /proc"); 583 bb_error_msg_and_die("no process info in /proc");
@@ -585,6 +590,7 @@ int top_main(int argc, char **argv)
585 continue; 590 continue;
586 } 591 }
587 do_stats(); 592 do_stats();
593/* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
588 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp); 594 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
589#else 595#else
590 qsort(top, ntop, sizeof(top_status_t), (void*)sort_function); 596 qsort(top, ntop, sizeof(top_status_t), (void*)sort_function);