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