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/sched.h>
12 #include <linux/proc_fs.h>
13 #include <linux/stat.h>
14 #include <linux/tty.h>
15 #include <asm/bitops.h>
16 
17 extern struct tty_driver *tty_drivers;	/* linked list of tty drivers */
18 
19 static int tty_drivers_read_proc(char *page, char **start, off_t off,
20 				 int count, int *eof, void *data);
21 static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
22 				int count, int *eof, void *data);
23 
24 /*
25  * The /proc/tty directory inodes...
26  */
27 static struct proc_dir_entry *proc_tty_ldisc, *proc_tty_driver;
28 
29 /*
30  * This is the handler for /proc/tty/drivers
31  */
tty_drivers_read_proc(char * page,char ** start,off_t off,int count,int * eof,void * data)32 static int tty_drivers_read_proc(char *page, char **start, off_t off,
33 				 int count, int *eof, void *data)
34 {
35 	int	len = 0;
36 	off_t	begin = 0;
37 	struct tty_driver *p;
38 	char	range[20], deftype[20];
39 	char	*type;
40 
41 	for (p = tty_drivers; p; p = p->next) {
42 		if (p->num > 1)
43 			sprintf(range, "%d-%d", p->minor_start,
44 				p->minor_start + p->num - 1);
45 		else
46 			sprintf(range, "%d", p->minor_start);
47 		switch (p->type) {
48 		case TTY_DRIVER_TYPE_SYSTEM:
49 			if (p->subtype == SYSTEM_TYPE_TTY)
50 				type = "system:/dev/tty";
51 			else if (p->subtype == SYSTEM_TYPE_SYSCONS)
52 				type = "system:console";
53 			else if (p->subtype == SYSTEM_TYPE_CONSOLE)
54 				type = "system:vtmaster";
55 			else
56 				type = "system";
57 			break;
58 		case TTY_DRIVER_TYPE_CONSOLE:
59 			type = "console";
60 			break;
61 		case TTY_DRIVER_TYPE_SERIAL:
62 			if (p->subtype == 2)
63 				type = "serial:callout";
64 			else
65 				type = "serial";
66 			break;
67 		case TTY_DRIVER_TYPE_PTY:
68 			if (p->subtype == PTY_TYPE_MASTER)
69 				type = "pty:master";
70 			else if (p->subtype == PTY_TYPE_SLAVE)
71 				type = "pty:slave";
72 			else
73 				type = "pty";
74 			break;
75 		default:
76 			sprintf(deftype, "type:%d.%d", p->type, p->subtype);
77 			type = deftype;
78 			break;
79 		}
80 		len += sprintf(page+len, "%-20s /dev/%-8s %3d %7s %s\n",
81 			       p->driver_name ? p->driver_name : "unknown",
82 			       p->name, p->major, range, type);
83 		if (len+begin > off+count)
84 			break;
85 		if (len+begin < off) {
86 			begin += len;
87 			len = 0;
88 		}
89 	}
90 	if (!p)
91 		*eof = 1;
92 	if (off >= len+begin)
93 		return 0;
94 	*start = page + (off-begin);
95 	return ((count < begin+len-off) ? count : begin+len-off);
96 }
97 
98 /*
99  * This is the handler for /proc/tty/ldiscs
100  */
tty_ldiscs_read_proc(char * page,char ** start,off_t off,int count,int * eof,void * data)101 static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
102 				int count, int *eof, void *data)
103 {
104 	int	i;
105 	int	len = 0;
106 	off_t	begin = 0;
107 	struct tty_ldisc *ld;
108 
109 	for (i=0; i < NR_LDISCS; i++) {
110 		ld = tty_ldisc_get(i);
111 		if (ld == NULL)
112 			continue;
113 		len += sprintf(page+len, "%-10s %2d\n",
114 			       ld->name ? ld->name : "???", i);
115 		tty_ldisc_put(i);
116 		if (len+begin > off+count)
117 			break;
118 		if (len+begin < off) {
119 			begin += len;
120 			len = 0;
121 		}
122 	}
123 	if (i >= NR_LDISCS)
124 		*eof = 1;
125 	if (off >= len+begin)
126 		return 0;
127 	*start = page + (off-begin);
128 	return ((count < begin+len-off) ? count : begin+len-off);
129 }
130 
131 /*
132  * This function is called by tty_register_driver() to handle
133  * registering the driver's /proc handler into /proc/tty/driver/<foo>
134  */
proc_tty_register_driver(struct tty_driver * driver)135 void proc_tty_register_driver(struct tty_driver *driver)
136 {
137 	struct proc_dir_entry *ent;
138 
139 	if ((!driver->read_proc && !driver->write_proc) ||
140 	    !driver->driver_name ||
141 	    driver->proc_entry)
142 		return;
143 
144 	ent = create_proc_entry(driver->driver_name, 0, proc_tty_driver);
145 	if (!ent)
146 		return;
147 	ent->read_proc = driver->read_proc;
148 	ent->write_proc = driver->write_proc;
149 	ent->data = driver;
150 
151 	driver->proc_entry = ent;
152 }
153 
154 /*
155  * This function is called by tty_unregister_driver()
156  */
proc_tty_unregister_driver(struct tty_driver * driver)157 void proc_tty_unregister_driver(struct tty_driver *driver)
158 {
159 	struct proc_dir_entry *ent;
160 
161 	ent = driver->proc_entry;
162 	if (!ent)
163 		return;
164 
165 	remove_proc_entry(driver->driver_name, proc_tty_driver);
166 
167 	driver->proc_entry = 0;
168 }
169 
170 /*
171  * Called by proc_root_init() to initialize the /proc/tty subtree
172  */
proc_tty_init(void)173 void __init proc_tty_init(void)
174 {
175 	if (!proc_mkdir("tty", 0))
176 		return;
177 	proc_tty_ldisc = proc_mkdir("tty/ldisc", 0);
178 	/*
179 	 * /proc/tty/driver/serial reveals the exact character counts for
180 	 * serial links which is just too easy to abuse for inferring
181 	 * password lengths and inter-keystroke timings during password
182 	 * entry.
183 	 */
184 	proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR | S_IXUSR, 0);
185 
186 	create_proc_read_entry("tty/ldiscs", 0, 0, tty_ldiscs_read_proc,NULL);
187 	create_proc_read_entry("tty/drivers", 0, 0, tty_drivers_read_proc,NULL);
188 }
189