synchronize history patches with busybox git
[busybox-power] / debian / patches / obselete / pidof-fix.patch
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
5
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].
10
11 Signed-off-by: Alexander Shishkin <ext-alexander.shishkin@nokia.com>
12 ---
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(-)
17
18 diff --git a/include/libbb.h b/include/libbb.h
19 index 859b3bc..4f75d96 100644
20 --- a/include/libbb.h
21 +++ b/include/libbb.h
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) */
25         char comm[COMM_LEN];
26 +       /* where /proc/$PID/exe points to */
27 +       char *exe;
28         /* user/group? - use passwd/group parsing functions */
29  } procps_status_t;
30  enum {
31 @@ -1112,7 +1114,7 @@ enum {
32         PSSCAN_COMM     = 1 << 5,
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,
38         PSSCAN_VSZ      = 1 << 10,
39         PSSCAN_RSS      = 1 << 11,
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;
46  
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))) {
50                 if (
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? */
59                 ) {
60                         pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
61                         pidList[i++] = p->pid;
62                 }
63 +
64 +               if (p->exe)
65 +                       free(p->exe);
66         }
67  
68         pidList[i] = 0;
69 diff --git a/libbb/procps.c b/libbb/procps.c
70 index 8946917..f33c64e 100644
71 --- a/libbb/procps.c
72 +++ b/libbb/procps.c
73 @@ -378,6 +378,12 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
74                         }
75                 }
76  #else
77 +               if (flags & (PSSCAN_EXE)) {
78 +                       strcpy(filename_tail, "/exe");
79 +
80 +                       sp->exe = xmalloc_readlink(filename);
81 +               }
82 +
83                 if (flags & (PSSCAN_ARGV0|PSSCAN_ARGVN)) {
84                         free(sp->argv0);
85                         sp->argv0 = NULL;
86 -- 
87 1.6.1.3
88