aboutsummaryrefslogtreecommitdiff
path: root/libbb/find_pid_by_name.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/find_pid_by_name.c')
-rw-r--r--libbb/find_pid_by_name.c20
1 files changed, 18 insertions, 2 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 }