1 From 02d849739e15552046128abb8dba79c584266f61 Mon Sep 17 00:00:00 2001
2 From: Alexander Shishkin <ext-alexander.shishkin@nokia.com>
3 Date: Fri, 6 Mar 2009 15:24:52 +0200
4 Subject: [PATCH] pidof/killall5: fix DNA problems of hald children and the like
6 Children of hal daemon make a perfect example of why reading
7 /proc/$pid/exe symlink is about the only reliable way to figure
8 if the process is what we're actually looking for: they clobber
9 their argv[0] and argv[1].
11 Signed-off-by: Alexander Shishkin <ext-alexander.shishkin@nokia.com>
13 include/libbb.h | 4 +++-
14 libbb/find_pid_by_name.c | 6 +++++-
15 libbb/procps.c | 6 ++++++
16 3 files changed, 14 insertions(+), 2 deletions(-)
18 diff --git a/include/libbb.h b/include/libbb.h
19 index 859b3bc..4f75d96 100644
22 @@ -1101,6 +1101,8 @@ typedef struct procps_status_t {
23 * (if executable is symlink or script, it is NOT replaced
24 * by link target or interpreter name) */
26 + /* where /proc/$PID/exe points to */
28 /* user/group? - use passwd/group parsing functions */
31 @@ -1112,7 +1114,7 @@ enum {
33 /* PSSCAN_CMD = 1 << 6, - use read_cmdline instead */
34 PSSCAN_ARGV0 = 1 << 7,
35 - /* PSSCAN_EXE = 1 << 8, - not implemented */
36 + PSSCAN_EXE = 1 << 8, /* almost implemented */
37 PSSCAN_STATE = 1 << 9,
40 diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
41 index 8dcdb13..832116d 100644
42 --- a/libbb/find_pid_by_name.c
43 +++ b/libbb/find_pid_by_name.c
44 @@ -55,7 +55,7 @@ pid_t* find_pid_by_name(const char* procName)
45 procps_status_t* p = NULL;
47 pidList = xmalloc(sizeof(*pidList));
48 - while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0))) {
49 + while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0|PSSCAN_EXE))) {
51 /* we require comm to match and to not be truncated */
52 /* in Linux, if comm is 15 chars, it may be a truncated
53 @@ -63,11 +63,15 @@ pid_t* find_pid_by_name(const char* procName)
54 (!p->comm[sizeof(p->comm)-2] && strcmp(p->comm, procName) == 0)
55 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
56 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
57 + || (p->exe && strcmp(bb_basename(p->exe), procName) == 0)
58 /* TOOD: we can also try /proc/NUM/exe link, do we want that? */
60 pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
61 pidList[i++] = p->pid;
69 diff --git a/libbb/procps.c b/libbb/procps.c
70 index 8946917..f33c64e 100644
73 @@ -378,6 +378,12 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
77 + if (flags & (PSSCAN_EXE)) {
78 + strcpy(filename_tail, "/exe");
80 + sp->exe = xmalloc_readlink(filename);
83 if (flags & (PSSCAN_ARGV0|PSSCAN_ARGVN)) {