From 4cd4ed189a5112c8b64359fdde253f51113f126f Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sat, 3 Sep 2022 06:24:03 +0900 Subject: [PATCH] lsfd: show classes of anonyomous inodes in AINODECLASS column 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 --- misc-utils/lsfd-unkn.c | 28 ++++++++++++++++ misc-utils/lsfd.1.adoc | 3 ++ misc-utils/lsfd.c | 3 ++ misc-utils/lsfd.h | 1 + tests/expected/lsfd/column-ainodeclass | 4 +++ tests/ts/lsfd/column-ainodeclass | 45 ++++++++++++++++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 tests/expected/lsfd/column-ainodeclass create mode 100755 tests/ts/lsfd/column-ainodeclass diff --git a/misc-utils/lsfd-unkn.c b/misc-utils/lsfd-unkn.c index 876e7f1288..4bcebfcffa 100644 --- a/misc-utils/lsfd-unkn.c +++ b/misc-utils/lsfd-unkn.c @@ -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, diff --git a/misc-utils/lsfd.1.adoc b/misc-utils/lsfd.1.adoc index 8ebabe3251..0c3c56bfa4 100644 --- a/misc-utils/lsfd.1.adoc +++ b/misc-utils/lsfd.1.adoc @@ -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. diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 4bd6016417..f67301a1b2 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -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, diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 617fefb061..7e243c5fa2 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -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 index 0000000000..fa164eb2a1 --- /dev/null +++ b/tests/expected/lsfd/column-ainodeclass @@ -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 index 0000000000..2e768bc35c --- /dev/null +++ b/tests/ts/lsfd/column-ainodeclass @@ -0,0 +1,45 @@ +#!/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="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 -- 2.47.3