]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: add cwd, exe, and root associations
authorMasatake YAMATO <yamato@redhat.com>
Fri, 16 Apr 2021 21:50:00 +0000 (06:50 +0900)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Oct 2021 09:01:53 +0000 (11:01 +0200)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-cdev-file.c
misc-utils/lsfd-file.c
misc-utils/lsfd.c
misc-utils/lsfd.h

index 1d05dcff4bb461150d3af592b854cc27110fcef4..3bcf1bd911b65467f11d4b1dc2233d8156efdc46 100644 (file)
@@ -55,7 +55,7 @@ static bool cdev_file_fill_column(struct proc *proc __attribute__((__unused__)),
 
 const struct file_class cdev_file_class = {
        .super = &file_class,
-       .size = sizeof (struct file),
+       .size = sizeof(struct file),
        .fill_column = cdev_file_fill_column,
        .free_content = NULL,
 };
index d554030210ad687ce026c0505d594287d83a303d..9e94b817297ebaa1ce67ed68551d21135e9ba460 100644 (file)
 
 #include "lsfd.h"
 
+static const char *assocstr[N_ASSOCS] = {
+       [ASSOC_CWD]       = "cwd",
+       [ASSOC_EXE]       = "exe",
+       /* "root" appears as user names, too.
+        * So we use "rtd" here instead of "root". */
+       [ASSOC_ROOT]      = "rtd",
+};
+
 static const char *strftype(mode_t ftype)
 {
        switch (ftype) {
@@ -88,7 +96,14 @@ static bool file_fill_column(struct proc *proc,
                        return false;
                /* FALL THROUGH */
        case COL_ASSOC:
-               xasprintf(&str, "%d", file->association);
+               if (file->association >= 0)
+                       xasprintf(&str, "%d", file->association);
+               else {
+                       int assoc = file->association * -1;
+                       if (assoc >= N_ASSOCS)
+                               return false; /* INTERNAL ERROR */
+                       xasprintf(&str, "%s", assocstr[assoc]);
+               }
                break;
        case COL_INODE:
                xasprintf(&str, "%llu", (unsigned long long)file->stat.st_ino);
@@ -137,7 +152,7 @@ struct file *make_file(const struct file_class *class,
 
 const struct file_class file_class = {
        .super = NULL,
-       .size = sizeof (struct file),
+       .size = sizeof(struct file),
        .fill_column = file_fill_column,
        .free_content = file_free_content,
 };
index 4c53224e28fb50d07ea79ba242d5413b5a36461d..629e0cf8df9e3ee4faec5414b677bc83ca02c0f6 100644 (file)
@@ -225,7 +225,21 @@ static void collect(struct list_head *procs)
        run_collectors(procs);
 }
 
-static struct file *collect_file(int dd, struct dirent *dp)
+static struct file *collect_file(struct stat *sb, char *name, int assoc)
+{
+       switch (sb->st_mode & S_IFMT) {
+       case S_IFREG:
+               return make_regular_file(NULL, sb, name, assoc);
+       case S_IFCHR:
+               return make_cdev_file(NULL, sb, name, assoc);
+       case S_IFBLK:
+               return make_bdev_file(NULL, sb, name, assoc);
+       }
+
+       return make_file(NULL, sb, name, assoc);
+}
+
+static struct file *collect_fd_file(int dd, struct dirent *dp)
 {
        long num;
        char *endptr = NULL;
@@ -245,16 +259,7 @@ static struct file *collect_file(int dd, struct dirent *dp)
        if ((len = readlinkat(dd, dp->d_name, sym, sizeof(sym) - 1)) < 0)
                return NULL;
 
-       switch (sb.st_mode & S_IFMT) {
-       case S_IFREG:
-               return make_regular_file(NULL, &sb, sym, (int)num);
-       case S_IFCHR:
-               return make_cdev_file(NULL, &sb, sym, (int)num);
-       case S_IFBLK:
-               return make_bdev_file(NULL, &sb, sym, (int)num);
-       }
-
-       return make_file(NULL, &sb, sym, (int)num);
+       return collect_file(&sb, sym, (int)num);
 }
 
 static void enqueue_file(struct proc *proc, struct file * file)
@@ -263,7 +268,7 @@ static void enqueue_file(struct proc *proc, struct file * file)
        list_add_tail(&file->files, &proc->files);
 }
 
-static void collect_files(struct proc *proc)
+static void collect_fd_files(struct proc *proc)
 {
        DIR *dirp;
        int dd;
@@ -279,7 +284,55 @@ static void collect_files(struct proc *proc)
        while ((dp = xreaddir(dirp))) {
                struct file *file;
 
-               if ((file = collect_file(dd, dp)) == NULL)
+               if ((file = collect_fd_file(dd, dp)) == NULL)
+                       continue;
+
+               enqueue_file(proc, file);
+       }
+       closedir(dirp);
+}
+
+static struct file *collect_outofbox_file(int dd, const char *name, int association)
+{
+       struct stat sb;
+       ssize_t len;
+       char sym[PATH_MAX];
+
+       if (fstatat(dd, name, &sb, 0) < 0)
+               return NULL;
+
+       memset(sym, 0, sizeof(sym));
+       if ((len = readlinkat(dd, name, sym, sizeof(sym) - 1)) < 0)
+               return NULL;
+
+       return collect_file(&sb, sym, association);
+}
+
+static void collect_outofbox_files(struct proc *proc)
+{
+       DIR *dirp;
+       int dd;
+
+       dirp = opendirf("/proc/%d", proc->pid);
+       if (!dirp)
+               return;
+
+       if ((dd = dirfd(dirp)) < 0 )
+               return;
+
+       enum association assocs[] = { ASSOC_CWD, ASSOC_EXE, ASSOC_ROOT };
+       const char* assoc_names[] = {
+               [ASSOC_CWD]  = "cwd",
+               [ASSOC_EXE]  = "exe",
+               [ASSOC_ROOT] = "root",
+       };
+
+       for (unsigned int i = 0; i < ARRAY_SIZE(assocs); i++) {
+               struct file *file;
+
+               if ((file = collect_outofbox_file(dd,
+                                                 assoc_names[assocs[i]],
+                                                 assocs[i] * -1)) == NULL)
                        continue;
 
                enqueue_file(proc, file);
@@ -295,7 +348,8 @@ static void fill_proc(struct proc *proc)
        if (!proc->command)
                err(EXIT_FAILURE, _("failed to get command name"));
 
-       collect_files(proc);
+       collect_outofbox_files(proc);
+       collect_fd_files(proc);
 }
 
 
index 896bbf58afda436299987ef3608572e74acda3d5..839660a10c763940ab9b349d770edcae9ce7efc9 100644 (file)
@@ -66,6 +66,13 @@ enum {
 /*
  * Process structure
  */
+enum association {
+       ASSOC_CWD = 1,
+       ASSOC_EXE,
+       ASSOC_ROOT,
+       N_ASSOCS,
+};
+
 struct proc {
        pid_t pid;
        char *command;