aboutsummaryrefslogtreecommitdiff
path: root/libbb/find_pid_by_name.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-07-17 18:39:36 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-07-17 18:39:36 +0000
commit3b3ca113ed00a6781a28172bb3a6860a5f79ea02 (patch)
tree7834909b21af2b0fdbb7c3c52dc3fef12ba0c051 /libbb/find_pid_by_name.c
parent18875bf772d72c9c543be8f9ab46dec451f547ea (diff)
downloadbusybox-w32-3b3ca113ed00a6781a28172bb3a6860a5f79ea02.tar.gz
busybox-w32-3b3ca113ed00a6781a28172bb3a6860a5f79ea02.tar.bz2
busybox-w32-3b3ca113ed00a6781a28172bb3a6860a5f79ea02.zip
pidof/killall: allow find_pid_by_name to find running
processes started as scripts_with_name_longer_than_15_bytes.sh closes bug 4054 (and is generally neat)
Diffstat (limited to 'libbb/find_pid_by_name.c')
-rw-r--r--libbb/find_pid_by_name.c43
1 files changed, 34 insertions, 9 deletions
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
index ae2f11643..2ee423cb7 100644
--- a/libbb/find_pid_by_name.c
+++ b/libbb/find_pid_by_name.c
@@ -38,6 +38,35 @@ execXXX("/proc/self/exe", applet_name, params....)
38and therefore comm field contains "exe". 38and therefore comm field contains "exe".
39*/ 39*/
40 40
41static int comm_match(procps_status_t *p, const char *procName)
42{
43 int argv1idx;
44
45 /* comm does not match */
46 if (strncmp(p->comm, procName, 15) != 0)
47 return 0;
48
49 /* in Linux, if comm is 15 chars, it may be a truncated */
50 if (p->comm[14] == '\0') /* comm is not truncated - match */
51 return 1;
52
53 /* comm is truncated, but first 15 chars match.
54 * This can be crazily_long_script_name.sh!
55 * The telltale sign is basename(argv[1]) == procName. */
56
57 if (!p->argv0)
58 return 0;
59
60 argv1idx = strlen(p->argv0) + 1;
61 if (argv1idx >= p->argv_len)
62 return 0;
63
64 if (strcmp(bb_basename(p->argv0 + argv1idx), procName) != 0)
65 return 0;
66
67 return 1;
68}
69
41/* find_pid_by_name() 70/* find_pid_by_name()
42 * 71 *
43 * Modified by Vladimir Oleynik for use with libbb/procps.c 72 * Modified by Vladimir Oleynik for use with libbb/procps.c
@@ -48,24 +77,20 @@ and therefore comm field contains "exe".
48 * Returns a list of all matching PIDs 77 * Returns a list of all matching PIDs
49 * It is the caller's duty to free the returned pidlist. 78 * It is the caller's duty to free the returned pidlist.
50 */ 79 */
51pid_t* FAST_FUNC find_pid_by_name(const char* procName) 80pid_t* FAST_FUNC find_pid_by_name(const char *procName)
52{ 81{
53 pid_t* pidList; 82 pid_t* pidList;
54 int i = 0; 83 int i = 0;
55 procps_status_t* p = NULL; 84 procps_status_t* p = NULL;
56 85
57 pidList = xmalloc(sizeof(*pidList)); 86 pidList = xzalloc(sizeof(*pidList));
58 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0))) { 87 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN))) {
59 if ( 88 if (comm_match(p, procName)
60 /* we require comm to match and to not be truncated */
61 /* in Linux, if comm is 15 chars, it may be a truncated
62 * name, so we don't allow that to match */
63 (!p->comm[sizeof(p->comm)-2] && strcmp(p->comm, procName) == 0)
64 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/ 89 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
65 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0) 90 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
66 /* TOOD: we can also try /proc/NUM/exe link, do we want that? */ 91 /* TOOD: we can also try /proc/NUM/exe link, do we want that? */
67 ) { 92 ) {
68 pidList = xrealloc(pidList, sizeof(*pidList) * (i+2)); 93 pidList = xrealloc_vector(pidList, 2, i);
69 pidList[i++] = p->pid; 94 pidList[i++] = p->pid;
70 } 95 }
71 } 96 }