]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: show pid, comm, and nspid of pidfd in PIDFD.{PID,COMM,NSPID} columns
authorMasatake YAMATO <yamato@redhat.com>
Fri, 9 Sep 2022 17:03:45 +0000 (02:03 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Mon, 12 Sep 2022 12:48:53 +0000 (21:48 +0900)
The values shown in the new columns are already shown in NAME column.
Showing these values together in the NAME column helps users
understand the pidfds opened quickly. However, the showing them
together is not suitable for machine processing. The newly introduced
columns are for machine processing.

Below an example output:

    $ ./lsfd -o COMMAND,TYPE,PIDFD.COMM,PIDFD.PID -Q '(TYPE == "pidfd")'
    COMMAND          TYPE PIDFD.COMM  PIDFD.PID
    dbus-broker-lau pidfd dbus-broker      4661
    dbus-broker-lau pidfd dbus-broker      4924

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-unkn.c
misc-utils/lsfd.c
misc-utils/lsfd.h
tests/expected/lsfd/mkfds-pidfd
tests/ts/lsfd/mkfds-pidfd

index 470ca58ac9c931daa65be8eb4e9a60a6a7a67ee0..e1fd259410b3a1ecd96a8adbc3fe4d3204d8a868 100644 (file)
@@ -34,6 +34,13 @@ struct unkn {
 struct anon_ops {
        const char *class;
        char * (*get_name)(struct unkn *);
+       /* Return true is handled the column. */
+       bool (*fill_column)(struct proc *,
+                           struct unkn *,
+                           struct libscols_line *,
+                           int,
+                           size_t,
+                           char **str);
        void (*init)(struct unkn *);
        void (*free)(struct unkn *);
        int (*handle_fdinfo)(struct unkn *, const char *, const char *);
@@ -61,7 +68,7 @@ static char * anon_get_class(struct unkn *unkn)
        return strdup(name);
 }
 
-static bool unkn_fill_column(struct proc *proc __attribute__((__unused__)),
+static bool unkn_fill_column(struct proc *proc,
                             struct file *file,
                             struct libscols_line *ln,
                             int column_id,
@@ -95,6 +102,11 @@ static bool unkn_fill_column(struct proc *proc __attribute__((__unused__)),
                }
                return false;
        default:
+               if (unkn->anon_ops && unkn->anon_ops->fill_column) {
+                       if (unkn->anon_ops->fill_column(proc, unkn, ln,
+                                                       column_id, column_index, &str))
+                               break;
+               }
                return false;
        }
 
@@ -204,9 +216,45 @@ static int anon_pidfd_handle_fdinfo(struct unkn *unkn, const char *key, const ch
        return 0;
 }
 
+static bool anon_pidfd_fill_column(struct proc *proc  __attribute__((__unused__)),
+                                  struct unkn *unkn,
+                                  struct libscols_line *ln __attribute__((__unused__)),
+                                  int column_id,
+                                  size_t column_index __attribute__((__unused__)),
+                                  char **str)
+{
+       struct anon_pidfd_data *data = (struct anon_pidfd_data *)unkn->anon_data;
+
+       switch(column_id) {
+       case COL_PIDFD_COMM: {
+               struct proc *pidfd_proc = get_proc(data->pid);
+               char *pidfd_comm = NULL;
+               if (pidfd_proc)
+                       pidfd_comm = pidfd_proc->command;
+               if (pidfd_comm) {
+                       *str = strdup(pidfd_comm);
+                       return true;
+               }
+               break;
+       }
+       case COL_PIDFD_NSPID:
+               if (data->nspid) {
+                       *str = strdup(data->nspid);
+                       return true;
+               }
+               break;
+       case COL_PIDFD_PID:
+               xasprintf(str, "%d", (int)data->pid);
+               return true;
+       }
+
+       return false;
+}
+
 static struct anon_ops anon_pidfd_ops = {
        .class = "pidfd",
        .get_name = anon_pidfd_get_name,
+       .fill_column = anon_pidfd_fill_column,
        .init = anon_pidfd_init,
        .free = anon_pidfd_free,
        .handle_fdinfo = anon_pidfd_handle_fdinfo,
@@ -218,6 +266,7 @@ static struct anon_ops anon_pidfd_ops = {
 static struct anon_ops anon_generic_ops = {
        .class = NULL,
        .get_name = NULL,
+       .fill_column = NULL,
        .init = NULL,
        .free = NULL,
        .handle_fdinfo = NULL,
index f3b008d7a283d1a89976f8e2aabfe424dd4222d2..9981b6a363d81ca4c1e16a8586cccbe8553f0747 100644 (file)
@@ -171,6 +171,12 @@ static struct colinfo infos[] = {
                N_("PID of the process opening the file") },
        [COL_PARTITION]={ "PARTITION",0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
                N_("block device name resolved by /proc/partition") },
+       [COL_PIDFD_COMM]={"PIDFD.COMM",0.2,SCOLS_FL_TRUNC,SCOLS_JSON_STRING,
+               N_("command of the process targeted by the pidfd") },
+       [COL_PIDFD_NSPID]={"PIDFD.NSPID",0.2,SCOLS_FL_TRUNC,SCOLS_JSON_STRING,
+               N_("NSpid field in fdinfo of the pidfd") },
+       [COL_PIDFD_PID]={ "PIDFD.PID",5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
+               N_("PID of the process targeted by the pidfd") },
        [COL_POS]     = { "POS",      5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
                N_("file position") },
        [COL_PROTONAME]={ "PROTONAME",0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
index 794e2cc2a61696d24534aab4325a435a55e43eeb..41f574ff4ad1ff1742529589436e26153823fa95 100644 (file)
@@ -59,6 +59,9 @@ enum {
        COL_NLINK,
        COL_PARTITION,
        COL_PID,
+       COL_PIDFD_COMM,
+       COL_PIDFD_NSPID,
+       COL_PIDFD_PID,
        COL_POS,
        COL_PROTONAME,
        COL_RDEV,
index 8fc4b55d703f601bba6b4dc22b6bae89e5afb42d..3f7edbb41fcb38f2d783377744baea31dba878ea 100644 (file)
@@ -1,2 +1,2 @@
-    3   UNKN anon_inodefs pid=1 comm=systemd nspid=1
-ASSOC,STTYPE,SOURCE,NAME: 0
+    3   UNKN anon_inodefs pid=1 comm=systemd nspid=1 systemd            1
+ASSOC,STTYPE,SOURCE,NAME,PIDFD.COMM,PIDFD.PID: 0
index c707cd19704a2f23855f7cb40caccd867f927db3..fc31bd6b28195d112059f0e96fabe38879302a4c 100755 (executable)
@@ -31,13 +31,13 @@ ts_cd "$TS_OUTDIR"
 PID=
 FD=3
 TARGET=1
-EXPR="(PID != ${TARGET}) and (FD == 3)"
+EXPR="(PID != ${TARGET}) and (FD == 3) and (PIDFD.PID == ${TARGET})"
 
 {
     coproc MKFDS { "$TS_HELPER_MKFDS" pidfd $FD target-pid=${TARGET} ; }
     if read -u ${MKFDS[0]} PID; then
-       ${TS_CMD_LSFD} -n -o ASSOC,STTYPE,SOURCE,NAME -p "${PID}" -p ${TARGET} -Q "${EXPR}"
-       echo 'ASSOC,STTYPE,SOURCE,NAME': $?
+       ${TS_CMD_LSFD} -n -o ASSOC,STTYPE,SOURCE,NAME,PIDFD.COMM,PIDFD.PID -p "${PID}" -p ${TARGET} -Q "${EXPR}"
+       echo 'ASSOC,STTYPE,SOURCE,NAME,PIDFD.COMM,PIDFD.PID': $?
 
        kill -CONT ${PID}
        wait ${MKFDS_PID}