aboutsummaryrefslogtreecommitdiff
path: root/libbb
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 /libbb
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
Diffstat (limited to 'libbb')
-rw-r--r--libbb/find_pid_by_name.c20
-rw-r--r--libbb/procps.c37
2 files changed, 40 insertions, 17 deletions
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 }