From: Masatake YAMATO Date: Mon, 10 May 2021 03:54:55 +0000 (+0900) Subject: lsfd: add MAPLEN column X-Git-Tag: v2.38-rc1~144^2~130 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=63f3938581d4b54d4951cd471f61d3245130930a;p=thirdparty%2Futil-linux.git lsfd: add MAPLEN column Signed-off-by: Masatake YAMATO --- diff --git a/misc-utils/lsfd-bdev.c b/misc-utils/lsfd-bdev.c index 7f37d00d1a..2bfca240c7 100644 --- a/misc-utils/lsfd-bdev.c +++ b/misc-utils/lsfd-bdev.c @@ -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) diff --git a/misc-utils/lsfd-cdev.c b/misc-utils/lsfd-cdev.c index 01f2928910..e83fe6db43 100644 --- a/misc-utils/lsfd-cdev.c +++ b/misc-utils/lsfd-cdev.c @@ -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) diff --git a/misc-utils/lsfd-fifo.c b/misc-utils/lsfd-fifo.c index c89ac55d46..e745fd5547 100644 --- a/misc-utils/lsfd-fifo.c +++ b/misc-utils/lsfd-fifo.c @@ -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 = { diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index 3147a6440c..1684d038da 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -22,6 +22,8 @@ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include + #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) diff --git a/misc-utils/lsfd-sock.c b/misc-utils/lsfd-sock.c index bd0a2a0b91..bfbccaf4f3 100644 --- a/misc-utils/lsfd-sock.c +++ b/misc-utils/lsfd-sock.c @@ -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; diff --git a/misc-utils/lsfd-unkn.c b/misc-utils/lsfd-unkn.c index be69e44647..64613a2816 100644 --- a/misc-utils/lsfd-unkn.c +++ b/misc-utils/lsfd-unkn.c @@ -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 = { diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 95e97131e8..3a5213d63e 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -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) diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 624b90dd91..76911573c8 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -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