]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: show classes of anonyomous inodes in AINODECLASS column
authorMasatake YAMATO <yamato@redhat.com>
Fri, 2 Sep 2022 21:24:03 +0000 (06:24 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Thu, 8 Sep 2022 18:05:00 +0000 (03:05 +0900)
Below an example output:

    $ ./lsfd -oCOMMAND,PID,USER,ASSOC,TYPE,AINODECLASS -Q '(SOURCE == "anon_inodefs")' | head
    COMMAND             PID USER ASSOC TYPE AINODECLASS
    systemd            1360  jet     4 UNKN   eventpoll
    systemd            1360  jet     5 UNKN    signalfd
    systemd            1360  jet     6 UNKN     inotify
    systemd            1360  jet     8 UNKN     timerfd
    systemd            1360  jet     9 UNKN   eventpoll
    systemd            1360  jet    11 UNKN     inotify
    systemd            1360  jet    13 UNKN     inotify
    systemd            1360  jet    21 UNKN     timerfd
    gnome-keyring-d    4598  jet     5 UNKN     eventfd

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-unkn.c
misc-utils/lsfd.1.adoc
misc-utils/lsfd.c
misc-utils/lsfd.h
tests/expected/lsfd/column-ainodeclass [new file with mode: 0644]
tests/ts/lsfd/column-ainodeclass [new file with mode: 0755]

index 876e7f12889f44b1288eb9b26b4681c2a9c7736c..4bcebfcffa5940cd2a906862fa2577ce9c40a3ae 100644 (file)
@@ -32,6 +32,7 @@ struct unkn {
 };
 
 struct anon_ops {
+       const char *class;
        char * (*get_name)(struct unkn *);
        void (*init)(struct unkn *);
        void (*free)(struct unkn *);
@@ -41,6 +42,25 @@ struct anon_ops {
 static struct anon_ops anon_generic_ops;
 static struct anon_ops anon_pidfd_ops;
 
+static char * anon_get_class(struct unkn *unkn)
+{
+       char *name;
+
+       if (unkn->anon_ops->class)
+               return strdup(unkn->anon_ops->class);
+
+       /* See unkn_init_content() */
+       name = ((struct file *)unkn)->name + 11;
+       /* Does it have the form anon_inode:[class]? */
+       if (*name == '[') {
+               size_t len = strlen(name + 1);
+               if (*(name + 1 + len - 1) == ']')
+                       return strndup(name + 1, len - 1);
+       }
+
+       return strdup(name);
+}
+
 static bool unkn_fill_column(struct proc *proc __attribute__((__unused__)),
                             struct file *file,
                             struct libscols_line *ln,
@@ -62,6 +82,12 @@ static bool unkn_fill_column(struct proc *proc __attribute__((__unused__)),
                if (scols_line_set_data(ln, column_index, "UNKN"))
                        err(EXIT_FAILURE, _("failed to add output data"));
                return true;
+       case COL_AINODECLASS:
+               if (unkn->anon_ops) {
+                       str = anon_get_class(unkn);
+                       break;
+               }
+               return false;
        case COL_SOURCE:
                if (unkn->anon_ops) {
                        str = strdup("anon_inodefs");
@@ -179,6 +205,7 @@ static int anon_pidfd_handle_fdinfo(struct unkn *unkn, const char *key, const ch
 }
 
 static struct anon_ops anon_pidfd_ops = {
+       .class = "pidfd",
        .get_name = anon_pidfd_get_name,
        .init = anon_pidfd_init,
        .free = anon_pidfd_free,
@@ -189,6 +216,7 @@ static struct anon_ops anon_pidfd_ops = {
  * generic (fallback implementation)
  */
 static struct anon_ops anon_generic_ops = {
+       .class = NULL,
        .get_name = NULL,
        .init = NULL,
        .free = NULL,
index 8ebabe3251cdee5a6e5fa8849773d3f21e76b7ae..0c3c56bfa435fcfc7907017153978856ccf77e56 100644 (file)
@@ -112,6 +112,9 @@ Each column has a type. Types are surround by < and >.
 CAUTION{colon} The names and types of columns are not stable yet.
 They may be changed in the future releases.
 
+AINODECLASS <``string``>::
+Class of anonymous inode.
+
 ASSOC <``string``>::
 Association between file and process.
 
index 4bd6016417ef4bf1d25b427b58341c2fb2c2a74c..f67301a1b27fe19434ea7fcfa32d0ddbe7b93ce5 100644 (file)
@@ -120,6 +120,9 @@ struct colinfo {
 
 /* columns descriptions */
 static struct colinfo infos[] = {
+       [COL_AINODECLASS]
+                     = { "AINODECLASS",0,SCOLS_FL_RIGHT,SCOLS_JSON_STRING,
+               N_("class of anonymous inode") },
        [COL_ASSOC]   = { "ASSOC",    0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
                N_("association between file and process") },
        [COL_BLKDRV]  = { "BLKDRV",   0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
index 617fefb061ddb56503d3dff18e68d6b5c3ec38f2..7e243c5fa2189f9ac6191d6523bf9dcdbe9e3dc8 100644 (file)
@@ -36,6 +36,7 @@
  * column IDs
  */
 enum {
+       COL_AINODECLASS,
        COL_ASSOC,
        COL_BLKDRV,
        COL_CHRDRV,
diff --git a/tests/expected/lsfd/column-ainodeclass b/tests/expected/lsfd/column-ainodeclass
new file mode 100644 (file)
index 0000000..fa164eb
--- /dev/null
@@ -0,0 +1,4 @@
+    3 UNKN       pidfd
+pidfd:ASSOC,TYPE,AINODECLASS: 0
+    3 UNKN     inotify
+inotify:ASSOC,TYPE,AINODECLASS: 0
diff --git a/tests/ts/lsfd/column-ainodeclass b/tests/ts/lsfd/column-ainodeclass
new file mode 100755 (executable)
index 0000000..2e768bc
--- /dev/null
@@ -0,0 +1,45 @@
+#!/bin/bash
+#
+# Copyright (C) 2022 Masatake YAMATO <yamato@redhat.com>
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="ainodeclass column"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_LSFD"
+ts_check_test_command "$TS_HELPER_MKFDS"
+
+ts_cd "$TS_OUTDIR"
+
+PID=
+FD=3
+EXPR="(FD == 3)"
+
+{
+    for C in pidfd inotify; do
+       coproc MKFDS { "$TS_HELPER_MKFDS" $C $FD ; }
+       if read -u ${MKFDS[0]} PID; then
+           ${TS_CMD_LSFD} -n -o ASSOC,TYPE,AINODECLASS -p "${PID}" -Q "${EXPR}"
+           echo "$C"':ASSOC,TYPE,AINODECLASS': $?
+
+           kill -CONT ${PID}
+           wait ${MKFDS_PID}
+       fi
+    done
+} > $TS_OUTPUT 2>&1
+
+ts_finalize