]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: add FLAGS, MNTID, and POS columns
authorMasatake YAMATO <yamato@redhat.com>
Thu, 6 May 2021 05:28:58 +0000 (14:28 +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-file.c
misc-utils/lsfd.c
misc-utils/lsfd.h

index 83433f198b2871e4885fd342c4d475065bab4301..ab55d25eb28fb6f5890638256f5f1d8eaca7ecdd 100644 (file)
@@ -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,
 };
index c655996bb2b0f2a264e6240f0fef1e2844823949..9259974a251aeb10589fbcc3f41c7187aa165716 100644 (file)
@@ -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,
 };
index f487e115c2d4091015ef3d3191a6c1f7b1b7eac3..473d477ad4ad13568c5abaef7a59064449c46a2e 100644 (file)
@@ -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 {