xref: /openbmc/linux/fs/proc/proc_tty.c (revision 96de0e252cedffad61b3cb5e05662c591898e69a)
1 /*
2  * proc_tty.c -- handles /proc/tty
3  *
4  * Copyright 1997, Theodore Ts'o
5  */
6 
7 #include <asm/uaccess.h>
8 
9 #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>
17 
18 static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
19 				int count, int *eof, void *data);
20 
21 /*
22  * The /proc/tty directory inodes...
23  */
24 static struct proc_dir_entry *proc_tty_ldisc, *proc_tty_driver;
25 
26 /*
27  * This is the handler for /proc/tty/drivers
28  */
29 static void show_tty_range(struct seq_file *m, struct tty_driver *p,
30 	dev_t from, int num)
31 {
32 	seq_printf(m, "%-20s ", p->driver_name ? p->driver_name : "unknown");
33 	seq_printf(m, "/dev/%-8s ", p->name);
34 	if (p->num > 1) {
35 		seq_printf(m, "%3d %d-%d ", MAJOR(from), MINOR(from),
36 			MINOR(from) + num - 1);
37 	} else {
38 		seq_printf(m, "%3d %7d ", MAJOR(from), MINOR(from));
39 	}
40 	switch (p->type) {
41 	case TTY_DRIVER_TYPE_SYSTEM:
42 		seq_printf(m, "system");
43 		if (p->subtype == SYSTEM_TYPE_TTY)
44 			seq_printf(m, ":/dev/tty");
45 		else if (p->subtype == SYSTEM_TYPE_SYSCONS)
46 			seq_printf(m, ":console");
47 		else if (p->subtype == SYSTEM_TYPE_CONSOLE)
48 			seq_printf(m, ":vtmaster");
49 		break;
50 	case TTY_DRIVER_TYPE_CONSOLE:
51 		seq_printf(m, "console");
52 		break;
53 	case TTY_DRIVER_TYPE_SERIAL:
54 		seq_printf(m, "serial");
55 		break;
56 	case TTY_DRIVER_TYPE_PTY:
57 		if (p->subtype == PTY_TYPE_MASTER)
58 			seq_printf(m, "pty:master");
59 		else if (p->subtype == PTY_TYPE_SLAVE)
60 			seq_printf(m, "pty:slave");
61 		else
62 			seq_printf(m, "pty");
63 		break;
64 	default:
65 		seq_printf(m, "type:%d.%d", p->type, p->subtype);
66 	}
67 	seq_putc(m, '\n');
68 }
69 
70 static int show_tty_driver(struct seq_file *m, void *v)
71 {
72 	struct tty_driver *p = list_entry(v, struct tty_driver, tty_drivers);
73 	dev_t from = MKDEV(p->major, p->minor_start);
74 	dev_t to = from + p->num;
75 
76 	if (&p->tty_drivers == tty_drivers.next) {
77 		/* pseudo-drivers first */
78 		seq_printf(m, "%-20s /dev/%-8s ", "/dev/tty", "tty");
79 		seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 0);
80 		seq_printf(m, "system:/dev/tty\n");
81 		seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console");
82 		seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1);
83 		seq_printf(m, "system:console\n");
84 #ifdef CONFIG_UNIX98_PTYS
85 		seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx");
86 		seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2);
87 		seq_printf(m, "system\n");
88 #endif
89 #ifdef CONFIG_VT
90 		seq_printf(m, "%-20s /dev/%-8s ", "/dev/vc/0", "vc/0");
91 		seq_printf(m, "%3d %7d ", TTY_MAJOR, 0);
92 		seq_printf(m, "system:vtmaster\n");
93 #endif
94 	}
95 
96 	while (MAJOR(from) < MAJOR(to)) {
97 		dev_t next = MKDEV(MAJOR(from)+1, 0);
98 		show_tty_range(m, p, from, next - from);
99 		from = next;
100 	}
101 	if (from != to)
102 		show_tty_range(m, p, from, to - from);
103 	return 0;
104 }
105 
106 /* iterator */
107 static void *t_start(struct seq_file *m, loff_t *pos)
108 {
109 	mutex_lock(&tty_mutex);
110 	return seq_list_start(&tty_drivers, *pos);
111 }
112 
113 static void *t_next(struct seq_file *m, void *v, loff_t *pos)
114 {
115 	return seq_list_next(v, &tty_drivers, pos);
116 }
117 
118 static void t_stop(struct seq_file *m, void *v)
119 {
120 	mutex_unlock(&tty_mutex);
121 }
122 
123 static struct seq_operations tty_drivers_op = {
124 	.start	= t_start,
125 	.next	= t_next,
126 	.stop	= t_stop,
127 	.show	= show_tty_driver
128 };
129 
130 static int tty_drivers_open(struct inode *inode, struct file *file)
131 {
132 	return seq_open(file, &tty_drivers_op);
133 }
134 
135 static const struct file_operations proc_tty_drivers_operations = {
136 	.open		= tty_drivers_open,
137 	.read		= seq_read,
138 	.llseek		= seq_lseek,
139 	.release	= seq_release,
140 };
141 
142 /*
143  * This is the handler for /proc/tty/ldiscs
144  */
145 static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
146 				int count, int *eof, void *data)
147 {
148 	int	i;
149 	int	len = 0;
150 	off_t	begin = 0;
151 	struct tty_ldisc *ld;
152 
153 	for (i=0; i < NR_LDISCS; i++) {
154 		ld = tty_ldisc_get(i);
155 		if (ld == NULL)
156 			continue;
157 		len += sprintf(page+len, "%-10s %2d\n",
158 			       ld->name ? ld->name : "???", i);
159 		tty_ldisc_put(i);
160 		if (len+begin > off+count)
161 			break;
162 		if (len+begin < off) {
163 			begin += len;
164 			len = 0;
165 		}
166 	}
167 	if (i >= NR_LDISCS)
168 		*eof = 1;
169 	if (off >= len+begin)
170 		return 0;
171 	*start = page + (off-begin);
172 	return ((count < begin+len-off) ? count : begin+len-off);
173 }
174 
175 /*
176  * This function is called by tty_register_driver() to handle
177  * registering the driver's /proc handler into /proc/tty/driver/<foo>
178  */
179 void proc_tty_register_driver(struct tty_driver *driver)
180 {
181 	struct proc_dir_entry *ent;
182 
183 	if ((!driver->read_proc && !driver->write_proc) ||
184 	    !driver->driver_name ||
185 	    driver->proc_entry)
186 		return;
187 
188 	ent = create_proc_entry(driver->driver_name, 0, proc_tty_driver);
189 	if (!ent)
190 		return;
191 	ent->read_proc = driver->read_proc;
192 	ent->write_proc = driver->write_proc;
193 	ent->owner = driver->owner;
194 	ent->data = driver;
195 
196 	driver->proc_entry = ent;
197 }
198 
199 /*
200  * This function is called by tty_unregister_driver()
201  */
202 void proc_tty_unregister_driver(struct tty_driver *driver)
203 {
204 	struct proc_dir_entry *ent;
205 
206 	ent = driver->proc_entry;
207 	if (!ent)
208 		return;
209 
210 	remove_proc_entry(driver->driver_name, proc_tty_driver);
211 
212 	driver->proc_entry = NULL;
213 }
214 
215 /*
216  * Called by proc_root_init() to initialize the /proc/tty subtree
217  */
218 void __init proc_tty_init(void)
219 {
220 	struct proc_dir_entry *entry;
221 	if (!proc_mkdir("tty", NULL))
222 		return;
223 	proc_tty_ldisc = proc_mkdir("tty/ldisc", NULL);
224 	/*
225 	 * /proc/tty/driver/serial reveals the exact character counts for
226 	 * serial links which is just too easy to abuse for inferring
227 	 * password lengths and inter-keystroke timings during password
228 	 * entry.
229 	 */
230 	proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR | S_IXUSR, NULL);
231 
232 	create_proc_read_entry("tty/ldiscs", 0, NULL, tty_ldiscs_read_proc, NULL);
233 	entry = create_proc_entry("tty/drivers", 0, NULL);
234 	if (entry)
235 		entry->proc_fops = &proc_tty_drivers_operations;
236 }
237