From 7388e0b3594febd73c2495a5c020bec954fa6df4 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sat, 17 Apr 2021 07:33:07 +0900 Subject: [PATCH] lsfd: add mem associations --- misc-utils/lsfd-file.c | 2 +- misc-utils/lsfd.c | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index e749578921..435608c49f 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -45,7 +45,7 @@ static const char *assocstr[N_ASSOCS] = { [ASSOC_NS_TIME4C] = "time4c", [ASSOC_NS_USER] = "user", [ASSOC_NS_UTS] = "uts", - [ASSOC_MEM] = "mmap", + [ASSOC_MEM] = "mem", }; static const char *strftype(mode_t ftype) diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 40c2860fb4..d580e4aed7 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -263,19 +263,37 @@ static struct file *collect_fd_file(int dd, struct dirent *dp) return collect_file(&sb, sym, (int)num); } +static struct file *collect_mem_file(int dd, struct dirent *dp) +{ + struct stat sb; + ssize_t len; + char sym[PATH_MAX]; + + if (fstatat(dd, dp->d_name, &sb, 0) < 0) + return NULL; + + memset(sym, 0, sizeof(sym)); + if ((len = readlinkat(dd, dp->d_name, sym, sizeof(sym) - 1)) < 0) + return NULL; + + return collect_file(&sb, sym, -ASSOC_MEM); +} + static void enqueue_file(struct proc *proc, struct file * file) { INIT_LIST_HEAD(&file->files); list_add_tail(&file->files, &proc->files); } -static void collect_fd_files(struct proc *proc) + +static void collect_fd_files_generic(struct proc *proc, const char *proc_template, + struct file *(*collector)(int, struct dirent *)) { DIR *dirp; int dd; struct dirent *dp; - dirp = opendirf("/proc/%d/fd/", proc->pid); + dirp = opendirf(proc_template, proc->pid); if (!dirp) return; @@ -285,7 +303,7 @@ static void collect_fd_files(struct proc *proc) while ((dp = xreaddir(dirp))) { struct file *file; - if ((file = collect_fd_file(dd, dp)) == NULL) + if ((file = (* collector)(dd, dp)) == NULL) continue; enqueue_file(proc, file); @@ -293,6 +311,16 @@ static void collect_fd_files(struct proc *proc) closedir(dirp); } +static void collect_fd_files(struct proc *proc) +{ + collect_fd_files_generic(proc, "/proc/%d/fd/", collect_fd_file); +} + +static void collect_mem_files(struct proc *proc) +{ + collect_fd_files_generic(proc, "/proc/%d/map_files/", collect_mem_file); +} + static struct file *collect_outofbox_file(int dd, const char *name, int association) { struct stat sb; @@ -387,6 +415,7 @@ static void fill_proc(struct proc *proc) namespace_assocs, namespace_assoc_names, ARRAY_SIZE(namespace_assocs)); + collect_mem_files(proc); collect_fd_files(proc); } -- 2.47.3