From f3609f0ac5d4a7ede5e0a6de3fb5252a5cddb65c Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Tue, 6 Sep 2022 06:27:50 +0900 Subject: [PATCH] lsfd: use NAME column to show cooked file names 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 --- misc-utils/lsfd-file.c | 1 + misc-utils/lsfd-sock.c | 7 ----- misc-utils/lsfd-unkn.c | 2 +- misc-utils/lsfd.1.adoc | 26 ++++++++------- misc-utils/lsfd.c | 4 ++- misc-utils/lsfd.h | 1 + tests/expected/lsfd/column-name | 6 ++++ tests/expected/lsfd/mkfds-pidfd | 2 +- tests/ts/lsfd/column-name | 56 +++++++++++++++++++++++++++++++++ 9 files changed, 83 insertions(+), 22 deletions(-) create mode 100644 tests/expected/lsfd/column-name create mode 100755 tests/ts/lsfd/column-name diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index 2b4e8dc968..c82105c75e 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -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)) diff --git a/misc-utils/lsfd-sock.c b/misc-utils/lsfd-sock.c index 78fbd19c87..c7adbf3582 100644 --- a/misc-utils/lsfd-sock.c +++ b/misc-utils/lsfd-sock.c @@ -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) { diff --git a/misc-utils/lsfd-unkn.c b/misc-utils/lsfd-unkn.c index 1dd9d4b669..470ca58ac9 100644 --- a/misc-utils/lsfd-unkn.c +++ b/misc-utils/lsfd-unkn.c @@ -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: ""); diff --git a/misc-utils/lsfd.1.adoc b/misc-utils/lsfd.1.adoc index 51d92673e1..f782b83c21 100644 --- a/misc-utils/lsfd.1.adoc +++ b/misc-utils/lsfd.1.adoc @@ -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_. diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 093f55e06c..f3b008d7a2 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -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, diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index bea5f8cfb1..794e2cc2a6 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -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 index 0000000000..0e0ac1d9da --- /dev/null +++ b/tests/expected/lsfd/column-name @@ -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 diff --git a/tests/expected/lsfd/mkfds-pidfd b/tests/expected/lsfd/mkfds-pidfd index 5fbb3aa102..8fc4b55d70 100644 --- a/tests/expected/lsfd/mkfds-pidfd +++ b/tests/expected/lsfd/mkfds-pidfd @@ -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 index 0000000000..c2b80cb8e9 --- /dev/null +++ b/tests/ts/lsfd/column-name @@ -0,0 +1,56 @@ +#!/bin/bash +# +# Copyright (C) 2022 Masatake YAMATO +# +# 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 -- 2.47.3