From: Masatake YAMATO Date: Fri, 26 Mar 2021 07:55:57 +0000 (+0900) Subject: lsfd: fill USER field X-Git-Tag: v2.38-rc1~144^2~177 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1deac7dbb4cec9c49654364b72cf15ffa675a567;p=thirdparty%2Futil-linux.git lsfd: fill USER field --- diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index 6b41f06b7f..3a981bf83c 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -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; diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 9b42d151a6..344cf0a4bb 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -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; } diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 0d9c2939c9..b4e85c1955 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -28,6 +28,7 @@ #include #include +#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 */