From: Masatake YAMATO Date: Sun, 25 Jun 2023 17:41:33 +0000 (+0900) Subject: lsfd: add TUN.IFFACE, a column for interfaces behind tun devices X-Git-Tag: v2.40-rc1~352^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7cbec02a4768a9e2e49a0cf0c6e3c85e8bff9c14;p=thirdparty%2Futil-linux.git lsfd: add TUN.IFFACE, a column for interfaces behind tun devices An example output: # ./lsfd -Q '(SOURCE == "misc:tun")' -o+TUN.IFACE COMMAND PID USER ASSOC MODE TYPE SOURCE MNTID INODE NAME TUN.IFACE qemu-system-x86 2846033 qemu 48 rw- CHR misc:tun 23 306 iface=vnet89 vnet89 qemu-system-x86 2865022 qemu 56 rw- CHR misc:tun 23 306 iface=vnet103 vnet103 openvpn 3113730 nm-openvpn 7 rw- CHR misc:tun 588 306 iface=tun0 tun0 qemu-system-x86 3608136 qemu 36 rw- CHR misc:tun 23 306 iface=vnet104 vnet104 qemu-system-x86 3608136 qemu 39 rw- CHR misc:tun 23 306 iface=vnet105 vnet105 qemu-system-x86 3608238 qemu 40 rw- CHR misc:tun 23 306 iface=vnet106 vnet106 qemu-system-x86 3608238 qemu 43 rw- CHR misc:tun 23 306 iface=vnet107 vnet107 qemu-system-x86 3608338 qemu 54 rw- CHR misc:tun 23 306 iface=vnet108 vnet108 qemu-system-x86 3608338 qemu 60 rw- CHR misc:tun 23 306 iface=vnet109 vnet109 Signed-off-by: Masatake YAMATO --- diff --git a/misc-utils/lsfd-cdev.c b/misc-utils/lsfd-cdev.c index 4b37b128ad..5f0f306500 100644 --- a/misc-utils/lsfd-cdev.c +++ b/misc-utils/lsfd-cdev.c @@ -255,7 +255,82 @@ static struct cdev_ops cdev_misc_ops = { .fill_column = cdev_misc_fill_column, }; +/* + * tun devcie driver + */ +static bool cdev_tun_probe(const struct cdev *cdev) +{ + const char *miscdev; + + if ((!cdev->devdrv) || strcmp(cdev->devdrv, "misc")) + return false; + + miscdev = get_miscdev(minor(cdev->file.stat.st_rdev)); + if (miscdev && strcmp(miscdev, "tun") == 0) + return true; + return false; +} + +static void cdev_tun_free(const struct cdev *cdev) +{ + if (cdev->cdev_data) + free(cdev->cdev_data); +} + +static char * cdev_tun_get_name(struct cdev *cdev) +{ + char *str = NULL; + + if (cdev->cdev_data == NULL) + return NULL; + + xasprintf(&str, "iface=%s", (const char *)cdev->cdev_data); + return str; +} + +static bool cdev_tun_fill_column(struct proc *proc __attribute__((__unused__)), + struct cdev *cdev, + struct libscols_line *ln __attribute__((__unused__)), + int column_id, + size_t column_index __attribute__((__unused__)), + char **str) +{ + switch(column_id) { + case COL_MISCDEV: + *str = xstrdup("tun"); + return true; + case COL_SOURCE: + *str = xstrdup("misc:tun"); + return true; + case COL_TUN_IFACE: + if (cdev->cdev_data) { + *str = xstrdup(cdev->cdev_data); + return true; + } + } + return false; +} + +static int cdev_tun_handle_fdinfo(struct cdev *cdev, const char *key, const char *val) +{ + if (strcmp(key, "iff") == 0 && cdev->cdev_data == NULL) { + cdev->cdev_data = xstrdup(val); + return 1; + } + return false; +} + +static struct cdev_ops cdev_tun_ops = { + .parent = &cdev_misc_ops, + .probe = cdev_tun_probe, + .free = cdev_tun_free, + .get_name = cdev_tun_get_name, + .fill_column = cdev_tun_fill_column, + .handle_fdinfo = cdev_tun_handle_fdinfo, +}; + static const struct cdev_ops *cdev_ops[] = { + &cdev_tun_ops, &cdev_misc_ops, &cdev_generic_ops /* This must be at the end. */ }; diff --git a/misc-utils/lsfd.1.adoc b/misc-utils/lsfd.1.adoc index ee53cec499..8a78d8e233 100644 --- a/misc-utils/lsfd.1.adoc +++ b/misc-utils/lsfd.1.adoc @@ -247,6 +247,9 @@ id=_EVENTFD.ID_ inotify::: inodes=_INOTIFY.INODES_ + +misc:tun::: +iface=_TUN.IFACE_ ++ NETLINK::: protocol=_NETLINK.PROTOCOL_[ lport=_NETLINK.LPORT_[ group=_NETLINK.GROUPS_]] + @@ -426,6 +429,9 @@ Interval. TIMERFD.REMAINING <``number``>:: Remaining time. +TUN.IFACE <``string``>:: +Network intrface behind the tun device. + TYPE <``string``>:: Cooked version of STTYPE. It is same as STTYPE with exceptions. For SOCK, print the value for SOCK.PROTONAME. diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 7672a026aa..520fe182c9 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -312,6 +312,9 @@ static const struct colinfo infos[] = { [COL_TIMERFD_REMAINING]= { "TIMERFD.REMAINING", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, N_("remaining time") }, + [COL_TUN_IFACE] = { "TUN.IFACE", + 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING, + N_("network interface behind the tun device") }, [COL_TYPE] = { "TYPE", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING, N_("file type (cooked)") }, diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 1b86b625b3..f2c4c0e973 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -101,6 +101,7 @@ enum { COL_TIMERFD_CLOCKID, COL_TIMERFD_INTERVAL, COL_TIMERFD_REMAINING, + COL_TUN_IFACE, COL_TYPE, COL_UDP_LADDR, COL_UDP_RADDR,