]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/proc/proc_tty.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[thirdparty/kernel/linux.git] / fs / proc / proc_tty.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * proc_tty.c -- handles /proc/tty
4 *
5 * Copyright 1997, Theodore Ts'o
6 */
7
7c0f6ba6 8#include <linux/uaccess.h>
b640a89d 9#include <linux/module.h>
1da177e4
LT
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
1da177e4
LT
19/*
20 * The /proc/tty directory inodes...
21 */
849d20a1 22static struct proc_dir_entry *proc_tty_driver;
1da177e4
LT
23
24/*
25 * This is the handler for /proc/tty/drivers
26 */
27static void show_tty_range(struct seq_file *m, struct tty_driver *p,
28 dev_t from, int num)
29{
30 seq_printf(m, "%-20s ", p->driver_name ? p->driver_name : "unknown");
31 seq_printf(m, "/dev/%-8s ", p->name);
32 if (p->num > 1) {
33 seq_printf(m, "%3d %d-%d ", MAJOR(from), MINOR(from),
34 MINOR(from) + num - 1);
35 } else {
36 seq_printf(m, "%3d %7d ", MAJOR(from), MINOR(from));
37 }
38 switch (p->type) {
39 case TTY_DRIVER_TYPE_SYSTEM:
9d6de12f 40 seq_puts(m, "system");
1da177e4 41 if (p->subtype == SYSTEM_TYPE_TTY)
9d6de12f 42 seq_puts(m, ":/dev/tty");
1da177e4 43 else if (p->subtype == SYSTEM_TYPE_SYSCONS)
9d6de12f 44 seq_puts(m, ":console");
1da177e4 45 else if (p->subtype == SYSTEM_TYPE_CONSOLE)
9d6de12f 46 seq_puts(m, ":vtmaster");
1da177e4
LT
47 break;
48 case TTY_DRIVER_TYPE_CONSOLE:
9d6de12f 49 seq_puts(m, "console");
1da177e4
LT
50 break;
51 case TTY_DRIVER_TYPE_SERIAL:
9d6de12f 52 seq_puts(m, "serial");
1da177e4
LT
53 break;
54 case TTY_DRIVER_TYPE_PTY:
55 if (p->subtype == PTY_TYPE_MASTER)
9d6de12f 56 seq_puts(m, "pty:master");
1da177e4 57 else if (p->subtype == PTY_TYPE_SLAVE)
9d6de12f 58 seq_puts(m, "pty:slave");
1da177e4 59 else
9d6de12f 60 seq_puts(m, "pty");
1da177e4
LT
61 break;
62 default:
63 seq_printf(m, "type:%d.%d", p->type, p->subtype);
64 }
65 seq_putc(m, '\n');
66}
67
68static int show_tty_driver(struct seq_file *m, void *v)
69{
25216b00 70 struct tty_driver *p = list_entry(v, struct tty_driver, tty_drivers);
1da177e4
LT
71 dev_t from = MKDEV(p->major, p->minor_start);
72 dev_t to = from + p->num;
73
74 if (&p->tty_drivers == tty_drivers.next) {
75 /* pseudo-drivers first */
76 seq_printf(m, "%-20s /dev/%-8s ", "/dev/tty", "tty");
77 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 0);
9d6de12f 78 seq_puts(m, "system:/dev/tty\n");
1da177e4
LT
79 seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console");
80 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1);
9d6de12f 81 seq_puts(m, "system:console\n");
1da177e4
LT
82#ifdef CONFIG_UNIX98_PTYS
83 seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx");
84 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2);
9d6de12f 85 seq_puts(m, "system\n");
1da177e4
LT
86#endif
87#ifdef CONFIG_VT
88 seq_printf(m, "%-20s /dev/%-8s ", "/dev/vc/0", "vc/0");
89 seq_printf(m, "%3d %7d ", TTY_MAJOR, 0);
9d6de12f 90 seq_puts(m, "system:vtmaster\n");
1da177e4
LT
91#endif
92 }
93
94 while (MAJOR(from) < MAJOR(to)) {
95 dev_t next = MKDEV(MAJOR(from)+1, 0);
96 show_tty_range(m, p, from, next - from);
97 from = next;
98 }
99 if (from != to)
100 show_tty_range(m, p, from, to - from);
101 return 0;
102}
103
104/* iterator */
105static void *t_start(struct seq_file *m, loff_t *pos)
106{
ca509f69 107 mutex_lock(&tty_mutex);
25216b00 108 return seq_list_start(&tty_drivers, *pos);
1da177e4
LT
109}
110
111static void *t_next(struct seq_file *m, void *v, loff_t *pos)
112{
25216b00 113 return seq_list_next(v, &tty_drivers, pos);
1da177e4
LT
114}
115
116static void t_stop(struct seq_file *m, void *v)
117{
ca509f69 118 mutex_unlock(&tty_mutex);
1da177e4
LT
119}
120
03a44825 121static const struct seq_operations tty_drivers_op = {
1da177e4
LT
122 .start = t_start,
123 .next = t_next,
124 .stop = t_stop,
125 .show = show_tty_driver
126};
127
128static int tty_drivers_open(struct inode *inode, struct file *file)
129{
130 return seq_open(file, &tty_drivers_op);
131}
132
00977a59 133static const struct file_operations proc_tty_drivers_operations = {
1da177e4
LT
134 .open = tty_drivers_open,
135 .read = seq_read,
136 .llseek = seq_lseek,
137 .release = seq_release,
138};
139
1da177e4
LT
140/*
141 * This function is called by tty_register_driver() to handle
142 * registering the driver's /proc handler into /proc/tty/driver/<foo>
143 */
144void proc_tty_register_driver(struct tty_driver *driver)
145{
146 struct proc_dir_entry *ent;
147
0f043a81
AD
148 if (!driver->driver_name || driver->proc_entry ||
149 !driver->ops->proc_fops)
1da177e4
LT
150 return;
151
0f043a81
AD
152 ent = proc_create_data(driver->driver_name, 0, proc_tty_driver,
153 driver->ops->proc_fops, driver);
1da177e4
LT
154 driver->proc_entry = ent;
155}
156
157/*
158 * This function is called by tty_unregister_driver()
159 */
160void proc_tty_unregister_driver(struct tty_driver *driver)
161{
162 struct proc_dir_entry *ent;
163
164 ent = driver->proc_entry;
165 if (!ent)
166 return;
167
168 remove_proc_entry(driver->driver_name, proc_tty_driver);
169
170 driver->proc_entry = NULL;
171}
172
173/*
174 * Called by proc_root_init() to initialize the /proc/tty subtree
175 */
176void __init proc_tty_init(void)
177{
1da177e4
LT
178 if (!proc_mkdir("tty", NULL))
179 return;
849d20a1 180 proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
1da177e4
LT
181 /*
182 * /proc/tty/driver/serial reveals the exact character counts for
183 * serial links which is just too easy to abuse for inferring
184 * password lengths and inter-keystroke timings during password
185 * entry.
186 */
b640a89d
AD
187 proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR|S_IXUSR, NULL);
188 proc_create("tty/ldiscs", 0, NULL, &tty_ldiscs_proc_fops);
0d5c9f5f 189 proc_create("tty/drivers", 0, NULL, &proc_tty_drivers_operations);
1da177e4 190}