]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: fill USER field
authorMasatake YAMATO <yamato@redhat.com>
Fri, 26 Mar 2021 07:55:57 +0000 (16:55 +0900)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Oct 2021 09:01:53 +0000 (11:01 +0200)
misc-utils/lsfd-file.c
misc-utils/lsfd.c
misc-utils/lsfd.h

index 6b41f06b7f2f75ba976319d8b3f85c884da0b4ee..3a981bf83c763b420926bbb1c6a5c0973a049a50 100644 (file)
@@ -76,6 +76,13 @@ static bool file_fill_column(struct proc *proc,
                if (scols_line_set_data(ln, column_index, strftype(ftype)))
                        err(EXIT_FAILURE, _("failed to add output data"));
                return true;
+       case COL_USER:
+               add_uid(username_cache, (int)file->stat.st_uid);
+               if (scols_line_set_data(ln, column_index,
+                                       get_id(username_cache,
+                                              (int)file->stat.st_uid)->name))
+                       err(EXIT_FAILURE, _("failed to add output data"));
+               return true;
        case COL_PID:
                xasprintf(&str, "%d", (int)proc->pid);
                break;
index 9b42d151a6e5feb8c5332262fe66c3c3e76fbc94..344cf0a4bb1dafff7536173839330412c742f1f2 100644 (file)
@@ -38,6 +38,7 @@
 #include "strutils.h"
 #include "procutils.h"
 #include "fileutils.h"
+#include "idcache.h"
 
 #include "libsmartcols.h"
 
@@ -55,6 +56,11 @@ static struct list_head *current_proc;
 
 static void *fill_procs(void *arg);
 
+/*
+ * idcaches
+ */
+struct idcache *username_cache;
+
 /*
  * Column related stuffs
  */
@@ -74,6 +80,7 @@ static struct colinfo infos[] = {
        [COL_NAME]    = { "NAME",     0, 0,              N_("name of the file") },
        [COL_COMMAND] = { "COMMAND",  0, 0,              N_("command of the process opening the file") },
        [COL_TYPE]    = { "TYPE",     0, SCOLS_FL_RIGHT, N_("file type") },
+       [COL_USER]    = { "USER",     0, SCOLS_FL_RIGHT, N_("user of the process") },
        [COL_UID]     = { "UID",      0, SCOLS_FL_RIGHT, N_("user ID number") },
        /* DEVICE */
        /* SIZE/OFF */
@@ -463,7 +470,7 @@ int main(int argc, char *argv[])
        if (!ncolumns) {
                columns[ncolumns++] = COL_COMMAND;
                columns[ncolumns++] = COL_PID;
-               columns[ncolumns++] = COL_UID; /* This should be COL_USER. */
+               columns[ncolumns++] = COL_USER;
                columns[ncolumns++] = COL_FD;
                columns[ncolumns++] = COL_TYPE;
                columns[ncolumns++] = COL_NAME;
@@ -473,6 +480,10 @@ int main(int argc, char *argv[])
                                            &ncolumns, column_name_to_id) < 0)
                return EXIT_FAILURE;
 
+       username_cache = new_idcache();
+       if (!username_cache)
+               err(EXIT_FAILURE, _("failed to allocate UID cache"));
+
        scols_init_debug(0);
        ctl.tb = scols_new_table();
 
@@ -500,6 +511,7 @@ int main(int argc, char *argv[])
                        case COL_NAME:
                        case COL_COMMAND:
                        case COL_TYPE:
+                       case COL_USER:
                                scols_column_set_json_type(cl, SCOLS_JSON_STRING);
                                break;
                        case COL_PID:
@@ -520,6 +532,8 @@ int main(int argc, char *argv[])
        emit(&ctl);
        delete(&procs, &ctl);
 
+       free_idcache(username_cache);
+
        return 0;
 }
 
index 0d9c2939c91730ee802e233542a29c9c9d097808..b4e85c195514520d6d23d3102bcdd8e96fa381a0 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <dirent.h>
 
+#include "idcache.h"
 #include "list.h"
 
 /*
@@ -56,6 +57,7 @@ enum {
        COL_COMMAND,
        COL_TYPE,
        COL_UID,
+       COL_USER,
 };
 
 /*
@@ -105,4 +107,6 @@ struct file *make_fd_file(const struct file_class *class,
 struct file *make_regular_fd_file(const struct file_class *class,
                                  struct stat *sb, const char *name, int fd);
 
+extern struct idcache *username_cache;
+
 #endif /* UTIL_LINUX_LSFD_H */