]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: add flags, [-lL], representing file lock/lease states to XMODE column
authorMasatake YAMATO <yamato@redhat.com>
Wed, 5 Jul 2023 02:48:33 +0000 (11:48 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Wed, 5 Jul 2023 07:01:09 +0000 (16:01 +0900)
The flags hide the details of locking: methods for locking (including
lease), mandatory or advisory, and ranges. The flags can be used only
for distinguishing three states: not locked, read (or shared) locked,
or write (or exclusive) locked.

An example output:

  # ./lsfd -Q '(XMODE =~ "....[lL]")'
  COMMAND             PID   USER ASSOC XMODE TYPE SOURCE MNTID     INODE NAME
  abrtd              1854   root     7 rw--L  REG  tmpfs    27      3093 /run/abrt/abrtd.pid
  ...
  qemu-system-x86 2846033   qemu    11 rw--l  REG   dm-2  1313   5146111 /var/lib/libvirt/images/acn.qcow2

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-file.c
misc-utils/lsfd.1.adoc
misc-utils/lsfd.h
tests/expected/lsfd/column-xmode-XMODE-D-bit
tests/expected/lsfd/column-xmode-XMODE-r-bit
tests/expected/lsfd/column-xmode-XMODE-w-bit
tests/expected/lsfd/column-xmode-XMODE-x-bit

index 029cc81fc880a1058f4586cf4d453019a001d37f..c4546126ff0ad113614a70fa38718f43430978cf 100644 (file)
@@ -299,6 +299,10 @@ static bool file_fill_column(struct proc *proc,
        case COL_XMODE: {
                char r, w, x;
                char D = file->stat.st_nlink == 0? 'D': '-';
+               char L = file->locked.write? 'L'
+                       :file->locked.read?  'l'
+                       :                    '-';
+
                if (does_file_has_fdinfo_alike(file)) {
                        r = file->mode & S_IRUSR? 'r': '-';
                        w = file->mode & S_IWUSR? 'w': '-';
@@ -306,7 +310,7 @@ static bool file_fill_column(struct proc *proc,
                             && file->mode & S_IXUSR)? 'x': '-';
                } else
                        r = w = x = '-';
-               xasprintf(&str, "%c%c%c%c", r, w, x, D);
+               xasprintf(&str, "%c%c%c%c%c", r, w, x, D, L);
                break;
        }
        case COL_POS:
@@ -344,6 +348,40 @@ static bool file_fill_column(struct proc *proc,
        return true;
 }
 
+enum lock_mode {
+       LOCK_NONE,
+       READ_LOCK,
+       WRITE_LOCK,
+};
+
+static unsigned int parse_lock_line(const char *line)
+{
+       char mode[6] = {0};
+
+       /* Exapmles of lines:
+          ----------------------------------------------------
+          1: FLOCK  ADVISORY  READ 2283292 fd:03:26219728 0 EOF
+          1: FLOCK  ADVISORY  WRITE 2283321 fd:03:26219728 0 EOF
+          1: POSIX  ADVISORY  READ 2283190 fd:03:26219728 0 0
+          1: POSIX  ADVISORY  WRITE 2283225 fd:03:26219728 0 0
+          1: OFDLCK ADVISORY  READ -1 fd:03:26219728 0 0
+          1: OFDLCK ADVISORY  WRITE -1 fd:03:26219728 0 0
+          1: LEASE  ACTIVE    WRITE 2328907 fd:03:26219472 0 EOF
+          1: LEASE  ACTIVE    READ 2326777 fd:03:26219472 0 EOF
+          ---------------------------------------------------- */
+
+       if (sscanf(line, "%*d: %*s %*s %5s %*s", mode) != 1)
+               return LOCK_NONE;
+
+       if (strcmp(mode, "READ") == 0)
+               return READ_LOCK;
+
+       if (strcmp(mode, "WRITE") == 0)
+               return WRITE_LOCK;
+
+       return LOCK_NONE;
+}
+
 static int file_handle_fdinfo(struct file *file, const char *key, const char* value)
 {
        int rc;
@@ -357,6 +395,16 @@ static int file_handle_fdinfo(struct file *file, const char *key, const char* va
        } else if (strcmp(key, "mnt_id") == 0) {
                rc = ul_strtou32(value, &file->mnt_id, 10);
 
+       } else if (strcmp(key, "lock") == 0) {
+               switch (parse_lock_line(value)) {
+               case READ_LOCK:
+                       file->locked.read = 1;
+                       break;
+               case WRITE_LOCK:
+                       file->locked.write = 1;
+                       break;
+               }
+               rc = 1;
        } else
                return 0;       /* ignore -- unknown item */
 
index ec67e0c34654d48d4025cd5621e9df40846a096b..907fe2d1539d0919a70eab386545187a02a40c7a 100644 (file)
@@ -479,7 +479,7 @@ User of the process.
 
 XMODE <``string``>::
 Extended version of _MODE_. This column may grow; new letters may be
-added to _XMODE_ when *lsfd* supports a new state of file descriptors
+appended to _XMODE_ when *lsfd* supports a new state of file descriptors
 and/or memory mappings.
 +
 [-r]:::
@@ -492,7 +492,13 @@ opened of mapped for writing. This is also in _MODE_.
 mapped for executing the code. This is also in _MODE_.
 +
 [-D]:::
-opened file is deleted from  the file system. See also _DELETED._.
+deleted from  the file system. See also _DELETED._.
++
+[-Ll]:::
+locked or leased. _l_ represents a read, a shared lock or a read lease.
+_L_ represents a write or an exclusive lock or a write lease. If both
+read/shared and write/exclusive locks or leases are taken by a file
+descriptor, _L_ is used as the flag.
 
 == FILTER EXPRESSION
 
@@ -737,6 +743,7 @@ mailto:kzak@redhat.com[Karel Zak]
 
 == SEE ALSO
 
+*lslocks*(8)
 *lsof*(8)
 *pidof*(1)
 *proc*(5)
index 123134559a9f02a98b2ddcc7e09ef7d23e18149e..ef02d4c3b47fb17d5381f2822e9a739d92d2f85d 100644 (file)
@@ -171,6 +171,10 @@ struct file {
 
        unsigned int sys_flags;
        unsigned int mnt_id;
+
+       struct {
+               uint8_t read:1, write:1;
+       } locked;
 };
 
 #define is_opened_file(_f) ((_f)->association >= 0)
index 906370d0cf704a3d333d1e8039d0f38d4732b03b..9460c979ee024063f2c7433babc1a83a7754bd19 100644 (file)
@@ -1,2 +1,2 @@
- -w-D
+-w-D-
 XMODE(D-bit):  0
index dfbdf4b5db3fb934a37094b134f2a1fb6ea3a97a..b4a53caac8aa38554a482e3b9f5bd88b7105680f 100644 (file)
@@ -1,2 +1,2 @@
- r---
+r----
 XMODE(r-bit):  0
index 90c2665b76aa6b9d905920239e7e542eb3b70eac..72d693e9e1f2eccab73acb461d6c746bc4aaae62 100644 (file)
@@ -1,2 +1,2 @@
- -w--
+-w---
 XMODE(w-bit):  0
index f8560693304c713f2ca1f53cd3bdb75ef0c708f2..831a27049db873084ff7ee955adf08efc0a8be41 100644 (file)
@@ -1,2 +1,2 @@
- r-x-
+r-x--
 XMODE(x-bit):  0