From 99ce42501ce4077359b014c3e20da33b83c02d85 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 20 Sep 2021 12:28:50 +0200 Subject: [PATCH] lsfd: split new_file(), remove map_file_data Don't initialize entire 'struct file' in one step. It seems better to use different ways according to file source (map, symlink, etc.). Signed-off-by: Karel Zak --- misc-utils/lsfd-file.c | 18 +++++++++- misc-utils/lsfd-sock.c | 12 +++---- misc-utils/lsfd.c | 75 +++++++++++++++++++++--------------------- misc-utils/lsfd.h | 12 +++---- 4 files changed, 64 insertions(+), 53 deletions(-) diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index 12517d41cd..4eb2e51e4a 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -174,6 +174,22 @@ static void file_fill_flags_buf(struct ul_buffer *buf, int flags) || (file)->association == -ASSOC_SHM \ || (file)->association == -ASSOC_MEM) +static unsigned long get_map_length(struct file *file) +{ + unsigned long res = 0; + + if (is_association(file, SHM) || is_association(file, MEM)) { + static size_t pagesize = 0; + + if (!pagesize) + pagesize = getpagesize(); + + res = (file->map_end - file->map_start) / pagesize; + } + + return res; +} + static bool file_fill_column(struct proc *proc, struct file *file, struct libscols_line *ln, @@ -311,7 +327,7 @@ static bool file_fill_column(struct proc *proc, if (file->association != -ASSOC_SHM && file->association != -ASSOC_MEM) return true; - xasprintf(&str, "%lu", file->assoc_data.map_length); + xasprintf(&str, "%lu", get_map_length(file)); break; default: return false; diff --git a/misc-utils/lsfd-sock.c b/misc-utils/lsfd-sock.c index 495528eb96..4ef82ba785 100644 --- a/misc-utils/lsfd-sock.c +++ b/misc-utils/lsfd-sock.c @@ -76,8 +76,7 @@ static bool sock_fill_column(struct proc *proc __attribute__((__unused__)), return true; } -static void init_sock_content(struct file *file, - struct map_file_data *map_file_data) +static void init_sock_content(struct file *file) { int fd; @@ -95,13 +94,12 @@ static void init_sock_content(struct file *file, if (fd >= 0) sprintf(path, "/proc/%d/fd/%d", file->proc->pid, fd); - else { - assert(map_file_data); + else sprintf(path, "/proc/%d/map_files/%lx-%lx", file->proc->pid, - map_file_data->start, - map_file_data->end); - } + file->map_start, + file->map_end); + len = getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1); if (len > 0) { buf[len] = '\0'; diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 93d4135f85..7de7ee73d3 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -248,44 +248,35 @@ static const struct file_class *stat2class(struct stat *sb) return &unkn_class; } -static struct file *new_file( - struct proc *proc, - struct stat *sb, const char *name, - struct map_file_data *map_file_data, - int association) +static struct file *new_file(struct proc *proc, const struct file_class *class) { struct file *file; - const struct file_class *class = stat2class(sb); - - 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 (is_association(file, SHM) || is_association(file, MEM)) { - static size_t pagesize = 0; + return file; +} - assert(map_file_data); - if (!pagesize) - pagesize = getpagesize(); +static void file_set_path(struct file *file, struct stat *sb, const char *name, int association) +{ + const struct file_class *class = stat2class(sb); - file->assoc_data.map_length = - (map_file_data->end - map_file_data->start) / pagesize; - } + assert(class); - if (file->class->initialize_content) - file->class->initialize_content(file, map_file_data); + file->class = class; + file->association = association; + file->name = xstrdup(name); + file->stat = *sb; +} - return file; +static void file_init_content(struct file *file) +{ + if (file->class && file->class->initialize_content) + file->class->initialize_content(file); } static void free_file(struct file *file) @@ -361,9 +352,10 @@ static struct file *collect_file_symlink(struct path_cxt *pc, if (ul_path_readlink(pc, sym, sizeof(sym), name) < 0) return NULL; - f = new_file(proc, &sb, sym, NULL, assoc); - if (!f) - return NULL; + f = new_file(proc, stat2class(&sb)); + + file_set_path(f, &sb, sym, assoc); + file_init_content(f); if (is_association(f, EXE)) proc->uid = sb.st_uid; @@ -381,6 +373,7 @@ static struct file *collect_file_symlink(struct path_cxt *pc, fclose(fdinfo); } } + return f; } @@ -404,9 +397,10 @@ static struct file *collect_mem_file(struct proc *proc, int dd, struct dirent *d ssize_t len; char sym[PATH_MAX]; struct file *f; - struct map_file_data map_file_data; + unsigned long start, end; struct map *map; enum association assoc; + mode_t mode = 0; if (fstatat(dd, dp->d_name, &sb, 0) < 0) return NULL; @@ -417,18 +411,23 @@ static struct file *collect_mem_file(struct proc *proc, int dd, struct dirent *d map = NULL; - if (sscanf(dp->d_name, "%lx-%lx", &map_file_data.start, &map_file_data.end) == 2) - map = find_map(maps, map_file_data.start); + if (sscanf(dp->d_name, "%lx-%lx", &start, &end) == 2) + map = find_map(maps, start); assoc = (map && map->shared)? ASSOC_SHM: ASSOC_MEM; - f = new_file(proc, &sb, sym, &map_file_data, -assoc); - if (!f) - return NULL; - if (map) { - f->mode = (map->read? S_IRUSR: 0) | (map->write? S_IWUSR: 0) | (map->exec? S_IXUSR: 0); - f->pos = map->file_offset; - } + f = new_file(proc, stat2class(&sb)); + file_set_path(f, &sb, sym, -assoc); + + if (map) + mode = (map->read? S_IRUSR: 0) | (map->write? S_IWUSR: 0) | (map->exec? S_IXUSR: 0); + + f->map_start = start; + f->map_end = end; + f->pos = map ? map->file_offset : 0; + f->mode = mode; + + file_init_content(f); return f; } diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 5f4f7d9bc7..a3c40277ba 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -108,10 +108,6 @@ struct fdinfo_data { int mnt_id; }; -struct map_file_data { - unsigned long start, end; -}; - struct file { struct list_head files; const struct file_class *class; @@ -121,9 +117,12 @@ struct file { mode_t mode; struct proc *proc; unsigned long long pos; + + unsigned long map_start; + unsigned long map_end; + union assoc_data { struct fdinfo_data fdinfo; - unsigned long map_length; } assoc_data; }; @@ -140,8 +139,7 @@ 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 map_file_data *map_file_data); + void (*initialize_content)(struct file *file); void (*free_content)(struct file *file); }; -- 2.47.3