]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: use NAME column to show cooked file names
authorMasatake YAMATO <yamato@redhat.com>
Mon, 5 Sep 2022 21:27:50 +0000 (06:27 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Mon, 12 Sep 2022 12:46:54 +0000 (21:46 +0900)
This change splits the NAME column
into cooked NAME column and raw KNAME column.

KNAME represents the type information extracted from /proc/$pid/fd or
/proc/$pid/map_files. NAME is the same as KNAME but humans friendly
and talkative if a special handler is implemented for the type of the
file.

Currently only pidfd has such a handler.

Below an example output:

    $ ./lsfd -o TYPE,STTYPE,KNAME,NAME -Q '(TYPE == "pidfd")'
     TYPE STTYPE KNAME              NAME
    pidfd   UNKN anon_inode:[pidfd] pid=4661 comm=dbus-broker nspid=4661
    pidfd   UNKN anon_inode:[pidfd] pid=4924 comm=dbus-broker nspid=4924

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

index 2b4e8dc96843bb88ab3a6f3436d2c6bab4f542f7..c82105c75ec3ce68abf38ac1b0b628bab571dc11 100644 (file)
@@ -121,6 +121,7 @@ static bool file_fill_column(struct proc *proc,
                    && scols_line_set_data(ln, column_index, proc->command))
                        err(EXIT_FAILURE, _("failed to add output data"));
                return true;
+       case COL_KNAME:
        case COL_NAME:
                if (file->name
                    && scols_line_set_data(ln, column_index, file->name))
index 78fbd19c8790748d58ed31bfb10b333063bf4ce3..c7adbf35828bde2c0ab0bb1816f0439500e562a0 100644 (file)
@@ -51,13 +51,6 @@ static bool sock_fill_column(struct proc *proc __attribute__((__unused__)),
                        if (scols_line_set_data(ln, column_index, sock->protoname))
                                err(EXIT_FAILURE, _("failed to add output data"));
                return true;
-       case COL_NAME:
-               if (sock->protoname
-                   && file->name && strncmp(file->name, "socket:", 7) == 0) {
-                       xasprintf(&str, "%s:%s", sock->protoname, file->name + 7);
-                       break;
-               }
-               return false;
        case COL_SOURCE:
                if (major(file->stat.st_dev) == 0
                    && strncmp(file->name, "socket:", 7) == 0) {
index 1dd9d4b669ce4e57f51ea225b222a65d2ec50e85..470ca58ac9c931daa65be8eb4e9a60a6a7a67ee0 100644 (file)
@@ -164,7 +164,7 @@ static char *anon_pidfd_get_name(struct unkn *unkn)
        if (proc)
                comm = proc->command;
 
-       xasprintf(&str, "pidfd: pid=%d comm=%s nspid=%s",
+       xasprintf(&str, "pid=%d comm=%s nspid=%s",
                  data->pid,
                  comm? comm: "",
                  data->nspid? data->nspid: "");
index 51d92673e18663ae2039d1456cced1565b011a0c..f782b83c21080bb8c7374ba892678f3e2144dba9 100644 (file)
@@ -164,6 +164,18 @@ User ID number of the file's owner.
 INODE <``number``>::
 Inode number.
 
+KNAME <``string``>::
+//
+// It seems that the manpage backend of asciidoctor has limitations
+// about emitting text with nested face specifications like:
+//
+//   `_u_` p
+//
+// Not only u but also p is decorated with underline.
+//
+Raw file name extracted from
+from ``/proc/``_pid_``/fd/``_fd_ or ``/proc/``_pid_``/map_files/``_region_.
+
 KTHREAD <``boolean``>::
 Whether the process is a kernel thread or not.
 
@@ -183,22 +195,12 @@ MODE <``string``>::
 Access mode (rwx).
 
 NAME <``string``>::
-Name of the file.
-//
-// It seems that the manpage backend of asciidoctor has limitations
-// about emitting text with nested face specifications like:
-//
-//   `_u_` p
-//
-// Not only u but also p is decorated with underline.
-//
-For the most of all files, *lsfd* extracts their names
-from ``/proc/``_pid_``/fd/``_fd_ or ``/proc/``_pid_``/map_files/``_region_.
+Cooked version of KNAME. It is mostly same as KNAME.
 +
 Some files have special formats and information sources:
 +
 pidfd:::
-pidfd: pid=_TARGET-PID_ comm=_TARGET-COMMAND_ nspid=_TARGET-NSPIDS_
+pid=_TARGET-PID_ comm=_TARGET-COMMAND_ nspid=_TARGET-NSPIDS_
 +
 *lsfd* extracts _TARGET-PID_ and _TARGET-NSPIDS_ from
 ``/proc/``_pid_``/fdinfo/``_fd_.
index 093f55e06c8620cc0e0a16e5f5f1faf271817507..f3b008d7a283d1a89976f8e2aabfe424dd4222d2 100644 (file)
@@ -147,6 +147,8 @@ static struct colinfo infos[] = {
                N_("user ID number of the file's owner") },
        [COL_INODE]   = { "INODE",    0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
                N_("inode number") },
+       [COL_KNAME]   = { "KNAME",  0.4, SCOLS_FL_TRUNC, SCOLS_JSON_STRING,
+               N_("name of the file (raw)") },
        [COL_KTHREAD] = { "KTHREAD",  0, SCOLS_FL_RIGHT, SCOLS_JSON_BOOLEAN,
                N_("opened by a kernel thread") },
        [COL_MAJMIN]  = { "MAJ:MIN",  0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
@@ -160,7 +162,7 @@ static struct colinfo infos[] = {
        [COL_MODE]    = { "MODE",     0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
                N_("access mode (rwx)") },
        [COL_NAME]    = { "NAME",   0.4, SCOLS_FL_TRUNC, SCOLS_JSON_STRING,
-               N_("name of the file") },
+               N_("name of the file (cooked)") },
        [COL_NLINK]   = { "NLINK",    0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
                N_("link count") },
        [COL_OWNER]   = { "OWNER",    0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
index bea5f8cfb1bb86cb7d0b70a5ca9c8c7561a95724..794e2cc2a61696d24534aab4325a435a55e43eeb 100644 (file)
@@ -48,6 +48,7 @@ enum {
        COL_FD,
        COL_FLAGS,
        COL_INODE,
+       COL_KNAME,
        COL_KTHREAD,
        COL_MAJMIN,
        COL_MAPLEN,
diff --git a/tests/expected/lsfd/column-name b/tests/expected/lsfd/column-name
new file mode 100644 (file)
index 0000000..0e0ac1d
--- /dev/null
@@ -0,0 +1,6 @@
+    3 /etc/passwd /etc/passwd
+ro-regular-file:ASSOC,KNAME,NAME: 0
+    3 anon_inode:[pidfd] pid=1 comm= nspid=1
+pidfd:ASSOC,KNAME,NAME: 0
+    3 socket:[INODENUM] socket:[INODENUM]
+socketpair:ASSOC,KNAME,NAME: 0
index 5fbb3aa10290880c106242a94353aa50b70fe48f..8fc4b55d703f601bba6b4dc22b6bae89e5afb42d 100644 (file)
@@ -1,2 +1,2 @@
-    3   UNKN anon_inodefs pidfd: pid=1 comm=systemd nspid=1
+    3   UNKN anon_inodefs pid=1 comm=systemd nspid=1
 ASSOC,STTYPE,SOURCE,NAME: 0
diff --git a/tests/ts/lsfd/column-name b/tests/ts/lsfd/column-name
new file mode 100755 (executable)
index 0000000..c2b80cb
--- /dev/null
@@ -0,0 +1,56 @@
+#!/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="NAME and KNAME column"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_LSFD"
+ts_check_test_command "$TS_HELPER_MKFDS"
+ts_check_prog "sed"
+
+ts_cd "$TS_OUTDIR"
+
+PID=
+FD=3
+EXPR="(FD == 3)"
+
+{
+    for C in ro-regular-file pidfd socketpair; do
+       coproc MKFDS {
+           case $C in
+               socketpair)
+                   argv="$((FD + 1)) socktype=DGRAM"
+           esac
+           "$TS_HELPER_MKFDS" $C $FD ${argv}
+       }
+       if read -u ${MKFDS[0]} PID; then
+           ${TS_CMD_LSFD} -n -o ASSOC,KNAME,NAME -p "${PID}" -Q "${EXPR}" | {
+               # Replace the unpredictable an inode number for the socket
+               # with "INODENUM".
+               sed -e 's/\[[0-9]\+\]/[INODENUM]/g'
+           }
+           echo "$C"':ASSOC,KNAME,NAME': $?
+
+           kill -CONT ${PID}
+           wait ${MKFDS_PID}
+       fi
+    done
+} > $TS_OUTPUT 2>&1
+
+ts_finalize