]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: split new_file(), remove map_file_data
authorKarel Zak <kzak@redhat.com>
Mon, 20 Sep 2021 10:28:50 +0000 (12:28 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Oct 2021 09:01:54 +0000 (11:01 +0200)
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 <kzak@redhat.com>
misc-utils/lsfd-file.c
misc-utils/lsfd-sock.c
misc-utils/lsfd.c
misc-utils/lsfd.h

index 12517d41cdc125e52e4e293b60eca9f9411b2a82..4eb2e51e4a6b0260ef338abf604bbee4abbe9f14 100644 (file)
@@ -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;
index 495528eb96c49ae2fc142cc85bf35d24b06ef380..4ef82ba7853616cf56a5c9a01f23f7ff8ca0f2d8 100644 (file)
@@ -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';
index 93d4135f85c554b1f3e22afdfe7814786a3e3a34..7de7ee73d3798a67429626099dc21f76f62cf23b 100644 (file)
@@ -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;
 }
index 5f4f7d9bc7eba000d53fbdd92563fd0d410fec6f..a3c40277bab595b475d444f826e6c7b5f939cfef 100644 (file)
@@ -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);
 };