From 22b7cf51cc36cf72988bedbe3d4275adfe0033bd Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Thu, 6 May 2021 14:28:58 +0900 Subject: [PATCH] lsfd: add FLAGS, MNTID, and POS columns Signed-off-by: Masatake YAMATO --- misc-utils/lsfd-file.c | 131 +++++++++++++++++++++++++++++++++++++++++ misc-utils/lsfd.c | 12 ++-- misc-utils/lsfd.h | 6 ++ 3 files changed, 145 insertions(+), 4 deletions(-) diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index 83433f198b..ab55d25eb2 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -24,6 +24,7 @@ #include "xalloc.h" #include "nls.h" +#include "buffer.h" #include "libsmartcols.h" @@ -70,6 +71,98 @@ static const char *strftype(mode_t ftype) } } +/* See /usr/include/asm-generic/fcntl.h */ +static void file_fill_flags_buf(struct ul_buffer *buf, int flags) +{ +#define SET_FLAG_FULL(L,s) \ + do { \ + if (flags & (L)) { \ + if (!ul_buffer_is_empty(buf)) \ + ul_buffer_append_data(buf, ",", 1); \ + ul_buffer_append_string(buf, #s); \ + } \ + } while (0) + +#define SET_FLAG(L,s) SET_FLAG_FULL(O_##L,s) + +#ifdef O_WRONLY + SET_FLAG(WRONLY,wronly); +#endif + +#ifdef O_RDWR + SET_FLAG(RDWR,rdwr); +#endif + +#ifdef O_CREAT + SET_FLAG(CREAT,creat); +#endif + +#ifdef O_EXCL + SET_FLAG(EXCL,excl); +#endif + +#ifdef O_NOCTTY + SET_FLAG(NOCTTY,noctty); +#endif + +#ifdef O_NOCTTY + SET_FLAG(NOCTTY,noctty); +#endif + +#ifdef O_APPEND + SET_FLAG(APPEND,append); +#endif + +#ifdef O_NONBLOCK + SET_FLAG(NONBLOCK,nonblock); +#endif + +#ifdef O_DSYNC + SET_FLAG(DSYNC,dsync); +#endif + +#ifdef O_FASYNC + SET_FLAG(FASYNC,fasync); +#endif + +#ifdef O_DIRECT + SET_FLAG(DIRECT,direct); +#endif + +#ifdef O_LARGEFILE + SET_FLAG(LARGEFILE,largefile); +#endif + +#ifdef O_DIRECTORY + SET_FLAG(DIRECTORY,directory); +#endif + +#ifdef O_FOLLOW + SET_FLAG(FOLLOW,follow); +#endif + +#ifdef O_NOATIME + SET_FLAG(NOATIME,noatime); +#endif + +#ifdef O_CLOEXEC + SET_FLAG(CLOEXEC,cloexec); +#endif + +#ifdef __O_SYNC + SET_FLAG_FULL(__O_SYNC,_sync); +#endif + +#ifdef O_PATH + SET_FLAG(PATH,path); +#endif + +#ifdef __O_TMPFILE + SET_FLAG_FULL(__O_TMPFILE,_tmpfile); +#endif + +} + static bool file_fill_column(struct proc *proc, struct file *file, struct libscols_line *ln, @@ -148,6 +241,28 @@ static bool file_fill_column(struct proc *proc, case COL_DELETED: xasprintf(&str, "%d", file->stat.st_nlink == 0); break; + case COL_MNT_ID: + xasprintf(&str, "%d", file->association < 0? 0: file->mnt_id); + break; + case COL_POS: + xasprintf(&str, "%llu", + file->association < 0? 0: file->pos); + break; + case COL_FLAGS: { + struct ul_buffer buf = UL_INIT_BUFFER; + + if (file->association < 0) + return true; + + if (file->flags == 0) + return true; + + file_fill_flags_buf(&buf, file->flags); + if (ul_buffer_is_empty(&buf)) + return true; + str = ul_buffer_get_data(&buf, NULL, NULL); + break; + } default: return false; }; @@ -159,6 +274,21 @@ static bool file_fill_column(struct proc *proc, return true; } +static int file_handle_fdinfo(struct file *file, const char *key, const char* value) +{ + if (strcmp(key, "pos") == 0) { + file->pos = strtoull(value, NULL, 10); + return 1; + } else if (strcmp(key, "flags") == 0) { + file->flags = (int)strtol(value, NULL, 8); + return 1; + } else if (strcmp(key, "mnt_id") == 0) { + file->mnt_id = (int)strtol(value, NULL, 10); + return 1; + } + return 0; +} + static void file_free_content(struct file *file) { free(file->name); @@ -183,5 +313,6 @@ const struct file_class file_class = { .super = NULL, .size = sizeof(struct file), .fill_column = file_fill_column, + .handle_fdinfo = file_handle_fdinfo, .free_content = file_free_content, }; diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index c655996bb2..9259974a25 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -94,16 +94,22 @@ static struct colinfo infos[] = { N_("ID of device containing file") }, [COL_DEVICE] = { "DEVICE", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING, N_("device ID for special, or ID of device containing file") }, + [COL_FLAGS] = { "FLAGS", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING, + N_("flags specified when opening the file") }, [COL_FD] = { "FD", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, N_("file descriptor for the file") }, [COL_INODE] = { "INODE", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, N_("inode number") }, + [COL_MNT_ID] = { "MNTID", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, + N_("mount id") }, [COL_NAME] = { "NAME", 45, 0, SCOLS_JSON_STRING, N_("name of the file") }, [COL_NLINK] = { "NLINK", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, N_("link count") }, [COL_PID] = { "PID", 5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, N_("PID of the process opening the file") }, + [COL_POS] = { "POS", 5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, + N_("file position") }, [COL_RDEV] = { "RDEV", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING, N_("device ID (if special file)") }, [COL_SIZE] = { "SIZE", 4, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, @@ -116,8 +122,6 @@ static struct colinfo infos[] = { N_("user ID number") }, [COL_USER] = { "USER", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING, N_("user of the process") }, - /* SIZE/OFF */ - /* MNTID */ }; static int default_columns[] = { @@ -127,7 +131,7 @@ static int default_columns[] = { COL_ASSOC, COL_TYPE, COL_DEVICE, - COL_SIZE, + COL_POS, COL_INODE, COL_NAME, }; @@ -140,7 +144,7 @@ static int default_threads_columns[] = { COL_ASSOC, COL_TYPE, COL_DEVICE, - COL_SIZE, + COL_POS, COL_INODE, COL_NAME, }; diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index f487e115c2..473d477ad4 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -57,10 +57,13 @@ enum { COL_DEVICE, COL_DEV, COL_FD, + COL_FLAGS, COL_INODE, + COL_MNT_ID, COL_NAME, COL_NLINK, COL_PID, + COL_POS, COL_RDEV, COL_SIZE, COL_TID, @@ -108,6 +111,9 @@ struct file { int association; char *name; struct stat stat; + unsigned long long pos; + int flags; + int mnt_id; }; struct file_class { -- 2.47.3