diff options
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/find_pid_by_name.c | 20 | ||||
-rw-r--r-- | libbb/procps.c | 37 |
2 files changed, 40 insertions, 17 deletions
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c index 13ccb54..cfc5b34 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; } diff --git a/libbb/procps.c b/libbb/procps.c index 5924d60..8413ce8 100644 --- a/libbb/procps.c +++ b/libbb/procps.c @@ -92,7 +92,7 @@ static int read_to_buf(const char *filename, void *buf) return ret; } -procps_status_t* alloc_procps_scan(int flags) +procps_status_t *alloc_procps_scan(int flags) { procps_status_t* sp = xzalloc(sizeof(procps_status_t)); sp->dir = xopendir("/proc"); @@ -133,7 +133,7 @@ static char *skip_fields(char *str, int count) #endif void BUG_comm_size(void); -procps_status_t* procps_scan(procps_status_t* sp, int flags) +procps_status_t *procps_scan(procps_status_t* sp, int flags) { struct dirent *entry; char buf[PROCPS_BUFSIZE]; @@ -266,24 +266,31 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags) } - if (flags & PSSCAN_CMD) { - free(sp->cmd); - sp->cmd = NULL; + if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) { + if (sp->argv0) { + free(sp->argv0); + sp->argv0 = NULL; + } + if (sp->cmd) { + free(sp->cmd); + sp->cmd = NULL; + } strcpy(filename_tail, "/cmdline"); + /* TODO: to get rid of size limits, read into malloc buf, + * then realloc it down to real size. */ n = read_to_buf(filename, buf); if (n <= 0) break; - if (buf[n-1] == '\n') { - if (!--n) - break; - buf[n] = '\0'; + if (flags & PSSCAN_ARGV0) + sp->argv0 = xstrdup(buf); + if (flags & PSSCAN_CMD) { + do { + n--; + if ((unsigned char)(buf[n]) < ' ') + buf[n] = ' '; + } while (n); + sp->cmd = xstrdup(buf); } - do { - n--; - if ((unsigned char)(buf[n]) < ' ') - buf[n] = ' '; - } while (n); - sp->cmd = xstrdup(buf); } break; } |