]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: add MAPLEN column
authorMasatake YAMATO <yamato@redhat.com>
Mon, 10 May 2021 03:54:55 +0000 (12:54 +0900)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Oct 2021 09:01:53 +0000 (11:01 +0200)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-bdev.c
misc-utils/lsfd-cdev.c
misc-utils/lsfd-fifo.c
misc-utils/lsfd-file.c
misc-utils/lsfd-sock.c
misc-utils/lsfd-unkn.c
misc-utils/lsfd.c
misc-utils/lsfd.h

index 7f37d00d1a868d5f238559d2858aefeaa4d8e4dd..2bfca240c741ba3b629f43aefa7f233253abc79c 100644 (file)
@@ -72,10 +72,12 @@ static bool bdev_fill_column(struct proc *proc __attribute__((__unused__)),
 }
 
 struct file *make_bdev(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd)
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd)
 {
        return make_file(class? class: &bdev_class,
-                        sb, name, fd);
+                        sb, name, map_file_data, fd);
 }
 
 static struct partition *make_partition(dev_t dev, const char *name)
index 01f2928910cfaed2d78c6f1bef8e7131378a4be6..e83fe6db438e330173a6028315bc0c4c73dd1e7c 100644 (file)
@@ -107,10 +107,12 @@ static bool cdev_fill_column(struct proc *proc __attribute__((__unused__)),
 }
 
 struct file *make_cdev(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd)
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd)
 {
        return make_file(class? class: &cdev_class,
-                        sb, name, fd);
+                        sb, name, map_file_data, fd);
 }
 
 static struct chrdrv *make_chrdrv(unsigned long major, const char *name)
index c89ac55d462d6e6055cc0e471851b2c5d90b1615..e745fd55473e796f7d43ffe7b840fca251d6e1b0 100644 (file)
@@ -57,10 +57,12 @@ static bool fifo_fill_column(struct proc *proc __attribute__((__unused__)),
 }
 
 struct file *make_fifo(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd)
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd)
 {
        return make_file(class? class: &fifo_class,
-                        sb, name, fd);
+                        sb, name, map_file_data, fd);
 }
 
 const struct file_class fifo_class = {
index 3147a6440c15d78a1e07ba8b219f60a65fd79fde..1684d038da40c347c7fc6b7b5924326e6e98d184 100644 (file)
@@ -22,6 +22,8 @@
  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <unistd.h>
+
 #include "xalloc.h"
 #include "nls.h"
 #include "buffer.h"
@@ -32,6 +34,7 @@
 #include "lsfd.h"
 
 static struct idcache *username_cache;
+static size_t pagesize;
 
 static const char *assocstr[N_ASSOCS] = {
        [ASSOC_CWD]       = "cwd",
@@ -301,6 +304,12 @@ static bool file_fill_column(struct proc *proc,
                str = ul_buffer_get_data(&buf, NULL, NULL);
                break;
        }
+       case COL_MAPLEN:
+               if (file->association != -ASSOC_SHM
+                   && file->association != -ASSOC_MEM)
+                       return true;
+               xasprintf(&str, "%lu", file->assoc_data.map_length);
+               break;
        default:
                return false;
        };
@@ -333,7 +342,9 @@ static void file_free_content(struct file *file)
 }
 
 struct file *make_file(const struct file_class *class,
-                      struct stat *sb, const char *name, int association)
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int association)
 {
        struct file *file;
 
@@ -344,6 +355,11 @@ struct file *make_file(const struct file_class *class,
        file->association = association;
        file->name = xstrdup(name);
        file->stat = *sb;
+
+       if (file->association == -ASSOC_SHM
+           || file->association == -ASSOC_MEM)
+               file->assoc_data.map_length = (map_file_data->end - map_file_data->start) / pagesize;
+
        return file;
 }
 
@@ -352,6 +368,8 @@ static void file_class_initialize(void)
        username_cache = new_idcache();
        if (!username_cache)
                err(EXIT_FAILURE, _("failed to allocate UID cache"));
+
+       pagesize = getpagesize();
 }
 
 static void file_class_finalize(void)
index bd0a2a0b91b540db60a7e2e20504f13d9e4dd822..bfbccaf4f30a578c7b4b0454b4ce1623114421de 100644 (file)
@@ -77,11 +77,13 @@ static bool sock_fill_column(struct proc *proc __attribute__((__unused__)),
 }
 
 struct file *make_sock(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd,
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd,
                       struct proc *proc)
 {
        struct file *file = make_file(class? class: &sock_class,
-                                     sb, name, fd);
+                                     sb, name, map_file_data, fd);
        if (fd >= 0) {
                struct sock *sock = (struct sock *)file;
 
index be69e4464798bc552236a1c29045bdcb8b72f409..64613a281695fea0c7f3a626961abb5c36185c5a 100644 (file)
@@ -57,10 +57,12 @@ static bool unkn_fill_column(struct proc *proc __attribute__((__unused__)),
 }
 
 struct file *make_unkn(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd)
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd)
 {
        return make_file(class? class: &unkn_class,
-                        sb, name, fd);
+                        sb, name, map_file_data, fd);
 }
 
 const struct file_class unkn_class = {
index 95e97131e84212eea67a63b742bb8b54b4f2daa6..3a5213d63eae0245534c7c381d009bb2e28c0b75 100644 (file)
@@ -118,6 +118,8 @@ static struct colinfo infos[] = {
                N_("file descriptor for the file") },
        [COL_INODE]   = { "INODE",    0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
                N_("inode number") },
+       [COL_MAPLEN]  = { "MAPLEN",   0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
+               N_("length of file mapping (in page)") },
        [COL_MISCDEV] = { "MISCDEV",  0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
                N_("misc character device name resolved by /procmisc") },
        [COL_MNT_ID]  = { "MNTID",    0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
@@ -373,23 +375,25 @@ static void collect(struct list_head *procs, struct lsfd_control *ctl)
 }
 
 static struct file *collect_file(struct proc *proc,
-                                struct stat *sb, char *name, int assoc)
+                                struct stat *sb, char *name,
+                                struct map_file_data *map_file_data,
+                                int assoc)
 {
        switch (sb->st_mode & S_IFMT) {
        case S_IFCHR:
-               return make_cdev(NULL, sb, name, assoc);
+               return make_cdev(NULL, sb, name, map_file_data, assoc);
        case S_IFBLK:
-               return make_bdev(NULL, sb, name, assoc);
+               return make_bdev(NULL, sb, name, map_file_data, assoc);
        case S_IFSOCK:
-               return make_sock(NULL, sb, name, assoc, proc);
+               return make_sock(NULL, sb, name, map_file_data, assoc, proc);
        case S_IFIFO:
-               return make_fifo(NULL, sb, name, assoc);
+               return make_fifo(NULL, sb, name, map_file_data, assoc);
        case S_IFLNK:
        case S_IFREG:
        case S_IFDIR:
-               return make_file(NULL, sb, name, assoc);
+               return make_file(NULL, sb, name, map_file_data, assoc);
        default:
-               return make_unkn(NULL, sb, name, assoc);
+               return make_unkn(NULL, sb, name, map_file_data, assoc);
        }
 }
 
@@ -440,7 +444,7 @@ static struct file *collect_fd_file(struct proc *proc, int dd, struct dirent *dp
        if ((len = readlinkat(dd, dp->d_name, sym, sizeof(sym) - 1)) < 0)
                return NULL;
 
-       f = collect_file(proc, &sb, sym, (int)num);
+       f = collect_file(proc, &sb, sym, NULL, (int)num);
        if (!f)
                return NULL;
 
@@ -479,7 +483,7 @@ static struct file *collect_mem_file(struct proc *proc, int dd, struct dirent *d
        ssize_t len;
        char sym[PATH_MAX];
        struct file *f;
-       unsigned long start, end;
+       struct map_file_data map_file_data;
        struct map *map;
        enum association assoc;
 
@@ -492,11 +496,11 @@ static struct file *collect_mem_file(struct proc *proc, int dd, struct dirent *d
 
 
        map = NULL;
-       if (sscanf(dp->d_name, "%lx-%lx", &start, &end) == 2)
-               map = find_map(maps, start);
+       if (sscanf(dp->d_name, "%lx-%lx", &map_file_data.start, &map_file_data.end) == 2)
+               map = find_map(maps, map_file_data.start);
 
        assoc = (map && map->shared)? ASSOC_SHM: ASSOC_MEM;
-       f = collect_file(proc, &sb, sym, -assoc);
+       f = collect_file(proc, &sb, sym, &map_file_data, -assoc);
        if (!f)
                return NULL;
 
@@ -648,7 +652,7 @@ static struct file *collect_outofbox_file(struct proc *proc,
        if ((len = readlinkat(dd, name, sym, sizeof(sym) - 1)) < 0)
                return NULL;
 
-       return collect_file(proc, &sb, sym, association);
+       return collect_file(proc, &sb, sym, NULL, association);
 }
 
 static void collect_proc_uid(struct proc *proc, int dd)
index 624b90dd91c2107f4fa6b9434e124fbd6bcdb578..76911573c8a348458eb487d9d256d750c085333d 100644 (file)
@@ -61,6 +61,7 @@ enum {
        COL_FD,
        COL_FLAGS,
        COL_INODE,
+       COL_MAPLEN,
        COL_MISCDEV,
        COL_MNT_ID,
        COL_MODE,
@@ -118,6 +119,10 @@ struct fdinfo_data {
        int mnt_id;
 };
 
+struct map_file_data {
+       unsigned long start, end;
+};
+
 struct file {
        struct list_head files;
        const struct file_class *class;
@@ -128,6 +133,7 @@ struct file {
        unsigned long long pos;
        union assoc_data {
                struct fdinfo_data fdinfo;
+               unsigned long map_length;
        } assoc_data;
 };
 
@@ -148,18 +154,30 @@ struct file_class {
 extern const struct file_class file_class, cdev_class, bdev_class, sock_class, unkn_class, fifo_class;
 
 struct file *make_file(const struct file_class *class,
-                      struct stat *sb, const char *name, int association);
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int association);
 struct file *make_cdev(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd);
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd);
 struct file *make_bdev(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd);
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd);
 struct file *make_sock(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd,
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd,
                       struct proc *proc);
 struct file *make_unkn(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd);
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd);
 struct file *make_fifo(const struct file_class *class,
-                      struct stat *sb, const char *name, int fd);
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int fd);
 
 /*
  * Name managing