]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: cleanup new file initialization
authorKarel Zak <kzak@redhat.com>
Wed, 1 Sep 2021 17:10:20 +0000 (19:10 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Oct 2021 09:01:54 +0000 (11:01 +0200)
Don't use new_<class>() functions if we have already have
file_class callbacks.

Signed-off-by: Karel Zak <kzak@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 24ef05596b2a99b3aefe741b4cd7363c5ff4f86c..e640a0d39efd0e61583d409f1f27570137b79aa8 100644 (file)
@@ -71,15 +71,6 @@ static bool bdev_fill_column(struct proc *proc __attribute__((__unused__)),
        return true;
 }
 
-struct file *new_bdev(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd)
-{
-       return new_file(class? class: &bdev_class,
-                        sb, name, map_file_data, fd);
-}
-
 static struct partition *new_partition(dev_t dev, const char *name)
 {
        struct partition *partition = xcalloc(1, sizeof(*partition));
index f697a5bf7a3f734c9f45cb37cc63c83b6521f2e1..c56123110db4b072d8904b06ab1830692741a397 100644 (file)
@@ -106,15 +106,6 @@ static bool cdev_fill_column(struct proc *proc __attribute__((__unused__)),
        return true;
 }
 
-struct file *new_cdev(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd)
-{
-       return new_file(class? class: &cdev_class,
-                        sb, name, map_file_data, fd);
-}
-
 static struct chrdrv *new_chrdrv(unsigned long major, const char *name)
 {
        struct chrdrv *chrdrv = xcalloc(1, sizeof(*chrdrv));
index bae969d821734ebf0085ab3575d30b775d27b7a2..3617d5afb622b1a3bce895f3c4074f7aadc000fb 100644 (file)
@@ -56,15 +56,6 @@ static bool fifo_fill_column(struct proc *proc __attribute__((__unused__)),
        return true;
 }
 
-struct file *new_fifo(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd)
-{
-       return new_file(class? class: &fifo_class,
-                        sb, name, map_file_data, fd);
-}
-
 const struct file_class fifo_class = {
        .super = &file_class,
        .size = sizeof(struct file),
index b496e214d0cb5737ef13a81075617f660a94bc26..0fd4f54da04232dec5a1716c24eed95d23ca4011 100644 (file)
@@ -34,7 +34,6 @@
 #include "lsfd.h"
 
 static struct idcache *username_cache;
-static size_t pagesize;
 
 static const char *assocstr[N_ASSOCS] = {
        [ASSOC_CWD]       = "cwd",
@@ -340,35 +339,11 @@ static void file_free_content(struct file *file)
        free(file->name);
 }
 
-struct file *new_file(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int association)
-{
-       struct file *file;
-
-       class = class? class: &file_class;
-       file = xcalloc(1, class->size);
-
-       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;
-}
-
 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 d5bf88b79df633069608c15ceb02500b1edb04a6..59ff8e8f410662d0e290af8949d90db3f2b56f8f 100644 (file)
@@ -76,35 +76,38 @@ static bool sock_fill_column(struct proc *proc __attribute__((__unused__)),
        return true;
 }
 
-struct file *new_sock(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd,
-                      struct proc *proc)
+static void init_sock_content(struct file *file,
+                             struct proc *proc,
+                             struct map_file_data *map_file_data)
 {
-       struct file *file = new_file(class? class: &sock_class,
-                                     sb, name, map_file_data, fd);
+       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 buf[256];
                ssize_t len;
+
                memset(path, 0, sizeof(path));
 
                if (fd >= 0)
                        sprintf(path, "/proc/%d/fd/%d", proc->pid, fd);
-               else
+               else {
+                       assert(map_file_data);
                        sprintf(path, "/proc/%d/map_files/%lx-%lx", proc->pid,
                                map_file_data->start, map_file_data->end);
+               }
                len = getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1);
                if (len > 0) {
                        buf[len] = '\0';
                        sock->protoname = xstrdup(buf);
                }
        }
-
-       return file;
 }
 
 static void free_sock_content(struct file *file)
@@ -120,5 +123,6 @@ const struct file_class sock_class = {
        .super = &file_class,
        .size = sizeof(struct sock),
        .fill_column = sock_fill_column,
+       .initialize_content = init_sock_content,
        .free_content = free_sock_content,
 };
index 302f312ca159fbbfd355d79b45cfe096968ad95c..2479d16a0b30e4108ec6642a341d0f134b7ae3d1 100644 (file)
@@ -56,18 +56,8 @@ static bool unkn_fill_column(struct proc *proc __attribute__((__unused__)),
        return true;
 }
 
-struct file *new_unkn(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd)
-{
-       return new_file(class? class: &unkn_class,
-                        sb, name, map_file_data, fd);
-}
-
 const struct file_class unkn_class = {
        .super = &file_class,
        .size = sizeof(struct file),
        .fill_column = unkn_fill_column,
-       .free_content = NULL,
 };
index 3935f7fcd4610d4713f4d25079cc238a04c22b7a..d4726dbc8a1dbb850add0e3d17b4b0e79a3b89bc 100644 (file)
@@ -52,6 +52,7 @@ static int kcmp(pid_t pid1, pid_t pid2, int type,
 
 #include "lsfd.h"
 
+
 static void fill_proc(struct proc *proc);
 static void add_nodev(unsigned long minor, const char *filesystem);
 
@@ -225,6 +226,40 @@ static const struct colinfo *get_column_info(int num)
 }
 
 
+static struct file *new_file(const struct file_class *class,
+                      struct proc *proc,
+                      struct stat *sb, const char *name,
+                      struct map_file_data *map_file_data,
+                      int association)
+{
+       struct file *file;
+
+       assert(class);
+
+       file = xcalloc(1, class->size);
+       file->class = class;
+       file->association = association;
+       file->name = xstrdup(name);
+       file->stat = *sb;
+
+       if (file->association == -ASSOC_SHM || file->association == -ASSOC_MEM) {
+               static size_t pagesize = 0;
+
+               assert(map_file_data);
+               if (!pagesize)
+                       pagesize = getpagesize();
+
+               file->assoc_data.map_length =
+                       (map_file_data->end - map_file_data->start) / pagesize;
+       }
+
+       if (file->class->initialize_content)
+               file->class->initialize_content(file, proc, map_file_data);
+
+       return file;
+}
+
+
 static struct proc *new_prococess(pid_t pid, struct proc * leader)
 {
        struct proc *proc = xcalloc(1, sizeof(*proc));
@@ -363,27 +398,36 @@ static void collect(struct list_head *procs, struct lsfd_control *ctl)
        }
 }
 
-static struct file *collect_file(struct proc *proc,
-                                struct stat *sb, char *name,
-                                struct map_file_data *map_file_data,
-                                int assoc)
+static const struct file_class *stat2class(struct stat *sb)
 {
+       assert(sb);
+
        switch (sb->st_mode & S_IFMT) {
        case S_IFCHR:
-               return new_cdev(NULL, sb, name, map_file_data, assoc);
+               return &cdev_class;
        case S_IFBLK:
-               return new_bdev(NULL, sb, name, map_file_data, assoc);
+               return &bdev_class;
        case S_IFSOCK:
-               return new_sock(NULL, sb, name, map_file_data, assoc, proc);
+               return &sock_class;
        case S_IFIFO:
-               return new_fifo(NULL, sb, name, map_file_data, assoc);
+               return &fifo_class;
        case S_IFLNK:
        case S_IFREG:
        case S_IFDIR:
-               return new_file(NULL, sb, name, map_file_data, assoc);
+               return &file_class;
        default:
-               return new_unkn(NULL, sb, name, map_file_data, assoc);
+               break;
        }
+
+       return &unkn_class;
+}
+
+static struct file *collect_file(struct proc *proc,
+                                struct stat *sb, char *name,
+                                struct map_file_data *map_file_data,
+                                int assoc)
+{
+       return new_file(stat2class(sb), proc, sb, name, map_file_data, assoc);
 }
 
 static void read_fdinfo(struct file *file, FILE *fdinfo)
index 089cff4ca0236126c309470ca6ab72de6cc34d2f..c07ff02ca4dd5c579fa0cb99ff5e12b973da55c3 100644 (file)
@@ -147,37 +147,14 @@ struct file_class {
                            int column_id,
                            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);
 };
 
 extern const struct file_class file_class, cdev_class, bdev_class, sock_class, unkn_class, fifo_class;
 
-struct file *new_file(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int association);
-struct file *new_cdev(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd);
-struct file *new_bdev(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd);
-struct file *new_sock(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd,
-                      struct proc *proc);
-struct file *new_unkn(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd);
-struct file *new_fifo(const struct file_class *class,
-                      struct stat *sb, const char *name,
-                      struct map_file_data *map_file_data,
-                      int fd);
-
 /*
  * Name managing
  */