[PATCH 1/1] procps: Strip attributes from filenames in the output of pmap

From: Mel Gorman
Date: Mon Oct 06 2008 - 05:40:33 EST


It is possible that additional attributes about a file are printed in
/proc/PID/maps such as the pagesize used to back a hugetlbfs mapping. It
is not expected that this be printed in the output of pmap. This patch
strips all attributes but (deleted) from the output of pmap. (deleted)
is left as it was historically displayed.

Signed-off-by: Mel Gorman <mel@xxxxxxxxx>
---
pmap.c | 57 +++++++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/pmap.c b/pmap.c
index a46c696..a56ee94 100644
--- a/pmap.c
+++ b/pmap.c
@@ -17,6 +17,7 @@
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
+#include <errno.h>

#include <sys/ipc.h>
#include <sys/shm.h>
@@ -93,8 +94,11 @@ out_destroy:
}


-static const char *mapping_name(proc_t *p, unsigned KLONG addr, unsigned KLONG len, const char *mapbuf, unsigned showpath, unsigned dev_major, unsigned dev_minor, unsigned long long inode){
- const char *cp;
+static const char *mapping_name(proc_t *p, unsigned KLONG addr, unsigned KLONG len, char *mapbuf, unsigned showpath, unsigned dev_major, unsigned dev_minor, unsigned long long inode){
+
+ char *cp;
+ char *cpfull;
+ const char *anon_cp;

if(!dev_major && dev_minor==shm_minor && strstr(mapbuf,"/SYSV")){
static char shmbuf[64];
@@ -102,21 +106,46 @@ static const char *mapping_name(proc_t *p, unsigned KLONG addr, unsigned KLONG l
return shmbuf;
}

- cp = strrchr(mapbuf,'/');
- if(cp){
- if(showpath) return strchr(mapbuf,'/');
- return cp[1] ? cp+1 : cp;
- }
+ cpfull = strchr(mapbuf,'/');
+ if(cpfull){
+ struct stat statbuf;
+
+ /*
+ * Strip out attributes from the filename. Attributes can be printed
+ * after a filename like (attribute[=value]) and we don't print them
+ * out here with the exception of (deleted). We use stat() to determine
+ * if something is part of the filename or an attribute
+ */
+ while (stat(cpfull, &statbuf) == -1 && errno == ENOENT){
+ cp = strrchr(cpfull,'(');
+
+ /* Stop if there are no other attributes */
+ if (!cp || strchr(cp,')') == NULL)
+ break;
+
+ /* If the attribute looks like deleted, just stop and leave (deleted) */
+ if (cp && !strncmp(cp+1, "deleted", 7))
+ break;
+
+ /* Move back to see if this looks like an attribute */
+ if (--cp <= cpfull)
+ break;
+
+ /* If this looks like an attribute, remove it */
+ if (cp[0] == ' ')
+ *cp = '\0';
+ }
+
+ if(showpath)
+ return cpfull;

- cp = strchr(mapbuf,'/');
- if(cp){
- if(showpath) return cp;
- return strrchr(cp,'/') + 1; // it WILL succeed
+ cp = strrchr(cpfull,'/');
+ return cp[1] ? cp+1 : cp;
}

- cp = " [ anon ]";
- if( (p->start_stack >= addr) && (p->start_stack <= addr+len) ) cp = " [ stack ]";
- return cp;
+ anon_cp = " [ anon ]";
+ if( (p->start_stack >= addr) && (p->start_stack <= addr+len) ) anon_cp = " [ stack ]";
+ return anon_cp;
}

static int one_proc(proc_t *p){
--
1.5.6.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/