blob: 5c6a5ceab2f1b7965c60ccf849dc4d55685a1931 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * proc_tty.c -- handles /proc/tty
4 *
5 * Copyright 1997, Theodore Ts'o
6 */
Alexey Dobriyanb640a892008-04-29 01:01:58 -07007#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#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>
nixiaomingc79dde62017-09-15 17:45:56 +080016#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
19 * The /proc/tty directory inodes...
20 */
Alexey Dobriyan849d20a2014-08-08 14:21:31 -070021static struct proc_dir_entry *proc_tty_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/*
24 * This is the handler for /proc/tty/drivers
25 */
26static void show_tty_range(struct seq_file *m, struct tty_driver *p,
27 dev_t from, int num)
28{
29 seq_printf(m, "%-20s ", p->driver_name ? p->driver_name : "unknown");
30 seq_printf(m, "/dev/%-8s ", p->name);
31 if (p->num > 1) {
32 seq_printf(m, "%3d %d-%d ", MAJOR(from), MINOR(from),
33 MINOR(from) + num - 1);
34 } else {
35 seq_printf(m, "%3d %7d ", MAJOR(from), MINOR(from));
36 }
37 switch (p->type) {
38 case TTY_DRIVER_TYPE_SYSTEM:
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080039 seq_puts(m, "system");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (p->subtype == SYSTEM_TYPE_TTY)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080041 seq_puts(m, ":/dev/tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 else if (p->subtype == SYSTEM_TYPE_SYSCONS)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080043 seq_puts(m, ":console");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 else if (p->subtype == SYSTEM_TYPE_CONSOLE)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080045 seq_puts(m, ":vtmaster");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 break;
47 case TTY_DRIVER_TYPE_CONSOLE:
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080048 seq_puts(m, "console");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 break;
50 case TTY_DRIVER_TYPE_SERIAL:
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080051 seq_puts(m, "serial");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53 case TTY_DRIVER_TYPE_PTY:
54 if (p->subtype == PTY_TYPE_MASTER)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080055 seq_puts(m, "pty:master");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 else if (p->subtype == PTY_TYPE_SLAVE)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080057 seq_puts(m, "pty:slave");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 else
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080059 seq_puts(m, "pty");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 break;
61 default:
62 seq_printf(m, "type:%d.%d", p->type, p->subtype);
63 }
64 seq_putc(m, '\n');
65}
66
67static int show_tty_driver(struct seq_file *m, void *v)
68{
Pavel Emelianov25216b02007-07-15 23:39:54 -070069 struct tty_driver *p = list_entry(v, struct tty_driver, tty_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 dev_t from = MKDEV(p->major, p->minor_start);
71 dev_t to = from + p->num;
72
73 if (&p->tty_drivers == tty_drivers.next) {
74 /* pseudo-drivers first */
75 seq_printf(m, "%-20s /dev/%-8s ", "/dev/tty", "tty");
76 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 0);
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080077 seq_puts(m, "system:/dev/tty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console");
79 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1);
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080080 seq_puts(m, "system:console\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#ifdef CONFIG_UNIX98_PTYS
82 seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx");
83 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2);
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080084 seq_puts(m, "system\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#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);
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080089 seq_puts(m, "system:vtmaster\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif
91 }
92
93 while (MAJOR(from) < MAJOR(to)) {
94 dev_t next = MKDEV(MAJOR(from)+1, 0);
95 show_tty_range(m, p, from, next - from);
96 from = next;
97 }
98 if (from != to)
99 show_tty_range(m, p, from, to - from);
100 return 0;
101}
102
103/* iterator */
104static void *t_start(struct seq_file *m, loff_t *pos)
105{
Alexey Dobriyanca509f62007-05-08 00:27:12 -0700106 mutex_lock(&tty_mutex);
Pavel Emelianov25216b02007-07-15 23:39:54 -0700107 return seq_list_start(&tty_drivers, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110static void *t_next(struct seq_file *m, void *v, loff_t *pos)
111{
Pavel Emelianov25216b02007-07-15 23:39:54 -0700112 return seq_list_next(v, &tty_drivers, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115static void t_stop(struct seq_file *m, void *v)
116{
Alexey Dobriyanca509f62007-05-08 00:27:12 -0700117 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Jan Engelhardt03a44822008-02-08 04:21:19 -0800120static const struct seq_operations tty_drivers_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .start = t_start,
122 .next = t_next,
123 .stop = t_stop,
124 .show = show_tty_driver
125};
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/*
128 * This function is called by tty_register_driver() to handle
129 * registering the driver's /proc handler into /proc/tty/driver/<foo>
130 */
131void proc_tty_register_driver(struct tty_driver *driver)
132{
133 struct proc_dir_entry *ent;
134
Alexey Dobriyan0f043a82009-03-31 15:19:25 -0700135 if (!driver->driver_name || driver->proc_entry ||
Christoph Hellwig8a8dcab2018-04-13 21:04:45 +0200136 !driver->ops->proc_show)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return;
138
Christoph Hellwig8a8dcab2018-04-13 21:04:45 +0200139 ent = proc_create_single_data(driver->driver_name, 0, proc_tty_driver,
140 driver->ops->proc_show, driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 driver->proc_entry = ent;
142}
143
144/*
145 * This function is called by tty_unregister_driver()
146 */
147void proc_tty_unregister_driver(struct tty_driver *driver)
148{
149 struct proc_dir_entry *ent;
150
151 ent = driver->proc_entry;
152 if (!ent)
153 return;
154
nixiaomingc79dde62017-09-15 17:45:56 +0800155 remove_proc_entry(ent->name, proc_tty_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 driver->proc_entry = NULL;
158}
159
160/*
161 * Called by proc_root_init() to initialize the /proc/tty subtree
162 */
163void __init proc_tty_init(void)
164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!proc_mkdir("tty", NULL))
166 return;
Alexey Dobriyan849d20a2014-08-08 14:21:31 -0700167 proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /*
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 */
Alexey Dobriyanb640a892008-04-29 01:01:58 -0700174 proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR|S_IXUSR, NULL);
Christoph Hellwigfddda2b2018-04-13 19:44:18 +0200175 proc_create_seq("tty/ldiscs", 0, NULL, &tty_ldiscs_seq_ops);
176 proc_create_seq("tty/drivers", 0, NULL, &tty_drivers_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}