From: Karel Zak Date: Wed, 1 Sep 2021 17:10:20 +0000 (+0200) Subject: lsfd: cleanup new file initialization X-Git-Tag: v2.38-rc1~144^2~121 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f9fa97d3952e026e1b226c818eef04a7dcc6fc85;p=thirdparty%2Futil-linux.git lsfd: cleanup new file initialization Don't use new_() functions if we have already have file_class callbacks. Signed-off-by: Karel Zak --- diff --git a/misc-utils/lsfd-bdev.c b/misc-utils/lsfd-bdev.c index 24ef05596b..e640a0d39e 100644 --- a/misc-utils/lsfd-bdev.c +++ b/misc-utils/lsfd-bdev.c @@ -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)); diff --git a/misc-utils/lsfd-cdev.c b/misc-utils/lsfd-cdev.c index f697a5bf7a..c56123110d 100644 --- a/misc-utils/lsfd-cdev.c +++ b/misc-utils/lsfd-cdev.c @@ -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)); diff --git a/misc-utils/lsfd-fifo.c b/misc-utils/lsfd-fifo.c index bae969d821..3617d5afb6 100644 --- a/misc-utils/lsfd-fifo.c +++ b/misc-utils/lsfd-fifo.c @@ -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), diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index b496e214d0..0fd4f54da0 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -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) diff --git a/misc-utils/lsfd-sock.c b/misc-utils/lsfd-sock.c index d5bf88b79d..59ff8e8f41 100644 --- a/misc-utils/lsfd-sock.c +++ b/misc-utils/lsfd-sock.c @@ -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, }; diff --git a/misc-utils/lsfd-unkn.c b/misc-utils/lsfd-unkn.c index 302f312ca1..2479d16a0b 100644 --- a/misc-utils/lsfd-unkn.c +++ b/misc-utils/lsfd-unkn.c @@ -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, }; diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 3935f7fcd4..d4726dbc8a 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -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) diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 089cff4ca0..c07ff02ca4 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -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 */