From: Karel Zak Date: Thu, 2 Sep 2021 12:30:58 +0000 (+0200) Subject: lsfd: add reference to proc from file X-Git-Tag: v2.38-rc1~144^2~117 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=34f3fd1ad8ac9118fb1de5cf01789545f1cdc2a9;p=thirdparty%2Futil-linux.git lsfd: add reference to proc from file * 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 --- diff --git a/misc-utils/lsfd-sock.c b/misc-utils/lsfd-sock.c index 59ff8e8f41..b5df8c1ebf 100644 --- a/misc-utils/lsfd-sock.c +++ b/misc-utils/lsfd-sock.c @@ -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) { diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index b403a472ca..c55a2b9eea 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -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); } diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 76802f24ea..0eb65978e9 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -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); };