From f7d07b1723c15ee818f0c1f5cce96c55274024a6 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sat, 30 Jun 2007 08:03:26 +0000 Subject: 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 --- libbb/find_pid_by_name.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'libbb/find_pid_by_name.c') 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....) and therefore comm field contains "exe". */ +static const char *bb_basename(const char *name) +{ + const char *cp = strrchr(name, '/'); + if (cp) + return cp + 1; + return name; +} + /* find_pid_by_name() * * Modified by Vladimir Oleynik for use with libbb/procps.c @@ -55,8 +63,16 @@ pid_t* find_pid_by_name(const char* procName) procps_status_t* p = NULL; pidList = xmalloc(sizeof(*pidList)); - while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM))) { - if (strncmp(p->comm, procName, sizeof(p->comm)-1) == 0) { + while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0))) { + if ( + /* we require comm to match and to not be truncated */ + /* in Linux, if comm is 15 chars, it may be a truncated + * name, so we don't allow that to match */ + (!p->comm[sizeof(p->comm)-2] && strcmp(p->comm, procName) == 0) + /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/ + || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0) + /* TOOD: we can also try exe, do we want that? */ + ) { pidList = xrealloc(pidList, sizeof(*pidList) * (i+2)); pidList[i++] = p->pid; } -- cgit v1.2.3