]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: add PTMX.TTY-INDEX column
authorMasatake YAMATO <yamato@redhat.com>
Wed, 1 Nov 2023 15:44:20 +0000 (00:44 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Sat, 4 Nov 2023 08:03:05 +0000 (17:03 +0900)
tty-index field in /proc/$pid/fdinfo/$fd is the data source of
the column. The value is helpful for looking up the conterpart
of /dev/ptmx.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-cdev.c
misc-utils/lsfd.1.adoc
misc-utils/lsfd.c
misc-utils/lsfd.h

index 3cb133fb0805b8a4b8fb16d6d2db7e6b9f54a10a..9bcbea3533264812ced7222995ec5ec130ebf044 100644 (file)
@@ -35,6 +35,7 @@ struct ttydrv {
        unsigned long major;
        unsigned long minor_start, minor_end;
        char *name;
+       bool is_ptmx;
 };
 
 struct cdev {
@@ -161,6 +162,9 @@ static struct ttydrv *new_ttydrv(unsigned int major,
        ttydrv->minor_start = minor_start;
        ttydrv->minor_end = minor_end;
        ttydrv->name = xstrdup(name);
+       ttydrv->is_ptmx = false;
+       if (strcmp(name, "ptmx") == 0)
+               ttydrv->is_ptmx = true;
 
        return ttydrv;
 }
@@ -430,6 +434,8 @@ static struct cdev_ops cdev_tun_ops = {
  */
 struct ttydata {
        const struct ttydrv *drv;
+#define NO_TTY_INDEX -1
+       int tty_index;          /* used only in ptmx devices */
 };
 
 static bool cdev_tty_probe(struct cdev *cdev) {
@@ -442,6 +448,7 @@ static bool cdev_tty_probe(struct cdev *cdev) {
 
        data = xmalloc(sizeof(struct ttydata));
        data->drv = ttydrv;
+       data->tty_index = NO_TTY_INDEX;
        cdev->cdev_data = data;
 
        return true;
@@ -453,6 +460,21 @@ static void cdev_tty_free(const struct cdev *cdev)
                free(cdev->cdev_data);
 }
 
+static char * cdev_tty_get_name(struct cdev *cdev)
+{
+       struct ttydata *data = cdev->cdev_data;
+       char *str = NULL;
+
+       if (!data->drv->is_ptmx)
+               return NULL;
+
+       if (data->tty_index == NO_TTY_INDEX)
+               str = xstrdup("tty-index=");
+       else
+               xasprintf(&str, "tty-index=%d", data->tty_index);
+       return str;
+}
+
 static bool cdev_tty_fill_column(struct proc *proc  __attribute__((__unused__)),
                                 struct cdev *cdev,
                                 struct libscols_line *ln __attribute__((__unused__)),
@@ -471,16 +493,44 @@ static bool cdev_tty_fill_column(struct proc *proc  __attribute__((__unused__)),
                        xasprintf(str, "%s:%u", data->drv->name,
                                  minor(file->stat.st_rdev));
                return true;
+       case COL_PTMX_TTY_INDEX:
+               if (data->drv->is_ptmx) {
+                       xasprintf(str, "%d", data->tty_index);
+                       return true;
+               }
+               return false;
        default:
                return false;
        }
 }
 
+static int cdev_tty_handle_fdinfo(struct cdev *cdev, const char *key, const char *val)
+{
+       struct ttydata *data = cdev->cdev_data;
+
+       if (!data->drv->is_ptmx)
+               return 0;
+
+       if (strcmp(key, "tty-index") == 0) {
+               errno = 0;
+               data->tty_index = (int)strtol(val, NULL, 10);
+               if (errno) {
+                       data->tty_index = NO_TTY_INDEX;
+                       return 0;
+               }
+               return 1;
+       }
+
+       return 0;
+}
+
 static struct cdev_ops cdev_tty_ops = {
        .parent = &cdev_generic_ops,
        .probe = cdev_tty_probe,
        .free = cdev_tty_free,
+       .get_name = cdev_tty_get_name,
        .fill_column = cdev_tty_fill_column,
+       .handle_fdinfo = cdev_tty_handle_fdinfo,
 };
 
 static const struct cdev_ops *cdev_ops[] = {
index 1f7e889d46c11739df342b4d5878cda1ab6961fa..1e9d6acb76ed0a6d8a6e7c5fc3e1485e42e661b5 100644 (file)
@@ -304,6 +304,12 @@ state=_SOCK.STATE_[ id=_PING.ID_][ laddr=_INET.LADDR_ [ raddr=_INET.RADDR_]]
 PINGv6:::
 state=_SOCK.STATE_[ id=_PING.ID_][ laddr=_INET6.LADDR_ [ raddr=_INET6.RADDR_]]
 +
+ptmx:::
+tty-index=_PTMX.TTY-INDEX_
++
+*lsfd* extracts _PTMX.TTY-INDEX_ from
+``/proc/``_pid_``/fdinfo/``_fd_.
++
 RAW:::
 state=_SOCK.STATE_[ protocol=_RAW.PROTOCOL_ [ laddr=_INET.LADDR_ [ raddr=_INET.RADDR_]]]
 +
@@ -484,6 +490,9 @@ Interval.
 TIMERFD.REMAINING <``number``>::
 Remaining time.
 
+PTMX.TTY-INDEX <``number``>::
+TTY index of the counterpart.
+
 TUN.IFACE <``string``>::
 Network intrface behind the tun device.
 
index 0bb02e282104a0f1e73d1463a75392406d4d1e9b..76e0e66e1796b367c81300084c71817dff780704 100644 (file)
@@ -275,6 +275,9 @@ static const struct colinfo infos[] = {
        [COL_POS]              = { "POS",
                                   5,   SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
                                   N_("file position") },
+       [COL_PTMX_TTY_INDEX]   = { "PTMX.TTY-INDEX",
+                                  0,   SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
+                                  N_("tty index of the counterpart") },
        [COL_RAW_PROTOCOL]     = { "RAW.PROTOCOL",
                                   0,   SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
                                   N_("protocol number of the raw socket") },
index c9c975392fc2ae0057d06c612b3381057167553d..a819e65096c1e440242635555471d04b7ebbe544 100644 (file)
@@ -92,6 +92,7 @@ enum {
        COL_PIDFD_PID,
        COL_PING_ID,
        COL_POS,
+       COL_PTMX_TTY_INDEX,
        COL_RAW_PROTOCOL,
        COL_RDEV,
        COL_SIGNALFD_MASK,