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