From 8c2ca9c973d5ee15cd7e21e1ef484569e2829e75 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sat, 4 Mar 2023 02:28:04 +0900 Subject: [PATCH] lsfd: add EVENTFD.ID column The files under of /proc/$pid/fdinfo/* provide IDs for eventfd files. With this change, lsfd extacts the IDs. All eventfd files share the same inode. So the IDs are useful to distinguish two different envetfds. An example output: $ ./lsfd -Q '(TYPE == "eventfd")' -o+EVENTFD.ID | head COMMAND PID USER ASSOC MODE TYPE SOURCE MNTID INODE NAME EVENTFD.ID gnome-session-b 17113 jet 3 rw- eventfd anon_inodefs 15 1060 id=43 43 gnome-session-b 17113 jet 4 rw- eventfd anon_inodefs 15 1060 id=44 44 gnome-session-b 17113 jet 6 rw- eventfd anon_inodefs 15 1060 id=158 158 gnome-session-b 17113 jet 8 rw- eventfd anon_inodefs 15 1060 id=167 167 at-spi-bus-laun 17160 jet 3 rw- eventfd anon_inodefs 15 1060 id=341 341 at-spi-bus-laun 17160 jet 4 rw- eventfd anon_inodefs 15 1060 id=342 342 at-spi-bus-laun 17160 jet 5 rw- eventfd anon_inodefs 15 1060 id=343 343 at-spi-bus-laun 17160 jet 9 rw- eventfd anon_inodefs 15 1060 id=344 344 gvfsd 17169 jet 3 rw- eventfd anon_inodefs 15 1060 id=369 369 Signed-off-by: Masatake YAMATO --- misc-utils/lsfd-unkn.c | 74 ++++++++++++++++++++++++++++++++++++++++++ misc-utils/lsfd.1.adoc | 6 ++++ misc-utils/lsfd.c | 3 ++ misc-utils/lsfd.h | 1 + 4 files changed, 84 insertions(+) diff --git a/misc-utils/lsfd-unkn.c b/misc-utils/lsfd-unkn.c index b1e0323c7c..1e4bf36021 100644 --- a/misc-utils/lsfd-unkn.c +++ b/misc-utils/lsfd-unkn.c @@ -263,6 +263,79 @@ static const struct anon_ops anon_pidfd_ops = { .handle_fdinfo = anon_pidfd_handle_fdinfo, }; +/* + * eventfd + */ +struct anon_eventfd_data { + int id; +}; + +static bool anon_eventfd_probe(const char *str) +{ + return (strncmp(str, "[eventfd]", 9) == 0); +} + +static char *anon_eventfd_get_name(struct unkn *unkn) +{ + char *str = NULL; + struct anon_eventfd_data *data = (struct anon_eventfd_data *)unkn->anon_data; + + xasprintf(&str, "id=%d", data->id); + return str; +} + +static void anon_eventfd_init(struct unkn *unkn) +{ + unkn->anon_data = xcalloc(1, sizeof(struct anon_eventfd_data)); +} + +static void anon_eventfd_free(struct unkn *unkn) +{ + free(unkn->anon_data); +} + +static int anon_eventfd_handle_fdinfo(struct unkn *unkn, const char *key, const char *value) +{ + if (strcmp(key, "eventfd-id") == 0) { + int64_t id; + + int rc = ul_strtos64(value, &id, 10); + if (rc < 0) + return 0; + ((struct anon_eventfd_data *)unkn->anon_data)->id = (int)id; + return 1; + } + return 0; +} + +static bool anon_eventfd_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_eventfd_data *data = (struct anon_eventfd_data *)unkn->anon_data; + + switch(column_id) { + case COL_EVENTFD_ID: + xasprintf(str, "%d", data->id); + return true; + default: + return false; + } +} + +static const struct anon_ops anon_eventfd_ops = { + .class = "eventfd", + .probe = anon_eventfd_probe, + .get_name = anon_eventfd_get_name, + .fill_column = anon_eventfd_fill_column, + .init = anon_eventfd_init, + .free = anon_eventfd_free, + .handle_fdinfo = anon_eventfd_handle_fdinfo, +}; + /* * generic (fallback implementation) */ @@ -277,6 +350,7 @@ static const struct anon_ops anon_generic_ops = { static const struct anon_ops *anon_ops[] = { &anon_pidfd_ops, + &anon_eventfd_ops, }; static const struct anon_ops *anon_probe(const char *str) diff --git a/misc-utils/lsfd.1.adoc b/misc-utils/lsfd.1.adoc index cbf50f0dd3..d389b2876a 100644 --- a/misc-utils/lsfd.1.adoc +++ b/misc-utils/lsfd.1.adoc @@ -160,6 +160,9 @@ write mode of the endpoint. *lsfd* scans; *lsfd* may miss some endpoints if you limits the processes with *-p* option. +EVENTFD.ID <``number``>:: +Eventfd ID. + FD <``number``>:: File descriptor for the file. @@ -219,6 +222,9 @@ Cooked version of KNAME. It is mostly same as KNAME. + Some files have special formats and information sources: + +eventfd::: +id=_EVENTFD.ID_ ++ NETLINK::: protocol=_NETLINK.PROTOCOL_[ lport=_NETLINK.LPORT_[ group=_NETLINK.GROUPS_]] + diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 5a5e37db6a..6d7fd13f3c 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -147,6 +147,9 @@ static const struct colinfo infos[] = { [COL_ENDPOINTS] = { "ENDPOINTS", 0, SCOLS_FL_WRAP, SCOLS_JSON_STRING, N_("IPC endpoints information communicated with the fd") }, + [COL_EVENTFD_ID] = {"EVENTFD.ID", + 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, + N_("eventfd ID") }, [COL_FLAGS] = { "FLAGS", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING, N_("flags specified when opening the file") }, diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index ea1c34242f..aec2c7a57e 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -46,6 +46,7 @@ enum { COL_DEV, COL_DEVTYPE, COL_ENDPOINTS, + COL_EVENTFD_ID, COL_FD, COL_FLAGS, COL_INODE, -- 2.47.3