]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: add reference to proc from file
authorKarel Zak <kzak@redhat.com>
Thu, 2 Sep 2021 12:30:58 +0000 (14:30 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Oct 2021 09:01:54 +0000 (11:01 +0200)
* add to file struct reference to process
* add file to the list of the process' files in new_file()

This makes things more straightforward.

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/lsfd-sock.c
misc-utils/lsfd.c
misc-utils/lsfd.h

index 59ff8e8f410662d0e290af8949d90db3f2b56f8f..b5df8c1ebfd978e416fd8842ba72d725a4c008a6 100644 (file)
@@ -77,30 +77,30 @@ static bool sock_fill_column(struct proc *proc __attribute__((__unused__)),
 }
 
 static void init_sock_content(struct file *file,
-                             struct proc *proc,
                              struct map_file_data *map_file_data)
 {
        int fd;
 
        assert(file);
-       assert(proc);
 
        fd = file->association;
 
        if (fd >= 0 || fd == -ASSOC_MEM || fd == -ASSOC_SHM) {
                struct sock *sock = (struct sock *)file;
-               char path[PATH_MAX];
+               char path[PATH_MAX] = {'\0'};
                char buf[256];
                ssize_t len;
 
-               memset(path, 0, sizeof(path));
+               assert(file->proc);
 
                if (fd >= 0)
-                       sprintf(path, "/proc/%d/fd/%d", proc->pid, fd);
+                       sprintf(path, "/proc/%d/fd/%d", file->proc->pid, fd);
                else {
                        assert(map_file_data);
-                       sprintf(path, "/proc/%d/map_files/%lx-%lx", proc->pid,
-                               map_file_data->start, map_file_data->end);
+                       sprintf(path, "/proc/%d/map_files/%lx-%lx",
+                               file->proc->pid,
+                               map_file_data->start,
+                               map_file_data->end);
                }
                len = getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1);
                if (len > 0) {
index b403a472caa3cb2b57e6a12429868370504d0129..c55a2b9eea46f60ca93b904ae81d1b9d978b69f7 100644 (file)
@@ -261,10 +261,16 @@ static struct file *new_file(
        assert(class);
 
        file = xcalloc(1, class->size);
+
        file->class = class;
        file->association = association;
        file->name = xstrdup(name);
        file->stat = *sb;
+       file->proc = proc;
+
+       /* add file to the process */
+       INIT_LIST_HEAD(&file->files);
+       list_add_tail(&file->files, &proc->files);
 
        if (file->association == -ASSOC_SHM || file->association == -ASSOC_MEM) {
                static size_t pagesize = 0;
@@ -278,7 +284,7 @@ static struct file *new_file(
        }
 
        if (file->class->initialize_content)
-               file->class->initialize_content(file, proc, map_file_data);
+               file->class->initialize_content(file, map_file_data);
 
        return file;
 }
@@ -538,13 +544,6 @@ static struct file *collect_mem_file(struct proc *proc, int dd, struct dirent *d
        return f;
 }
 
-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_generic(struct proc *proc, const char *proc_template,
                                     struct file *(*collector)(struct proc *,int, struct dirent *, void *),
                                     void *data)
@@ -560,14 +559,9 @@ static void collect_fd_files_generic(struct proc *proc, const char *proc_templat
        if ((dd = dirfd(dirp)) < 0 )
                return;
 
-       while ((dp = xreaddir(dirp))) {
-               struct file *file;
-
-               if ((file = (* collector)(proc, dd, dp, data)) == NULL)
-                       continue;
+       while ((dp = xreaddir(dirp)))
+               collector(proc, dd, dp, data);
 
-               enqueue_file(proc, file);
-       }
        closedir(dirp);
 }
 
@@ -705,17 +699,12 @@ static void collect_outofbox_files(struct proc *proc,
                return;
 
        for (unsigned int i = 0; i < count; i++) {
-               struct file *file;
 
                if (assocs[i] == ASSOC_EXE)
                        collect_proc_uid(proc, dd);
 
-               if ((file = collect_outofbox_file(proc, dd,
-                                                 assoc_names[assocs[i]],
-                                                 assocs[i] * -1)) == NULL)
-                       continue;
-
-               enqueue_file(proc, file);
+               collect_outofbox_file(proc, dd,
+                               assoc_names[assocs[i]], assocs[i] * -1);
        }
        closedir(dirp);
 }
index 76802f24ea4671c187a5443c6e835b2331ad7f56..0eb65978e964f7450c00c79aceb6fb3d6f5a59c9 100644 (file)
@@ -118,6 +118,7 @@ struct file {
        char *name;
        struct stat stat;
        mode_t mode;
+       struct proc *proc;
        unsigned long long pos;
        union assoc_data {
                struct fdinfo_data fdinfo;
@@ -137,7 +138,6 @@ struct file_class {
                            size_t column_index);
        int  (*handle_fdinfo)(struct file *file, const char *key, const char* value);
        void (*initialize_content)(struct file *file,
-                                  struct proc *proc,
                                   struct map_file_data *map_file_data);
        void (*free_content)(struct file *file);
 };