]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: add mem associations
authorMasatake YAMATO <yamato@redhat.com>
Fri, 16 Apr 2021 22:33:07 +0000 (07:33 +0900)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Oct 2021 09:01:53 +0000 (11:01 +0200)
misc-utils/lsfd-file.c
misc-utils/lsfd.c

index e749578921a7ec5f8380c5560a1ca2d6c7148279..435608c49ff7038a85f3a51c80a4a8cb1033b462 100644 (file)
@@ -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)
index 40c2860fb4505364ac12e1bf6e7dc8a7288e3edb..d580e4aed7828c00d2f9ad8623c9e829a518ba91 100644 (file)
@@ -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);
 }