blob: 7340c36978a3bca20a04ff55d59db0dfd5e4149e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * proc_tty.c -- handles /proc/tty
3 *
4 * Copyright 1997, Theodore Ts'o
5 */
6
7#include <asm/uaccess.h>
Alexey Dobriyanb640a892008-04-29 01:01:58 -07008#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/errno.h>
11#include <linux/time.h>
12#include <linux/proc_fs.h>
13#include <linux/stat.h>
14#include <linux/tty.h>
15#include <linux/seq_file.h>
16#include <linux/bitops.h>
nixiaoming92ad6c12017-09-15 17:45:56 +080017#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/*
20 * The /proc/tty directory inodes...
21 */
Alexey Dobriyan849d20a2014-08-08 14:21:31 -070022static struct proc_dir_entry *proc_tty_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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:
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080040 seq_puts(m, "system");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (p->subtype == SYSTEM_TYPE_TTY)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080042 seq_puts(m, ":/dev/tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 else if (p->subtype == SYSTEM_TYPE_SYSCONS)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080044 seq_puts(m, ":console");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 else if (p->subtype == SYSTEM_TYPE_CONSOLE)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080046 seq_puts(m, ":vtmaster");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 break;
48 case TTY_DRIVER_TYPE_CONSOLE:
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080049 seq_puts(m, "console");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 break;
51 case TTY_DRIVER_TYPE_SERIAL:
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080052 seq_puts(m, "serial");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 break;
54 case TTY_DRIVER_TYPE_PTY:
55 if (p->subtype == PTY_TYPE_MASTER)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080056 seq_puts(m, "pty:master");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 else if (p->subtype == PTY_TYPE_SLAVE)
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080058 seq_puts(m, "pty:slave");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 else
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080060 seq_puts(m, "pty");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 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{
Pavel Emelianov25216b02007-07-15 23:39:54 -070070 struct tty_driver *p = list_entry(v, struct tty_driver, tty_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 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);
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080078 seq_puts(m, "system:/dev/tty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console");
80 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1);
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080081 seq_puts(m, "system:console\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef CONFIG_UNIX98_PTYS
83 seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx");
84 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2);
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080085 seq_puts(m, "system\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#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);
Alexey Dobriyan9d6de122011-01-12 17:00:32 -080090 seq_puts(m, "system:vtmaster\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#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{
Alexey Dobriyanca509f62007-05-08 00:27:12 -0700107 mutex_lock(&tty_mutex);
Pavel Emelianov25216b02007-07-15 23:39:54 -0700108 return seq_list_start(&tty_drivers, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static void *t_next(struct seq_file *m, void *v, loff_t *pos)
112{
Pavel Emelianov25216b02007-07-15 23:39:54 -0700113 return seq_list_next(v, &tty_drivers, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116static void t_stop(struct seq_file *m, void *v)
117{
Alexey Dobriyanca509f62007-05-08 00:27:12 -0700118 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Jan Engelhardt03a44822008-02-08 04:21:19 -0800121static const struct seq_operations tty_drivers_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .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
Arjan van de Ven00977a52007-02-12 00:55:34 -0800133static const struct file_operations proc_tty_drivers_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .open = tty_drivers_open,
135 .read = seq_read,
136 .llseek = seq_lseek,
137 .release = seq_release,
138};
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
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
Alexey Dobriyan0f043a82009-03-31 15:19:25 -0700148 if (!driver->driver_name || driver->proc_entry ||
149 !driver->ops->proc_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return;
151
Alexey Dobriyan0f043a82009-03-31 15:19:25 -0700152 ent = proc_create_data(driver->driver_name, 0, proc_tty_driver,
153 driver->ops->proc_fops, driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 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
nixiaoming92ad6c12017-09-15 17:45:56 +0800168 remove_proc_entry(ent->name, proc_tty_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!proc_mkdir("tty", NULL))
179 return;
Alexey Dobriyan849d20a2014-08-08 14:21:31 -0700180 proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
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 */
Alexey Dobriyanb640a892008-04-29 01:01:58 -0700187 proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR|S_IXUSR, NULL);
188 proc_create("tty/ldiscs", 0, NULL, &tty_ldiscs_proc_fops);
Alexey Dobriyan0d5c9f52008-04-29 01:01:37 -0700189 proc_create("tty/drivers", 0, NULL, &proc_tty_drivers_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}