LCOV - code coverage report
Current view: top level - fs/proc - proc_tty.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 9 72 12.5 %
Date: 2023-03-27 20:00:47 Functions: 2 8 25.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * proc_tty.c -- handles /proc/tty
       4             :  *
       5             :  * Copyright 1997, Theodore Ts'o
       6             :  */
       7             : #include <linux/module.h>
       8             : #include <linux/init.h>
       9             : #include <linux/errno.h>
      10             : #include <linux/time.h>
      11             : #include <linux/proc_fs.h>
      12             : #include <linux/stat.h>
      13             : #include <linux/tty.h>
      14             : #include <linux/seq_file.h>
      15             : #include <linux/bitops.h>
      16             : #include "internal.h"
      17             : 
      18             : /*
      19             :  * The /proc/tty directory inodes...
      20             :  */
      21             : static struct proc_dir_entry *proc_tty_driver;
      22             : 
      23             : /*
      24             :  * This is the handler for /proc/tty/drivers
      25             :  */
      26           0 : static void show_tty_range(struct seq_file *m, struct tty_driver *p,
      27             :         dev_t from, int num)
      28             : {
      29           0 :         seq_printf(m, "%-20s ", p->driver_name ? p->driver_name : "unknown");
      30           0 :         seq_printf(m, "/dev/%-8s ", p->name);
      31           0 :         if (p->num > 1) {
      32           0 :                 seq_printf(m, "%3d %d-%d ", MAJOR(from), MINOR(from),
      33           0 :                         MINOR(from) + num - 1);
      34             :         } else {
      35           0 :                 seq_printf(m, "%3d %7d ", MAJOR(from), MINOR(from));
      36             :         }
      37           0 :         switch (p->type) {
      38             :         case TTY_DRIVER_TYPE_SYSTEM:
      39           0 :                 seq_puts(m, "system");
      40           0 :                 if (p->subtype == SYSTEM_TYPE_TTY)
      41           0 :                         seq_puts(m, ":/dev/tty");
      42           0 :                 else if (p->subtype == SYSTEM_TYPE_SYSCONS)
      43           0 :                         seq_puts(m, ":console");
      44           0 :                 else if (p->subtype == SYSTEM_TYPE_CONSOLE)
      45           0 :                         seq_puts(m, ":vtmaster");
      46             :                 break;
      47             :         case TTY_DRIVER_TYPE_CONSOLE:
      48           0 :                 seq_puts(m, "console");
      49           0 :                 break;
      50             :         case TTY_DRIVER_TYPE_SERIAL:
      51           0 :                 seq_puts(m, "serial");
      52           0 :                 break;
      53             :         case TTY_DRIVER_TYPE_PTY:
      54           0 :                 if (p->subtype == PTY_TYPE_MASTER)
      55           0 :                         seq_puts(m, "pty:master");
      56           0 :                 else if (p->subtype == PTY_TYPE_SLAVE)
      57           0 :                         seq_puts(m, "pty:slave");
      58             :                 else
      59           0 :                         seq_puts(m, "pty");
      60             :                 break;
      61             :         default:
      62           0 :                 seq_printf(m, "type:%d.%d", p->type, p->subtype);
      63             :         }
      64           0 :         seq_putc(m, '\n');
      65           0 : }
      66             : 
      67           0 : static int show_tty_driver(struct seq_file *m, void *v)
      68             : {
      69           0 :         struct tty_driver *p = list_entry(v, struct tty_driver, tty_drivers);
      70           0 :         dev_t from = MKDEV(p->major, p->minor_start);
      71           0 :         dev_t to = from + p->num;
      72             : 
      73           0 :         if (&p->tty_drivers == tty_drivers.next) {
      74             :                 /* pseudo-drivers first */
      75           0 :                 seq_printf(m, "%-20s /dev/%-8s ", "/dev/tty", "tty");
      76           0 :                 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 0);
      77           0 :                 seq_puts(m, "system:/dev/tty\n");
      78           0 :                 seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console");
      79           0 :                 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1);
      80           0 :                 seq_puts(m, "system:console\n");
      81             : #ifdef CONFIG_UNIX98_PTYS
      82           0 :                 seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx");
      83           0 :                 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2);
      84           0 :                 seq_puts(m, "system\n");
      85             : #endif
      86             : #ifdef CONFIG_VT
      87             :                 seq_printf(m, "%-20s /dev/%-8s ", "/dev/vc/0", "vc/0");
      88             :                 seq_printf(m, "%3d %7d ", TTY_MAJOR, 0);
      89             :                 seq_puts(m, "system:vtmaster\n");
      90             : #endif
      91             :         }
      92             : 
      93           0 :         while (MAJOR(from) < MAJOR(to)) {
      94           0 :                 dev_t next = MKDEV(MAJOR(from)+1, 0);
      95           0 :                 show_tty_range(m, p, from, next - from);
      96           0 :                 from = next;
      97             :         }
      98           0 :         if (from != to)
      99           0 :                 show_tty_range(m, p, from, to - from);
     100           0 :         return 0;
     101             : }
     102             : 
     103             : /* iterator */
     104           0 : static void *t_start(struct seq_file *m, loff_t *pos)
     105             : {
     106           0 :         mutex_lock(&tty_mutex);
     107           0 :         return seq_list_start(&tty_drivers, *pos);
     108             : }
     109             : 
     110           0 : static void *t_next(struct seq_file *m, void *v, loff_t *pos)
     111             : {
     112           0 :         return seq_list_next(v, &tty_drivers, pos);
     113             : }
     114             : 
     115           0 : static void t_stop(struct seq_file *m, void *v)
     116             : {
     117           0 :         mutex_unlock(&tty_mutex);
     118           0 : }
     119             : 
     120             : static const struct seq_operations tty_drivers_op = {
     121             :         .start  = t_start,
     122             :         .next   = t_next,
     123             :         .stop   = t_stop,
     124             :         .show   = show_tty_driver
     125             : };
     126             : 
     127             : /*
     128             :  * This function is called by tty_register_driver() to handle
     129             :  * registering the driver's /proc handler into /proc/tty/driver/<foo>
     130             :  */
     131           5 : void proc_tty_register_driver(struct tty_driver *driver)
     132             : {
     133             :         struct proc_dir_entry *ent;
     134             :                 
     135          10 :         if (!driver->driver_name || driver->proc_entry ||
     136           5 :             !driver->ops->proc_show)
     137             :                 return;
     138             : 
     139           0 :         ent = proc_create_single_data(driver->driver_name, 0, proc_tty_driver,
     140             :                                driver->ops->proc_show, driver);
     141           0 :         driver->proc_entry = ent;
     142             : }
     143             : 
     144             : /*
     145             :  * This function is called by tty_unregister_driver()
     146             :  */
     147           0 : void proc_tty_unregister_driver(struct tty_driver *driver)
     148             : {
     149             :         struct proc_dir_entry *ent;
     150             : 
     151           0 :         ent = driver->proc_entry;
     152           0 :         if (!ent)
     153             :                 return;
     154             :                 
     155           0 :         remove_proc_entry(ent->name, proc_tty_driver);
     156             :         
     157           0 :         driver->proc_entry = NULL;
     158             : }
     159             : 
     160             : /*
     161             :  * Called by proc_root_init() to initialize the /proc/tty subtree
     162             :  */
     163           1 : void __init proc_tty_init(void)
     164             : {
     165           1 :         if (!proc_mkdir("tty", NULL))
     166             :                 return;
     167           1 :         proc_mkdir("tty/ldisc", NULL);        /* Preserved: it's userspace visible */
     168             :         /*
     169             :          * /proc/tty/driver/serial reveals the exact character counts for
     170             :          * serial links which is just too easy to abuse for inferring
     171             :          * password lengths and inter-keystroke timings during password
     172             :          * entry.
     173             :          */
     174           1 :         proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR|S_IXUSR, NULL);
     175           1 :         proc_create_seq("tty/ldiscs", 0, NULL, &tty_ldiscs_seq_ops);
     176           1 :         proc_create_seq("tty/drivers", 0, NULL, &tty_drivers_op);
     177             : }

Generated by: LCOV version 1.14