[PATCH] pidof: use /proc/$PID/exe when nothing else works
Alexander Shishkin
virtuoso at slind.org
Tue Jul 28 00:19:12 UTC 2009
Found myself in a need of better pidof than is currently
available. This helps find tricky things (hald addons are
closest examples) that can't be found otherwise.
Signed-off-by: Alexander Shishkin <virtuoso at slind.org>
---
include/libbb.h | 3 ++-
libbb/find_pid_by_name.c | 4 ++--
libbb/procps.c | 10 ++++++++++
3 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/include/libbb.h b/include/libbb.h
index c7b4de1..51ac69e 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1280,6 +1280,7 @@ typedef struct procps_status_t {
/* Fields are set to 0/NULL if failed to determine (or not requested) */
uint16_t argv_len;
char *argv0;
+ char *exe;
IF_SELINUX(char *context;)
/* Everything below must contain no ptrs to malloc'ed data:
* it is memset(0) for each process in procps_scan() */
@@ -1327,7 +1328,7 @@ enum {
PSSCAN_COMM = 1 << 5,
/* PSSCAN_CMD = 1 << 6, - use read_cmdline instead */
PSSCAN_ARGV0 = 1 << 7,
- /* PSSCAN_EXE = 1 << 8, - not implemented */
+ PSSCAN_EXE = 1 << 8,
PSSCAN_STATE = 1 << 9,
PSSCAN_VSZ = 1 << 10,
PSSCAN_RSS = 1 << 11,
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
index 600d4e1..6a57fb1 100644
--- a/libbb/find_pid_by_name.c
+++ b/libbb/find_pid_by_name.c
@@ -83,11 +83,11 @@ pid_t* FAST_FUNC find_pid_by_name(const char *procName)
procps_status_t* p = NULL;
pidList = xzalloc(sizeof(*pidList));
- while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN))) {
+ while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN|PSSCAN_EXE))) {
if (comm_match(p, procName)
/* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
|| (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
- /* TODO: we can also try /proc/NUM/exe link, do we want that? */
+ || (p->exe && strcmp(bb_basename(p->exe), procName) == 0)
) {
pidList = xrealloc_vector(pidList, 2, i);
pidList[i++] = p->pid;
diff --git a/libbb/procps.c b/libbb/procps.c
index 6e122c4..db2a216 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -111,6 +111,8 @@ void FAST_FUNC free_procps_scan(procps_status_t* sp)
{
closedir(sp->dir);
free(sp->argv0);
+ if (sp->exe)
+ free(sp->exe);
IF_SELINUX(free(sp->context);)
free(sp);
}
@@ -450,6 +452,14 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
sp->argv0 = xstrdup(buf);
}
}
+
+ if (flags & PSSCAN_EXE) {
+ if (sp->exe)
+ free(sp->exe);
+
+ strcpy(filename_tail, "/exe");
+ sp->exe = xmalloc_readlink(filename);
+ }
#endif
break;
}
--
1.6.3.3
More information about the busybox
mailing list