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.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
index 600d4e1a8..52a0c6dab 100644
--- a/libbb/find_pid_by_name.c
+++ b/libbb/find_pid_by_name.c
@@ -41,18 +41,20 @@ and therefore comm field contains "exe".
41static int comm_match(procps_status_t *p, const char *procName) 41static int comm_match(procps_status_t *p, const char *procName)
42{ 42{
43 int argv1idx; 43 int argv1idx;
44 const char *argv1;
44 45
45 /* comm does not match */
46 if (strncmp(p->comm, procName, 15) != 0) 46 if (strncmp(p->comm, procName, 15) != 0)
47 return 0; 47 return 0; /* comm does not match */
48 48
49 /* in Linux, if comm is 15 chars, it may be a truncated */ 49 /* In Linux, if comm is 15 chars, it is truncated.
50 if (p->comm[14] == '\0') /* comm is not truncated - match */ 50 * (or maybe the name was exactly 15 chars, but there is
51 return 1; 51 * no way to know that) */
52 if (p->comm[14] == '\0')
53 return 1; /* comm is not truncated - matches */
52 54
53 /* comm is truncated, but first 15 chars match. 55 /* comm is truncated, but first 15 chars match.
54 * This can be crazily_long_script_name.sh! 56 * This can be crazily_long_script_name.sh!
55 * The telltale sign is basename(argv[1]) == procName. */ 57 * The telltale sign is basename(argv[1]) == procName */
56 58
57 if (!p->argv0) 59 if (!p->argv0)
58 return 0; 60 return 0;
@@ -60,8 +62,9 @@ static int comm_match(procps_status_t *p, const char *procName)
60 argv1idx = strlen(p->argv0) + 1; 62 argv1idx = strlen(p->argv0) + 1;
61 if (argv1idx >= p->argv_len) 63 if (argv1idx >= p->argv_len)
62 return 0; 64 return 0;
65 argv1 = p->argv0 + argv1idx;
63 66
64 if (strcmp(bb_basename(p->argv0 + argv1idx), procName) != 0) 67 if (strcmp(bb_basename(argv1), procName) != 0)
65 return 0; 68 return 0;
66 69
67 return 1; 70 return 1;
@@ -83,11 +86,12 @@ pid_t* FAST_FUNC find_pid_by_name(const char *procName)
83 procps_status_t* p = NULL; 86 procps_status_t* p = NULL;
84 87
85 pidList = xzalloc(sizeof(*pidList)); 88 pidList = xzalloc(sizeof(*pidList));
86 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN))) { 89 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN|PSSCAN_EXE))) {
87 if (comm_match(p, procName) 90 if (comm_match(p, procName)
88 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/ 91 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
89 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0) 92 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
90 /* TODO: we can also try /proc/NUM/exe link, do we want that? */ 93 /* or we require /proc/PID/exe link to match */
94 || (p->exe && strcmp(bb_basename(p->exe), procName) == 0)
91 ) { 95 ) {
92 pidList = xrealloc_vector(pidList, 2, i); 96 pidList = xrealloc_vector(pidList, 2, i);
93 pidList[i++] = p->pid; 97 pidList[i++] = p->pid;